Submission #1020436

#TimeUsernameProblemLanguageResultExecution timeMemory
1020436anHiepUntitled (POI11_rot)C++14
100 / 100
222 ms61776 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...