Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- struct node
- {
- node *l, *r;
- int key, tl, tr;
- node(int _tl, int _tr)
- {
- l = r = nullptr, key = 0, tl = _tl, tr = _tr;
- }
- };
- int get_key(node * a)
- {
- if (a == nullptr)
- return 0;
- return a->key;
- }
- void modify(node * cur, int pos, int val)
- {
- if (cur->tl + 1 == cur->tr)
- {
- cur->key += val;
- return;
- }
- int m = (cur->tl + cur->tr) / 2;
- if (pos < m)
- {
- if (cur->l == nullptr) cur->l = new node(cur->tl, m);
- modify(cur->l, pos, val);
- }
- else
- {
- if (cur->r == nullptr) cur->r = new node(m, cur->tr);
- modify(cur->r, pos, val);
- }
- cur->key = get_key(cur->l) + get_key(cur->r);
- }
- int get_sum(node * cur, int l, int r)
- {
- if (cur == nullptr)
- return 0;
- if (l <= cur->tl && cur->tr <= r)
- {
- return cur->key;
- }
- else if (l >= cur->tr || cur->tl >= r)
- {
- return 0;
- }
- int m = (cur->tl + cur->tr) / 2;
- return get_sum(cur->l, l, r) + get_sum(cur->r, l, r);
- }
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- node * tree = new node(-10, 10);
- modify(tree, 0, 3);
- modify(tree, -3, 6);
- cout << get_sum(tree, -10, 10) << ' ' << get_sum(tree, -3, -2) << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement