Cod sursa(job #1001357)
Utilizator | Data | 24 septembrie 2013 21:08:56 | |
---|---|---|---|
Problema | Arbori indexati binar | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.66 kb |
#include<stdio.h>
unsigned v[100005],s[100005];
int main()
{
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
unsigned n,m,i,j,x,k,a,b,y,c,p,st,dr;
scanf("%u%u",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%u",&v[i]);
p=i;
while(p<=n)
{
s[p]=s[p]+v[i];
p=p+(p & (-p));
}
}
for(i=1;i<=m;i++)
{
scanf("%u%u",&c,&a);
if(c!=2)
scanf("%u",&b);
if(c==0)
{
p=a;
v[a]+=b;
while(p<=n)
{
s[p]=s[p]+b;
p=p+(p & (-p));
}
}
else
if(c==1)
{
j=b;
y=0;
while(j>0)
{
y=y+s[j];
j=j-(j & (-j));
}
j=a-1;
while(j>0)
{
y=y-s[j];
j=j-(j & (-j));
}
printf("%u\n",y);
}
else
{
st=1;dr=n;
while(st<=dr)
{
p=j=(st+dr)/2;
y=0;
while(j>0)
{
y=y+s[j];
j=j-(j & (-j));
}
if(y==a)
break;
if(y>a)
{
dr=p-1;
}
else
{
st=p+1;
}
}
printf("%u\n",p);
if(dr<st)
printf("-1\n");
}
}
return 0;
}