Advertisement
Galebickosikasa

Untitled

Aug 14th, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 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. // #pragma comment(linker, "/stack:200000000"]
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <unordered_set>
  10. #include <unordered_map>
  11. #include <set>
  12. #include <map>
  13. #include <queue>
  14. #include <deque>
  15. #include <bitset>
  16. #include <stack>
  17. #include <random>
  18. #include <fstream>
  19. #include <sstream>
  20. #include <chrono>
  21.  
  22. #define fi first
  23. #define se second
  24. #define pb push_back
  25. #define ll long long
  26. #define ld long double
  27. #define hm unordered_map
  28. #define pii pair<int, int>
  29. #define sz(a) (int)a.size()
  30. #define all(a) a.begin(), a.end()
  31. #define cinv(v) for (auto& x: v) cin >> x
  32. #define fr(i, n) for (int i = 0; i < n; ++i)
  33. #define fl(i, l, n) for (int i = l; i < n; ++i)
  34.  
  35. #define int ll
  36.  
  37. using namespace std;
  38.  
  39. #ifdef __LOCAL
  40. #define dbg(x) cerr << #x << " : " << x << '\n'
  41. const int maxn = 20;
  42. #else
  43. #define dbg(x)
  44. const int maxn = 2e5 + 20;
  45. #endif
  46.  
  47. //tg: @galebickosikasa
  48.  
  49. ostream& operator << (ostream& out, vector<int>& v) {
  50. for (auto& x: v) out << x << ' ';
  51. return out;
  52. }
  53.  
  54. ostream& operator << (ostream& out, pii& v) {
  55. out << v.fi << ", " << v.se;
  56. return out;
  57. }
  58.  
  59. istream& operator >> (istream& in, pii& a) {
  60. in >> a.fi >> a.se;
  61. return in;
  62. }
  63.  
  64. const ll inf = (ll) 2e9;
  65. const ld pi = asin (1) * 2;
  66. const ld eps = 1e-8;
  67. const ll mod = (ll)1e9 + 7;
  68. const ll ns = 97;
  69.  
  70. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  71.  
  72. struct Tree {
  73. Tree* left;
  74. Tree* right;
  75. int value, priority, ans, sz;
  76.  
  77. Tree (int x) {
  78. value = ans = x;
  79. sz = 1;
  80. priority = rnd ();
  81. left = right = nullptr;
  82. }
  83. };
  84.  
  85. int get_ans (Tree* t) {
  86. if (t == nullptr) return 0;
  87. return t->ans;
  88. }
  89.  
  90. int get_sz (Tree* t) {
  91. if (t == nullptr) return 0;
  92. return t->sz;
  93. }
  94.  
  95. void render (Tree* t) {
  96. if (t == nullptr) return;
  97. t->ans = t->value + get_ans (t->left) + get_ans (t->right);
  98. t->sz = get_sz (t->left) + get_sz (t->right) + 1;
  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. render (l);
  107. return l;
  108. } else {
  109. r->left = merge (l, r->left);
  110. render (r);
  111. return r;
  112. }
  113. }
  114.  
  115. pair<Tree*, Tree*> split (Tree* t, int k) {
  116. pair<Tree*, Tree*> ptt = {nullptr, nullptr};
  117. if (t == nullptr) return ptt;
  118. if (t->value > k) {
  119. ptt = split (t->left, k);
  120. t->left = ptt.se;
  121. ptt.se = t;
  122. } else {
  123. ptt = split (t->right, k);
  124. t->right = ptt.fi;
  125. ptt.fi = t;
  126. }
  127. render (t);
  128. return ptt;
  129. }
  130.  
  131. pair<Tree*, Tree*> split_l (Tree* t, int k) {
  132. pair<Tree*, Tree*> ptt = {nullptr, nullptr};
  133. if (t == nullptr) return ptt;
  134. if (get_sz (t->left) >= k) {
  135. ptt = split_l (t->left, k);
  136. t->left = ptt.se;
  137. ptt.se = t;
  138. } else {
  139. ptt = split_l (t->right, k - get_ans (t->left) - 1);
  140. t->right = ptt.fi;
  141. ptt.fi = t;
  142. }
  143. render (t);
  144. return ptt;
  145. }
  146.  
  147. pair<Tree*, Tree*> split_r (Tree* t, int k) {
  148. pair<Tree*, Tree*> ptt = {nullptr, nullptr};
  149. if (t == nullptr) return ptt;
  150. if (get_sz (t->right) >= k) {
  151. ptt = split_r (t->right, k);
  152. t->right = ptt.fi;
  153. ptt.fi = t;
  154. } else {
  155. ptt = split_r (t->left, k - get_ans (t->right) - 1);
  156. t->left = ptt.se;
  157. ptt.se = t;
  158. }
  159. render (t);
  160. return ptt;
  161. }
  162.  
  163. Tree* add (Tree* t, int x) {
  164. Tree* a = new Tree (x);
  165. auto ptt = split (t, x);
  166. ptt.fi = merge (ptt.fi, a);
  167. return merge (ptt.fi, ptt.se);
  168. }
  169.  
  170. Tree* erase (Tree* t, int x) {
  171. if (t == nullptr) return t;
  172. if (t->value == x) return merge (t->left, t->right);
  173. if (t->value > x) t->left = erase (t->left, x);
  174. else t->right = erase (t->right, x);
  175. return t;
  176. }
  177.  
  178. int check (Tree* a, Tree* b, int k) {
  179. if (get_sz (b) == 0) return
  180. auto ptt1 = split_r (a, k), ptt2 = split_l (b, k - 1);
  181.  
  182.  
  183.  
  184.  
  185. int BS (Tree* a, Tree* b) {
  186. int A = get_sz (a), B = get_sz (b);
  187. int l = -1, r = min (A, B) + 1;
  188. while (r - l > 1) {
  189. int m = (r + l) / 2;
  190. if (check (a, b, m) >= 0) l = m;
  191. else r = m;
  192. }
  193. return check (a, b, l);
  194. }
  195.  
  196. signed main () {
  197. ios_base::sync_with_stdio(false);
  198. cin.tie(nullptr);
  199. cout.tie(nullptr);
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement