Cod sursa(job #1426021)
| Utilizator | Data | 28 aprilie 2015 20:02:21 | |
|---|---|---|---|
| Problema | Arbori indexati binar | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 1.48 kb |
#include <cstdio>
using namespace std;
int n;
int aib[100005];
void update(int poz,int val)
{
for( ;poz<=n;poz+=poz&-poz)
aib[poz]+=val;
}
int query(int poz)
{
int s=0;
for( ;poz!=0;poz-=poz&-poz)
s=s+aib[poz];
return s;
}
int main()
{
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
int m,i,tip,x,y,st,dr,med,last;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d",&x);
update(i,x);
}
for(i=1;i<=m;i++)
{
scanf("%d",&tip);
if(tip==0)
{
scanf("%d%d",&x,&y);
update(x,y);
}
else
{
if(tip==1)
{
scanf("%d%d",&x,&y);
printf("%d\n",query(y)-query(x-1));
}
else
{
if(tip==2)
{
scanf("%d",&x);
st=1;
dr=n;
last=-1;
while(st<=dr)
{
med=(st+dr)>>1;
if(query(med)>=x)
{
dr=med-1;
last=med;
}
else
st=med+1;
}
printf("%d\n",last);
}
}
}
}
return 0;
}
