Submission #1021985

# Submission time Handle Problem Language Result Execution time Memory
1021985 2024-07-13T08:28:51 Z BuiDucManh123 Tree Rotations (POI11_rot) C++14
0 / 100
137 ms 44820 KB
#include <bits/stdc++.h>
using namespace std;
const int N=4e5;
vector <int> g[N+9],v[N+9];
int n,value[N+9],bit[N+9],ans[N+9],x,i=0,l,r,siz[N+9];
void input(){
    cin>>x;
    if(x){
        value[i]=x;
        return;
    }
    int k=i;
    i++;
    g[k].push_back(i);
    input();
    i++;
    g[k].push_back(i);
    input();
}
void cnt(int i){
    if(value[i]){
        siz[i]=1;
        return;
    }for(auto x:g[i]){
        cnt(x);
        siz[i]+=siz[x];
    }
}
void update(int i, int val){
    while(i<=N){
        bit[i]+=val;
        i+=i&-i;
    }
}
int get(int i){
    int val=0;
    while(i){
        val+=bit[i];
        i-=i&-i;
    }
    return val;
}
void add(int i, int val){
    if (value[i]){
        update(value[i],val);
        return;
    }
    for(auto x:g[i]){
        add(x,val);
    }
}
void tinh(int i){
    if (value[i]){
        l+=get(value[i]);
        r+=get(n)-get(value[i]);
        return;
    }
    for (auto x : g[i]) tinh(x);
}
void solve(int i){
    if (value[i]){
        v[i].push_back(value[i]);
        update(value[i],1);
        return;
    }
    for(auto x:g[i]){
        solve(x);
        ans[i]+=ans[x];
    }int x=g[i][0];
    int y=g[i][1];
    if (siz[x]>siz[y]) swap(x,y);
    l=0;
    for(auto j:v[x]){
        update(j,-1);
    }
    for(auto j:v[x])
        l+=get(j);
    r=v[x].size();
    r*=v[y].size();
    swap(v[i],v[y]);
    for(auto j:v[x]) v[i].push_back(j);
    ans[i]+=min(l,r-l);
}
int32_t main(){
    cin>>n;
    input();
    cnt(0);
    solve(0);
    cout<<ans[0];
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 19292 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 19292 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 19288 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 19804 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 21160 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 43 ms 25424 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 53 ms 34764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 75 ms 32396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 137 ms 44820 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 127 ms 41404 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 126 ms 42700 KB Output isn't correct
2 Halted 0 ms 0 KB -