Pagini recente » Autentificare | Cod sursa (job #2269267) | Clasament rar16 | Cod sursa (job #1530783) | Cod sursa (job #3038652)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
int n, v[100005], aib[100005], cer, x, y, nrq, pw2;
void update(int poz, int val)
{
for(int i = poz; i <= n; i += i & (-i))
aib[i] += val;
}
int sum(int poz)
{
int sum = 0;
for(int i = poz; i > 0; i -= i & (-i))
sum += aib[i];
return sum;
}
int findd(int cauta)
{
int poz = 0;
for(int step = pw2; step > 0; step >>= 1)
{
if((poz | step) <= n && aib[poz | step] <= cauta)
{
poz |= step;
cauta -= aib[poz];
if(!cauta)
return poz;
}
}
return -1;
}
int main()
{
fin >> n >> nrq;
for(pw2 = 1; pw2 <= n; pw2 <<= 1);
pw2 >>= 1;
for(int i = 1; i <= n; ++ i)
{
fin >> v[i];
update(i, v[i]);
}
while(nrq--)
{
fin >> cer >> x;
if(cer == 0)
{
fin >> y;
update(x, y);
} else if(cer == 1) {
fin >> y;
fout << sum(y) - sum(x - 1) << '\n';
} else if(cer == 2) {
fout << findd(x) << '\n';
}
}
return 0;
}