Pagini recente » Cod sursa (job #147467) | Cod sursa (job #2088954) | Cod sursa (job #302136) | Cod sursa (job #2435275) | Cod sursa (job #1653815)
#include <iostream>
#include <fstream>
#define NMAX 100005
#define cin fin
#define cout fout
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; 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];
}
}
return (poz == 0? -1 : poz);
}
};
int main()
{
int n, m;
ios_base::sync_with_stdio(false);
ifstream fin("aib.in");
ofstream fout("aib.out");
cin >> n >> m;
AIB arb(n);
for (int i = 1; i <= n; ++i)
{
int x;
cin >> x;
arb.Update(i, x);
}
for (int i = 1; i <= m; ++i)
{
int t, a, b;
cin >> t;
switch (t)
{
case 0:
cin >> a >> b;
arb.Update(a, b);
break;
case 1:
cin >> a >> b;
cout << arb.Query(b) - arb.Query(a - 1) << '\n';
break;
case 2:
cin >> a;
cout << arb.SearchSum(a) << '\n';
break;
}
}
return 0;
}