Pagini recente » Cod sursa (job #309725) | Cod sursa (job #1174975) | Cod sursa (job #1359169) | Cod sursa (job #1744659) | Cod sursa (job #2964725)
#include <bits/stdc++.h>
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int a[100001],n,m;
void make(int a[]){
for(int i=1 ; i<=n ; ++i){
int p = i + (i & -i);
if(p<=n){
a[p] = a[p] + a[i];
}
}
}
void update(int i , int val){
int c=0;
while(i<=n){
a[i] +=val;
while(!(i & (1<<c))) c++;
i += (1<<c);
c+=1;
}
}
int query(int i){
int c=0;
long long s=0;
while(i > 0){
s+= a[i];
while(!(i & (1<<c))){
c++;
}
i-=(1<<c);
c+=1;
}
return s;
}
int Search(int sum)
{
int pozitie = n+1 , b=n , s = 0;
s = query(n);
int limst = 0;
int limdr = n+1;
while(b){
b = (limst + limdr) >> 1;
s = query(b);
if(s > sum) {
if(limdr > b){
limdr = b;
}
b-=1;
} else if(s == sum) {
pozitie = min (pozitie , b);
limdr = b;
b-=1;
} else {
limst = b;
b+=1;
}
if(b>=limdr || b<=limst){
break;
}
}
if(pozitie == n+1){
return -1;
}
return pozitie;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
f >> n >> m;
for(int i=1 ; i<=n ; ++i){
f >> a[i];
}
make(a);
for(int i=1 ; i<=m ; ++i){
int d;
f >> d;
if(d < 2){
int x,y;
f >> x >>y;
if(!d){
update(x , y);
} else {
if( x > y){
swap(x,y);
}
cout << query(y) - query(x-1) << '\n';
}
} else {
int x;
f >> x;
cout << Search(x) << '\n';
}
}
}