Pagini recente » Cod sursa (job #333212) | Cod sursa (job #2937234) | Cod sursa (job #2660659) | Cod sursa (job #1929227) | Cod sursa (job #1653827)
#include <iostream>
#include <fstream>
#include <cstring>
#define NMAX 100005
using namespace std;
inline int zeros(int x)
{
return x & (-x);
}
class AIB
{
private:
int tree[NMAX];
int size;
public:
AIB(int dim)
{
size = dim;
memset(tree, 0, sizeof(tree));
};
void Update(int poz, int val)
{
for (; poz <= size; poz += zeros(poz))
tree[poz] += val;
}
int Query(int poz)
{
int result = 0;
for (; poz > 0; poz -= zeros(poz))
result += tree[poz];
return result;
}
int SearchSum(int k)
{
int poz = 0;
int step = 1;
for (; step < size; step <<= 1);
for (; step; step >>= 1)
{
if (poz + step <= size && tree[poz + step] <= k)
{
poz += step;
k -= tree[poz];
if (k == 0) return poz;
}
}
return -1;
}
};
int main()
{
int n, m;
ios_base::sync_with_stdio(false);
ifstream fin("aib.in");
ofstream fout("aib.out");
fin >> n >> m;
AIB arb(n + 2);
for (int i = 1; i <= n; ++i)
{
int x;
fin >> x;
arb.Update(i, x);
}
for (int i = 1; i <= m; ++i)
{
int t, a, b;
fin >> t;
switch (t)
{
case 0:
fin >> a >> b;
arb.Update(a, b);
break;
case 1:
fin >> a >> b;
fout << arb.Query(b) - arb.Query(a - 1) << '\n';
break;
case 2:
fin >> a;
fout << arb.SearchSum(a) << '\n';
break;
}
}
return 0;
}