답안 #942986

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
942986 2024-03-11T07:32:43 Z manizare Tree Rotations (POI11_rot) C++14
0 / 100
37 ms 11864 KB
#include <bits/stdc++.h>

#pragma GCC optimize("O3,unroll-loops")
#define pb push_back
#define F first
#define S second 
#define ld long double
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define PII pair<pii , pii>
#define int long long 
#define sz(v) (int)v.size()
#define rep(i , a , b) for(int i=a;i <= (b);i++)
#define per(i , a , b) for(int i=a;i >= (b);i--)
#define deb(x) cout <<#x << " : " << x << "\n" ;
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 2e5+10 , lg = 48,  inf = 1e9+10 , mod = 998244353 ;
int fen[maxn] , n , cnt , sb[maxn] , l[maxn] , r[maxn]  ;;

void upd(int x , int w){
    while(x <= n){
        fen[x] += w ;
        x += x&-x ;
    }
}
int que(int x){
    int ans =0 ;
    while(x){
        ans += fen[x] ;
        x -= x&-x ;
    }
    return ans ; 
}
int bui(){
    int a;
    cin >> a; 
    if(a != 0){
        sb[a] =1 ;
        return a; 
    }
    cnt++;
    int id = cnt - 1; 
    l[id] = bui();
    r[id] = bui() ;
    sb[id] = sb[l[id]] + sb[r[id]]; 
    if(sb[l[id]] < sb[r[id]])swap(l[id],r[id]) ;
    return id ;
}

int sm = 0 , ans =0 ; 
void rem(int v){
    if(v <= n){
        upd(n+1-v , -1) ;
    }else{
        rem(l[v]) ;
        rem(r[v]) ;
    }
}
void add(int v){
    if(v <= n){
        sm += que(v); 
        upd(n+1-v , 1) ;
    }else{
        add(l[v]);
        add(r[v]);
    }
}

void dfs(int v){
    if(v <= n){
        upd(n+1-v , 1) ;
        return ;
    }
    dfs(r[v]) ;
    rem(r[v]) ;
    dfs(l[v]) ;
    sm = 0; 
    add(r[v]) ;
    ans += min(sm , sb[l[v]] * sb[r[v]] - sm) ;
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 
    cin >> n ;
    cnt = n + 1 ;
    int rt= bui() ;
    dfs(rt) ;
    cout << ans << '\n' ;
    return 0 ;
}
/*


*/
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 4440 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 4444 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 4444 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 4700 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 5208 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 23 ms 4956 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 10072 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 19 ms 10072 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 37 ms 9044 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 8 ms 11864 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 7 ms 11352 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -