Pagini recente » Cod sursa (job #407615) | Cod sursa (job #1717941) | Cod sursa (job #1033165) | Utilizatori inregistrati la .com 2009, Runda 1 | Cod sursa (job #3197170)
#include <bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) ;
#define ll long long
ifstream fin("aib.in") ;
ofstream fout("aib.out") ;
const int NMAX=1e5+5 ;
int n,m,aib[NMAX],x ;
void update(int pos,int val)
{
while(pos<=n) aib[pos]+=val,pos+=pos&(-pos) ;
}
int query(int pos)
{
int s=0 ;
while(pos>0) s+=aib[pos],pos-=pos&(-pos) ;
return s ;
}
int main()
{
fin>>n>>m ;
for(int i=1; i<=n; ++i)
{
fin>>x ;
update(i,x) ;
}
while(m--)
{
int op,x,y ;
fin>>op ;
if(op==0)
{
fin>>x>>y;
update(x,y) ;
}
else if(op==1)
{
fin>>x>>y;
fout<<query(y)-query(x-1)<<'\n' ;
}
else
{
fin>>x ;
int pos=0,val=0 ;
for(int b=17; b>=0; --b) if(pos+(1<<b)<=n && val+aib[pos+(1<<b)]<x) val+=aib[pos+(1<<b)],pos+=(1<<b) ;
if(val!=x) fout<<pos+1<<'\n' ;
else fout<<-1<<'\n' ;
}
}
return 0;
}