Pagini recente » Cod sursa (job #4370) | Cod sursa (job #2476223) | Cod sursa (job #2068382) | Cod sursa (job #205563) | Cod sursa (job #2590564)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("aib.in");
ofstream out("aib.out");
const int A = 100001;
const int L = 16;
int n, m, t, type, aib[A];
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){
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 >> t;
update(i, t);
}
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;
}