답안 #765977

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765977 2023-06-25T08:24:31 Z vjudge1 Tree Rotations (POI11_rot) C++17
100 / 100
282 ms 44188 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 = 5e5+5 , inf = 2e9 + 7;
const ll INF = 1e18 ,   mod = 1e9+7;	

int ans[N] , n , mxson[N] , sz[N] , t[N] , m; 
vector<int> g[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 i, int x){
    for(; i <= n; i |= (i + 1)) t[i] += x;
}

int get(int i){
    int res = 0;
    for(; i > 0; i = (i & (i + 1)) - 1) res += t[i];
    return res;
}
int ob;

pair<int,int> calc(int v, int pr){
	if(v <= n) {
		int num = get(v);
		return {num,ob-num};
	}
	int res = 0 , res1 = 0;
	for(int to : g[v]){
		 if(to != pr) {
		 	pair<int,int> a = calc(to,v);
		 	res += a.F;
		 	res1 += a.S;
		 }
	}
	return {res,res1};
}
void del(int v, int pr){
	if(v <= n) upd(v,-1);
	for(int to : g[v]) if(to != pr) del(to,v);
}
void add(int v, int pr){
	if(v <= n) upd(v,1);
	for(int to : g[v]) if(to != pr) add(to,v);
}
void dfs(int v , int pr){
	for(int to : g[v]){
		if(to != pr && to != mxson[v]){
			dfs(to,v);
			del(to,v);
		}
	}
	if(mxson[v]) {
		dfs(mxson[v],v);
		ans[v] += ans[mxson[v]];
	}
	int res = 0 , res1 = 0;
	for(int to : g[v]){
		ob = get(n);
		if(to != pr && to != mxson[v]) {
			pair<int,int> b = calc(to,v);
			res += b.F;
			res1 += b.S;
			add(to,v);
			ans[v] += ans[to];
		}
	}
	if(v <= n) upd(v,1);
	else ans[v] += min(res,res1);
} 
int NUM;
void go(int v){
	int x;
	cin >> x;
	if(x == 0){
		int to = ++NUM;
		g[v].push_back(to);
		g[to].push_back(v);
		go(to);
	} else {
		g[v].push_back(x);
		g[x].push_back(v);
	}
	cin >> x;
	if(x == 0){
		int to = ++NUM;
		g[v].push_back(to);
		g[to].push_back(v);
		go(to);
	} else {
		g[v].push_back(x);
		g[x].push_back(v);
	}
}
void solve(){
	cin >> n;
	NUM = n+1;
	int a; cin >> a;
	go(n+1);
	precalc(n+1,0);
	dfs(n+1,0);
	cout << ans[n+1] << "\n";
}
/*

*/
signed main(){
	ios;
	solve();
	return 0;
} 	
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 5 ms 11988 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 12036 KB Output is correct
5 Correct 5 ms 11988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 11988 KB Output is correct
2 Correct 5 ms 11988 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12116 KB Output is correct
2 Correct 6 ms 12116 KB Output is correct
3 Correct 7 ms 12116 KB Output is correct
4 Correct 6 ms 12116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12756 KB Output is correct
2 Correct 8 ms 12500 KB Output is correct
3 Correct 8 ms 12756 KB Output is correct
4 Correct 9 ms 12884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14164 KB Output is correct
2 Correct 14 ms 13524 KB Output is correct
3 Correct 34 ms 15528 KB Output is correct
4 Correct 13 ms 14148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 17976 KB Output is correct
2 Correct 41 ms 19220 KB Output is correct
3 Correct 39 ms 20812 KB Output is correct
4 Correct 39 ms 21188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 27684 KB Output is correct
2 Correct 64 ms 25292 KB Output is correct
3 Correct 68 ms 23444 KB Output is correct
4 Correct 51 ms 22820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 26432 KB Output is correct
2 Correct 75 ms 28384 KB Output is correct
3 Correct 102 ms 31776 KB Output is correct
4 Correct 77 ms 31392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 206 ms 35504 KB Output is correct
2 Correct 136 ms 35024 KB Output is correct
3 Correct 141 ms 35020 KB Output is correct
4 Correct 139 ms 34732 KB Output is correct
5 Correct 227 ms 33036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 150 ms 35760 KB Output is correct
2 Correct 143 ms 42308 KB Output is correct
3 Correct 188 ms 39992 KB Output is correct
4 Correct 125 ms 43252 KB Output is correct
5 Correct 261 ms 35736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 165 ms 36556 KB Output is correct
2 Correct 154 ms 38408 KB Output is correct
3 Correct 208 ms 36432 KB Output is correct
4 Correct 170 ms 37416 KB Output is correct
5 Correct 172 ms 44188 KB Output is correct
6 Correct 282 ms 36344 KB Output is correct