Submission #1020707

# Submission time Handle Problem Language Result Execution time Memory
1020707 2024-07-12T08:47:11 Z cnn008 Tree Rotations (POI11_rot) C++17
72 / 100
161 ms 46676 KB
#include "bits/stdc++.h"
using namespace std;
 
#ifdef N_N_C
#include "debug.h"
#else
#define cebug(...) "Arya"
#endif
 
#define ll long long
 
const int N=1e6+5;
const int mod=1e9+7;
 
int n,node=1,a[N],sz[N],cur,leaf[N];
ll ans;
vector <int> g[N];
struct BIT{
	#define gb(x) (x&(-x))
   	int n;
   	vector <int> bit;
   	void init(int _n){
   		bit.resize(_n,0);
   		n=_n;
   	}
   	void update(int pos, int val){
   		int i=pos;
      	for (; i<n; i+=gb(i)) bit[i]+=val;
   	}
   	int get(int pos){
    	int ans=0,i=pos;
      	for (; i>=1; i-=gb(i)) ans+=bit[i];
      	return ans;
   	}
}fw;
void input(){
	int x;
	cin>>x;
	a[node]=x;
	if(x) return;
	int nw=node;
	g[nw].push_back(++node);
	input();
	g[nw].push_back(++node);
	input();
}
void pre_dfs(int u){
	if(a[u]) leaf[u]=1;
	sz[u]=1;
	for(auto v:g[u]){
		pre_dfs(v);
		sz[u]+=sz[v];
		leaf[u]+=leaf[v];
	}
}
void add(int u){
	if(a[u]) cur+=fw.get(a[u]-1);
	for(auto v:g[u]) add(v);
}
void del(int u){
	if(a[u]) fw.update(a[u],-1);
	for(auto v:g[u]) del(v);
}
void upd(int u){
	if(a[u]) fw.update(a[u],1);
	for(auto v:g[u]) upd(v);
}
void dfs(int u){
	if(a[u]){
		fw.update(a[u],1);
		return;
	}
	int big=0,small=0;
	for(auto v:g[u]) if(sz[v]>sz[big]) big=v;
	for(auto v:g[u]){
		if(v!=big){
			dfs(v);
			del(v);
		}
	}
	assert(big);
	dfs(big);
	cur=0;
	for(auto v:g[u]) if(v!=big) add(v),upd(v),small=v;
	ans+=min(cur,leaf[big]*leaf[small]-cur);
}
void sol(){
	fw.init(N);
	cin>>n;
	input();
	pre_dfs(1);
	dfs(1);
	cout<<ans;
}
signed main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // freopen(".inp", "r", stdin);
    // freopen(".out", "w", stdout);
    int tt=1;
    //cin>>tt; 
    while(tt--){
    	sol();
    }
    cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 33112 KB Output is correct
2 Correct 7 ms 33112 KB Output is correct
3 Correct 6 ms 33116 KB Output is correct
4 Correct 6 ms 33144 KB Output is correct
5 Correct 6 ms 33284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 33112 KB Output is correct
2 Correct 6 ms 33116 KB Output is correct
3 Correct 6 ms 33112 KB Output is correct
4 Correct 5 ms 33116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 33112 KB Output is correct
2 Correct 6 ms 33116 KB Output is correct
3 Correct 6 ms 33116 KB Output is correct
4 Correct 6 ms 33140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 33372 KB Output is correct
2 Correct 9 ms 33372 KB Output is correct
3 Correct 7 ms 33372 KB Output is correct
4 Correct 7 ms 33624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 34136 KB Output is correct
2 Correct 14 ms 33628 KB Output is correct
3 Correct 32 ms 34140 KB Output is correct
4 Correct 12 ms 34136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 34868 KB Output is correct
2 Correct 27 ms 35932 KB Output is correct
3 Correct 34 ms 36956 KB Output is correct
4 Correct 30 ms 37004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 40536 KB Output is correct
2 Correct 39 ms 38488 KB Output is correct
3 Correct 49 ms 37212 KB Output is correct
4 Correct 41 ms 36444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 37236 KB Output is correct
2 Correct 54 ms 38228 KB Output is correct
3 Correct 56 ms 40780 KB Output is correct
4 Correct 54 ms 40540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 161 ms 43096 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 85 ms 42324 KB Output is correct
2 Incorrect 100 ms 46676 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 102 ms 44412 KB Output isn't correct
2 Halted 0 ms 0 KB -