Pagini recente » Cod sursa (job #1229866) | Cod sursa (job #1782880) | Cod sursa (job #1444885) | Cod sursa (job #3288250) | Cod sursa (job #2998894)
#include <bits/stdc++.h>
#define next(x) ((x^(x-1))&x)
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int aib[100011],n;
void update(int x,int poz)
{
while(poz<=n)
{
aib[poz]=aib[poz]+x;
poz=poz+next(poz);
}
}
int query(int poz)
{
int s=0;
while(poz)
{
s=s+aib[poz];
poz=poz-next(poz);
}
return s;
}
int m,x,i,C,y,st,dr,found,mij,s;
int main()
{
f>>n>>m;
for(i=1;i<=n;i++)
{
f>>x;
update(x,i);
}
for(i=1;i<=m;i++)
{
f>>C;
if(C==0)
{
f>>x>>y;
update(x,y);
}
else
if(C==1)
{
f>>x>>y;
g<<query(y)-query(x-1)<<'\n';
}
else
{
f>>x;
st=1;
dr=n;
found=-1;
while(st<=dr)
{
mij=(st+dr)/2;
s=query(mij);
g<<s<<" ";
if(s==x)
{
found=mij;
st=dr+1;
}
else
if(s>x)
dr=mij-1;
else
st=mij+1;
}
g<<found<<'\n';
}
}
return 0;
}