Pagini recente » Cod sursa (job #3279861) | Cod sursa (job #1906987) | Cod sursa (job #2919447) | Cod sursa (job #3214078) | Cod sursa (job #3215327)
#include <fstream>
#define MAX 100000
using namespace std;
ifstream cin ("aib.in");
ofstream cout ("aib.out");
int aib[MAX + 10], n;
int lsb(int x)
{
return (x & (-x));
}
void update(int pos, int val)
{
for (int i = pos; i <= n; i = i + lsb(i))
aib[i] = aib[i] + val;
}
int query(int pos)
{
int sum = 0;
for (int i = pos; i >= 1; i = i - lsb(i))
sum = sum + aib[i];
return sum;
}
int main()
{
int q;
cin >> n >> q;
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
update(i, x);
}
for (int i = 1; i <= q; i++)
{
int type;
cin >> type;
if (type == 0)
{
int pos, val;
cin >> pos >> val;
update(pos, val);
}
if (type == 1)
{
int x, y;
cin >> x >> y;
cout << query(y) - query(x - 1) << '\n';
}
if (type == 2)
{
int x;
cin >> x;
int sum = 0, pos = 0, step = (1 << 17), ans = -1;
while (step != 0 && ans == -1)
{
if (pos + step <= n && sum + aib[pos + step] <= x)
{
sum = sum + aib[pos + step];
pos = pos + step;
if (sum == x)
ans = pos;
}
step = step / 2;
}
cout << ans << '\n';
}
}
return 0;
}