Pagini recente » Cod sursa (job #669622) | Cod sursa (job #2755777) | Cod sursa (job #1381987) | Cod sursa (job #994272) | Cod sursa (job #1400585)
#include <stdio.h>
#define NMax 100005
using namespace std;
int n,m,x,y,z,arb[4*NMax],val;
void _update(int pos)
{
int bit = 0;
while( pos <= n)
{
arb[pos] += val;
while( ! (pos&(1<<bit)) )++bit;
pos = pos + (1<<bit);
++bit;
}
}
int _query(int pos)
{
int bit = 0,suma = 0;
while( pos > 0 )
{
suma = suma + arb[pos];
while( ! (pos&(1<<bit)) )++bit;
pos = pos - (1<<bit);
++bit;
}
return suma;
}
int main()
{
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
scanf("%d %d",&n,&m);
for(int i=1;i<=n;++i)
{
scanf("%d",&x);
val = x;
_update(i);
}
for(int i=1;i<=m;++i)
{
scanf("%d %d",&x,&y);
if(x==0)
{
scanf("%d",&z);
val = z;
_update(y);
}
else if(x==1)
{
scanf("%d",&z);
printf("%d\n",_query(z)-_query(y-1));
}
else if(x==2)
{
int suma = 0,p = 1,u = n,mij;
bool gasit = false;
while(p<=u)
{
mij = ((u+p)>>1);
suma = _query(mij);
if(suma<y)p = mij+1;
else if(suma>y)u = mij;
else
{
gasit = true;
break;
}
}
if(gasit)printf("%d\n",mij);
else printf("-1\n");
}
}
return 0;
}