답안 #1020535

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1020535 2024-07-12T06:46:57 Z khanhtb Tree Rotations (POI11_rot) C++14
100 / 100
243 ms 63060 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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 48216 KB Output is correct
2 Correct 21 ms 48220 KB Output is correct
3 Correct 26 ms 48476 KB Output is correct
4 Correct 21 ms 48216 KB Output is correct
5 Correct 24 ms 48220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 48216 KB Output is correct
2 Correct 20 ms 48220 KB Output is correct
3 Correct 25 ms 48220 KB Output is correct
4 Correct 25 ms 48216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 48220 KB Output is correct
2 Correct 20 ms 48356 KB Output is correct
3 Correct 20 ms 48220 KB Output is correct
4 Correct 20 ms 48216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 48472 KB Output is correct
2 Correct 21 ms 48476 KB Output is correct
3 Correct 20 ms 48568 KB Output is correct
4 Correct 20 ms 48732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 49240 KB Output is correct
2 Correct 26 ms 48732 KB Output is correct
3 Correct 41 ms 49232 KB Output is correct
4 Correct 25 ms 49496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 49492 KB Output is correct
2 Correct 41 ms 51404 KB Output is correct
3 Correct 41 ms 52588 KB Output is correct
4 Correct 46 ms 52668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 56668 KB Output is correct
2 Correct 51 ms 54604 KB Output is correct
3 Correct 66 ms 52888 KB Output is correct
4 Correct 48 ms 51796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 51792 KB Output is correct
2 Correct 61 ms 54100 KB Output is correct
3 Correct 64 ms 57168 KB Output is correct
4 Correct 55 ms 56912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 158 ms 56152 KB Output is correct
2 Correct 118 ms 56144 KB Output is correct
3 Correct 91 ms 56376 KB Output is correct
4 Correct 115 ms 55636 KB Output is correct
5 Correct 166 ms 54356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 54800 KB Output is correct
2 Correct 105 ms 62036 KB Output is correct
3 Correct 112 ms 59668 KB Output is correct
4 Correct 90 ms 63060 KB Output is correct
5 Correct 228 ms 55128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 110 ms 54716 KB Output is correct
2 Correct 91 ms 57228 KB Output is correct
3 Correct 154 ms 55376 KB Output is correct
4 Correct 121 ms 56404 KB Output is correct
5 Correct 84 ms 63060 KB Output is correct
6 Correct 243 ms 55300 KB Output is correct