Advertisement
lalalalalalalaalalla

Untitled

Nov 10th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 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. const int MAXN = 100001;
  55.  
  56. mt19937 my_rand(163721);
  57.  
  58. struct node {
  59. int x, y = my_rand(), size = 0;
  60. node *l, *r;
  61. node() {
  62. x = INF;
  63. l = r = nullptr;
  64. size = 1;
  65. }
  66. };
  67.  
  68. typedef node* pnode;
  69. typedef pair<pnode, pnode> Pair;
  70.  
  71. vector<node> a(MAXN);
  72. int ind = 0;
  73.  
  74. pnode root = nullptr;
  75.  
  76. void recalc(pnode a) {
  77. if (!a) return;
  78. a->size = 1;
  79. if (a->l) a->size += a->l->size;
  80. if (a->r) a->size += a->r->size;
  81. }
  82.  
  83. pnode merge(pnode a, pnode b) {
  84. if (!a) return b;
  85. if (!b) return a;
  86. if (a->y > b->y) {
  87. a->r = merge(a->r, b);
  88. recalc(a);
  89. return a;
  90. } else {
  91. b->l = merge(a, b->l);
  92. recalc(b);
  93. return b;
  94. }
  95. }
  96.  
  97. void split(pnode a, int x, pnode &l, pnode &r) {
  98. if (!a) {
  99. l = r = nullptr;
  100. return;
  101. }
  102. if (a->x > x) {
  103. split(a->l, x, l, r);
  104. a->l = r;
  105. recalc(a);
  106. r = a;
  107. } else {
  108. split(a->r, x, l, r);
  109. a->r = l;
  110. recalc(a);
  111. l = a;
  112. }
  113. }
  114.  
  115. void add(int x) {
  116. Pair q;
  117. split(root, x, q.first, q.second);
  118. pnode t = &a[ind++];
  119. t->size = 1;
  120. t->x = x;
  121. root = merge(q.first, merge(t, q.second));
  122. }
  123.  
  124. // split: x](
  125.  
  126. void kth(pnode a, int k) {
  127. // cout << "kth: " << k << "\n";
  128. if (!a) exit(0);
  129. if (k == 1) {
  130. cout << a->x << "\n";
  131. return;
  132. }
  133. if (k > (a->l ? a->l->size : 0)) {
  134. return kth(a->r, k - (a->l ? a->l->size : 0) - 1);
  135. } else {
  136. return kth(a->l, k);
  137. }
  138. }
  139.  
  140. void del(int x) {
  141. Pair q, q1;
  142. split(root, x, q.first, q.second);
  143. split(q.first, x - 1, q1.first, q1.second);
  144. root = merge(q1.first, q.second);
  145. }
  146.  
  147. signed main() {
  148. ios_base::sync_with_stdio(0);
  149. cin.tie(0);
  150. cout.tie(0);
  151. int n;
  152. cin >> n;
  153. string s;
  154. int x;
  155. Pair q;
  156. for (int i = 0; i < n; i++) {
  157. cin >> s >> x;
  158. if (s == "1" || s == "+1") {
  159. add(x);
  160. } else if (s == "0") {
  161. // if (root == nullptr) {
  162. // cout << "root is null\n";
  163. // return 1;
  164. // }
  165. kth(root, root->size - x + 1);
  166. } else {
  167. del(x);
  168. }
  169. // for (int i = 0; i < ind; i++) {
  170. // cout << a[i].x << " " << a[i].y << " " << a[i].size << "\n";
  171. // }
  172. }
  173. }
  174. /*
  175. 11
  176. +1 5
  177. +1 3
  178. +1 7
  179. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement