답안 #1093344

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1093344 2024-09-26T16:30:54 Z asli_bg Tree Rotations (POI11_rot) C++11
0 / 100
133 ms 65536 KB
#pragma GCC optimize("O3,unroll-loops")
 
#include <bits/stdc++.h>
using namespace std;
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
 
typedef tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update> inset;
 
#define int long long
 
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define pb push_back
 
#define FOR(i,a) for(int i=0;i<(a);i++)
#define FORE(i,a,b) for(int i=(a);i<(b);i++)
#define cont(x) {for(auto el:x) cout<<el<<' ';cout<<endl;}
#define contp(x) {for(auto el:x) cout<<el.fi<<'-'<<el.se<<' ';cout<<endl;}
#define sp <<" "<<
#define mid (l+r)/2
#define endl '\n'
 
#define DEBUG(X) {cout<<#X<<' '<<(X)<<endl;}
 
#define carp(x,y) ((x%MOD)*(y%MOD))%MOD
#define topla(x,y) ((x%MOD)+(y%MOD))%MOD
 
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
 
const int MAXN=2e5+5;
const int INF=1e9+7;
const int MOD=1e9+7;
 
int n;
int t[MAXN];
int adj[MAXN][2];
int cur;
 
void oku(int nd){
    int el;
    cin>>el;
    if(el!=0){
        t[nd]=el;
        return;
    }

    t[nd]=-1;
    adj[nd][0]=++cur;
    oku(cur);

    adj[nd][1]=++cur;
    oku(cur);
}
 
int inv[MAXN];
inset s[MAXN];
 
int merge(int nd,int sol,int sag){
    bool f=false;
    if(s[sol].size() < s[sag].size()){
        swap(sag,sol);
        f=true;
    }

    /*DEBUG(nd);
    DEBUG(sol);
    DEBUG(sag);*/

    int say=inv[sol];
    s[nd]=s[sol];
 
    if(t[nd]!=-1) s[sag].insert(t[nd]);

    /*cont(s[sol]);
    cont(s[sag]);
    cont(s[nd]);*/
    
    int cnt=0;
    if(!f){
        for(auto el:s[sag]){
            say+=s[nd].size()-s[nd].order_of_key(el);    
            s[nd].insert(el);
        }
    }
    else{
        for(auto el:s[sag]){
            say+=s[nd].order_of_key(el);
        }

        for(auto el:s[sag]){
            s[nd].insert(el);
        }
    }

    if(t[nd]!=-1) s[sag].erase(t[nd]);
    return say;
}
 
void dfs(int nd,int ata){ 
    if(t[nd]==0) return;
    dfs(adj[nd][0],ata);
    dfs(adj[nd][1],ata);
    inv[nd]=merge(nd,adj[nd][0],adj[nd][1]);
    inv[nd]=min(inv[nd],merge(nd,adj[nd][1],adj[nd][0]));
}
 
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    cin>>n;
    cur=1;

    oku(1);
    dfs(1,0);
 
    cout<<inv[1]<<endl;
}

Compilation message

rot.cpp: In function 'long long int merge(long long int, long long int, long long int)':
rot.cpp:85:9: warning: unused variable 'cnt' [-Wunused-variable]
   85 |     int cnt=0;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 15976 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 15976 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 16744 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 76 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 78 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 133 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 79 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 37 ms 41300 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 34 ms 41820 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 42 ms 42068 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 36 ms 41464 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -