Pagini recente » Cod sursa (job #2522129) | Cod sursa (job #1025161) | Cod sursa (job #761103) | Cod sursa (job #2191435) | Cod sursa (job #2637354)
#include <bits/stdc++.h>
#define MAX 100005
#define lsb(x) (x & (-x))
using namespace std;
ifstream in("aib.in");
ofstream out("aib.out");
int AIB[MAX];
int n, m, val, t, a, b;
void update(int poz){
for(int i = poz; i <= n; i += lsb(i))
AIB[i] += val;
}
int query(int poz){
unsigned long long int sum = 0;
for(int i = poz; i > 0; i -= lsb(i))
sum += AIB[i];
return sum;
}
int cautare(){
int i, step;
for(step = 1; step < n; step <<= 1);
for(i = 0; step; step >>= 1){
if(i + step <= n){
if(AIB[i + step] <= val){
i += step, val -= AIB[i];
if(!val){
return i;
}
}
}
}
return -1;
}
int main(){
in>>n>>m;
for(int i = 1; i <= n; i++){
in>>val;
update(i);
}
for(int i = 1; i <= m; i++){
in>>t;
if(t < 2){
in>>a>>b;
val = b;
if(!t)
update(a);
else
out<<(query(b) - query(a - 1))<<"\n";
}
else{
in>>val;
out<<cautare()<<"\n";
}
}
}