Advertisement
skimono

Untitled

Jun 22nd, 2022
1,025
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <stack>
  9. #include <iomanip>
  10. #include <fstream>
  11. #include <string>
  12. #include <set>
  13. #include <deque>
  14. #include <queue>
  15. #include <map>
  16. #include <bitset>
  17. #include <random>
  18. #include <list>
  19. #include <unordered_map>
  20. #include <unordered_set>
  21. #include <cassert>
  22.  
  23. using namespace std;
  24.  
  25. typedef long long ll;
  26. typedef unsigned long long ull;
  27. typedef long double ld;
  28. typedef string str;
  29. //typedef __int128 ultraint;
  30. #define endl "\n"
  31. #define sqrt sqrtl
  32. #define all(vc666) vc666.begin(), vc666.end()
  33. //#define pow fast_pow
  34.  
  35.  
  36. const ll inf = (ll)1e18 + 7;
  37. ld eps = 1e-6;
  38. ld Pi = 3.1415926535897932384;
  39. const ll mod = 1e9 + 7;
  40. const ll max_sz = 1e3;
  41.  
  42. struct Node {
  43.     ll key, pr;
  44.     ll left = -1, right = -1;
  45.     ll sz = 1;
  46. };
  47.  
  48. struct dd {
  49.     //Djinchuriki Djubi
  50.     Node nodes[max_sz];
  51.     ll nodesNumber = 0;
  52.     ll kor = -1;
  53.     ll makeNewNode(ll x) {
  54.         nodes[nodesNumber] = { x,rand(), -1,-1,1 };
  55.         return nodesNumber++;
  56.     }
  57.     ll getSZ(ll v) { return (v >= 0) ? nodes[v].sz : 0; }
  58.     void recalculate(ll v) {
  59.         nodes[v].sz = getSZ(nodes[v].left) + getSZ(nodes[v].right) + 1;
  60.     }
  61.     bool exists(ll root, ll x) {
  62.         ll v = root;
  63.         while (v != -1) {
  64.             Node& nd = nodes[v];
  65.             if (nd.key == x)
  66.                 return true;
  67.             else if (nd.key > x)
  68.                 v = nodes[v].left;
  69.             else
  70.                 v = nodes[v].right;
  71.         }
  72.         return false;
  73.     }
  74.     pair <ll, ll> split(ll root, ll x) {
  75.         if (root == -1)
  76.             return { -1,-1 };
  77.         if (x > nodes[root].key) {
  78.             pair <ll, ll> p = split(nodes[root].right, x);
  79.             nodes[root].right = p.first;
  80.             recalculate(root);
  81.             return { root,p.second };
  82.         }
  83.         else {
  84.             pair <ll, ll> p = split(nodes[root].left, x);
  85.             nodes[root].left = p.second;
  86.             recalculate(root);
  87.             return { p.first, root };
  88.         }
  89.     }
  90.     ll merge(ll root1, ll root2) {
  91.         if (root2 == -1) {
  92.             return root1;
  93.         }
  94.         if (root1 == -1) {
  95.             return root2;
  96.         }
  97.         if (nodes[root1].pr < nodes[root2].pr) {
  98.             nodes[root1].right = merge(nodes[root1].right, root2);
  99.             recalculate(root1);
  100.             return root1;
  101.         }
  102.         else {
  103.             nodes[root2].left = merge(root1, nodes[root2].left);
  104.             recalculate(root2);
  105.             return root2;
  106.         }
  107.     }
  108.     ll insert(ll root, ll x) {
  109.         ll newNode = makeNewNode(x);
  110.         pair <ll, ll> p = split(root, x);
  111.         return merge(p.first, merge(p.second, newNode));
  112.     }
  113.     ll erase(ll root, ll x) {
  114.         pair <ll, ll> p = split(root, x);
  115.         pair <ll, ll> pp = split(p.first, x - 1);
  116.         return merge(pp.first, p.second);
  117.     }
  118.     ll fndKth(ll root, ll k) {
  119.         if (getSZ(nodes[root].left) >= k) {
  120.             return fndKth(nodes[root].left, k);
  121.         }
  122.         else if (getSZ(nodes[root].left) == k - 1) {
  123.             return nodes[root].key;
  124.         }
  125.         else {
  126.             return fndKth(nodes[root].right, k - getSZ(nodes[root].left) - 1);
  127.         }
  128.     }
  129. };
  130.  
  131. signed main() {
  132. #ifdef _DEBUG
  133.     freopen("in.txt", "r", stdin);
  134.     freopen("out.txt", "w", stdout);
  135. #endif
  136.     ios_base::sync_with_stdio(0);
  137.     cin.tie(NULL);
  138.     cout.tie(NULL);
  139.     ll t = 1;
  140.     //cin >> t;
  141.     while (t--) {
  142.         string s;
  143.         dd tree;
  144.         ll x;
  145.         while (getline(cin, s)) {
  146.             if (s.size() != 0) {
  147.                 ll x = atoi(s.substr(s.find(' ') + 1, s.size() - s.find(' ') - 1).c_str());
  148.                 if (s[0] == 'i') {
  149.                     if (!tree.exists(tree.kor, x)) {
  150.                         tree.kor = tree.insert(tree.kor, x);
  151.                     }
  152.                     //insert
  153.                 }
  154.                 else if (s[0] == 'd') {
  155.                     tree.erase(tree.kor, x);
  156.                     //erase
  157.                 }
  158.                 else {
  159.                     if (tree.exists(tree.kor, x)) {
  160.                         cout << "true" << endl;
  161.                     }
  162.                     else {
  163.                         cout << "false" << endl;
  164.                     }
  165.                     //exists
  166.                 }
  167.             }
  168.         }
  169.     }
  170. }
  171. //痛みを受け入れる。 痛みを知っています。 痛みを感じる。 痛みを参照してください。 真の痛みを知らなければ、真の世界を理解することは不可能です。 今、あなたは痛みを知るでしょう。 神の罰!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement