Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. #include <algorithm>
  5. #include <cstdio>
  6.  
  7. #define fi first
  8. #define se second
  9. #define sc scanf
  10. #define pr printf
  11. #define pb push_back
  12. #define rank rank1
  13. #define sz(c) (int)(c).size()
  14. #define all(c) (c).begin(), (c).end()
  15. #define rall(c) (c).rbegin(), (c).rend()
  16. #define fin(s) freopen( s, "r", stdin );
  17. #define fout(s) freopen( s, "w", stdout );
  18.  
  19. using ll = long long;
  20. using ld = long double;
  21. using ull = unsigned long long;
  22. using namespace std;
  23.  
  24. const long long N = 100100;
  25. const long long MOD = 1e9 + 7;
  26. const long long INF = 1e15;
  27. const int block = 25;
  28. const long double PI = acos(-1.0);
  29.  
  30. int TN = 1;
  31.  
  32. struct Node {
  33.     Node* left;
  34.     Node* right;
  35.     int sz;
  36.     int x, y;
  37.     Node(int xx) {
  38.         x = xx; y = rand(); left = NULL; right = NULL;
  39.     };
  40. };
  41.  
  42. Node* T = NULL;
  43.  
  44. Node* _merge(Node* A, Node* B) {
  45.     if (!A) {
  46.         return B;
  47.     }
  48.     if (!B) {
  49.         return A;
  50.     }
  51.     if (A->y > B->y) {
  52.         A->right = _merge(A->right, B);
  53.         return A;
  54.     }
  55.     else if (A->y <= B->y) {
  56.         B->left = _merge(A, B->left);
  57.         return B;
  58.     }
  59. }
  60.  
  61. pair<Node*, Node*> _split(Node* A, int k) {
  62.     if (!A) {
  63.         return {NULL, NULL};
  64.     }
  65.     if (A->x < k) {
  66.         auto T = _split(A->right, k);
  67.         A->right = T.fi;
  68.         return {A, T.se};
  69.     }
  70.     else {
  71.         auto T = _split(A->left, k);
  72.         A->left = T.se;
  73.         return {T.fi, A};
  74.     }
  75. }
  76.  
  77. Node* _find(Node* A, int k) {
  78.     if (!A) {
  79.         return 0;
  80.     }
  81.     if (A->x == k) {
  82.         return A;
  83.     }
  84.     if (A->x < k) {
  85.         return _find(A->right, k);
  86.     }
  87.     else {
  88.         return _find(A->left, k);
  89.     }
  90. }
  91.  
  92. void _insert(int x) {
  93.     Node* k = new Node(x);
  94.     if (!_find(T, k->x)) {
  95.         auto t = _split(T, k->x);
  96.         t.fi = _merge(t.fi, k);
  97.         T = _merge(t.fi, t.se);
  98.     }
  99. }
  100.  
  101. Node* _delete(Node* A, int x) {
  102.     auto T1 = _split(A, x);
  103.     auto T2 = _split(T1.se, x + 1);
  104.     if (T2.fi) {
  105.         delete T2.fi;
  106.     }
  107.     return _merge(T1.fi, T2.se);
  108. }
  109.  
  110. Node* _next(Node* T, int x) {
  111.     if (!T) {
  112.         return 0;
  113.     }
  114.     if (T->x > x) {
  115.         auto to = _next(T->left, x);
  116.         if (to)
  117.             return to;
  118.         else
  119.             return T;
  120.  
  121.     }
  122.     else {
  123.         return _next(T->right, x);
  124.     }
  125. }
  126.  
  127. Node* _prev(Node* T, int x) {
  128.     if (!T) {
  129.         return 0;
  130.     }
  131.     if (T->x < x) {
  132.         auto to = _prev(T->right, x);
  133.         if (to)
  134.             return to;
  135.         else
  136.             return T;
  137.  
  138.     }
  139.     else {
  140.         return _prev(T->left, x);
  141.     }
  142. }
  143.  
  144. string s;
  145. int x;
  146.  
  147. void solve() {
  148.     while (cin >> s) {
  149.         cin >> x;
  150.         if (s == "insert") {
  151.             _insert(x);
  152.         }
  153.         if (s == "delete") {
  154.             T = _delete(T, x);
  155.         }
  156.         if (s == "exists") {
  157.             if (_find(T, x)) {
  158.                 cout << "true\n";
  159.             }
  160.             else {
  161.                 cout << "false\n";
  162.             }
  163.         }
  164.         if (s == "next") {
  165.             auto ans = _next(T, x);
  166.             if (!ans) {
  167.                 cout << "none\n";
  168.             }
  169.             else {
  170.                 cout << ans->x << "\n";
  171.             }
  172.         }
  173.         if (s == "prev") {
  174.             auto ans = _prev(T, x);
  175.             if (!ans) {
  176.                 cout << "none\n";
  177.             }
  178.             else {
  179.                 cout << ans->x << "\n";
  180.             }
  181.         }
  182.     }
  183.     return;
  184. }
  185.  
  186. int main() {
  187.     ios_base::sync_with_stdio(0);
  188.     /// cin.tie(NULL); cout.tie(NULL);
  189.     /// ------------------------------------------------
  190.     /// fin("permutation.in"); fout("permutation.out");
  191.     /// cin >> TN;
  192.     /// ------------------------------------------------
  193.     while (TN--) solve();
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement