Submission #1020606

# Submission time Handle Problem Language Result Execution time Memory
1020606 2024-07-12T07:32:30 Z toan2602 Tree Rotations (POI11_rot) C++14
90 / 100
467 ms 65536 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;
set<int> S;
map<int,int> mp;
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;
	for (int i = 1; i <= n; i++) if(a[i]) S.insert(a[i]);
	int d = 0;
	for (int i: S) mp[i] = ++d;
	for (int i = 1; i <= n; i++) a[i] = mp[a[i]];
	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:60: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]
   60 |  for (int i = 0; i < adj[u].size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~
rot.cpp: In function 'int main()':
rot.cpp:92:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |   freopen("input.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:93:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |   freopen("output.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23900 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 11 ms 23900 KB Output is correct
4 Correct 12 ms 23900 KB Output is correct
5 Correct 11 ms 23900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23900 KB Output is correct
2 Correct 11 ms 23896 KB Output is correct
3 Correct 11 ms 23896 KB Output is correct
4 Correct 11 ms 23896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23900 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 12 ms 23900 KB Output is correct
4 Correct 11 ms 24156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24668 KB Output is correct
2 Correct 15 ms 24668 KB Output is correct
3 Correct 14 ms 24924 KB Output is correct
4 Correct 14 ms 24924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 26712 KB Output is correct
2 Correct 31 ms 26264 KB Output is correct
3 Correct 54 ms 29588 KB Output is correct
4 Correct 22 ms 26748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 33364 KB Output is correct
2 Correct 59 ms 34128 KB Output is correct
3 Correct 78 ms 36176 KB Output is correct
4 Correct 68 ms 36512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 43880 KB Output is correct
2 Correct 99 ms 42500 KB Output is correct
3 Correct 129 ms 40784 KB Output is correct
4 Correct 103 ms 40784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 161 ms 46672 KB Output is correct
2 Correct 155 ms 47188 KB Output is correct
3 Correct 138 ms 50116 KB Output is correct
4 Correct 129 ms 49852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 298 ms 58708 KB Output is correct
2 Correct 215 ms 57940 KB Output is correct
3 Correct 245 ms 57572 KB Output is correct
4 Correct 230 ms 57680 KB Output is correct
5 Correct 335 ms 55816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 247 ms 60552 KB Output is correct
2 Correct 264 ms 65440 KB Output is correct
3 Correct 301 ms 63572 KB Output is correct
4 Correct 221 ms 65536 KB Output is correct
5 Correct 467 ms 60524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 279 ms 62404 KB Output is correct
2 Correct 270 ms 63120 KB Output is correct
3 Correct 349 ms 61524 KB Output is correct
4 Correct 305 ms 62260 KB Output is correct
5 Runtime error 221 ms 65536 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -