답안 #1020438

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1020438 2024-07-12T04:28:35 Z vjudge1 Tree Rotations (POI11_rot) C++17
100 / 100
222 ms 61804 KB
#include<bits/stdc++.h>

using namespace std;

#define fi first 
#define se second
#define pb push_back
#define ll long long
#define ii pair<int, int>
#define vi vector<int> 
#define vll vector<ll>
#define vii vector<ii>
#define cd complex<double>
const ll mod = 1e9 + 7;
const ll INF = 1e18L + 5;
const double PI = acos(-1);
const int block = 320;
const int N = 4e6;
int tc, tt = 1;

int n, u, v;
int tdfs = 0, cnt = 0;
ll c1 = 0, c2 = 0;
int a[N + 5], sz[N + 5];
int g[N + 5][2];
ll dp[N + 5];
void build_tree(int u) {
    for(int i=0; i<2; i++) {
        int x;
        cin>>x;
        if(x == 0) {
            tdfs++;
            g[u][i] = tdfs;
            build_tree(tdfs);
        }
        else {
            tdfs++;
            g[u][i] = tdfs;
            a[tdfs] = x;
            cnt++;
        }
        if(cnt == n) return;
    }
}
struct fenwick_tree {
    int bit[N + 5];
    void update(int pos, int v) {
        while(pos <= n) {
            bit[pos] += v;
            pos += (pos & (-pos));
        }
    }
    int get(int pos) {
        int ans = 0;
        while(pos) {
            ans += bit[pos];
            pos -= (pos & (-pos));
        }
        return ans;
    }
} ft;
void dfs_sz(int u, int par) {
    sz[u] = 1;
    for(int i=0; i<2; i++)
        if(g[u][i] != par && g[u][i] != -1) {
            dfs_sz(g[u][i], u);
            sz[u] += sz[g[u][i]];
        } 
} 
void add(int u, int par, int val) {
    if(a[u] != -1) ft.update(a[u], val);
    for(int i=0; i<2; i++)
        if(g[u][i] != par && g[u][i] != -1) add(g[u][i], u, val);
}
void cal(int u, int par) {
    if(a[u] != -1) {
        c1 += ft.get(a[u] - 1);
        c2 += ft.get(n) - ft.get(a[u]);
    }
    for(int i=0; i<2; i++)
        if(g[u][i] != par && g[u][i] != -1) cal(g[u][i], u);
}
void dfs(int u, int par) {
    int bc = 0;
    for(int i=0; i<2; i++)
        if(g[u][i] != par && sz[g[u][i]] > sz[bc] && g[u][i] != -1) 
            bc = g[u][i];
    for(int i=0; i<2; i++)
        if(g[u][i] != par && g[u][i] != bc && g[u][i] != -1) {
            dfs(g[u][i], u);
            dp[u] += dp[g[u][i]];
            add(g[u][i], u, -1);
        }
    if(bc) {
        dfs(bc, u);
        dp[u] += dp[bc];
    }
    if(a[u] != -1) ft.update(a[u], 1);
    c1 = c2 = 0;
    for(int i=0; i<2; i++)
        if(g[u][i] != par && g[u][i] != bc && g[u][i] != -1)
            cal(g[u][i], u);
    for(int i=0; i<2; i++)
        if(g[u][i] != par && g[u][i] != bc && g[u][i] != -1) 
            add(g[u][i], u, 1);
    dp[u] += min(c1, c2);
}
void solve() {
    memset(a, -1, sizeof(a));
    for(int i=0; i<N + 5; i++)
        g[i][0] = g[i][1] = -1;
    cin>>n;
    build_tree(0);
    n = tdfs;
    dfs_sz(1, -1);
    dfs(1, -1);
    cout<<dp[1];
}
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen(".INP", "r", stdin);
    // freopen(".OUT", "w", stdout);
    for(tc=1; tc<=tt; tc++) solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 47184 KB Output is correct
2 Correct 20 ms 47452 KB Output is correct
3 Correct 18 ms 47452 KB Output is correct
4 Correct 17 ms 47452 KB Output is correct
5 Correct 17 ms 47392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 47452 KB Output is correct
2 Correct 25 ms 47192 KB Output is correct
3 Correct 17 ms 47192 KB Output is correct
4 Correct 18 ms 47452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 47452 KB Output is correct
2 Correct 19 ms 47276 KB Output is correct
3 Correct 18 ms 47416 KB Output is correct
4 Correct 18 ms 47444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 47708 KB Output is correct
2 Correct 19 ms 47452 KB Output is correct
3 Correct 18 ms 47688 KB Output is correct
4 Correct 19 ms 47708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 48472 KB Output is correct
2 Correct 26 ms 47732 KB Output is correct
3 Correct 42 ms 48364 KB Output is correct
4 Correct 24 ms 48464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 48996 KB Output is correct
2 Correct 40 ms 50516 KB Output is correct
3 Correct 43 ms 51780 KB Output is correct
4 Correct 45 ms 51796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 56400 KB Output is correct
2 Correct 46 ms 53860 KB Output is correct
3 Correct 58 ms 51864 KB Output is correct
4 Correct 55 ms 51028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 51816 KB Output is correct
2 Correct 58 ms 53176 KB Output is correct
3 Correct 63 ms 56400 KB Output is correct
4 Correct 62 ms 56148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 56656 KB Output is correct
2 Correct 145 ms 55128 KB Output is correct
3 Correct 89 ms 55640 KB Output is correct
4 Correct 110 ms 54612 KB Output is correct
5 Correct 170 ms 53076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 55124 KB Output is correct
2 Correct 109 ms 60752 KB Output is correct
3 Correct 116 ms 58636 KB Output is correct
4 Correct 99 ms 61780 KB Output is correct
5 Correct 209 ms 53840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 55120 KB Output is correct
2 Correct 97 ms 56140 KB Output is correct
3 Correct 155 ms 54252 KB Output is correct
4 Correct 134 ms 55188 KB Output is correct
5 Correct 84 ms 61804 KB Output is correct
6 Correct 222 ms 54020 KB Output is correct