Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define pb emplace_back
- #define AI(i) begin(i), end(i)
- template<class T>
- bool chmax(T &val, T nv) {
- return val < nv ? (val = nv, true) : false;
- }
- template<class T>
- bool chmin(T &val, T nv) {
- return nv < val ? (val = nv, true) : false;
- }
- using namespace std;
- using ll = long long;
- #ifdef KEV
- #define DE(args...) kout("[ " + string(#args) + " ] = ", args)
- void debug(auto L, auto R) { while (L < R) cerr << *L << " \n"[L+1==R], ++L; }
- void kout(){ cerr << endl; }
- template<class T1, class ...T2> void kout(T1 a, T2 ...e) { cerr << a << ' ', kout(e...); }
- #else
- #define DE(...) 0
- #define debug(...) 0
- #endif
- // What I should check
- // 1. overflow
- // 2. corner cases
- // Enjoy the problem instead of hurrying to AC
- // Good luck !
- const int MAX_N = 200010;
- #include <bits/extc++.h>
- using namespace __gnu_pbds;
- using bst = tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>;
- ll solve(bst &pa) {
- bst now;
- int p;
- cin >> p;
- // branch
- ll res = 0;
- if (p == 0) {
- res += solve(now);
- res += solve(now);
- }
- // leaf
- else {
- now.insert(p);
- }
- if (pa.empty()) {
- pa.swap(now);
- }
- else {
- if (now.size() > pa.size()) pa.swap(now);
- ll f = 0, b = 0;
- for (int u : now) {
- int k = pa.order_of_key(u);
- f += k, b += pa.size() - k;
- }
- res += min(f, b);
- for (int u : now)
- pa.insert(u);
- }
- return res;
- }
- int32_t main() {
- ios_base::sync_with_stdio(0), cin.tie(0);
- int n;
- cin >> n;
- bst _;
- cout << solve(_) << '\n';
- }
RAW Paste Data