Cod sursa(job #1001333)
Utilizator | Data | 24 septembrie 2013 20:40:39 | |
---|---|---|---|
Problema | Arbori indexati binar | Scor | 40 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.42 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;
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
{
y=0;
for(j=1;j<=n;j++)
{
y=y+v[j];
if(y==a)
{
printf("%u\n",j);
break;
}
}
if(j>n)
printf("-1\n");
}
}
return 0;
}