rembocoder

Untitled

May 7th, 2023
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.25 KB | None | 0 0
  1. #pragma GCC target ("avx2")
  2. #pragma GCC optimization ("O3")
  3. #pragma GCC optimization ("unroint-loops")
  4. #pragma GCC optimize("Ofast,no-stack-protector")
  5. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
  6. #pragma GCC optimize("unroll-loops")
  7. #include <bits/stdc++.h>
  8. #define int           int64_t
  9. #define ull           unsigned long long
  10. #define ld            long double
  11. #define INF           (int)2e16
  12.  
  13. #define rng(i,a,b)    for(int i=int(a);i<int(b);i++)
  14. #define gnr(i,a,b)    for(int i=int(b)-1;i>=int(a);i--)
  15. #define per(i,b)      gnr(i,0,b)
  16. #define rep(i,b)      rng(i,0,b)
  17.  
  18. #define endl          '\n'
  19. #define yes           "YES\n"
  20. #define no            "NO\n"
  21.  
  22. #define F             first
  23. #define S             second
  24.  
  25. #define all(a)        a.begin(),a.end()
  26. #define rall(a)       a.rbegin(),a.rend()
  27. #define __sort(x)     sort(x.begin(), x.end())
  28. #define __rsort(x)    sort(x.rbegin(), x.rend())
  29. #define __lcm(a, b)   (int(a) * int(b) / (__gcd(int(a), int(b))))
  30.  
  31. #define out_1arr(a, ch123)   for(auto el_ : a) cout << el_ << ch123;
  32. #define out_2arr(a, ch123)   for(auto row_ : a) {for(auto col_ : row_) cout << col_ << ch123; cout << endl;}
  33.  
  34. #define optimize()    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
  35. #define multitest()   int ttt;cin >> ttt;rep(t_cnt, ttt)
  36. using namespace std;
  37. #ifdef LOCAL
  38. mt19937 tw(9450189);
  39. #else
  40. mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
  41. #endif
  42. uniform_int_distribution<int> ll_distr;
  43. int rnd(int a, int b) { return ll_distr(tw) % (b - a + 1) + a; }
  44.  
  45. struct Node {
  46.     int l, r, key, priority, size;
  47.  
  48.     Node(int k = -1, int l = -1, int r = -1,
  49.          int pr = rnd(1, 1000000000)): key(k), priority(pr), l(l),
  50.          r(r), size(1) {}
  51. };
  52.  
  53. const int NMAX = 2e6;
  54. vector<Node> t(NMAX);
  55. int cur_new = 0, root = -1;
  56.  
  57. int get_size(int v) {
  58.     if (v == -1)
  59.         return 0;
  60.     return t[v].size;
  61. }
  62.  
  63. void update_size(int v) {
  64.     if (v == -1) return;
  65.     t[v].size = get_size(t[v].l) + get_size(t[v].r) + 1;
  66. }
  67.  
  68. int merge(int l, int r) {
  69.     if (l == -1) return r;
  70.     if (r == -1) return l;
  71.     if (t[l].priority < t[r].priority) {
  72.         t[l].r = merge(t[l].r, r);
  73.         update_size(l);
  74.         return l;
  75.     } else {
  76.         t[l].l = merge(l, t[r].l);
  77.         update_size(r);
  78.         return r;
  79.     }
  80. }
  81.  
  82. void split(int v, int key, int &l, int &r) {
  83.     if (v == -1)
  84.         l = r = -1;
  85.     else if (t[v].key < key) {
  86.         split(t[v].r, key, t[v].r, r);
  87.         l = v;
  88.     } else {
  89.         split(t[v].l, key, l, t[v].l);
  90.         r = v;
  91.     }
  92.     update_size(v);
  93. }
  94.  
  95. int insert(int v, int cur) {
  96.     if (v == -1) {
  97.         return cur;
  98.     } else if (t[cur].priority < t[v].priority) {
  99.         split(v, t[cur].key, t[cur].l, t[cur].r);
  100.         update_size(cur);
  101.         return cur;
  102.     }
  103.     if (t[cur].key < t[v].key) {
  104.         t[v].l = insert(t[v].l, cur);
  105.     } else {
  106.         t[v].r = insert(t[v].r, cur);
  107.     }
  108.     update_size(v);
  109.     return v;
  110. }
  111.  
  112. int erase(int v, int key) {
  113.     if (v == -1) {
  114.         return v;
  115.     }
  116.     if (t[v].key == key) {
  117.         return merge(t[v].l, t[v].r);
  118.     }
  119.     if (key < t[v].key) {
  120.         t[v].l = erase(t[v].l, key);
  121.     } else {
  122.         t[v].r = erase(t[v].r, key);
  123.     }
  124.     update_size(v);
  125.     return v;
  126. }
  127.  
  128. bool contains(int v, int key) {
  129.     if (v == -1) {
  130.         return 0;
  131.     } else if (t[v].key == key) {
  132.         return 1;
  133.     }
  134.     if (key > t[v].key) {
  135.         return contains(t[v].r, key);
  136.     } else {
  137.         return contains(t[v].l, key);
  138.     }
  139. }
  140.  
  141. int find_greater(int v, int x) {
  142.     if (v == -1) {
  143.         return INF;
  144.     }
  145.     if (t[v].key > x) {
  146.         return min(find_greater(t[v].l, x), t[v].key);
  147.     } else {
  148.         return find_greater(t[v].r, x);
  149.     }
  150. }
  151.  
  152. int find_lower(int v, int x) {
  153.     if (v == -1) {
  154.         return -INF;
  155.     }
  156.     if (t[v].key < x) {
  157.         return max(find_lower(t[v].r, x), t[v].key);
  158.     } else {
  159.         return find_lower(t[v].l, x);
  160.     }
  161. }
  162.  
  163. int find_kth(int v, int k) {
  164.     if (v == -1)
  165.         return -INF;
  166.     int sz = get_size(t[v].l);
  167.     if (k == sz) {
  168.         return t[v].key;
  169.     } else if (k < sz) {
  170.         return find_kth(t[v].l, k);
  171.     } else {
  172.         return find_kth(t[v].r, k - sz - 1);
  173.     }
  174. }
  175.  
  176. void insert(int x) {
  177.     cur_new++;
  178.     t[cur_new - 1] = {x};
  179.     root = insert(root, cur_new - 1);
  180. }
  181.  
  182. void erase(int x) {
  183.     root = erase(root, x);
  184. }
  185.  
  186. int find_greater(int x) {
  187.     return find_greater(root, x);
  188. }
  189.  
  190. int find_lower(int x) {
  191.     return find_lower(root, x);
  192. }
  193.  
  194. bool contains(int x) {
  195.     return contains(root, x);
  196. }
  197.  
  198. int find_kth(int x) {
  199.     return find_kth(root, x);
  200. }
  201.  
  202. void solve() {
  203.     string type;
  204.     while (cin >> type) {
  205.         int x;
  206.         cin >> x;
  207.         if (type == "insert") {
  208.             if (!contains(x)) {
  209.                 insert(x);
  210.             }
  211.         } else if (type == "delete") {
  212.             erase(x);
  213.         } else if (type == "exists") {
  214.             if (contains(x)) {
  215.                 cout << "true\n";
  216.             } else {
  217.                 cout << "false\n";
  218.             }
  219.         } else if (type == "next") {
  220.             int res = find_greater(x);
  221.             if (res == INF) {
  222.                 cout << "none\n";
  223.             } else {
  224.                 cout << res << endl;
  225.             }
  226.         } else if (type == "prev") {
  227.             int res = find_lower(x);
  228.             if (res == -INF) {
  229.                 cout << "none\n";
  230.             } else {
  231.                 cout << res << endl;
  232.             }
  233.         } else if (type == "kth") {
  234.             x--;
  235.             int res = find_kth(x);
  236.             if (res == -INF) {
  237.                 cout << "none\n";
  238.             } else {
  239.                 cout << res << endl;
  240.             }
  241.         }
  242.         cout.flush();
  243.     }
  244. }
  245.  
  246. int32_t main() {
  247. #ifdef LOCAL
  248.     freopen("..\\input.txt","r",stdin);
  249.     freopen("..\\output.txt","w",stdout);
  250.     freopen("..\\error.txt","w",stderr);
  251.     auto start_time = clock();
  252.     cerr << setprecision(3) << fixed;
  253. #endif
  254.     optimize();
  255.     //multitest()
  256.     solve();
  257. #ifdef LOCAL
  258.     auto end_time = clock();
  259.     cerr << "Execution time: " << (end_time - start_time) * (int) 1e3 / CLOCKS_PER_SEC << " ms\n";
  260. #endif
  261. }
  262.  
Advertisement
Add Comment
Please, Sign In to add comment