Submission #1022440

# Submission time Handle Problem Language Result Execution time Memory
1022440 2024-07-13T13:58:40 Z BuiDucManh123 Tree Rotations (POI11_rot) C++17
0 / 100
160 ms 28752 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 taskname ""
#define int long long
using namespace std;
const int N=4e5+5;
int bit[N],sz[N],value[N],id=0,l,r,n,ans[N];
vector<int>g[N];
void input(){
    int x;
    cin>>x;
    value[id]=x;
    if (x) return;
    int k=id;
    id++;
    g[k].push_back(id);
    input();
    id++;
    g[k].push_back(id);
    input();
}
int getSum(int p) {
    int idx = p, ans = 0;
    while (idx > 0) {
        ans += bit[idx];
        idx -= (idx & (-idx));
    }
    return ans;
}
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(auto 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(auto x:g[id]) add(x,val);
}
void cal(int id){
    if(value[id]){
        l+=getSum(value[id]);
        r+=getSum(n)-getSum(value[id]);
        return;
    }for(auto 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];
    solve(x);solve(y);
    ans[id]=ans[x]+ans[y];
    if(sz[x]>sz[y]) swap(x,y);
    add(x,-1);
    l=0;r=0;
    cal(x);
    add(x,1);
    ans[id]+=min(l,r);
}
int32_t main() {
	if (fopen(taskname".inp","r")) {
		freopen(taskname".inp","r",stdin);
		freopen(taskname".out","w",stdout);
	}
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    cin>>n;
    input();
    cnt(0);
    solve(0);
    cout<<ans[0];
	return 0;
}

Compilation message

rot.cpp: In function 'int32_t main()':
rot.cpp:82:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |   freopen(taskname".inp","r",stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:83:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |   freopen(taskname".out","w",stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 9820 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 9820 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 9820 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 10332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 11408 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 44 ms 14420 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 38 ms 21992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 60 ms 21072 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 160 ms 28500 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 99 ms 28316 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 118 ms 28752 KB Output isn't correct
2 Halted 0 ms 0 KB -