Pagini recente » Cod sursa (job #3137306) | Cod sursa (job #153883) | Cod sursa (job #153165) | Cod sursa (job #251641)
Cod sursa(job #251641)
#include <stdio.h>
#define Nmax 100100
int x[Nmax],n;
void up(int poz,int nr)
{
for (;poz<=n;poz+=poz&-poz)
x[poz] += nr;
}
int q(int poz)
{
int ret=0;
for (;poz>0;poz-=poz&-poz)
ret += x[poz];
return ret;
}
void solve()
{
int op;
scanf("%d", &op);
if (op==0)
{
int a,b;
scanf("%d%d", &a,&b);
up(a,b);
}
else
if (op==1)
{
int a,b;
scanf("%d%d", &a,&b);
printf("%d\n",q(b)-q(a-1));
}
else
if (op==2)
{
int ret=0, sum;
scanf("%d", &sum);
for (int i=1<<18;i>0;i/=2) if (ret+i <= n)
if (q(ret+i)<sum) ret+=i;
while (q(ret)<sum && ret <= n) ++ret;
if (q(ret) == sum && ret <= n && sum > 0)
printf("%d\n", ret);
else
printf("-1\n");
}
}
int main()
{
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
int t,el;
scanf("%d%d", &n,&t);
for (int i=1;i<=n;++i)
{
scanf("%d", &el);
up(i,el);
}
while (t--) solve();
return 0;
}