Advertisement
GerONSo

Untitled

Jun 8th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. //#define pragma
  2.  
  3. #ifdef pragma
  4. #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
  5. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  6. #endif // pragma
  7.  
  8. #include<bits/stdc++.h>
  9. #include <ext/pb_ds/assoc_container.hpp>
  10. #include <ext/pb_ds/tree_policy.hpp>
  11.  
  12. #define ll long long
  13. #define all(x) begin(x), end(x)
  14. #define pb push_back
  15. #define x first
  16. #define y second
  17. //#define int long long
  18. #define zero(two) memset(two, 0, sizeof(two))
  19.  
  20. using namespace std;
  21. using namespace __gnu_pbds;
  22.  
  23.  
  24. typedef vector<int> vi;
  25. typedef vector<bool> vb;
  26. typedef pair<int, int> pii;
  27. typedef long double ld;
  28. typedef vector<vi> matrix;
  29. template<typename T>
  30. using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  31.  
  32. const ld PI = atan2(0, -1);
  33.  
  34. void seriy() {
  35. ios::sync_with_stdio(0);
  36. cin.tie(0);
  37. cout.tie(0);
  38. // cout << fixed << setprecision(10);
  39. #if _offline
  40. freopen("input.txt", "r", stdin);
  41. freopen("output.txt", "w", stdout);
  42. #else
  43. freopen("next.in", "r", stdin);
  44. freopen("next.out", "w", stdout);
  45. #endif
  46. }
  47.  
  48. const int MAXN = 2e5 + 10;
  49. const int INF = 2e9 + 7;
  50. const int MAXLOG = 20;
  51. const int MOD = 1e9;
  52. const int BASE = 47;
  53. const int MAX = (1ll << 32) - 1;
  54.  
  55. struct treap {
  56. int x, y;
  57. treap *left = nullptr;
  58. treap *right = nullptr;
  59.  
  60. treap(int x1, int y1) {
  61. x = x1;
  62. y = y1;
  63. }
  64.  
  65. treap() {
  66. }
  67. };
  68.  
  69. struct pt {
  70. int x, y, num;
  71. };
  72.  
  73. treap *root = nullptr;
  74. int sz = 1;
  75.  
  76. void split(treap *t, int k, treap *&a, treap *&b) {
  77. if(t == nullptr) {
  78. a = nullptr;
  79. b = nullptr;
  80. return;
  81. }
  82. if(k > t -> x) {
  83. split(t -> right, k, t -> right, b);
  84. a = t;
  85. }
  86. else {
  87. split(t -> left, k, a, t -> left);
  88. b = t;
  89. }
  90. }
  91.  
  92. treap *unite(treap *t1, treap *t2) {
  93. if(t2 == nullptr) {
  94. return t1;
  95. }
  96. if(t1 == nullptr) {
  97. return t2;
  98. }
  99. if(t1 -> y > t2 -> y) {
  100. t1 -> right = unite(t1 -> right, t2);
  101. return t1;
  102. }
  103. else {
  104. t2 -> left = unite(t1, t2 -> left);
  105. return t2;
  106. }
  107. }
  108.  
  109. void add(int x) {
  110. // cerr << x << '\n';
  111. mt19937 rr(chrono::high_resolution_clock::now().time_since_epoch().count());
  112. treap *cur = new treap(x, rr());
  113. if(root == nullptr) {
  114. root = cur;
  115. return;
  116. }
  117. pair<treap*, treap*> kek;
  118. split(root, x, kek.x, kek.y);
  119. kek.x = unite(kek.x, cur);
  120. root = unite(kek.x, kek.y);
  121. }
  122.  
  123. int get_ans(treap *kek) {
  124. // cerr << kek -> x << '\n';
  125. if(kek -> left == nullptr) {
  126. return kek -> x;
  127. }
  128. return get_ans(kek -> left);
  129. }
  130.  
  131. int get(int x) {
  132. pair<treap*, treap*> kek;
  133. split(root, x, kek.x, kek.y);
  134. // if(x == 2) {
  135. // cerr << (kek.x == nullptr) << '\n';
  136. // }
  137. // cerr << '\n';
  138. if(kek.y == nullptr) {
  139. root = unite(kek.x, kek.y);
  140. return -1;
  141. }
  142. // cerr << x << '\n';
  143. int ans = get_ans(kek.y);
  144.  
  145. root = unite(kek.x, kek.y);
  146. return ans;
  147. }
  148.  
  149. signed main() {
  150. seriy();
  151. int q;
  152. cin >> q;
  153. int prev = -1;
  154. while(q--) {
  155. char tp;
  156. cin >> tp;
  157. if(tp == '+') {
  158. int n;
  159. cin >> n;
  160. if(prev == -1) {
  161. // cerr << n << '\n';
  162. add(n);
  163. }
  164. else {
  165. // cerr << (n + prev) % MOD << '\n';
  166. add((n + prev) % MOD);
  167. }
  168. prev = -1;
  169. }
  170. else {
  171. int n;
  172. cin >> n;
  173. prev = get(n);
  174. cout << prev << '\n';
  175. }
  176. }
  177. return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement