Advertisement
lalalalalalalaalalla

Untitled

Nov 10th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  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.  
  55. struct node{
  56. node *l, *r;
  57. int x, y = rand();
  58. node() {
  59. x = -1;
  60. l = r = nullptr;
  61. }
  62. node(int x_) {
  63. x = x_;
  64. l = r = nullptr;
  65. }
  66. };
  67.  
  68. typedef node* pnode;
  69.  
  70. vector<node> a(300000);
  71. int ind = 0;
  72.  
  73. pnode merge(pnode a, pnode b) {
  74. if (!a) return b;
  75. if (!b) return a;
  76. if (a->y > b->y) {
  77. a->r = merge(a->r, b);
  78. return a;
  79. } else {
  80. b->l = merge(a, b->l);
  81. return b;
  82. }
  83. }
  84.  
  85. void split(pnode a, int x, pnode& l, pnode& r) {
  86. if (!a) {
  87. l = r = nullptr;
  88. return;
  89. }
  90. if (a->x < x) {
  91. split(a->r, x, l, r);
  92. a->r = l;
  93. l = a;
  94. } else {
  95. split(a->l, x, l, r);
  96. a->l = r;
  97. r = a;
  98. }
  99. }
  100.  
  101.  
  102. pnode root = nullptr;
  103.  
  104. int left_most(pnode t) {
  105. if (t->l) {
  106. return left_most(t->l);
  107. } else return t->x;
  108. }
  109.  
  110. bool check(pnode cur, int x) {
  111. if (!cur) return 0;
  112. if (cur->x == x) return 1;
  113. if (cur->x > x) {
  114. return check(cur->l, x);
  115. } else return check(cur->r, x);
  116. }
  117.  
  118. void add(int x) {
  119. if (check(root, x)) return;
  120. pair<pnode, pnode> q;
  121. split(root, x, q.first, q.second);
  122. pnode t = &a[ind++];
  123. t->x = x;
  124. root = merge(q.first, merge(t, q.second));
  125. }
  126.  
  127. int last = 0;
  128.  
  129.  
  130. void next(int x) {
  131. pair<pnode, pnode> q;
  132. split(root, x, q.first, q.second);
  133. if (!q.second) {
  134. last = -1;
  135. } else {
  136. last = left_most(q.second);
  137. }
  138. root = merge(q.first, q.second);
  139. cout << last << "\n";
  140. }
  141.  
  142. signed main() {
  143. ios_base::sync_with_stdio(0);
  144. cin.tie(0);
  145. cout.tie(0);
  146. int n;
  147. cin >> n;
  148. char t;
  149. int x;
  150. while (n--) {
  151. // for (int i = 0; i < ind; i++) {
  152. // cout << a[i].x << " " << a[i].y << "\n";
  153. // }
  154. cin >> t >> x;
  155. if (t == '+') {
  156. add((x + last)%1000000000);
  157. } else {
  158. next(x);
  159. }
  160. }
  161. }
  162. /*
  163.  
  164. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement