Pagini recente » Cod sursa (job #3197053) | Cod sursa (job #1604511) | Cod sursa (job #1015951) | Cod sursa (job #848116) | Cod sursa (job #2589569)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("aib.in");
ofstream out("aib.out");
const int N = 100001;
const int A = 10001;
const int L = 16;
int n, m, type, aib[A], v[N];
int search(int z) {
if (z == 0) return -1;
int p = 0, pas = 1 << L;
while (pas != 0) {
if (p + pas <= n && aib[p + pas] <= z) {
p += pas;
z -= aib[p];
}
pas /= 2;
}
if (z != 0) return -1;
return p;
}
int question(int p) {
//calc v[1] + v[2] + ... + v[p]
int s = 0;
while (p != 0) {
s += aib[p];
p -= (p & (-p));
}
return s;
}
void update(int p, int val) {
while (p <= n) {
aib[p] += val;
p += (p & (-p));
}
}
int main() {
in >> n >> m;
for (int i = 1; i <= n; i++) {
in >> v[i];
update(1, v[i]);
}
for (int i = 1; i <= m; i++) {
int x, y;
in >> type;
if (type == 0) {
in >> x >> y;
update(x, y);
}
if (type == 1) {
in >> x >> y;
out << question(y) - question(x - 1) << '\n';
}
if (type == 2) {
in >> x;
out << search(x) << '\n';
}
}
return 0;
}