Pagini recente » Cod sursa (job #736373) | Cod sursa (job #411030) | Cod sursa (job #3346951) | Cod sursa (job #2220413) | Cod sursa (job #2097059)
#include <bits/stdc++.h>
using namespace std;
ifstream in("aib.in");
ofstream out("aib.out");
int n, m, aib[100100], c, x, y, st, dr, mid;
void update(int pos, int val){
while(pos <= n){
aib[pos] += val;
pos += (pos & -pos);
}
}
int query(int pos){
int rs = 0;
while(pos > 0){
rs += aib[pos];
pos -= (pos & -pos);
}
return rs;
}
int range_query(int a, int b){
return query(b) - query(a - 1);
}
int main(){
in >> n >> m;
for(int i = 1; i <= n; i++){
in >> x;
update(i, x);
}
while(m--){
in >> c;
if(c == 0){
in >> x >> y;
update(x, y);
continue;
}
if(c == 1){
in >> x >> y;
out << range_query(x, y) << '\n';
continue;
}
if(c == 2){
in >> x;
st = 1;
dr = n;
while(st <= dr){
mid = st + dr >> 1;
if(query(mid) >= x)
dr = mid - 1;
else
st = mid + 1;
}
if(query(st) == x)
out << st;
else
out << -1;
out << '\n';
continue;
}
}
return 0;
}