Pagini recente » Profil taloibogdan | Istoria paginii utilizator/puma | Diferente pentru propuneri intre reviziile 19 si 42 | Istoria paginii utilizator/cristi_chira | Cod sursa (job #795280)
Cod sursa(job #795280)
#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 step,poz,act=0;
for (step=1; step<=n; step<<=1);
for (poz=0; step; step>>=1)
if (poz+step<=n && act+aib[poz+step]<=val)
poz+=step,act+=aib[poz];
return poz;
}
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);
y=find(x);
if (suma(y)!=x)
printf("-1\n");
else
printf("%d\n",y);
}
}
}
int main()
{
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
read();
solve();
return 0;
}