Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
- #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
- // #pragma comment(linker, "/stack:200000000"]
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- #include <unordered_set>
- #include <unordered_map>
- #include <set>
- #include <map>
- #include <queue>
- #include <deque>
- #include <bitset>
- #include <stack>
- #include <random>
- #include <fstream>
- #include <sstream>
- #include <chrono>
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define ld long double
- #define hm unordered_map
- #define pii pair<int, int>
- #define sz(a) (int)a.size()
- #define all(a) a.begin(), a.end()
- #define cinv(v) for (auto& x: v) cin >> x
- #define fr(i, n) for (int i = 0; i < n; ++i)
- #define fl(i, l, n) for (int i = l; i < n; ++i)
- #define int ll
- using namespace std;
- #ifdef __LOCAL
- #define dbg(x) cerr << #x << " : " << x << '\n'
- const int maxn = 20;
- #else
- #define dbg(x)
- const int maxn = 2e5 + 20;
- #endif
- //tg: @galebickosikasa
- ostream& operator << (ostream& out, vector<int>& v) {
- for (auto& x: v) out << x << ' ';
- return out;
- }
- ostream& operator << (ostream& out, pii& v) {
- out << v.fi << ", " << v.se;
- return out;
- }
- istream& operator >> (istream& in, pii& a) {
- in >> a.fi >> a.se;
- return in;
- }
- const ll inf = (ll) 2e9;
- const ld pi = asin (1) * 2;
- const ld eps = 1e-8;
- const ll mod = (ll)1e9 + 7;
- const ll ns = 97;
- mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
- struct Tree {
- Tree* left;
- Tree* right;
- int value, priority, ans, sz;
- Tree (int x) {
- value = ans = x;
- sz = 1;
- priority = rnd ();
- left = right = nullptr;
- }
- };
- int get_ans (Tree* t) {
- if (t == nullptr) return 0;
- return t->ans;
- }
- int get_sz (Tree* t) {
- if (t == nullptr) return 0;
- return t->sz;
- }
- void render (Tree* t) {
- if (t == nullptr) return;
- t->ans = t->value + get_ans (t->left) + get_ans (t->right);
- t->sz = get_sz (t->left) + get_sz (t->right) + 1;
- }
- Tree* merge (Tree* l, Tree* r) {
- if (l == nullptr) return r;
- if (r == nullptr) return l;
- if (l-> priority >= r->priority) {
- l->right = merge (l->right, r);
- render (l);
- return l;
- } else {
- r->left = merge (l, r->left);
- render (r);
- return r;
- }
- }
- pair<Tree*, Tree*> split (Tree* t, int k) {
- pair<Tree*, Tree*> ptt = {nullptr, nullptr};
- if (t == nullptr) return ptt;
- if (t->value > k) {
- ptt = split (t->left, k);
- t->left = ptt.se;
- ptt.se = t;
- } else {
- ptt = split (t->right, k);
- t->right = ptt.fi;
- ptt.fi = t;
- }
- render (t);
- return ptt;
- }
- pair<Tree*, Tree*> split_l (Tree* t, int k) {
- pair<Tree*, Tree*> ptt = {nullptr, nullptr};
- if (t == nullptr) return ptt;
- if (get_sz (t->left) >= k) {
- ptt = split_l (t->left, k);
- t->left = ptt.se;
- ptt.se = t;
- } else {
- ptt = split_l (t->right, k - get_ans (t->left) - 1);
- t->right = ptt.fi;
- ptt.fi = t;
- }
- render (t);
- return ptt;
- }
- pair<Tree*, Tree*> split_r (Tree* t, int k) {
- pair<Tree*, Tree*> ptt = {nullptr, nullptr};
- if (t == nullptr) return ptt;
- if (get_sz (t->right) >= k) {
- ptt = split_r (t->right, k);
- t->right = ptt.fi;
- ptt.fi = t;
- } else {
- ptt = split_r (t->left, k - get_ans (t->right) - 1);
- t->left = ptt.se;
- ptt.se = t;
- }
- render (t);
- return ptt;
- }
- Tree* add (Tree* t, int x) {
- Tree* a = new Tree (x);
- auto ptt = split (t, x);
- ptt.fi = merge (ptt.fi, a);
- return merge (ptt.fi, ptt.se);
- }
- Tree* erase (Tree* t, int x) {
- if (t == nullptr) return t;
- if (t->value == x) return merge (t->left, t->right);
- if (t->value > x) t->left = erase (t->left, x);
- else t->right = erase (t->right, x);
- return t;
- }
- int check (Tree* a, Tree* b, int k) {
- if (get_sz (b) == 0) return
- auto ptt1 = split_r (a, k), ptt2 = split_l (b, k - 1);
- int BS (Tree* a, Tree* b) {
- int A = get_sz (a), B = get_sz (b);
- int l = -1, r = min (A, B) + 1;
- while (r - l > 1) {
- int m = (r + l) / 2;
- if (check (a, b, m) >= 0) l = m;
- else r = m;
- }
- return check (a, b, l);
- }
- signed main () {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement