답안 #1020520

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1020520 2024-07-12T06:28:26 Z toan2602 Tree Rotations (POI11_rot) C++17
0 / 100
1000 ms 21840 KB
#include<bits/stdc++.h>

using namespace std;
int n, x, idx = 1, a[400005], bit[400005], subtree[400005], ans[400005];
vector<int> adj[400005];
int RES = 0;
void add(int pos, int val) {
	while(pos <= idx) {
		bit[pos] += val;
		pos += pos & -pos;
	}
}

int get(int x) {
	int res = 0;
	while(x > 0) {
		res += bit[x];
		x -= x & -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)};
	for (int node: adj[u]) {pair<int, int> val = dfs2(node); res.first+=val.first,res.second+=val.second;}
	return res;
}

void dfs(int u) {
	int mx = 0, p = 0;
	for (int node: adj[u]) if(mx < subtree[node]) mx = subtree[node], p = node;
	if(p == 0) {
		add(a[u],1);
		return;
	}
	dfs(p);
	if(p == 1) swap(adj[u][0], adj[u][1]), p = 0;
	pair<int, int> val = dfs2(adj[u][1]);
	RES += min(val.first, val.second);
	dfs(adj[u][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 'int main()':
rot.cpp:76:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |   freopen("input.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:77:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |   freopen("output.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 9816 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 9820 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 173 ms 9816 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1028 ms 10072 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1048 ms 11100 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1025 ms 12376 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1045 ms 19804 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1028 ms 15952 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1028 ms 21840 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1026 ms 20804 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1071 ms 20396 KB Time limit exceeded
2 Halted 0 ms 0 KB -