Pagini recente » Cod sursa (job #1726272) | Cod sursa (job #2716857) | Cod sursa (job #2358931) | Cod sursa (job #94092) | Cod sursa (job #3157362)
#include <bits/stdc++.h>
using namespace std;
#define zeros(x) ((x ^ (x - 1)) & x)
ifstream f("aib.in");
ofstream g("aib.out");
int n, m;
int aib[100005];
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 sum = 0;
for(int i = pos;i >= 1;i -= zeros(i)) {
sum += aib[i];
}
return sum;
}
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 == 2) {
f>>y;
g<<find(y)<<'\n';
continue;
}
f>>y>>z;
if(!x) {
update(y, z);
continue;
}
g<<query(z) - query(y - 1)<<'\n';
}
}
int main() {
read();
solve();
return 0;
}