SHARE
TWEET

Untitled

lalalalalalalaalalla Nov 10th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     a->size = 1;
  78.     if (a->l) a->size += a->l->size;
  79.     if (a->r) a->size += a->r->size;
  80. }
  81.  
  82. pnode merge(pnode a, pnode b) {
  83.     if (!a) return b;
  84.     if (!b) return a;
  85.     if (a->y > b->y) {
  86.         a->r = merge(a->r, b);
  87.         recalc(a);
  88.         return a;
  89.     } else {
  90.         b->l = merge(a, b->l);
  91.         recalc(b);
  92.         return b;
  93.     }
  94. }
  95.  
  96. void split_by_x(pnode a, int x, pnode &l, pnode& r) {
  97.     if (!a) {
  98.         l = r = nullptr;
  99.         return;
  100.     }
  101.     // x == a.x -> x в правой половине
  102.     if (a->x < x) {
  103.         split_by_x(a->r, x, l, r);
  104.         a->r = l;
  105.         recalc(a);
  106.         l = a;
  107.     } else {
  108.         split_by_x(a->l, x, l, r);
  109.         a->l = r;
  110.         recalc(a);
  111.         r = a;
  112.     }
  113. }
  114.  
  115. bool check (pnode a, int x) {
  116.     if (!a) return 0;
  117.     if (a->x == x) return 1;
  118.     if (a->x > x) return check(a->l, x);
  119.     return check(a->r, x);
  120. }
  121.  
  122. void add(int x) {
  123.     Pair q;
  124.     split_by_x(root, x, q.first, q.second);
  125.     pnode t = &a[ind++];
  126.     t->x = x;
  127.     t->size = 1;
  128.     root = merge(q.first, merge(t, q.second));
  129. }
  130.  
  131. int right_most(pnode a) {
  132.     if (a->r) return right_most(a->r);
  133.     return a->x;
  134. }
  135.  
  136. void kth(pnode a, int k) {
  137. //    cout << "kth: " << a->x << "\n";
  138. //    cout << k << "\n";
  139.     if (k == 1) {
  140.         cout << a->x << "\n";
  141.         return;
  142.     }
  143.     if (k > (a->l ? a->l->size : 0)) {
  144.         k -= (a->l ? a->l->size : 0) + 1;
  145.         kth(a->r, k);
  146.     } else {
  147.         kth(a->l, k);
  148.     }
  149. }
  150.  
  151. void del(pnode a, int x) {
  152.     if (a->x == x) {
  153.         cout << "here : ";
  154.         cout << a->size << " ";
  155.         a = merge(a->l, a->r);
  156.         cout << a->size << "\n";
  157.         cout << "a.x" << " " << a->x << "\n";
  158.         cout << "a.y " << a->y << "\n";
  159.         return;
  160.     }
  161.     if (a->x > x) {
  162.         del(a->l, x);
  163.     } else {
  164.         del(a->r, x);
  165.     }
  166.     recalc(a);
  167. }
  168.  
  169. signed main() {
  170.     ios_base::sync_with_stdio(0);
  171.     cin.tie(0);
  172.     cout.tie(0);
  173.     int n;
  174.     cin >> n;
  175.     string t;
  176.     int x;
  177.     for (int i = 0; i < n; i++) {
  178.         cin >> t >> x;
  179.         if (t == "+1" || t == "1") {
  180.             add(x);
  181.         } else if (t == "0") {
  182.             kth(root, root->size - x + 1);
  183.         } else del(root, x);
  184.         for (int i = 0; i < ind; i++) {
  185.             cout << a[i].x << " " << a[i].y << " " << a[i].size << "\n";
  186.         }
  187.     }
  188. }
  189. /*
  190. 11
  191. +1 5
  192. +1 3
  193. +1 7
  194. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top