Submission #1020587

# Submission time Handle Problem Language Result Execution time Memory
1020587 2024-07-12T07:18:25 Z BuzzyBeez Tree Rotations (POI11_rot) C++17
72 / 100
160 ms 43948 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 8e5;

int a[N + 8];
int adj[N + 8][2];
vector<int> _list[N + 8];
int sz[N + 8];

int n, tmp, id = 1, x;
void input() {
	cin >> x;
	if (x) {_list[id].push_back(x); ++sz[id]; a[id] = x; return;}
	int t = id;
	adj[t][0] = id + 1;
	++id; input();
	adj[t][1] = id + 1;
	++id; input();
}

void DFS_sz(int u, int p) {
	if (!adj[u][0]) return;
	DFS_sz(adj[u][0], u); sz[u] += sz[adj[u][0]];
	DFS_sz(adj[u][1], u); sz[u] += sz[adj[u][1]];
}

struct Binary_Indexed_Tree {
	vector<int> BIT;
	
	Binary_Indexed_Tree() {BIT.assign(N / 4 + 8, 0);}
	
	int pt;
	void update(int u, int v) {
		pt = u;
		while (pt <= N / 4) {
			BIT[pt] += v;
			pt += pt & -pt;
		}
	}
	
	int res;
	int pf(int p) {
		if (p <= 0) return 0;
		res = 0; pt = p;
		while (pt) {
			res += BIT[pt];
			pt -= pt & -pt;
		}
		return res;
	}
	
	int range(int l, int r) {
		if (l > r) return 0;
		return pf(r) - pf(l - 1);
	}
} tree;

int costLR, costRL; long long ans = 0;
void DFS(int u, int p) {
	if (sz[u] > 1) { 
		int L = adj[u][0], R = adj[u][1];
		if (sz[L] < sz[R]) swap(L, R);
		// cout << u << " -> " << R << endl;
		DFS(R, u);
		for (int item : _list[R]) tree.update(item, -1);
		// cout << u << " -> " << L << endl;
		DFS(L, u);
		costLR = costRL = 0;
		for (int item : _list[R]) costLR += tree.range(item + 1, n), costRL += tree.range(1, item - 1);
		ans += min(costLR, costRL);
		// cout << u << ' ' << costLR << ' ' << costRL << '\n';
		for (int item : _list[R]) tree.update(item, +1);
		for (int v : {L, R}) {
			if (_list[u].size() < _list[v].size()) swap(_list[u], _list[v]);
			for (int item : _list[v]) _list[u].push_back(item);
		}
	}
	if (a[u]) tree.update(a[u], +1);
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n; input();
	DFS_sz(1, 0);
	DFS(1, 0); cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 20060 KB Output is correct
2 Correct 12 ms 19952 KB Output is correct
3 Correct 9 ms 20056 KB Output is correct
4 Correct 9 ms 20056 KB Output is correct
5 Correct 8 ms 20056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 20060 KB Output is correct
2 Correct 10 ms 20060 KB Output is correct
3 Correct 9 ms 20060 KB Output is correct
4 Correct 8 ms 20040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 20060 KB Output is correct
2 Correct 9 ms 20108 KB Output is correct
3 Correct 9 ms 20060 KB Output is correct
4 Correct 8 ms 20060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 20572 KB Output is correct
2 Correct 11 ms 20316 KB Output is correct
3 Correct 10 ms 20632 KB Output is correct
4 Correct 11 ms 20572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21848 KB Output is correct
2 Correct 17 ms 21012 KB Output is correct
3 Correct 41 ms 22868 KB Output is correct
4 Correct 16 ms 21848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 24864 KB Output is correct
2 Correct 33 ms 25948 KB Output is correct
3 Correct 35 ms 27728 KB Output is correct
4 Correct 38 ms 27728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 34012 KB Output is correct
2 Correct 44 ms 30876 KB Output is correct
3 Correct 59 ms 29396 KB Output is correct
4 Correct 43 ms 27056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 28900 KB Output is correct
2 Correct 57 ms 30680 KB Output is correct
3 Correct 55 ms 34932 KB Output is correct
4 Correct 53 ms 34648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 160 ms 40724 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 109 ms 35788 KB Output is correct
2 Incorrect 115 ms 43948 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 103 ms 36236 KB Output isn't correct
2 Halted 0 ms 0 KB -