Advertisement
lalalalalalalaalalla

Untitled

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