Pagini recente » Cod sursa (job #609334) | Cod sursa (job #1985679) | Cod sursa (job #799268) | Cod sursa (job #2454006) | Cod sursa (job #2498325)
#include <bits/stdc++.h>
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
const int NMAX = 1e5 + 10;
int n,m,aib[NMAX];
void add(int val, int pos){
while(pos <= n){
aib[pos] += val;
pos += pos & -pos;
}
}
int sum(int pos){
int sum = 0;
while(pos > 0){
sum += aib[pos];
pos -= pos & -pos;
}
return sum;
}
int position(int value){
int step,i;
for(step = 1 ; step < n ; step <<= 1);
for(i = 0 ; step > 0 ; step >>= 1){
if(i + step <= n){
if(aib[i + step] <= value){
i += step;
value -= aib[i];
}
if(!value && i)
return i;
}
}
return -1;
}
int main(){
int i,type,x,y;
f >> n >> m;
for(i = 1 ; i <= n ; i++){
f >> x;
add(x,i);
}
while(m--){
f >> type;
if(type == 0){
f >> x >> y;
add(y, x);
}else
if(type == 1){
f >> x >> y;
g << sum(y) - sum(x - 1) << "\n";
}else{
f >> x;
g << position(x) << "\n";
}
}
return 0;
}