Pagini recente » Cod sursa (job #393755) | Cod sursa (job #1980841) | Cod sursa (job #814848) | Cod sursa (job #1749478) | Cod sursa (job #2719414)
#include <bits/stdc++.h>
#define zeros(x) ((x ^ (x - 1)) & x)
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int n, m, aib[100005], x, y, z;
void update(int pos, int val)
{
for(int i = pos;i <= n;i += zeros(i))
aib[i] += val;
}
int query(int pos)
{
int sum = 0;
for(int i = pos;i > 0;i -= zeros(i))
sum += aib[i];
return sum;
}
int find(int val)
{
int rep, i;
for(rep = 1;rep < n;rep <<= 1);
for(i = 0;rep;rep >>= 1)
if(i + rep <= n && aib[i + rep] <= val)
{
i += rep;
val -= aib[i];
if(!val)
return i;
}
return -1;
}
void Read()
{
f>>n>>m;
for(int i = 1;i <= n;++i)
f>>x, update(i, x);
}
void Solve()
{
for(int i = 1;i <= m;++i)
{
f>>x>>y;
if(x == 2)
{
g<<find(y)<<'\n';
continue;
}
f>>z;
if(!x)
update(y, z);
else
g<<query(z) - query(y - 1)<<'\n';
}
}
int main()
{
Read();
Solve();
return 0;
}