Pagini recente » Cod sursa (job #2998495) | Cod sursa (job #2571461) | Cod sursa (job #1395711) | Cod sursa (job #2615721) | Cod sursa (job #1555682)
#include<bits/stdc++.h>
using namespace std;
#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
typedef long long int lld;
const int INF = (1LL << 30) - 1;
const lld LINF = (1LL << 62) - 1;
template<class T>
class FenwickTree {
private:
int N;
vector<T> AIB;
void upd(const int &position, const T &value) {
for (int i = position; i <= N; i += i & (-i))
AIB[i] += value;
}
T qry(const int &position) {
T answer = 0;
for (int i = position; i >= 1; i -= i & (-i))
answer += AIB[i];
return answer;
}
public:
FenwickTree() {
this->N = 0;
this->AIB.clear();
}
FenwickTree(const int &N) {
this->N = N;
this->AIB.resize(N + 5);
}
FenwickTree(const int &N, const vector<T> &V) {
this->N = N;
this->AIB.resize(N + 5);
int index = 1;
for (auto x : V) {
upd(index, x);
index++;
}
}
FenwickTree(const int &N, const T *V) {
this->N = N;
this->AIB.resize(N + 5);
for (int index = 1; index <= N; index++)
upd(index, V[index]);
}
FenwickTree(const int &N, const T *V, const int &offset) {
this->N = N;
this->AIB.resize(N + 5);
for (int index = 1; index <= N; index++)
upd(index, V[offset - 1 + index]);
}
void update(const int &position, const T &value) {
upd(position, value);
}
T query(const int &position) {
return qry(position);
}
T query(const int &p0, const int &p1) {
return qry(p1) - qry(p0 - 1);
}
T search(T sum) {
int step = 0;
for (step = 1; step < N; step *= 2);
for (int i = 0; step >= 1; step /= 2)
if (i + step <= N && sum >= AIB[i + step]) {
i += step;
sum -= AIB[i];
if (sum == 0)
return i;
}
return -1;
}
};
int N, M;
FenwickTree<int> AIB;
int main() {
cin.sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("aib.in", "r", stdin);
freopen("aib.out", "w", stdout);
#endif
scanf("%d%d", &N, &M);
AIB = FenwickTree<int>(N);
for (int i = 1, x; i <= N; i++) {
scanf("%d", &x);
AIB.update(i, x);
}
for (int i = 1, op, a, b; i <= M; i++) {
scanf("%d", &op);
if (op == 0) {
scanf("%d%d", &a, &b);
AIB.update(a, b);
continue;
}
if (op == 1) {
scanf("%d%d", &a, &b);
printf("%d\n", AIB.query(a, b));
continue;
}
if (op == 2) {
scanf("%d", &a);
printf("%d\n", AIB.search(a));
continue;
}
}
return 0;
}