Submission #1100039

# Submission time Handle Problem Language Result Execution time Memory
1100039 2024-10-12T14:35:58 Z sssamui Tree Rotations (POI11_rot) C++17
100 / 100
467 ms 37008 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using Tree =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main() {
	int n;
	cin >> n;

	int timer = 0;
	vector<ll> inversions(2 * n - 1);
	map<int, Tree<int>> sets;
	function<void(int)> dfs = [&](int idx) {
		int val;
		cin >> val;

		if (val == 0) {
			int idx_l = ++timer;
			dfs(idx_l);
			int idx_r = ++timer;
			dfs(idx_r);

			if (sets[idx_l].size() > sets[idx_r].size()) {
				sets[idx_l].swap(sets[idx_r]);
			}

			ll way_1 = 0;  // way 1: left set is joined to the right set
			ll way_2 = 0;  // way 2: right set is joined to the left set
			for (int v : sets[idx_l]) {
				int loc = sets[idx_r].order_of_key(v);
				way_1 += loc;
				way_2 += (int)sets[idx_r].size() - loc;
			}

			for (int v : sets[idx_l]) { sets[idx_r].insert(v); }
			sets[idx].swap(sets[idx_r]);
			sets.erase(idx_l);
			sets.erase(idx_r);

			inversions[idx] = inversions[idx_l] + inversions[idx_r] + min(way_1, way_2);
		} else if (val > 0) {
			sets[idx].insert(val);
		}
	};

	dfs(0);

	cout << inversions[0] << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 432 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1116 KB Output is correct
2 Correct 7 ms 604 KB Output is correct
3 Correct 5 ms 1404 KB Output is correct
4 Correct 4 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 3420 KB Output is correct
2 Correct 20 ms 1372 KB Output is correct
3 Correct 55 ms 3156 KB Output is correct
4 Correct 18 ms 2780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 4952 KB Output is correct
2 Correct 65 ms 8532 KB Output is correct
3 Correct 77 ms 11380 KB Output is correct
4 Correct 71 ms 11232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 22872 KB Output is correct
2 Correct 134 ms 16468 KB Output is correct
3 Correct 132 ms 10240 KB Output is correct
4 Correct 96 ms 8532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 160 ms 9812 KB Output is correct
2 Correct 181 ms 12628 KB Output is correct
3 Correct 153 ms 23124 KB Output is correct
4 Correct 147 ms 22612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 300 ms 22868 KB Output is correct
2 Correct 291 ms 16596 KB Output is correct
3 Correct 254 ms 19540 KB Output is correct
4 Correct 268 ms 18112 KB Output is correct
5 Correct 382 ms 15664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 263 ms 19284 KB Output is correct
2 Correct 289 ms 34828 KB Output is correct
3 Correct 363 ms 25172 KB Output is correct
4 Correct 264 ms 37008 KB Output is correct
5 Correct 467 ms 18516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 306 ms 22356 KB Output is correct
2 Correct 304 ms 19452 KB Output is correct
3 Correct 339 ms 15700 KB Output is correct
4 Correct 316 ms 22020 KB Output is correct
5 Correct 306 ms 33364 KB Output is correct
6 Correct 373 ms 19224 KB Output is correct