Pagini recente » Cod sursa (job #2604815) | Cod sursa (job #730228) | Cod sursa (job #2919498) | Cod sursa (job #2301593) | Cod sursa (job #1665530)
#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
ifstream fin ("aib.in");
ofstream fout ("aib.out");
const int INF = 0x3f3f3f3f;
const int Nmax = 100555;
int N, mxbit = 1;
int aib[Nmax];
void add(int pos, int val);
int query(int pos);
int search(int num);
int main() {
int M, x, a, b;
fin >> N >> M;
while (mxbit < N)
mxbit *= 2;
for(int i = 1; i <= N; ++i) {
fin >> a;
add(i, a);
}
while(M--) {
fin >> x;
if (x == 0) {
fin >> a >> b;
add(a, b);
} else if (x == 1) {
fin >> a >> b;
fout << query(b) - query(a-1) << '\n';
} else {
fin >> a;
//cout << "Operation 2\n";
fout << search(a) << '\n';
}
}
return 0;
}
void add(int pos, int val) {
while(pos <= N) {
aib[pos] += val;
pos += (pos & -pos);
}
}
int query(int pos) {
int sum = 0;
while(pos) {
sum += aib[pos];
pos -= (pos & -pos);
}
return sum;
}
int search(int num) {
int pos = 0;
int bt = mxbit;
while(bt) {
if (pos + bt > N)
bt >>= 1;
else if (aib[pos + bt] == num) {
return pos + bt;
} else if (aib[pos + bt] > num) {
bt >>= 1;
} else if (aib[pos + bt] < num) {
num -= aib[pos+bt];
pos += bt;
bt >>= 1;
}
}
if (num != 0)
return -1;
return pos;
}