Pagini recente » Cod sursa (job #102845) | Cod sursa (job #2025472) | Cod sursa (job #2097282) | Cod sursa (job #2745751) | Cod sursa (job #1163390)
#include <fstream>
#include <iostream>
using namespace std;
ifstream in("aib.in");
ofstream out("aib.out");
int aib[100001];
int n, m;
int maxPas = 1;
void update(int x, int val) {
while (x <= n) {
aib[x] += val;
x += x & (-x);
}
}
int query(int x) {
int rez = 0;
while (x != 0) {
rez += aib[x];
x -= x & (-x);
}
return rez;
}
int cauta(int x) {
int i = 0, pas = maxPas;
while (pas != 0) {
if (i + pas <= n && aib[i + pas] <= x) {
x -= aib[i + pas];
i += pas;
}
pas /= 2;
}
if ( x != 0)
return -1;
return i;
}
int main() {
in>>n>>m;
for (; maxPas <= n; maxPas <<= 1);
for (int i = 1; i <= n; i++) {
int x;
in>>x;
update(i, x);
}
for (int i = 1; i <= m; i++) {
int type;
in>>type;
if (type == 0) {
int a, b;
in>>a>>b;
update(a, b);
}
if (type == 1) {
int a, b;
in>>a>>b;
out<<query(b) - query(a - 1)<<"\n";
}
if (type == 2) {
int a;
in>>a;
out<<cauta(a)<<"\n";
}
}
return 0;
}