Pagini recente » Cod sursa (job #2838866) | Cod sursa (job #3168464) | Cod sursa (job #1616954) | Cod sursa (job #426932) | Cod sursa (job #730185)
Cod sursa(job #730185)
#include <stdio.h>
#define f(x) ((x) & (-x))
using namespace std;
const int MAX = 100050;
int arb[MAX],n, m;
void update(int poz, int val)
{
for(;poz <= n; poz += f(poz))
arb[poz] += val;
}
int query(int poz)
{
int val = 0;
for(;poz; poz -= f(poz))
val += arb[poz];
return val;
}
int search(int val)
{
int step, i;
for(step = 1; step < n; step <<= 1);
for(i = 0; step; step >>= 1)
{
if(i + step <= n)
if(val >= arb[step + i])
{
val -= arb[step + i];
i += step;
if(!val) return i;
}
}
return -1;
}
int main()
{
freopen("aib.in", "r", stdin);
freopen("aib.out", "w", stdout);
scanf("%d %d", &n, &m);
int x, a, b;
for(int i = 1; i <= n; i++) {scanf("%d", &x); update(i, x);}
while(m--)
{
scanf("%d", &x);
switch(x)
{
case 0: scanf("%d %d", &a, &b); update(a, b); break;
case 1: scanf("%d %d", &a, &b); printf("%d\n", query(b) - query(a - 1)); break;
case 2: scanf("%d", &a); printf("%d\n", search(a)); break;
}
}
fclose(stdin);
fclose(stdout);
return 0;
}