Pagini recente » Cod sursa (job #2252232) | Cod sursa (job #2277555) | Cod sursa (job #3213436) | Cod sursa (job #1490887) | Cod sursa (job #2452157)
#include <stdio.h>
#define MAX_N 100000
int num[MAX_N];
int g( int n ){
return n & (n + 1);
}
int h( int n ){
return n | (n + 1);
}
int prefixSum( int n ){
return n >= 0 ? num[n] + prefixSum(g(n) - 1) : 0;
}
void add( int n, int i, int a ){
while( i < n ){
num[i] += a;
i = h(i);
}
}
int main(){
FILE *fin = fopen("aib.in", "r");
FILE *fout = fopen("aib.out", "w");
int n, num_q, i, a, b, task, st, dr, mij;
fscanf(fin, "%d%d", &n, &num_q);
for( i = 0 ; i < n ; i++ ){
fscanf(fin, "%d", &a);
add(n, i, a);
}
for( i = 0 ; i < num_q ; i++ ){
fscanf(fin, "%d%d", &task, &a);
if( task == 0 ){
fscanf(fin, "%d", &b);
add(n, a, b);
}else if( task == 1 ){
fscanf(fin, "%d", &b);
fprintf(fout, "%d\n", prefixSum(b - 1) - prefixSum(a - 2));
}else{
st = 0;
dr = n;
while( dr - st > 1 ){
mij = (st + dr) / 2;
if( prefixSum(mij) > a )
dr = mij;
else
st = mij;
}
fprintf(fout, "%d\n", st + 1);
}
}
fclose(fin);
fclose(fout);
return 0;
}