Pagini recente » Cod sursa (job #1412896) | Cod sursa (job #2182530) | Cod sursa (job #872532) | Cod sursa (job #2499597) | Cod sursa (job #1812828)
#include <fstream>
using namespace std;
int n,m,c,i,q,a[100005],x,y;
void adauga(int poz, int val)
{
int p=0;
while(poz<=n)
{
a[poz]+=val;
while(!(poz&(1<<p))) p++;
poz+=(1<<p);
}
}
int suma(int poz)
{
int p=0,s=0;
while(poz>=1)
{
s+=a[poz];
while(!(poz&(1<<p))) p++;
poz-=(1<<p);
}
return s;
}
int pozi(int val)
{
int l=1,r=n;
while(l!=r)
{
m=(l+r)/2;
if(suma(m)>=val) r=m;
else l=m+1;
}
if(suma(l)==val) return l;
return -1;
}
int main()
{
ifstream f("aib.in");
ofstream g("aib.out");
f>>n>>q;
for(i=1; i<=n; i++)
{
f>>x;
adauga(i,x);
}
for(i=1;i<=q;i++)
{
f>>c;
if(c==0)
{
f>>x>>y;
adauga(x,y);
}
if(c==1)
{
f>>x>>y;
g<<suma(y)-suma(x-1) << '\n';
}
if(c==2)
{
f>>x;
g<<pozi(x)<<'\n';
}
}
f.close(); g.close();
return 0;
}