답안 #1022465

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1022465 2024-07-13T14:37:51 Z BuiDucManh123 Tree Rotations (POI11_rot) C++17
100 / 100
292 ms 44256 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 9 ms 19292 KB Output is correct
2 Correct 10 ms 19200 KB Output is correct
3 Correct 9 ms 19292 KB Output is correct
4 Correct 8 ms 19288 KB Output is correct
5 Correct 9 ms 19288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19292 KB Output is correct
2 Correct 9 ms 19320 KB Output is correct
3 Correct 9 ms 19292 KB Output is correct
4 Correct 10 ms 19288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19292 KB Output is correct
2 Correct 10 ms 19292 KB Output is correct
3 Correct 10 ms 19388 KB Output is correct
4 Correct 10 ms 19292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 19804 KB Output is correct
2 Correct 13 ms 19548 KB Output is correct
3 Correct 11 ms 19820 KB Output is correct
4 Correct 12 ms 19800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 20828 KB Output is correct
2 Correct 20 ms 20424 KB Output is correct
3 Correct 43 ms 21844 KB Output is correct
4 Correct 18 ms 20828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 23964 KB Output is correct
2 Correct 44 ms 24660 KB Output is correct
3 Correct 55 ms 26196 KB Output is correct
4 Correct 48 ms 26448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 31428 KB Output is correct
2 Correct 63 ms 29648 KB Output is correct
3 Correct 76 ms 28116 KB Output is correct
4 Correct 64 ms 27472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 93 ms 30540 KB Output is correct
2 Correct 81 ms 31372 KB Output is correct
3 Correct 105 ms 33872 KB Output is correct
4 Correct 87 ms 33616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 205 ms 37716 KB Output is correct
2 Correct 145 ms 37032 KB Output is correct
3 Correct 125 ms 36692 KB Output is correct
4 Correct 153 ms 36176 KB Output is correct
5 Correct 228 ms 35108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 171 ms 37712 KB Output is correct
2 Correct 149 ms 42396 KB Output is correct
3 Correct 163 ms 41040 KB Output is correct
4 Correct 134 ms 43344 KB Output is correct
5 Correct 275 ms 37456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 166 ms 38168 KB Output is correct
2 Correct 137 ms 39576 KB Output is correct
3 Correct 212 ms 37972 KB Output is correct
4 Correct 167 ms 38400 KB Output is correct
5 Correct 135 ms 44256 KB Output is correct
6 Correct 292 ms 37968 KB Output is correct