Submission #886768

# Submission time Handle Problem Language Result Execution time Memory
886768 2023-12-12T22:19:29 Z OAleksa Tree Rotations (POI11_rot) C++14
36 / 100
1000 ms 36180 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
 
#define f first
#define s second
#define int long long
const int N = 2e5 + 69;
int n, t = 1, id, ans;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
ordered_set;
ordered_set st[N];
int napravi() {
	int x;
	cin >> x;
	if (x == 0) {
		int l = napravi();
		int r = napravi();
		if (st[l].size() < st[r].size()) {
			st[l].swap(st[r]);
			swap(l, r);
		}
		int k1 = 0, k2 = 0;
		for (auto u : st[r]) {
			int x = st[l].order_of_key(u + 1);
			k1 += (int)st[l].size() - x;
		}
		//b a
		for (auto u : st[r]) {
			int x = st[l].order_of_key(u + 1);
			k2 += x;
		}
		for (auto u : st[r])
			st[l].insert(u);
		st[r].clear();
		ans += min(k1, k2);
		return l;
	}
	else {
		int id = t++;
		st[id].insert(x);
		return id;
	}
}
 
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen("snowcow.in", "r", stdin);
	//freopen("snowcow.out", "w", stdout);
  int tt = 1;
	//cin >> tt;
  while (tt--) {
  	cin >> n;
  	napravi();
  	cout << ans;
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19244 KB Output is correct
2 Correct 10 ms 19036 KB Output is correct
3 Correct 11 ms 19036 KB Output is correct
4 Correct 11 ms 19080 KB Output is correct
5 Correct 11 ms 19036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19036 KB Output is correct
2 Correct 11 ms 19136 KB Output is correct
3 Correct 11 ms 19036 KB Output is correct
4 Correct 11 ms 19036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19292 KB Output is correct
2 Correct 12 ms 19292 KB Output is correct
3 Correct 12 ms 19268 KB Output is correct
4 Correct 19 ms 19292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 309 ms 20092 KB Output is correct
2 Correct 22 ms 19548 KB Output is correct
3 Correct 352 ms 20076 KB Output is correct
4 Correct 485 ms 20564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1057 ms 21844 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 185 ms 24144 KB Output is correct
2 Execution timed out 1039 ms 23588 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1056 ms 34720 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1031 ms 20572 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1048 ms 36180 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1073 ms 22580 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1064 ms 22100 KB Time limit exceeded
2 Halted 0 ms 0 KB -