Advertisement
lalalalalalalaalalla

Untitled

Nov 10th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 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. pnode root = nullptr;
  102.  
  103. void add(int x) {
  104. pair<pnode, pnode> q;
  105. split(root, x, q.first, q.second);
  106. pnode t = &a[ind++];
  107. t->x = x;
  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, 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. // for (int i = 0; i < ind; i++) {
  141. // cout << a[i].x << " " << a[i].y << "\n";
  142. // }
  143. cin >> t >> x;
  144. if (t == '+') {
  145. add(x + last);
  146. } else {
  147. next(x);
  148. }
  149. }
  150. }
  151. /*
  152.  
  153. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement