Pagini recente » Cod sursa (job #226734) | Cod sursa (job #742536) | Cod sursa (job #571756) | Cod sursa (job #2400836) | Cod sursa (job #2566897)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <string>
#include <cstring>
#define lsb(x) x & -x
using namespace std;
void update(int pos, int toadd, int n, vector<int> &aib) {
for(; pos <= n; pos += lsb(pos))
aib[pos] += toadd;
}
int query(int pos, vector<int> &aib) {
if(pos == 0)
return 0;
int ans = 0;
for(; pos; pos -= lsb(pos))
ans += aib[pos];
return ans;
}
int main() {
ifstream in("aib.in");
ofstream out("aib.out");
int n, m;
in >> n >> m;
vector<int> aib(n + 1, 0);
for(int i = 1; i <= n; i ++) {
int x;
in >> x;
update(i, x, n, aib);
}
while(m --) {
int tip;
in >> tip;
if(tip == 0) {
int a, b;
in >> a >> b;
update(a, b, n, aib);
} else if(tip == 1) {
int a, b;
in >> a >> b;
out << (query(b, aib) - query(a - 1, aib)) << "\n";
} else if(tip == 2) {
int a;
in >> a;
int ans = 0, flag = 1;
for(int step = (1 << 16); step && flag; step >>= 1)
if(ans + step <= n && aib[ans + step] <= a) {
ans += step;
a -= aib[ans];
if(a == 0) {
flag = 0;
}
}
if(flag)
ans = -1;
out << ans << "\n";
}
}
return 0;
}