Submission #1020701

# Submission time Handle Problem Language Result Execution time Memory
1020701 2024-07-12T08:45:23 Z fimh Tree Rotations (POI11_rot) C++17
63 / 100
154 ms 46676 KB
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005, mod = 998244353;
int n;
int hvy[MAXN], ans[MAXN], a[MAXN], dp[MAXN], leave[MAXN];
vector<int> adj[MAXN];

struct FenwickTree{
  int bit[MAXN];
  void upd(int i, int x){
    while (i <= n){
      bit[i] += x;
      i += i & (-i);
    }
  }
  int sum(int i){
    int ans = 0;
    while (i){
      ans += bit[i];
      i -= i & (-i);
    }
    return ans;
  }
  int get(int l, int r){
    if (l > r) return 0;
    return sum(r) - sum(l - 1);
  }
}ft;

int node = 1;
void input(){
  int x; cin >> x;
  a[node] = x;
  if (x) return;
  int nw = node;
  adj[nw].push_back(++node);
  input();
  adj[nw].push_back(++node);
  input();
}

int pre_dfs(int u, int p){
  if (adj[u].empty()) leave[u] = 1;
  int sz = 1, mx = 0;
  for (int v : adj[u]){
    if (v != p){
      int csz = pre_dfs(v, u);
      leave[u] += leave[v];
      sz += csz;
      if (mx < csz){
        mx = csz;
        hvy[u] = v;
      }
    }
  }
  return sz;
}

void ope(int u, int p, int x){
  if (a[u]) ft.upd(a[u], x);
  for (int v : adj[u]){
    if (v != p) ope(v, u, x);
  }
}

int c1, c2;
void calc(int u, int p){
  if (a[u]){
    c1 += ft.get(a[u] + 1, n);
    c2 += ft.get(1, a[u] - 1);
  }
  for (int v : adj[u]){
    if (v != p) calc(v, u);
  }
}

void dfs(int u, int p){
  for (int &v : adj[u]){
    if (v != p && v != hvy[u]){
      dfs(v, u);
      dp[u] += dp[v];
      ope(v, u, -1);
    }
  }
  if (hvy[u]){
    dfs(hvy[u], u);
    dp[u] += dp[hvy[u]];
  }
  c1 = c2 = 0;
  for (int v : adj[u]){
    if (v != p && v != hvy[u]){
      calc(v, u);
    }
  }
  dp[u] += min(c1, c2);
  for (int v : adj[u]){
    if (v != p && v != hvy[u]){
      ope(v, u, 1);
    }
  }
  if (a[u]) ft.upd(a[u], 1);
}

void solve(){
  cin >> n;
  input();
  pre_dfs(1, 1);
  dfs(1, 1);
  cout << dp[1];
}

signed main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  int t = 1; // cin >> t;
  while (t--){
    solve();
  }  
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 31068 KB Output is correct
2 Correct 4 ms 31156 KB Output is correct
3 Correct 5 ms 31068 KB Output is correct
4 Correct 5 ms 31068 KB Output is correct
5 Correct 4 ms 31068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 31068 KB Output is correct
2 Correct 4 ms 31068 KB Output is correct
3 Correct 4 ms 31068 KB Output is correct
4 Correct 5 ms 31068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 31064 KB Output is correct
2 Correct 6 ms 31068 KB Output is correct
3 Correct 5 ms 31064 KB Output is correct
4 Correct 5 ms 31324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 31436 KB Output is correct
2 Correct 6 ms 31576 KB Output is correct
3 Correct 7 ms 31580 KB Output is correct
4 Correct 6 ms 31580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 32348 KB Output is correct
2 Correct 11 ms 31580 KB Output is correct
3 Correct 27 ms 34396 KB Output is correct
4 Correct 10 ms 32344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 37460 KB Output is correct
2 Correct 28 ms 38748 KB Output is correct
3 Correct 32 ms 40152 KB Output is correct
4 Correct 31 ms 40036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 44624 KB Output is correct
2 Correct 40 ms 42064 KB Output is correct
3 Correct 48 ms 40272 KB Output is correct
4 Correct 42 ms 39160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 53 ms 40276 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 154 ms 45648 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 99 ms 44620 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 112 ms 46676 KB Output isn't correct
2 Halted 0 ms 0 KB -