Submission #1020436

# Submission time Handle Problem Language Result Execution time Memory
1020436 2024-07-12T04:28:06 Z anHiep Tree Rotations (POI11_rot) C++14
100 / 100
222 ms 61776 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;
}
# Verdict Execution time Memory Grader output
1 Correct 25 ms 47192 KB Output is correct
2 Correct 19 ms 47288 KB Output is correct
3 Correct 21 ms 47452 KB Output is correct
4 Correct 21 ms 47200 KB Output is correct
5 Correct 19 ms 47208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 47404 KB Output is correct
2 Correct 19 ms 47452 KB Output is correct
3 Correct 22 ms 47368 KB Output is correct
4 Correct 18 ms 47452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 47364 KB Output is correct
2 Correct 17 ms 47452 KB Output is correct
3 Correct 25 ms 47452 KB Output is correct
4 Correct 27 ms 47400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 47708 KB Output is correct
2 Correct 24 ms 47380 KB Output is correct
3 Correct 24 ms 47708 KB Output is correct
4 Correct 21 ms 47712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 48436 KB Output is correct
2 Correct 26 ms 47648 KB Output is correct
3 Correct 42 ms 48152 KB Output is correct
4 Correct 29 ms 48220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 48724 KB Output is correct
2 Correct 50 ms 50308 KB Output is correct
3 Correct 47 ms 51464 KB Output is correct
4 Correct 51 ms 51212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 55684 KB Output is correct
2 Correct 52 ms 53064 KB Output is correct
3 Correct 70 ms 51376 KB Output is correct
4 Correct 57 ms 50296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 50820 KB Output is correct
2 Correct 64 ms 52236 KB Output is correct
3 Correct 70 ms 55396 KB Output is correct
4 Correct 75 ms 55108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 164 ms 55332 KB Output is correct
2 Correct 118 ms 53840 KB Output is correct
3 Correct 91 ms 54356 KB Output is correct
4 Correct 113 ms 53336 KB Output is correct
5 Correct 183 ms 52052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 53852 KB Output is correct
2 Correct 117 ms 59476 KB Output is correct
3 Correct 123 ms 57424 KB Output is correct
4 Correct 98 ms 61616 KB Output is correct
5 Correct 222 ms 53780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 54892 KB Output is correct
2 Correct 96 ms 55896 KB Output is correct
3 Correct 177 ms 54024 KB Output is correct
4 Correct 126 ms 54868 KB Output is correct
5 Correct 100 ms 61776 KB Output is correct
6 Correct 217 ms 53788 KB Output is correct