Submission #1020432

# Submission time Handle Problem Language Result Execution time Memory
1020432 2024-07-12T04:27:39 Z anHiep Tree Rotations (POI11_rot) C++14
54 / 100
128 ms 65536 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 = 5e6;
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 58972 KB Output is correct
2 Correct 26 ms 58968 KB Output is correct
3 Correct 27 ms 58972 KB Output is correct
4 Correct 25 ms 58968 KB Output is correct
5 Correct 32 ms 58960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 59132 KB Output is correct
2 Correct 24 ms 58972 KB Output is correct
3 Correct 25 ms 58968 KB Output is correct
4 Correct 30 ms 58972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 58960 KB Output is correct
2 Correct 25 ms 58972 KB Output is correct
3 Correct 25 ms 58972 KB Output is correct
4 Correct 24 ms 59052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 59484 KB Output is correct
2 Correct 26 ms 59072 KB Output is correct
3 Correct 26 ms 59484 KB Output is correct
4 Correct 24 ms 59516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 60252 KB Output is correct
2 Correct 31 ms 59484 KB Output is correct
3 Correct 45 ms 59988 KB Output is correct
4 Correct 31 ms 59992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 60500 KB Output is correct
2 Correct 51 ms 61984 KB Output is correct
3 Correct 50 ms 63108 KB Output is correct
4 Correct 52 ms 63060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 49 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 70 ms 62684 KB Output is correct
2 Correct 68 ms 63824 KB Output is correct
3 Runtime error 68 ms 65536 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 100 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 105 ms 65536 KB Output is correct
2 Runtime error 60 ms 65536 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 128 ms 65388 KB Output is correct
2 Runtime error 80 ms 65536 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -