Pagini recente » Cod sursa (job #586807) | Cod sursa (job #1463248) | Cod sursa (job #611687) | Cod sursa (job #2817557) | Cod sursa (job #1809524)
#include <bits/stdc++.h>
using namespace std;
#ifdef INFOARENA
#define cout fout
#endif // INFOARENA
ifstream fin("arbint.in");
ofstream fout("arbint.out");
#define nmax 100001
int n,m;
int aib[nmax];
void update(int pos,int val)
{
for(;pos<=n;pos+=(pos&-pos)) aib[pos]+=val;
}
int query(int pos)
{
int res=0;
for(;pos;pos-=(pos&-pos)) res+=aib[pos];
return res;
}
int binsrc(int val)
{
int step,pos=0;
for(step=1;step<=n;step<<=1);
for(step>>=1;step;step>>=1)
{
if(pos+step<=n)
if(aib[pos+step]<=val)
{
pos+=step;
val-=aib[pos];
if(val==0) return pos;
}
}
return -1;
}
int main()
{
fin>>n>>m;
int i,x,a,b;
for(i=1;i<=n;++i)
{
fin>>x;
update(i,x);
}
for(;m;--m)
{
fin>>x;
if(x<2) fin>>a>>b;
else fin>>a;
if(x==0) update(a,b);
if(x==1) cout<<query(b)-query(a-1)<<'\n';
if(x==2) cout<<binsrc(a)<<'\n';
}
return 0;
}