답안 #863449

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
863449 2023-10-20T11:31:35 Z vjudge1 Tree Rotations (POI11_rot) C++17
100 / 100
716 ms 45908 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, s;

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);
        s.push_back(a[v]);
        return;
    }
    add1(l[v]), add1(r[v]);
}

void gett(int v,bool cl=0,bool dir=0) {
    if (!l[v]) {
        if (!cl) {
            if (!dir)
                d.push_back(a[v]);
            else {
                upd(1,1,n,a[v],1);
                s.push_back(a[v]);
            }
        }
        return;
    }
    if (sz[l[v]]>sz[r[v]])
        swap(l[v],r[v]);
    gett(l[v],1,0), gett(r[v],0,1);
    add(l[v]);
    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 (cl) {
        d.clear();
        for (auto now: s)
            upd(1, 1, n, now, 0);
        s.clear();
    } else {
        if (!dir) {
            add(r[v]);
            for (auto now: s)
                upd(1, 1, n, now, 0);
            s.clear();
        } else {
            add1(l[v]);
            d.clear();
        }
    }
}

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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10584 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 1 ms 10584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10728 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 11096 KB Output is correct
2 Correct 7 ms 10844 KB Output is correct
3 Correct 4 ms 11100 KB Output is correct
4 Correct 4 ms 11356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14428 KB Output is correct
2 Correct 26 ms 13244 KB Output is correct
3 Correct 74 ms 11764 KB Output is correct
4 Correct 14 ms 12120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 127 ms 14884 KB Output is correct
2 Correct 57 ms 17304 KB Output is correct
3 Correct 70 ms 19280 KB Output is correct
4 Correct 63 ms 19024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 28880 KB Output is correct
2 Correct 82 ms 26384 KB Output is correct
3 Correct 140 ms 22236 KB Output is correct
4 Correct 97 ms 22724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 149 ms 24052 KB Output is correct
2 Correct 129 ms 26984 KB Output is correct
3 Correct 100 ms 31696 KB Output is correct
4 Correct 111 ms 30780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 453 ms 33248 KB Output is correct
2 Correct 243 ms 33484 KB Output is correct
3 Correct 233 ms 34220 KB Output is correct
4 Correct 283 ms 32196 KB Output is correct
5 Correct 546 ms 29388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 242 ms 31508 KB Output is correct
2 Correct 247 ms 42692 KB Output is correct
3 Correct 328 ms 38724 KB Output is correct
4 Correct 182 ms 44368 KB Output is correct
5 Correct 669 ms 31732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 265 ms 31596 KB Output is correct
2 Correct 264 ms 36496 KB Output is correct
3 Correct 517 ms 33296 KB Output is correct
4 Correct 304 ms 33200 KB Output is correct
5 Correct 203 ms 45908 KB Output is correct
6 Correct 716 ms 31896 KB Output is correct