Pagini recente » Cod sursa (job #1320497) | Cod sursa (job #1156117) | Cod sursa (job #1364922) | Cod sursa (job #174362) | Cod sursa (job #999280)
Cod sursa(job #999280)
#include <iostream>
#include <fstream>
#define zs(x) (x&(-x))
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int n,m,a[100005],s;
void Update(int x,int nr)
{ int i;
for(i=x;i<=n;i+=zs(i))
a[i]+=nr;
}
int Compute(int x)
{ int i,res=0;
for(i=x;i>=1;i-=zs(i))
res+=a[i];
return res;
}
int Search(int x)
{ int i,l=1,r=n,mid,sum;
while(l<r)
{ mid=(l+r)/2;
sum=Compute(mid);
if (x<=sum) r=mid; else l=mid+1;
}
if (Compute(l)==x) return l;
return -1;
}
int main()
{ int i,t,x,y,el;
f>>n>>m; s=0;
for(i=1;i<=n;i++)
{f>>el; s+=el;
a[i]=s-Compute(i-zs(i));
}
for(i=1;i<=m;i++)
{ f>>t;
if (t==0) { f>>x>>y; Update(x,y); }
if (t==1) { f>>x>>y; g<<Compute(y)-Compute(x-1)<<"\n"; }
if (t==2) { f>>x; g<<Search(x)<<"\n"; }
}
return 0;
}