Pagini recente » Cod sursa (job #151071) | Profil florinhaja | Cod sursa (job #149006) | Cod sursa (job #792764) | Cod sursa (job #485008)
Cod sursa(job #485008)
#include <cstdio>
#define lsb(i) (int)(i & -i)
const int MAX_N = 100001;
int n, m;
int aib[MAX_N];
void update(int poz, int val)
{
for (; poz <= n; poz += lsb(poz))
aib[poz] += val;
}
int query(int poz)
{
int ret = 0;
for (; poz; poz -= lsb(poz))
ret += aib[poz];
return ret;
}
int find(int val)
{
if (val == 0)
return -1;
int pow = 1, i;
for (; (pow << 1) <= n; pow <<= 1);
for (i = 0; pow && val; pow >>= 1)
if (i + pow <= n && aib[i + pow] <= val)
{
i += pow;
val -= aib[i];
}
if (val)
return -1;
return i;
}
int main()
{
int i;
freopen("aib.in", "r", stdin);
freopen("aib.out", "w", stdout);
scanf("%d %d", &n, &m);
for (i = 1; i <= n; ++i)
{
int x;
scanf("%d", &x);
aib[i] += x;
if (i + lsb(i) <= n)
aib[i + lsb(i)] += aib[i];
}
for (i = 1; i <= m; ++i)
{
int q, x, y;
scanf("%d", &q);
if (q == 0)
{
scanf("%d %d", &x, &y);
update(x, y);
}
if (q == 1)
{
scanf("%d %d", &x, &y);
printf("%d\n", query(y) - query(x - 1));
}
if (q == 2)
{
scanf("%d", &x);
printf("%d\n", find(x));
}
}
}