답안 #1020551

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1020551 2024-07-12T06:54:49 Z BuzzyBeez Tree Rotations (POI11_rot) C++17
72 / 100
225 ms 65536 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 4e5;

int a[N + 8];
int adj[N + 8][2];
set<int> _list[N + 8];
long long inv[N + 8];
int sz[N + 8];

int n, tmp, id = 1;
void input() {
	int x; cin >> x;
	if (x) {_list[id].insert(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] == 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 / 2 + 8, 0);}
	
	int pt;
	void update(int u, int v) {
		pt = u;
		while (pt <= N / 2) {
			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;
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);
		inv[u] += inv[R];
		for (int item : _list[R]) tree.update(item, -1);
		// cout << u << " -> " << L << endl;
		DFS(L, u);
		inv[u] += inv[L];
		costLR = costRL = 0;
		for (int item : _list[R]) costLR += tree.range(item + 1, n), costRL += tree.range(1, item - 1);
		inv[u] += min(costLR, costRL);
		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].insert(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 << inv[1];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 20312 KB Output is correct
2 Correct 8 ms 20316 KB Output is correct
3 Correct 8 ms 20316 KB Output is correct
4 Correct 8 ms 20316 KB Output is correct
5 Correct 8 ms 20444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 20316 KB Output is correct
2 Correct 7 ms 20316 KB Output is correct
3 Correct 9 ms 20316 KB Output is correct
4 Correct 11 ms 20312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 20572 KB Output is correct
2 Correct 8 ms 20668 KB Output is correct
3 Correct 8 ms 20572 KB Output is correct
4 Correct 8 ms 20672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 21340 KB Output is correct
2 Correct 13 ms 21588 KB Output is correct
3 Correct 10 ms 21340 KB Output is correct
4 Correct 11 ms 21596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23384 KB Output is correct
2 Correct 25 ms 24156 KB Output is correct
3 Correct 81 ms 32304 KB Output is correct
4 Correct 20 ms 23640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 93 ms 37820 KB Output is correct
2 Correct 56 ms 34032 KB Output is correct
3 Correct 79 ms 36176 KB Output is correct
4 Correct 70 ms 36432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 42068 KB Output is correct
2 Correct 107 ms 41812 KB Output is correct
3 Correct 125 ms 44120 KB Output is correct
4 Correct 72 ms 38128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 143 ms 44044 KB Output is correct
2 Correct 124 ms 44820 KB Output is correct
3 Correct 127 ms 51492 KB Output is correct
4 Correct 112 ms 50004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 166 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 211 ms 61784 KB Output is correct
2 Runtime error 155 ms 65536 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 225 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -