답안 #1020616

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1020616 2024-07-12T07:36:25 Z vjudge1 Tree Rotations (POI11_rot) C++17
100 / 100
315 ms 45140 KB
#include<bits/stdc++.h>
 
using namespace std;
#define int long long
int n, x, idx = 1, a[1000005], bit[1000005], subtree[1000005];
vector<int> adj[1000005];
int RES = 0;
void add(int pos, int val) {
	while(pos <= idx) {
		bit[pos] += val;
		pos += pos & -pos;
	}
}
 
int get(int x) {
	if(x <= 0) return 0;
	int res = 0;
	for (; x; x-=x&(-x)) res+=bit[x];
	return res;
}
int range(int l, int r) {return get(r) - get(l-1);}
void build_sub(int u, int par) {
	subtree[u] = 1;
	for (int node: adj[u]) {
		if(node == par) continue;
		build_sub(node, u);
		subtree[u] += subtree[node];
	}
}
void build(int u) {
	cin >> x;
	if(x == 0) {
		int l, r;
		l = idx+1, r = idx+2;
		idx+=2;
		adj[u].push_back(l);
		adj[u].push_back(r);
		build(l);
		build(r);
	}
	else if(adj[u].size() == 0) a[u] = x;
}
 
pair<int,int> dfs2(int u) {
	pair<int, int> res = {range(a[u]+1,idx), range(1,a[u]-1)};
	if(a[u] == 0) res = {0,0};
	for (int node: adj[u]) {pair<int, int> val = dfs2(node); res.first+=val.first,res.second+=val.second;}
	return res;
}
 
void upd(int u, int tp) {
	if(adj[u].empty()) add(a[u], tp);
	for (int node: adj[u]) upd(node,tp);
}
 
void dfs(int u) {
	int mx = 0, p = -1, idx;
	for (int i = 0; i < adj[u].size(); i++) {
		int node = adj[u][i];
		if(mx < subtree[node]) mx = subtree[node], p = node, idx = i;
	}
	if(p == -1) {
		add(a[u],1);
		return;
	}
	if(idx == 1) swap(adj[u][0], adj[u][1]);
	dfs(adj[u][1]);
	upd(adj[u][1], -1);
	dfs(p);
	pair<int, int> val = dfs2(adj[u][1]);
	RES += min(val.first, val.second);
	upd(adj[u][1], 1);
}
void solve() {
	cin >> n;
 	build(1);
	n = idx;
	build_sub(1,-1);
	dfs(1);
	cout << RES;
}
 
signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen("input.inp","r")){
		freopen("input.inp", "r", stdin);
		freopen("output.out", "w", stdout);
	}
	int t = 1;
	// cin >> t;
	while(t--) {
		solve();
	}
}

Compilation message

rot.cpp: In function 'void dfs(long long int)':
rot.cpp:58:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |  for (int i = 0; i < adj[u].size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~
rot.cpp: In function 'int main()':
rot.cpp:86:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |   freopen("input.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:87:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |   freopen("output.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23900 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 10 ms 23756 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 10 ms 23896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23900 KB Output is correct
2 Correct 12 ms 23804 KB Output is correct
3 Correct 12 ms 23900 KB Output is correct
4 Correct 12 ms 23900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23896 KB Output is correct
2 Correct 12 ms 23900 KB Output is correct
3 Correct 12 ms 24020 KB Output is correct
4 Correct 12 ms 23896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24664 KB Output is correct
2 Correct 13 ms 24156 KB Output is correct
3 Correct 17 ms 24492 KB Output is correct
4 Correct 11 ms 24544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 25436 KB Output is correct
2 Correct 19 ms 24920 KB Output is correct
3 Correct 35 ms 26204 KB Output is correct
4 Correct 20 ms 25432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 27864 KB Output is correct
2 Correct 45 ms 28764 KB Output is correct
3 Correct 39 ms 30036 KB Output is correct
4 Correct 37 ms 30296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 34896 KB Output is correct
2 Correct 48 ms 32920 KB Output is correct
3 Correct 58 ms 31628 KB Output is correct
4 Correct 52 ms 31064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 33200 KB Output is correct
2 Correct 63 ms 34128 KB Output is correct
3 Correct 62 ms 36948 KB Output is correct
4 Correct 58 ms 36592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 192 ms 39508 KB Output is correct
2 Correct 118 ms 38392 KB Output is correct
3 Correct 96 ms 38480 KB Output is correct
4 Correct 135 ms 38296 KB Output is correct
5 Correct 209 ms 36944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 39468 KB Output is correct
2 Correct 131 ms 43860 KB Output is correct
3 Correct 128 ms 41992 KB Output is correct
4 Correct 116 ms 44672 KB Output is correct
5 Correct 289 ms 38648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 125 ms 39760 KB Output is correct
2 Correct 104 ms 40528 KB Output is correct
3 Correct 195 ms 39140 KB Output is correct
4 Correct 144 ms 39804 KB Output is correct
5 Correct 92 ms 45140 KB Output is correct
6 Correct 315 ms 39080 KB Output is correct