Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <string>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <set>
  9. #include <queue>
  10.  
  11. using namespace std;
  12.  
  13. const long long maxx = long long(1e9) + 10;
  14. const long long sz = 4e5;
  15. const long long mod = (1e9);
  16.  
  17. struct node {
  18.     node* left;
  19.     node* right;
  20.     long long key;
  21.     long long prio;
  22.     long long ind;
  23.     long long w;
  24.     node() {
  25.         left = NULL;
  26.         right = NULL;
  27.     }
  28.     node(long long a, long long b, long long c) {
  29.         left = NULL;
  30.         right = NULL;
  31.         key = a;
  32.         prio = b;
  33.         ind = c;
  34.         w = ind;
  35.     }
  36. };
  37.  
  38. void update(node* cur) {
  39.     if (cur == NULL) {
  40.         return;
  41.     }
  42.     long long l = 0, r = 0;
  43.     if ((*cur).left != NULL) {
  44.         l = (*cur).left->w;
  45.     }
  46.     if ((*cur).right != NULL) {
  47.         r = (*cur).right->w;
  48.     }
  49.     cur->w = l + r + (*cur).ind;
  50. }
  51.  
  52. pair<node*, node*> split(long long arg, node* t) {
  53.     pair <node*, node*> res;
  54.     res.first = NULL;
  55.     res.second = NULL;
  56.     if (t == NULL) {
  57.         return res;
  58.     }
  59.     if (arg >= (*t).key) {
  60.         res.first = t;
  61.         pair<node*, node*> buf;
  62.         buf = split(arg, (*t).right);
  63.         (*t).right = buf.first;
  64.         res.second = buf.second;
  65.     }
  66.     else {
  67.         res.second = t;
  68.         pair<node*, node*> buf;
  69.         buf = split(arg, (*t).left);
  70.         (*t).left = buf.second;
  71.         res.first = buf.first;
  72.     }
  73.     update(res.first);
  74.     update(res.second);
  75.     return res;
  76. }
  77.  
  78. // PRE: a.key < b.key
  79. node* merge(node* a, node* b) {
  80.     if (a == NULL) {
  81.         return b;
  82.     }
  83.     if (b == NULL) {
  84.         return a;
  85.     }
  86.     if ((*a).prio > (*b).prio) {
  87.         (*a).right = merge((*a).right, b);
  88.         update(a);
  89.         return a;
  90.     }
  91.     else {
  92.         (*b).left = merge(a, (*b).left);
  93.         update(b);
  94.         return b;
  95.     }
  96. }
  97.  
  98. node* unite(node* a, node* b) {
  99.     if (a == NULL) {
  100.         return b;
  101.     }
  102.     if (b == NULL) {
  103.         return a;
  104.     }
  105.     if ((*a).prio < (*b).prio) {
  106.         swap(a, b);
  107.     }
  108.     pair <node*, node*> r = split((*a).key, b);
  109.     (*a).left = unite((*a).left, r.first);
  110.     (*a).right = unite((*a).right, r.second);
  111.     update(a);
  112.     return a;
  113. }
  114.  
  115. bool find(node* t, long long k) {
  116.     if (t == NULL) {
  117.         return false;
  118.     }
  119.     if ((*t).ind == k) {
  120.         return true;
  121.     }
  122.     if ((*t).ind < k) {
  123.         if ((*t).right != NULL) {
  124.             return find((*t).right, k);
  125.         }
  126.         return false;
  127.     }
  128.     if ((*t).left != NULL) {
  129.         return find((*t).left, k);
  130.     }
  131.     return false;
  132. }
  133.  
  134. node* root;
  135.  
  136. set <int> s;
  137.  
  138. int main()
  139. {
  140.     freopen("input.txt", "r", stdin);
  141.     freopen("output.txt", "w", stdout);
  142.     long long n;
  143.     root = NULL;
  144.     cin >> n;
  145.     long long last_type = -1;
  146.     long long last_sum = 0;
  147.     for (long long i = 0; i < n; i++) {
  148.         char c;
  149.         cin >> c;
  150.         if (c == '+') {
  151.             long long k;
  152.             cin >> k;
  153.             if (last_type == 2) {
  154.                 long long m = (k + last_sum) % mod;
  155.                 k = long long(m);
  156.             }
  157.             if (!find(root, k)) {
  158.                 node* new_one = new node(k, rand() % maxx, k);
  159.                 s.insert(k);
  160.                 if (root == NULL) {
  161.                     root = new_one;
  162.                 }
  163.                 else {
  164.                     root = unite(root, new_one);
  165.                 }
  166.             }
  167.             last_type = 1;
  168.         }
  169.         if (c == '?') {
  170.             long long l, r;
  171.             cin >> l >> r;
  172.             pair <node*, node*> x = split(l - 1, root);
  173.             pair <node*, node*> y = split(r, x.second);
  174.             if (y.first == NULL) {
  175.                 printf("0\n");
  176.                 last_sum = 0;
  177.             }
  178.             else {
  179.                 printf("%d\n", (*y.first).w);
  180.                 last_sum = (*y.first).w;
  181.             }
  182.             root = merge(y.first, y.second);
  183.             root = merge(x.first, root);
  184.             last_type = 2;
  185.         }
  186.     }
  187.     return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement