Pagini recente » Cod sursa (job #1516079) | Cod sursa (job #1163971) | Cod sursa (job #1023451) | Cod sursa (job #142583) | Cod sursa (job #3150638)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>
#include <bitset>
using namespace std;
#define zeros(x) ((x ^ (x - 1)) & x)
ifstream f("aib.in");
ofstream g("aib.out");
int n, m;
int aib[100001];
void update(const int& pos, const int& val) {
for(int i = pos;i <= n;i += zeros(i)) {
aib[i] += val;
}
}
int query(const int& pos) {
int result = 0;
for(int i = pos;i >= 1;i -= zeros(i)) {
result += aib[i];
}
return result;
}
int find(int val) {
int rep;
for(rep = 1;rep <= n;rep <<= 1);
for(int i = 0;rep;rep >>= 1) {
if(i + rep <= n && aib[i + rep] <= val) {
i += rep;
val -= aib[i];
if(!val) {
return i;
}
}
}
return -1;
}
void read() {
f>>n>>m;
int x;
for(int i = 1;i <= n;++i) {
f>>x;
update(i, x);
}
}
void solve() {
int x, y, z;
for(int i = 1;i <= m;++i) {
f>>x;
if(!x) {
f>>y>>z;
update(y, z);
} else if(x == 1) {
f>>y>>z;
g<<query(z) - query(y - 1)<<'\n';
} else {
f>>y;
g<<find(y)<<'\n';
}
}
}
int main() {
read();
solve();
return 0;
}