Pagini recente » Cod sursa (job #1284408) | Istoria paginii utilizator/vodkainthejar | Cod sursa (job #1775291) | Cod sursa (job #3282913) | Cod sursa (job #1999284)
#ifdef __GNUC__
#pragma GCC target("sse4,avx")
#endif
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
template<typename T>
class FenwickTree {
private:
vector<T> tree;
public:
FenwickTree(const int n = 0) : tree(n) {
}
FenwickTree(const vector<T>& v): tree(v.size()) {
for (int i = 0; i < (int)tree.size(); ++i) {
tree[i] += v[i];
const int j = i | (i + 1);
if (j < (int)tree.size()) {
tree[j] += tree[i];
}
}
}
void add(int idx, const T& value) {
while (idx < (int)tree.size()) {
tree[idx] += value;
idx |= idx + 1;
}
}
T queryPrefix(int idx) const {
T sum = 0;
while (idx >= 0) {
sum += tree[idx];
idx = (idx & (idx + 1)) - 1;
}
return sum;
}
T query(int lo, int hi) const {
return queryPrefix(hi) - queryPrefix(lo - 1);
}
T getValue(int idx) const {
T value = tree[idx];
const int parent = (idx & (idx + 1)) - 1;
idx--;
while (idx != parent) {
value -= tree[idx];
idx = (idx & (idx + 1)) - 1;
}
return value;
}
static int binaryLogarithm(int n) {
return 31 - __builtin_clz(n);
}
int lowerBound(T value) const {
value -= 1;
int idx = -1;
for (int step = 1 << binaryLogarithm(tree.size()); step > 0; step >>= 1) {
if (step + idx < (int)tree.size() && value >= tree[step + idx]) {
value -= tree[step + idx];
idx += step;
}
}
return idx + 1;
}
};
int main() {
#ifdef INFOARENA
ifstream cin("aib.in");
ofstream cout("aib.out");
#endif
int n, q; cin >> n >> q;
vector<int> a(n);
for (int& x: a) {
cin >> x;
}
FenwickTree<int> tree(a);
while (q--> 0) {
int op_type; cin >> op_type;
if (op_type == 0) {
int idx, value; cin >> idx >> value; idx--;
tree.add(idx, value);
} else if (op_type == 1) {
int idx0, idx1; cin >> idx0 >> idx1; idx0--; idx1--;
cout << tree.query(idx0, idx1) << '\n';
} else {
int sum; cin >> sum;
int where = tree.lowerBound(sum);
if (tree.queryPrefix(where) != sum) {
cout << "-1\n";
} else {
cout << where + 1 << '\n';
}
}
}
return 0;
}