답안 #1020638

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1020638 2024-07-12T07:56:56 Z quannnguyen2009 Tree Rotations (POI11_rot) C++14
100 / 100
160 ms 54752 KB
#include<bits/stdc++.h>
// #define int long long
#define fi first
#define se second
#define pb push_back
#define ii pair<int, int>
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
using namespace std;

const int N=400005;
int sum[4000005],ls[4000005],rs[4000005];
int root[N],l[N],r[N],val[N];
long long ans,s1,s2;
int cnt,n,seg;
void init(int ro) {
    cin >> val[ro];
    if (!val[ro]){
        l[ro]=++cnt;
        init(l[ro]);
        r[ro]=++cnt;
        init(r[ro]);
    }
}
void pushup(int rt){
    sum[rt]=sum[ls[rt]]+sum[rs[rt]];
}
void build(int l,int r,int &rt,int key){
    if (!rt)rt=++seg;
    if (l==r){sum[rt]++;return;}
    int mid=(l+r)>>1;
    if (key<=mid)build(l,mid,ls[rt],key);
    else build(mid+1,r,rs[rt],key);
    pushup(rt);
}
int merge(int r1,int r2){
    if (!r1 || !r2)return r1+r2;
    s1+=(long long)sum[rs[r1]]*sum[ls[r2]];
    s2+=(long long)sum[ls[r1]]*sum[rs[r2]];
    ls[r1]=merge(ls[r1],ls[r2]);
    rs[r1]=merge(rs[r1],rs[r2]);
    pushup(r1);
    return r1;
}
void solve(int ro){
    if (!ro)return;
    solve(l[ro]);solve(r[ro]);
    if (!val[ro]){
        s1=s2=0;
        root[ro]=merge(root[l[ro]],root[r[ro]]);
        ans+=min(s1,s2);
    }
}
signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n; cnt=1;
    init(1);
    for (int i=1;i<=cnt;++i)
        if (val[i])build(1,n,root[i],val[i]);
    solve(1);
    cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1116 KB Output is correct
2 Correct 3 ms 1116 KB Output is correct
3 Correct 2 ms 1372 KB Output is correct
4 Correct 2 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 2908 KB Output is correct
2 Correct 6 ms 2908 KB Output is correct
3 Correct 16 ms 7260 KB Output is correct
4 Correct 6 ms 3444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 12380 KB Output is correct
2 Correct 29 ms 12092 KB Output is correct
3 Correct 33 ms 13912 KB Output is correct
4 Correct 29 ms 14924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 22020 KB Output is correct
2 Correct 43 ms 21892 KB Output is correct
3 Correct 47 ms 21080 KB Output is correct
4 Correct 40 ms 21584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 30288 KB Output is correct
2 Correct 72 ms 30036 KB Output is correct
3 Correct 66 ms 31568 KB Output is correct
4 Correct 63 ms 31328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 45180 KB Output is correct
2 Correct 82 ms 45132 KB Output is correct
3 Correct 112 ms 44196 KB Output is correct
4 Correct 96 ms 44892 KB Output is correct
5 Correct 109 ms 43668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 48884 KB Output is correct
2 Correct 106 ms 51796 KB Output is correct
3 Correct 136 ms 50772 KB Output is correct
4 Correct 106 ms 52048 KB Output is correct
5 Correct 128 ms 50024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 130 ms 51540 KB Output is correct
2 Correct 151 ms 52308 KB Output is correct
3 Correct 132 ms 51476 KB Output is correct
4 Correct 135 ms 51540 KB Output is correct
5 Correct 160 ms 54752 KB Output is correct
6 Correct 109 ms 51544 KB Output is correct