Pagini recente » Cod sursa (job #862942) | Cod sursa (job #3125370) | Cod sursa (job #895876) | Cod sursa (job #1774245) | Cod sursa (job #2348435)
/*
* The reason that a good citizen does not use such destructive means to become
* wealthier is that, if everyone did so, we would all become poorer from the
* mutual destructiveness.
* Richard M. Stallman
*/
#include <fstream>
#include <iostream>
const int MAX_N = 100000;
const int MAX_LOG_N = 17;
struct {
int data[MAX_N + 1];
int n;
}aib;
int computeSum(int i)
{
int sum = 0;
while(i != 0)
{
sum += aib.data[i];
i -= i & (-i);
}
return sum;
}
void update(int i, int v)
{
while(i <= aib.n)
{
aib.data[i] += v;
i += i & (-i);
}
}
int search(int v)
{
int r = 0, step = 1 << MAX_LOG_N;
while(step != 0)
{
if(r + step < aib.n && aib.data[r + step] < v)
{
r += step;
v -= aib.data[r];
}
step /= 2;
}
return r + 1;
}
int main()
{
std::ifstream fin("aib.in");
std::ofstream fout("aib.out");
int n, k;
fin >> n >> k;
aib.n = n;
for(int i = 1; i <= n; i++)
{
int x;
fin >> x;
update(i, x);
}
for(; k > 0; k--)
{
int x, a, b;
fin >> x;
if(x == 0)
{
fin >> a >> b;
update(a, b);
}
else if(x == 1)
{
fin >> a >> b;
fout << computeSum(b) - (a == 1 ? 0 : computeSum(a - 1)) << '\n';
}
else if(x == 2)
{
fin >> a;
x = search(a);
fout << (aib.data[x] == a ? x : -1) << '\n';
}
}
}