Pagini recente » Cod sursa (job #1612874) | Cod sursa (job #1926670) | Cod sursa (job #1581301) | Cod sursa (job #1337058) | Cod sursa (job #957445)
Cod sursa(job #957445)
#include<fstream>
#include<vector>
using namespace std;
vector<int> a;
int n, m;
void add(int x,int val){
while(x<=n){
a[x] += val;
x += x^(x&(x-1));
}
}
int get(int x){
int sum = 0;
while(x>0){
sum+=a[x];
x-=x^x&(x-1);
}
return sum;
}
int find(int x){
int m, step = 1;
for(step=1; step<n; step<<=1);
for(m=1; step; step>>=1){
if(m+step<=n && get(m+step)<=x)
m+=step;
}
return m;
}
int main(){
int i, x, y, o;
freopen("aib.in", "rt", stdin);
freopen("aib.out", "wt", stdout);
scanf("%d %d", &n, &m);
a.resize(n+1, 0);
for(i=1; i<=n; i++)
scanf("%d", &x), add(i, x);
for(i=1; i<=m; i++){
scanf("%d", &o);
if(o==0){
scanf("%d %d", &x, &y);
add(x, y);
}else if(o==1){
scanf("%d %d", &x, &y);
printf("%d\n", get(y) - get(x-1));
}else{
scanf("%d\n", &x);
printf("%d\n", find(x));
}
}
fclose(stdin);
fclose(stdout);
return 0;
}