Pagini recente » Cod sursa (job #2898473) | Cod sursa (job #3303875) | Cod sursa (job #1882386) | Cod sursa (job #962936) | Cod sursa (job #1074967)
# include <cstdio>
const int N = 100000;
int v [N + 1], aIB [N + 1];
int n, t;
void update (int p, int val)
{
while (p <= n)
{
aIB [p] += val;
p += p & - p;
}
}
void citire ()
{
int i;
scanf ("%d %d", & n, & t);
for (i = 1; i <= n; i ++)
{
scanf ("%d", & v [i]);
update (i, v [i]);
}
}
void init ()
{
freopen ("aib.in", "r", stdin);
freopen ("aib.out", "w", stdout);
citire ();
}
int query (int p)
{
int s = 0;
while (p)
{
s += aIB [p];
p -= p & - p;
}
return s;
}
int cautBinara (int s)
{
int pas = 1 << 16, i = 0, cS = s;
while (pas)
{
if (i + pas <= n && aIB [i + pas] <= s)
{
s -= aIB [i + pas];
i += pas;
}
pas /= 2;
}
if (query (i) == cS)
return i;
return -1;
}
void rezolva ()
{
int i, op, poz, poz1, poz2, val, a, q1, q2;
for (i = 1; i <= t; i ++)
{
scanf ("%d", & op);
if (! op)
{
scanf ("%d %d", & poz, & val);
update (poz,val);
v [poz] += val;
}
else if (op < 2)
{
scanf ("%d %d", & poz1, & poz2);
q1 = query (poz2);
q2 = query (poz1 - 1);
printf ("%d\n", q1 - q2);
}
else
{
scanf ("%d", & a);
printf ("%d\n", cautBinara (a));
}
}
}
int main ()
{
init ();
rezolva ();
return 0;
}