Pagini recente » Cod sursa (job #2565304) | Cod sursa (job #2107241) | Cod sursa (job #209697) | Cod sursa (job #947115) | Cod sursa (job #3176603)
#include <stdio.h>
#include <iostream>
#define N_MAX 100005
#define N_LOG 20
using namespace std;
int n, m;
int aib[N_MAX];
int leastSignificantBit (int x){
return x & -x;
}
// void build (){
// for (int i=1; i<=n; ++i){
// aib[i] += arr[i];
// if (i + leastSignificantBit(i) <= n)
// aib[i + leastSignificantBit(i)] += aib[i];
// }
// }
void update (int idx, int value){
// arr[idx] = value;
while (idx <= n){
aib[idx] += value;
idx += leastSignificantBit(idx);
}
}
int query (int Idx){
long long Sum = 0;
while (Idx){
Sum += aib[Idx];
Idx -= leastSignificantBit(Idx);
}
return Sum;
}
int binaryLifting (int value){
int pos = 0, sum = 0;
for (int mask = 24; mask>=0; mask--){
if (pos + (1<<mask) <= n && sum + aib[pos+(1<<mask)] < value){
sum += aib[pos+(1<<mask)];
pos += (1<<mask);
}
}
pos++;
if (pos > n || query(pos) != value)
return -1;
return pos;
}
int main (){
FILE *in, *out;
in = fopen ("aib.in", "r");
out = fopen ("aib.out", "w");
fscanf(in, "%d%d", &n, &m);
int num;
for (int i=1; i<=n; ++i){
fscanf (in, "%d", &num);
update(i, num);
}
int op, x, y;
for (int i=1; i<=m; ++i){
fscanf(in, "%d", &op);
if (op == 0){
fscanf(in, "%d%d", &x, &y);
update(x, y);
}
else if (op == 1){
fscanf(in, "%d%d", &x, &y);
fprintf (out, "%d \n", query(y) - query(x-1));
}
else{
fscanf(in, "%d", &x);
fprintf(out, "%d \n", binaryLifting(x));
}
}
return 0;
}