SHARE
TWEET

Untitled

lalalalalalalaalalla Nov 10th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <iomanip>
  5. #include <queue>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <tuple>
  9. #include <iomanip>
  10. #include <stdio.h>
  11. #include <map>
  12. #include <bitset>
  13. #include <set>
  14. #include <stack>
  15. #include <queue>
  16. #include <unordered_set>
  17. #include <cassert>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include <random>
  21.  
  22.  
  23. //#pragma GCC optimize("Ofast,no-stack-protector")
  24. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
  25. //#pragma GCC optimize("unroll-loops")
  26. //#pragma GCC optimize("fast-math")
  27. //#pragma GCC optimize("section-anchors")
  28. //#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
  29. //#pragma GCC optimize("vpt")
  30. //#pragma GCC optimize("rename-registers")
  31. //#pragma GCC optimize("move-loop-invariants")
  32. //#pragma GCC optimize("unswitch-loops")
  33. //#pragma GCC optimize("function-sections")
  34. //#pragma GCC optimize("data-sections")
  35. //#pragma GCC optimize("branch-target-load-optimize")
  36. //#pragma GCC optimize("branch-target-load-optimize2")
  37. //#pragma GCC optimize("btr-bb-exclusive")
  38.  
  39.  
  40. #define int long long
  41. #define ll long long
  42. #define ull unsigned long long
  43. #define all(a) a.begin(), a.end()
  44. #define pii pair<int, int>
  45. #define pb push_back
  46. #define ld long double
  47.  
  48.  
  49. using namespace std;
  50.  
  51. const int INF = 1e17;
  52. //const int mod = 2600000069;
  53. //const int p = 179;
  54. const int MAXN = 300001;
  55.  
  56. mt19937 my_rand(time(0));
  57.  
  58. struct node {
  59.     int x, y = my_rand(), sum = 0;
  60.     node *l, *r;
  61.     node() {
  62.         x = -1;
  63.         l = r = 0;
  64.     }
  65. };
  66.  
  67. typedef node* pnode;
  68. typedef pair<pnode, pnode> p;
  69.  
  70. vector<node> a(MAXN);
  71. int ind = 0;
  72.  
  73. pnode root = 0;
  74.  
  75. pnode merge(pnode a, pnode b) {
  76.     if (!a) return b;
  77.     if (!b) return a;
  78.     if (a->y > b->y) {
  79.         a->r = merge(a->r, b);
  80.         a->sum = (a->r ? a->r->sum : 0) + (a->l ? a->l->sum : 0) + a->x;
  81.         return a;
  82.     } else {
  83.         b->l = merge(a, b->l);
  84.         b->sum = (b->r ? b->r->sum : 0) + (b->l ? b->l->sum : 0) + b->x;
  85.         return b;
  86.     }
  87. }
  88.  
  89. void split(pnode a, int x, pnode& l, pnode& r) {
  90.     if (!a) {
  91.         l = r = 0;
  92.         return;
  93.     }
  94.     if (a->x < x) {
  95.         split(a->r, x, l, r);
  96.         a->r = l;
  97.         a->sum = (a->r ? a->r->sum : 0) + (a->l ? a->l->sum : 0) + a->x;
  98.         l = a;
  99.     } else {
  100.         split(a->l, x, l, r);
  101.         a->l = r;
  102.         a->sum = (a->r ? a->r->sum : 0) + (a->l ? a->l->sum : 0) + a->x;
  103.         r = a;
  104.     }
  105. }
  106.  
  107. bool check(pnode a, int x) {
  108.     if (!a) return 0;
  109.     if (a->x == x) return 1;
  110.     if (a->x < x) return check(a->r, x);
  111.     else return check(a->l, x);
  112. }
  113.  
  114. void add(int x) {
  115.     if (check(root, x)) return;
  116.     p q;
  117.     split(root, x, q.first, q.second);
  118.     pnode t = &a[ind++];
  119.     t->x = x;
  120.     t->sum = x;
  121. //    cout << "here\n";
  122.     root = merge(q.first, merge(t, q.second));
  123. //    cout << "after\n";
  124. }
  125.  
  126. int last = 0;
  127.  
  128. void sum(int l, int r) {
  129.     if (!root) {last = 0; cout << "0\n"; return;}
  130.     p q1;
  131.     split(root, r + 1, q1.first, q1.second);
  132.     if (!q1.first) {
  133.         last = 0;
  134.         cout << "0\n";
  135.         root = merge(q1.first, q1.second);
  136.         return;
  137.     }
  138.     p q2;
  139.     split(q1.first, l, q2.first, q2.second);
  140.     if (!q2.second) {
  141.         last = 0;
  142.         cout << "0\n";
  143.         root = merge(merge(q2.first, q2.second), q1.second);
  144.         return;
  145.     }
  146.     last = q2.second->sum;
  147.     cout << q2.second->sum << "\n";
  148.     root = merge(merge(q2.first, q2.second), q1.second);
  149. }
  150.  
  151. const int mod = 1e9;
  152.  
  153. signed main() {
  154.     ios_base::sync_with_stdio(0);
  155.     cin.tie(0);
  156.     cout.tie(0);
  157.     int n;
  158.     cin >> n;
  159.     char t;
  160.     int x, x1;
  161.     for (int i = 0; i < n; i++) {
  162. //        for (int i = 0; i < ind; i++) {
  163. //            cout << a[i].x << " " << a[i].y << "\n";
  164. //        }
  165.         cin >> t >> x;
  166.         if (t == '+') {
  167. //            cout << "last: " << last <<"\n";
  168.             add((x + last) % mod);
  169.             last = 0;
  170.         } else {
  171.             cin >> x1;
  172.             sum(x, x1);
  173.         }
  174.     }
  175. }
  176. /*
  177. 4
  178. + 4
  179. + 7
  180. ? 5 6
  181. ? 3 5
  182. -> 0 4
  183. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top