Submission #1020738

# Submission time Handle Problem Language Result Execution time Memory
1020738 2024-07-12T09:07:58 Z kustizus Tree Rotations (POI11_rot) C++17
36 / 100
1000 ms 16196 KB
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,fma,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
#define int long long
using namespace std;
mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=4e5;
vector <int> v;
pair <int,int> mp[N+5];
int n,x,idx=0,cnt[N+5],sz[N+5],sum[N+5],FT[N+5];
void input(){
    int x;
    cin>>x;
    cnt[idx]=x;
    if (x) return;
    int nw=idx;
    mp[nw].first=++idx;
    input();
    mp[nw].second=++idx;
    input();
}
void dfs(int i){
    if (cnt[i]){
        sz[i]=1;
        return;
    }
    int x=mp[i].first;
    int y=mp[i].second;
    sz[i]=sz[x]+sz[y];
}
void Update(int pos, int val){
    for (;pos<=N;pos+=pos&(-pos)) FT[pos]+=val;
}
int Get(int pos){
    int val=0;
    for (;pos;pos-=pos&(-pos)) val+=FT[pos];
    return val;
}
void cl(int i, int val){
    if (cnt[i]){
        Update(cnt[i],val);
        return;
    }
    int x=mp[i].first;
    int y=mp[i].second;
    cl(x,val);
    cl(y,val);
}
int lh,nh;
void Cal(int i){
    if (cnt[i]){
        lh+=Get(cnt[i]);
        nh+=Get(n)-Get(cnt[i]);
        return;
    }
    int x=mp[i].first;
    int y=mp[i].second;
    Cal(x);
    Cal(y);
}
void dfs1(int i){
    if (cnt[i]){
        Update(cnt[i],1);
        return;
    }
    int x=mp[i].first;
    int y=mp[i].second;
    if (sz[x]>sz[y]) swap(x,y);
    dfs1(x);
    sum[i]+=sum[x];
    cl(x,-1);
    dfs1(y);
    sum[i]+=sum[y];
    lh=nh=0;
    Cal(x);
    cl(x,1);
    sum[i]+=min(lh,nh);
}
void Solve(){
    cin>>n;
    input();
    dfs(0);
    dfs1(0);
    cout<<sum[0];
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // freopen ("FILE.INP","r",stdin);
    // freopen ("FILE.OUT","w",stdout);
    int t=1;
    // cin>>t;
    while (t--) Solve();
    cerr<<"\nTIME: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6744 KB Output is correct
2 Correct 2 ms 6488 KB Output is correct
3 Correct 1 ms 6684 KB Output is correct
4 Correct 8 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 209 ms 6744 KB Output is correct
2 Correct 7 ms 7000 KB Output is correct
3 Correct 148 ms 6748 KB Output is correct
4 Correct 254 ms 6920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1027 ms 7256 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 96 ms 7936 KB Output is correct
2 Execution timed out 1051 ms 8280 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1045 ms 12368 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1061 ms 9792 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1062 ms 16196 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1010 ms 14168 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1018 ms 14168 KB Time limit exceeded
2 Halted 0 ms 0 KB -