Pagini recente » Cod sursa (job #3284586) | Cod sursa (job #2924688) | Cod sursa (job #2941412) | Diferente pentru implica-te/arhiva-educationala intre reviziile 223 si 185 | Cod sursa (job #795282)
Cod sursa(job #795282)
#include <stdio.h>
#define NMAX 100005
int n,m,A[NMAX],aib[NMAX];
void read()
{
scanf("%d%d",&n,&m);
int i;
for (i=1; i<=n; i++)
scanf("%d",&A[i]);
}
int lsb(int x)
{
return x & -x;
}
int suma(int poz)
{
int i,s=0;
for (i=poz; i>0; i-=lsb(i))
s+=aib[i];
return s;
}
void update(int poz,int val)
{
int i;
for (i=poz; i<=n; i+=lsb(i))
aib[i]+=val;
}
int find(int val)
{
int i,step;
for (step=1; step<=n; step<<=1);
for (i=0; step; step>>=1)
if (i+step<=n && aib[i+step]<=val)
{
i+=step;
val-=aib[i];
if (val==0)
return i;
}
return -1;
}
void solve()
{
int i,tip,x,y;
for (i=1; i<=n; i++)
update(i,A[i]);
for (i=1; i<=m; i++)
{
scanf("%d",&tip);
if (tip==0)
{
scanf("%d%d",&x,&y);
update(x,y);
}
if (tip==1)
{
scanf("%d%d",&x,&y);
printf("%d\n",suma(y)-suma(x-1));
}
if (tip==2)
{
scanf("%d",&x);
printf("%d\n",find(x));
}
}
}
int main()
{
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
read();
solve();
return 0;
}