Pagini recente » Cod sursa (job #892836) | Cod sursa (job #332128) | Cod sursa (job #3284005) | Istoria paginii preoni-2006/runda-4/solutii | Cod sursa (job #1344466)
#include <fstream>
#include <iostream>
using namespace std;
ifstream fin ("aib.in");
ofstream fout ("aib.out");
const int N = 100005;
int aib[N], n, m, type, a, b;
void update(int i, int &a) {
while (i <= n) {
aib[i] += a;
i += i & -i;
}
}
int query(int i) {
int sum = 0;
while (i > 0) {
sum += aib[i];
i -= i & -i;
}
return sum;
}
int bs(int &x) {
int sol = 0, step;
for (step = 1; (step << 1) <= n; step <<= 1);
for (; step; step >>= 1)
if (sol + step <= n && query(sol + step) <= x)
sol += step;
if (sol > 0 && query(sol) == x)
return sol;
return -1;
}
int main() {
fin >> n >> m;
for (int i = 1; i <= n; ++i) {
fin >> a;
update(i, a);
}
while (m--) {
fin >> type >> a;
if (!type) {
fin >> b;
update(a, b);
}
else
if (type == 1) {
fin >> b;
fout << query(b) - query(a-1) << "\n";
}
else
if (type == 2)
fout << bs(a) << "\n";
}
}