답안 #1022466

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1022466 2024-07-13T14:38:20 Z vjudge1 Tree Rotations (POI11_rot) C++14
100 / 100
293 ms 43640 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define int long long
using namespace std;
const int N=8e5+5;
int bit[N],sz[N],value[N],id=0,n,ans[N];
vector<int>g[N];
void input(){
    int x;
    cin>>x;
    value[id]=x;
    if (x) return;
    int k=id;
    g[k].push_back(++id);
    input();
    g[k].push_back(++id);
    input();
}
int getSum(int p) {
    int idx = p, res = 0;
    while (idx > 0) {
        res += bit[idx];
        idx -= (idx & (-idx));
    }
    return res;
}
void update(int u, int v) {
    int idx = u;
    while (idx <= N) {
        bit[idx] += v;
        idx += (idx & (-idx));
    }
}
void cnt(int id){
    if(value[id]){
        sz[id]=1;
        return;
    }for(int x:g[id]){
        cnt(x);
        sz[id]+=sz[x];
    }return;
}
void add(int id, int val){
    if(value[id]){
        update(value[id],val);
        return;
    }for(int x:g[id]) add(x,val);
}int l,r;
void cal(int id){
    if(value[id]){
        l+=getSum(value[id]);
        r+=getSum(n)-getSum(value[id]);
        return;
    }for(int x:g[id]) cal(x);
}
void solve(int id){
    if(value[id]){
        update(value[id],1);
        return;
    }int x=g[id][0];
    int y=g[id][1];
    if(sz[x]>sz[y]) swap(x,y);
    solve(x);
    ans[id]+=ans[x];
    add(x,-1);
    solve(y);
    ans[id]+=ans[y];
    l=0;r=0;
    cal(x);
    add(x,1);
    ans[id]+=min(l,r);
    return;
}
signed main() {
    cin>>n;
    input();
    cnt(0);
    solve(0);
    cout<<ans[0];
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19288 KB Output is correct
2 Correct 9 ms 19292 KB Output is correct
3 Correct 9 ms 19292 KB Output is correct
4 Correct 9 ms 19272 KB Output is correct
5 Correct 11 ms 19292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19288 KB Output is correct
2 Correct 9 ms 19292 KB Output is correct
3 Correct 9 ms 19292 KB Output is correct
4 Correct 9 ms 19292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19288 KB Output is correct
2 Correct 9 ms 19268 KB Output is correct
3 Correct 13 ms 19292 KB Output is correct
4 Correct 13 ms 19188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19800 KB Output is correct
2 Correct 12 ms 19548 KB Output is correct
3 Correct 12 ms 19680 KB Output is correct
4 Correct 11 ms 19804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 20828 KB Output is correct
2 Correct 21 ms 20312 KB Output is correct
3 Correct 43 ms 22108 KB Output is correct
4 Correct 18 ms 20800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 23972 KB Output is correct
2 Correct 52 ms 24712 KB Output is correct
3 Correct 49 ms 26096 KB Output is correct
4 Correct 49 ms 26456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 31320 KB Output is correct
2 Correct 67 ms 29488 KB Output is correct
3 Correct 76 ms 27988 KB Output is correct
4 Correct 61 ms 27472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 30252 KB Output is correct
2 Correct 80 ms 31312 KB Output is correct
3 Correct 83 ms 33620 KB Output is correct
4 Correct 82 ms 33504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 209 ms 37456 KB Output is correct
2 Correct 148 ms 36436 KB Output is correct
3 Correct 130 ms 36436 KB Output is correct
4 Correct 151 ms 35720 KB Output is correct
5 Correct 238 ms 34896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 140 ms 37200 KB Output is correct
2 Correct 144 ms 41804 KB Output is correct
3 Correct 162 ms 40272 KB Output is correct
4 Correct 133 ms 42576 KB Output is correct
5 Correct 276 ms 36944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 158 ms 37712 KB Output is correct
2 Correct 137 ms 38916 KB Output is correct
3 Correct 211 ms 37712 KB Output is correct
4 Correct 173 ms 37712 KB Output is correct
5 Correct 143 ms 43640 KB Output is correct
6 Correct 293 ms 37460 KB Output is correct