Pagini recente » Cod sursa (job #1797431) | Cod sursa (job #661303) | Cod sursa (job #2630933) | Cod sursa (job #1000128) | Cod sursa (job #3005247)
#include<bits/stdc++.h>
#define int long long
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int v[100005],n,m,aib[100005],put;
void update(int poz, int val)
{
int i;
for(i=poz;i<=n;i+=i&-i)
aib[i]+=val;
}
int query(int poz)
{
int i,ans=0;
for(i=poz;i>=1;i-=i&-i)
ans+=aib[i];
return ans;
}
int caut(int val)
{
int ind=0,p=put,ok=0;
while(p>=1)
{
if(aib[ind+p]==val)
ok=1;
if(aib[ind+p]<val)
val-=aib[ind+p],ind+=p;
p/=2;
while(ind+p>n && p>=1)
p/=2;
}
if(ok==1)
return ind+1;
else
return -1;
}
signed main()
{
int i,type,x,y;
f>>n>>m;
for(i=1;i<=n;i++)
f>>v[i],update(i,v[i]);
put=1;
while(put<=n)
put*=2;
put/=2;
for(i=1;i<=m;i++)
{
f>>type>>x;
if(type==0)
f>>y,update(x,y);
else
if(type==1)
f>>y,g<<query(y)-query(x-1)<<'\n';
else
g<<caut(x)<<'\n';
}
return 0;
}