Pagini recente » Cod sursa (job #2570669) | Cod sursa (job #2046208) | Cod sursa (job #555400) | Cod sursa (job #885795) | Cod sursa (job #3305225)
#include <fstream>
#include <vector>
using namespace std;
void addFenwick(int pos, int n, int v, const vector<int>& lsOne, vector<int>& fenwick)
{
while (pos <= n)
{
fenwick[pos] += v;
pos += lsOne[pos];
}
}
int totalFenwick(int pos, const vector<int>& lsOne, const vector<int>& fenwick)
{
int t = 0;
while (pos > 0)
{
t += fenwick[pos];
pos -= lsOne[pos];
}
return t;
}
int main()
{
ifstream fin("aib.in");
ofstream fout("aib.out");
int n, m;
fin >> n >> m;
vector<int> v(n + 1, 0);
vector<int>lsOne(n + 1, 0);
for (int i = 1; i <= n; ++i)
{
int x;
fin >> x;
v[i] = x;
lsOne[i] = i & (-i);
}
lsOne[n] = n & (-n);
vector<int>fenwick(n + 1, 0);
for (int i = 1; i <= n; ++i)
{
addFenwick(i, n, v[i], lsOne, fenwick);
}
for (int j = 0; j < m; ++j)
{
int task;
fin >> task;
if (task == 0)
{
int pos, x;
fin >> pos >> x;
addFenwick(pos, n, x, lsOne, fenwick);
}
else if (task == 1)
{
int left, right;
fin >> left >> right;
fout << totalFenwick(right, lsOne, fenwick) - totalFenwick(left - 1, lsOne, fenwick) << endl;
}
else if (task == 2)
{
int k;
fin >> k;
// upfenwick now
int bit = 1;
while (bit <= n)
bit <<= 1;
bit >>= 1;
int pos = 0;
while (k > 0 && bit > 0)
{
if (fenwick[pos + bit] <= k)
{
k -= fenwick[pos + bit];
pos += bit;
}
bit >>= 1;
}
if (k == 0)
fout << pos << endl;
else fout << "-1" << endl;
}
}
}