Advertisement
Galebickosikasa

Untitled

Apr 7th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
  2. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <random>
  12. #include <chrono>
  13. #include <cassert>
  14. #include <sstream>
  15. #include <fstream>
  16. #include <iomanip>
  17. #include <tuple>
  18.  
  19. #define fi first
  20. #define se second
  21. #define pb push_back
  22. #define ll long long
  23. #define ld long double
  24. #define hm unordered_map
  25. #define pii pair<int, int>
  26. #define sz(a) (int)a.size()
  27. #define all(a) a.begin(), a.end()
  28. #define cinv(v) for (auto& x: v) cin >> x
  29. #define fr(i, n) for (int i = 0; i < n; ++i)
  30. #define fl(i, l, n) for (int i = l; i < n; ++i)
  31.  
  32. // #define int ll
  33.  
  34. template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
  35. template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
  36.  
  37. using namespace std;
  38.  
  39. #ifdef LOCAL
  40. #define dbg(x) cerr << #x << " : " << x << '\n'
  41. #else
  42. #define dbg(x)
  43. #endif
  44.  
  45. //tg: @runningcherry
  46.  
  47. template <typename Collection> string Join (const Collection& col) {
  48. bool first = true;
  49. stringstream ss;
  50. for (const auto& x: col) {
  51. if (!first) ss << ", ";
  52. first = false;
  53. ss << x;
  54. }
  55. return ss.str ();
  56. }
  57.  
  58. template <typename T1, typename T2> ostream& operator << (ostream& out, const pair<T1, T2>& v) {
  59. return out << '{' << v.fi << ", " << v.se << '}';
  60. }
  61.  
  62. template<typename T> ostream& operator << (ostream& out, const vector<T>& v) {
  63. return out << '[' << Join (v) << ']';
  64. }
  65.  
  66. template <typename T1, typename T2> ostream& operator << (ostream& out, const map<T1, T2>& v) {
  67. return out << '{' << Join (v) << '}';
  68. }
  69.  
  70. template<typename T> ostream& operator << (ostream& out, const set<T>& v) {
  71. return out << '(' << Join (v) << ')';
  72. }
  73.  
  74. template<typename T> ostream& operator << (ostream& out, const multiset<T>& v) {
  75. return out << '(' << Join (v) << ')';
  76. }
  77.  
  78. template <typename T1, typename T2> istream& operator >> (istream& in, pair<T1, T2>& a) {
  79. return in >> a.fi >> a.se;
  80. }
  81.  
  82. const ll inf = (ll) 2e9;
  83. const ll mod = (ll)1e9 + 7;
  84.  
  85. const int maxn = 1e5 + 20;
  86.  
  87. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  88.  
  89. struct tree {
  90. int value;
  91. int priority;
  92. tree* left;
  93. tree* right;
  94.  
  95. tree (int x) : value (x) {
  96. priority = rnd ();
  97. left = right = nullptr;
  98. }
  99. };
  100.  
  101. tree* merge (tree* l, tree* r) {
  102. if (l == nullptr) return r;
  103. if (r == nullptr) return l;
  104. if (l->priority >= r->priority) {
  105. l->right = merge (l->right, r);
  106. return l;
  107. } else {
  108. r->left = merge (l, r->left);
  109. return r;
  110. }
  111. }
  112.  
  113. pair<tree*, tree*> split (tree* t, int x) { // left - <= x, right > x
  114. pair <tree*, tree*> ptt = {nullptr, nullptr};
  115. if (t == nullptr) return ptt;
  116. if (t->value > x) {
  117. ptt = split (t->left, x);
  118. t->left = ptt.se;
  119. ptt.se = t;
  120. } else {
  121. ptt = split (t->right, x);
  122. t->right = ptt.fi;
  123. ptt.fi = t;
  124. }
  125. return ptt;
  126. }
  127.  
  128. tree* erase (tree* t, int x) {
  129. if (t == nullptr) return t;
  130. if (t->value == x) {
  131. tree* a = merge (t->left, t->right);
  132. delete t;
  133. return a;
  134. }
  135. if (t->value > x) {
  136. t->left = erase (t->left, x);
  137. } else {
  138. t->right = erase (t->right, x);
  139. }
  140. return t;
  141. }
  142.  
  143. tree* add (tree* t, int x) {
  144. tree* a = new tree (x);
  145. auto ptt = split (t, x);
  146. ptt.fi = merge (ptt.fi, a);
  147. return merge (ptt.fi, ptt.se);
  148. }
  149.  
  150. signed main () {
  151. ios_base::sync_with_stdio (false);
  152. cin.tie (nullptr);
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement