Submission #1020625

# Submission time Handle Problem Language Result Execution time Memory
1020625 2024-07-12T07:49:04 Z vjudge1 Tree Rotations (POI11_rot) C++17
72 / 100
104 ms 65536 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=4e5+5, M = 4e6+5, mod = 1e9+7, inf = 1e18;
int sum[M], ls[M], rs[M];
int root[N], l[N], r[N], val[N];
int ans, s1, s2, 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+=(int)sum[rs[r1]]*sum[ls[r2]];
    s2+=(int)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;
}
# Verdict Execution time Memory 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 348 KB Output is correct
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1880 KB Output is correct
2 Correct 3 ms 2016 KB Output is correct
3 Correct 2 ms 2084 KB Output is correct
4 Correct 2 ms 2140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5208 KB Output is correct
2 Correct 7 ms 5604 KB Output is correct
3 Correct 18 ms 13656 KB Output is correct
4 Correct 9 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 23380 KB Output is correct
2 Correct 26 ms 22876 KB Output is correct
3 Correct 45 ms 25672 KB Output is correct
4 Correct 34 ms 27740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 40528 KB Output is correct
2 Correct 60 ms 41812 KB Output is correct
3 Correct 67 ms 40528 KB Output is correct
4 Correct 57 ms 42200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 58944 KB Output is correct
2 Correct 103 ms 58448 KB Output is correct
3 Correct 90 ms 60264 KB Output is correct
4 Correct 90 ms 59992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 66 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 68 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 66 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -