Pagini recente » Cod sursa (job #1351394) | Cod sursa (job #2123874) | Cod sursa (job #958214) | Cod sursa (job #140756) | Cod sursa (job #2062266)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in ("aib.in");
ofstream out ("aib.out");
int const nmax = 100000;
int v[5 + nmax];
int n ;
void update(int x ,int a){
while(x <= n){
v[x] += a;
x += x ^ (x & (x - 1));
}
}
int query(int x){
int sum = 0;
while(0 < x){
sum += v[x];
x &= x - 1;
}
return sum;
}
int binarysearch(int from ,int to ,int k){
if(from < to){
int mid = (from + to) / 2;
if(query(mid) < k){
return binarysearch(mid + 1 , to ,k);
} else
return binarysearch(from , mid ,k);
} else
return from;
}
int main()
{
int m;
in>>n>>m;
int a;
for(int i = 1 ; i <= n ;i++){
in>>a;
update(i , a);
}
for(int i = 1 ; i <= m ;i++){
int op;
in>>op;
if(op == 0){
int a , b;
in>>a>>b;
update(a , b);
} else if(op == 1){
int a , b;
in>>a>>b;
out<<query(b) - query(a - 1)<<'\n';
} else{
int k;
in>>k;
int result = binarysearch(1 , n , k);
if(query(result) == k){
out<<result<<'\n';
} else
out<<-1<<'\n';
}
}
return 0;
}