Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FenwickTree
- {
- int size;
- vector<int> a;
- vector<long long> t;
- long long get(int index) {
- long long result = 0;
- for (; index > 0; index -= index & -index) {
- result += t[index];
- }
- return result;
- }
- void update(int index, int delta) {
- for (; index < size; index += index & -index) {
- t[index] += delta;
- }
- }
- public:
- FenwickTree(vector<int> a)
- : a(a)
- , size(a.size() + 1)
- , t(size, 0)
- {
- for (int i = 0; i < a.size(); ++i) {
- update(i + 1, a[i]);
- }
- }
- void set(int index, int value) {
- int delta = value - a[index];
- a[index] = value;
- update(index + 1, delta);
- }
- long long get_segment(int start, int end) {
- return get(end + 1) - get(start);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement