Submission #765932

# Submission time Handle Problem Language Result Execution time Memory
765932 2023-06-25T07:50:17 Z vjudge1 Tree Rotations (POI11_rot) C++17
0 / 100
1000 ms 35664 KB
#include <bits/stdc++.h>
 
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(a) a.begin() , a.end()
#define F first
#define S second
#define int ll 
 
using namespace std;
using ll = long long;
 
const int N = 4e5+5 , inf = 2e9 + 7;
const ll INF = 1e18 ,   mod = 1e9+7;	

int a[N] , ans[N] , n , mxson[N] , sz[N] , len , t[N*5] , m; 
vector<int> g[N] , vec[N];
void precalc(int v , int pr){
	if(v <= n) sz[v] = 1;
	for(int to : g[v]){
		if(to != pr){
			precalc(to,v);
			sz[v] += sz[to];
			if(sz[to] > sz[mxson[v]]) mxson[v] = to;
		}
	}
}

void upd(int p, int value) { 
  	for (t[p += m] = value; p > 1; p >>= 1) t[p>>1] = t[p] + t[p^1];
}

int get(int l, int r) {
	r++;
 	int res = 0;
  	for (l += m, r += m; l < r; l >>= 1, r >>= 1) {
    	if (l&1) res += t[l++];
   		if (r&1) res += t[--r];
  	}
  	return res;
}

void dfs(int v , int pr){
	for(int to : g[v]){
		if(to != pr && to != mxson[v]){
			dfs(to,v);
			for(int x : vec[to]) upd(x-1,0);
		}
	}
	if(mxson[v]) {
		dfs(mxson[v],v);
		vec[v] = vec[mxson[v]];
		ans[v] += ans[mxson[v]];
	}
	int res = 0 , res1 = 0;
	for(int to : g[v]){
		int ob = t[1];
		if(to != pr && to != mxson[v]) {
			for(int x : vec[to]) {
				int num = get(x-1,n);
				res += num;
				res1 += ob-num;
			}
			for(int x : vec[to]) {
				upd(x-1,1);
				vec[v].push_back(x);
			}
			ans[v] += ans[to];
		}
	}
	if(v <= n) {
		vec[v].push_back(v);
		upd(v-1,1);
	}
	else {
		ans[v] += min(res,res1);
	}
} 
void go(int num){
	int x;
	cin >> x;
	if(x == 0){
		g[num].push_back(num+1);
		g[num+1].push_back(num);
		go(num+1);
	} else {
		g[num].push_back(x);
		g[x].push_back(num);
	}
	cin >> x;
	if(x == 0){
		g[num].push_back(num+1);
		g[num+1].push_back(num);
		go(num+1);
	} else {
		g[num].push_back(x);
		g[x].push_back(num);
	}
}
void solve(){
	cin >> n;
	m = (1 << 19);
	int a;
	cin >> a;
	go(n+1);
	precalc(n+1,0);
	len = 450;
	dfs(n+1,0);
	cout << ans[n+1] << "\n";
}
/*

*/
signed main(){
	ios;
	solve();
	return 0;
} 
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 19156 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1080 ms 19028 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1078 ms 19156 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1081 ms 19668 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1079 ms 21076 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1086 ms 22444 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1074 ms 33484 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1074 ms 28044 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1082 ms 34792 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1076 ms 35664 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1077 ms 34884 KB Time limit exceeded
2 Halted 0 ms 0 KB -