Submission #863442

# Submission time Handle Problem Language Result Execution time Memory
863442 2023-10-20T11:20:12 Z vjudge1 Tree Rotations (POI11_rot) C++17
0 / 100
1000 ms 25448 KB
// Bolatulu
#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define int long long
#define kanagattandirilmagandiktarinizdan ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define pb push_back
#define F first
#define S second
#define md (tl+tr)/2
#define TL v+v,tl,mid
#define TR v+v+1,mid+1,tr

#pragma GCC target( "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")

using namespace std;

int binpow(int a,int n,int M) {
    if (n==0)
        return 1;
    if (n%2!=0)
        return (a * binpow(a,n-1,M))%M;
    int z=binpow(a,n/2,M);
    return (z*z)%M;
}

const ll INF = 1e18;
const int N =  1e6+7;
const int M = 1e9+7;
const ll HZ = 1e5;
const int MAX = INT_MAX;
const int MIN = INT_MIN;
const db pi = 3.141592653;
const int P=31;

int sz[N],n,a[N],l[N],r[N],tame=1,ans,t[N];
vector <int> d;

void upd(int v,int tl,int tr,int pos,bool add) {
    if (tl==tr) {
        if (add)
            t[v]++;
        else
            t[v]=0;
        return;
    }
    if (pos<=md)
        upd(v+v,tl,md,pos,add);
    else
        upd(v+v+1,md+1,tr,pos,add);
    t[v]=t[v+v]+t[v+v+1];
}

int get(int v,int tl,int tr,int l,int r) {
    if (tl>=l and tr<=r)
        return t[v];
    if (tl>r or tr<l)
        return 0;
    return get(v+v,tl,md,l,r)+get(v+v+1,md+1,tr,l,r);
}

void read(int v) {
    int z;
    cin >> z;
    sz[v] = 1;
    if (z) {
        a[v] = z;
        return;
    }
    l[v] = ++tame;
    read(l[v]);
    sz[v] += sz[l[v]];
    r[v] = ++tame;
    read(r[v]);
    sz[v] += sz[r[v]];
}

void add(int v) {
    if (!l[v]) {
        d.push_back(a[v]);
        return;
    }
    add(l[v]), add(r[v]);
}

void add1(int v) {
    if (!l[v]) {
        upd(1,1,n,a[v],1);
        return;
    }
    add1(l[v]), add1(r[v]);
}

void gett(int v,bool dir=0) {
    if (!l[v]) {
        if (!dir)
            d.push_back(a[v]);
        else {
            upd(1, 1, n, a[v], 1);
        }
        return;
    }
    if (sz[l[v]] > sz[r[v]])
        swap(l[v], r[v]);
    gett(l[v], 0), gett(r[v], 1);
    int sum1 = 0, sum2 = 0;
    for (auto now: d) {
        if (now > 1)
            sum1 += get(1, 1, n, 1, now - 1);
        if (now < n)
            sum2 += get(1, 1, n, now + 1, n);
    }
    ans += min(sum1, sum2);
    if (!dir)
        add(r[v]);
    else
        add1(l[v]);
}

void solve() {
    cin >> n;
    read(1);
    gett(1);
    cout << ans;
}

signed main() {
    // freopen("lca.in", "r", stdin);
    // freopen("lca.out", "w", stdout);
    kanagattandirilmagandiktarinizdan
    int test = 1, count = 1;
    // cin >> test;
    while (test--) {
        // cout << "Case " << count << ":\n";
        solve();
        if (test) {
            cout << '\n';
        }
        count++;
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 8536 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 8540 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 70 ms 8696 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1035 ms 8976 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1035 ms 10492 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1061 ms 12120 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1101 ms 25448 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1058 ms 19384 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1064 ms 24336 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1012 ms 23856 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1041 ms 23064 KB Time limit exceeded
2 Halted 0 ms 0 KB -