Pagini recente » Cod sursa (job #1205508) | Cod sursa (job #2063180) | Cod sursa (job #2831521) | Cod sursa (job #2556868) | Cod sursa (job #2371428)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <map>
#include <stack>
#define ll long long
#define lsb(x) (x & -x)
using namespace std;
ifstream in("aib.in");
ofstream out("aib.out");
const int NMAX = 100005;
int aib[NMAX];
void update(int pos, int toadd, int n) {
for(; pos <= n; pos += lsb(pos))
aib[pos] += toadd;
}
int query(int pos) {
int ans = 0;
for(; pos; pos -= lsb(pos))
ans += aib[pos];
return ans;
}
int main() {
int n, m;
in >> n >> m;
for(int i = 1; i <= n; i ++) {
int x;
in >> x;
update(i, x, n);
}
while(m --) {
int type;
in >> type;
if(type == 0) {
int a, b;
in >> a >> b;
update(a, b, n);
} else if(type == 1) {
int a, b;
in >> a >> b;
out << query(b) - query(a - 1) << "\n";
} else if(type == 2) {
int a;
in >> a;
int ans = 0;
for(int step = (1 << 16); step; step >>= 1) {
if(ans + step <= n && aib[ans + step] < a) {
ans += step;
a -= aib[ans];
}
}
ans ++;
a -= aib[ans];
if(a != 0)
ans = -1;
out << ans << "\n";
}
}
return 0;
}