Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <cmath>
- #include <algorithm>
- #include <set>
- #include <queue>
- using namespace std;
- const long long maxx = long long(1e9) + 10;
- const long long sz = 4e5;
- const long long mod = (1e9);
- struct node {
- node* left;
- node* right;
- long long key;
- long long prio;
- long long ind;
- long long w;
- node() {
- left = NULL;
- right = NULL;
- }
- node(long long a, long long b, long long c) {
- left = NULL;
- right = NULL;
- key = a;
- prio = b;
- ind = c;
- w = ind;
- }
- };
- void update(node* cur) {
- if (cur == NULL) {
- return;
- }
- long long l = 0, r = 0;
- if ((*cur).left != NULL) {
- l = (*cur).left->w;
- }
- if ((*cur).right != NULL) {
- r = (*cur).right->w;
- }
- cur->w = l + r + (*cur).ind;
- }
- pair<node*, node*> split(long long arg, node* t) {
- pair <node*, node*> res;
- res.first = NULL;
- res.second = NULL;
- if (t == NULL) {
- return res;
- }
- if (arg >= (*t).key) {
- res.first = t;
- pair<node*, node*> buf;
- buf = split(arg, (*t).right);
- (*t).right = buf.first;
- res.second = buf.second;
- }
- else {
- res.second = t;
- pair<node*, node*> buf;
- buf = split(arg, (*t).left);
- (*t).left = buf.second;
- res.first = buf.first;
- }
- update(res.first);
- update(res.second);
- return res;
- }
- // PRE: a.key < b.key
- node* merge(node* a, node* b) {
- if (a == NULL) {
- return b;
- }
- if (b == NULL) {
- return a;
- }
- if ((*a).prio > (*b).prio) {
- (*a).right = merge((*a).right, b);
- update(a);
- return a;
- }
- else {
- (*b).left = merge(a, (*b).left);
- update(b);
- return b;
- }
- }
- node* unite(node* a, node* b) {
- if (a == NULL) {
- return b;
- }
- if (b == NULL) {
- return a;
- }
- if ((*a).prio < (*b).prio) {
- swap(a, b);
- }
- pair <node*, node*> r = split((*a).key, b);
- (*a).left = unite((*a).left, r.first);
- (*a).right = unite((*a).right, r.second);
- update(a);
- return a;
- }
- bool find(node* t, long long k) {
- if (t == NULL) {
- return false;
- }
- if ((*t).ind == k) {
- return true;
- }
- if ((*t).ind < k) {
- if ((*t).right != NULL) {
- return find((*t).right, k);
- }
- return false;
- }
- if ((*t).left != NULL) {
- return find((*t).left, k);
- }
- return false;
- }
- node* root;
- set <int> s;
- int main()
- {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- long long n;
- root = NULL;
- cin >> n;
- long long last_type = -1;
- long long last_sum = 0;
- for (long long i = 0; i < n; i++) {
- char c;
- cin >> c;
- if (c == '+') {
- long long k;
- cin >> k;
- if (last_type == 2) {
- long long m = (k + last_sum) % mod;
- k = long long(m);
- }
- if (!find(root, k)) {
- node* new_one = new node(k, rand() % maxx, k);
- s.insert(k);
- if (root == NULL) {
- root = new_one;
- }
- else {
- root = unite(root, new_one);
- }
- }
- last_type = 1;
- }
- if (c == '?') {
- long long l, r;
- cin >> l >> r;
- pair <node*, node*> x = split(l - 1, root);
- pair <node*, node*> y = split(r, x.second);
- if (y.first == NULL) {
- printf("0\n");
- last_sum = 0;
- }
- else {
- printf("%d\n", (*y.first).w);
- last_sum = (*y.first).w;
- }
- root = merge(y.first, y.second);
- root = merge(x.first, root);
- last_type = 2;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement