Submission #1020531

# Submission time Handle Problem Language Result Execution time Memory
1020531 2024-07-12T06:41:40 Z khanhtb Tree Rotations (POI11_rot) C++14
0 / 100
148 ms 57356 KB
#include <bits/stdc++.h>
#define ll int
#define ull unsigned long long
#define ld long double
#define pb push_back
#define pf push_front
#define vi vector<ll>
#define vii vector<vi>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7;
const ll inf = 2e18;
const ll base = 31;
const ll blocksz = 320;
const ll N = 4e6+8;
ll g[N][2];
ll a[N],tdfs,cnt,n;
long long dp[N], inv1, inv2;
void build_tree(ll u) {
    for(ll i = 0; i < 2; i++) {
        ll 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 Binary_Indexed_Tree{
    ll BIT[N+5];
    void update(ll i, ll x){
        while(i <= n){
            BIT[i] += x;
            i += (i&-i);
        }
    }
    ll get(ll i){
        ll ans = 0;
        while(i){
            ans += BIT[i];
            i -= (i&-i);
        }
        return ans;
    }
} bit;
ll sz[N];
void dfs_size(ll u, ll p){
    sz[u] = 1;
    for(ll i = 0; i < 2; i++){
        ll v = g[u][i];
        if(v != p && v != -1) {
            dfs_size(v,u);
            sz[u] += sz[v];
        }
    }
}
void sack_add(ll u, ll p){
    if(a[u] != -1) bit.update(a[u],1);
    for(ll i = 0; i < 2; i++){
        ll v = g[u][i];
        if(v != p && v != -1) sack_add(v,u);
    }
}   
void sack_rmv(ll u, ll p){
    if(a[u] != -1) bit.update(a[u],-1);
    for(ll i = 0; i < 2; i++){
        ll v = g[u][i];
        if(v != p && v != -1) sack_rmv(v,u);
    }
}   
void cal_inv(ll u, ll p){
    if(a[u] != -1){
        inv1 = bit.get(a[u]-1);
        inv2 = bit.get(n)-bit.get(a[u]);
    }
    for(ll i = 0; i < 2; i++){
        ll v = g[u][i];
        if(v != p && v != -1) cal_inv(v,u);
    }
}
void sack(ll u, ll p){
    ll big = 0;
    for(ll i = 0; i < 2; i++){
        ll v = g[u][i];
        if(v != p && v != -1 && sz[v] > sz[big]) big = v;
    }
    for(ll i = 0; i < 2; i++){
        ll v = g[u][i];
        if(v != p && v != -1 && v != big){
            sack(v,u);
            dp[u] += dp[v];
            sack_rmv(v,u);
        }
    }
    if(big){
        sack(big,u);
        dp[u] += dp[big];
    }
    if(a[u] != -1) bit.update(a[u],1);
    inv1 = inv2 = 0;
    for(ll i = 0; i < 2; i++){
        ll v = g[u][i];
        if(v != p && v != -1 && v != big){
            cal_inv(v,u);
        }
    }
    for(ll i = 0; i < 2; i++){
        ll v = g[u][i];
        if(v != p && v != -1 && v != big){
            sack_add(v,u);
        }
    }
    dp[u] += min(inv1,inv2);
}
void solve(){
    memset(a,-1,sizeof(a));
    memset(g,-1,sizeof(g));
    cin >> n;
    build_tree(0);
    n = tdfs;
    dfs_size(1,1);
    sack(1,1);
    cout << dp[1];
}
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen("test.inp", "r")){
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
    ll T = 1;
    // cin >> T;
    for (ll i = 1; i <= T; i++){
        solve();
        // cout << '\n';
    }
}

Compilation message

rot.cpp:16:16: warning: overflow in conversion from 'double' to 'int' changes value from '2.0e+18' to '2147483647' [-Woverflow]
   16 | const ll inf = 2e18;
      |                ^~~~
rot.cpp: In function 'int main()':
rot.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         freopen("test.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |         freopen("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 48216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 48220 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 48220 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 48536 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 49500 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 54 ms 50068 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 43 ms 57140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 66 ms 52612 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 148 ms 57356 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 92 ms 56148 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 108 ms 56148 KB Output isn't correct
2 Halted 0 ms 0 KB -