Submission #863941

# Submission time Handle Problem Language Result Execution time Memory
863941 2023-10-21T13:52:20 Z vjudge1 Tree Rotations (POI11_rot) C++17
100 / 100
782 ms 27536 KB
//order_of_key(k): Number of items strictly smaller than k .
//find_by_order(k): K-th element in a set (counting from zero).

//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define file(s) if(fopen(s".in","r")) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define len(x) (int)x.size()
#define tm (tl + tr >> 1)
#define ls v << 1, tl, tm
#define rs v << 1 | 1, tm + 1, tr
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define elif else if
#define F first
#define S second
#define int long long

using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ld;

const int MOD = 1e9 + 7;
const int N = 4e5 + 7;
const int P = 911;
const ll INF = 1e18;

int rnd() {
	int x = rand() << 15;
	return x ^ rand();
}

int n, t[N << 2], sz[N], l[N], r[N], a[N], ans = 0;

void rec(int idx, int val, int v = 1, int tl = 1, int tr = n) {
//	cout << "rec " << v << ' ' << tl << ' ' << tr << '\n';
	if (tl == tr) {
		t[v] += val;
		return;
	}
	if (tl > tr) return;
	if (idx <= tm) rec(idx, val, ls);
	else rec(idx, val, rs);
	t[v] = t[v << 1] + t[v << 1 | 1];
}

int get(int l, int r, int v = 1, int tl = 1, int tr = n) {
	if (l > r || r < tl || tr < l) return 0;
	if (l <= tl && tr <= r) return t[v];
	return get(l, r, ls) + get(l, r, rs);
}

void add(int v, int &cur1, int &cur2) {
//	cout << "add " << v << '\n';
	if (a[v]) {
		cur1 += get(1, a[v] - 1);
		cur2 += get(a[v] + 1, n);
//		cout << v << ' ' << cur1 << ' ' << cur2 << '\n';
		return;
	}
	add(l[v], cur1, cur2);
	add(r[v], cur1, cur2);
}

void upd(int v, int ty) {
//	cout << "upd " << v << '\n';
	if (a[v]) {
		rec(a[v], ty);
		return;
	}
	upd(l[v], ty);
	upd(r[v], ty);
}

void dfs(int v, bool clr) {
//	cout << "dfs " << v << '\n';
	if (a[v]) {
		if (!clr) {
			rec(a[v], 1, 1, 1, n);
//			cout << "upd " << v << ' ' << a[v] << ' ' << get(1, n) << '\n';
		}
		return;
	}
	if (sz[l[v]] > sz[r[v]]) swap(l[v], r[v]);
	dfs(l[v], 1);
	dfs(r[v], 0);
	int cur1 = 0, cur2 = 0;
	add(l[v], cur1, cur2);
//	cout << v << ' ' << cur1 << ' ' << cur2 << ' ' << get(1, n) << '\n';
	ans += min(cur1, cur2);
	if (clr) upd(r[v], -1);
	else upd(l[v], 1);
}

int temp = 1;

void input(int v = 1) {
	cin >> a[v];
	sz[v] = 1;
	if (!a[v]) {
		l[v] = ++temp;
		input(temp);
		sz[v] += sz[l[v]];
		r[v] = ++temp;
		input(temp);
		sz[v] += sz[r[v]];
	}
}

void GazizMadi() {
	cin >> n;
	input();
	dfs(1, 0);
	cout << ans;
}

const bool Cases = 0;

signed main() {
//	ios_base::sync_with_stdio(0);
//	cin.tie(0);
//	cout.tie(0);
//	srand(time(0));
	int TT = 1;
	if (Cases) cin >> TT;
	for (int i = 1; i <= TT; i++) {
		//cout << "Case " << i << ": ";
		GazizMadi();
	}
}

Compilation message

rot.cpp: In function 'void rec(long long int, long long int, long long int, long long int, long long int)':
rot.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 | #define tm (tl + tr >> 1)
      |             ~~~^~~~
rot.cpp:53:13: note: in expansion of macro 'tm'
   53 |  if (idx <= tm) rec(idx, val, ls);
      |             ^~
rot.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 | #define tm (tl + tr >> 1)
      |             ~~~^~~~
rot.cpp:14:24: note: in expansion of macro 'tm'
   14 | #define ls v << 1, tl, tm
      |                        ^~
rot.cpp:53:31: note: in expansion of macro 'ls'
   53 |  if (idx <= tm) rec(idx, val, ls);
      |                               ^~
rot.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 | #define tm (tl + tr >> 1)
      |             ~~~^~~~
rot.cpp:15:24: note: in expansion of macro 'tm'
   15 | #define rs v << 1 | 1, tm + 1, tr
      |                        ^~
rot.cpp:54:21: note: in expansion of macro 'rs'
   54 |  else rec(idx, val, rs);
      |                     ^~
rot.cpp: In function 'long long int get(long long int, long long int, long long int, long long int, long long int)':
rot.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 | #define tm (tl + tr >> 1)
      |             ~~~^~~~
rot.cpp:14:24: note: in expansion of macro 'tm'
   14 | #define ls v << 1, tl, tm
      |                        ^~
rot.cpp:61:19: note: in expansion of macro 'ls'
   61 |  return get(l, r, ls) + get(l, r, rs);
      |                   ^~
rot.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 | #define tm (tl + tr >> 1)
      |             ~~~^~~~
rot.cpp:15:24: note: in expansion of macro 'tm'
   15 | #define rs v << 1 | 1, tm + 1, tr
      |                        ^~
rot.cpp:61:35: note: in expansion of macro 'rs'
   61 |  return get(l, r, ls) + get(l, r, rs);
      |                                   ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8692 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8796 KB Output is correct
2 Correct 7 ms 8740 KB Output is correct
3 Correct 4 ms 8792 KB Output is correct
4 Correct 4 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 11612 KB Output is correct
2 Correct 27 ms 12900 KB Output is correct
3 Correct 84 ms 13144 KB Output is correct
4 Correct 20 ms 13400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 144 ms 13140 KB Output is correct
2 Correct 75 ms 15184 KB Output is correct
3 Correct 82 ms 15700 KB Output is correct
4 Correct 77 ms 15696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 21584 KB Output is correct
2 Correct 107 ms 19068 KB Output is correct
3 Correct 144 ms 17108 KB Output is correct
4 Correct 102 ms 18224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 162 ms 16180 KB Output is correct
2 Correct 148 ms 17492 KB Output is correct
3 Correct 129 ms 20600 KB Output is correct
4 Correct 132 ms 22508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 513 ms 21668 KB Output is correct
2 Correct 290 ms 22100 KB Output is correct
3 Correct 265 ms 20908 KB Output is correct
4 Correct 327 ms 19708 KB Output is correct
5 Correct 607 ms 18436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 282 ms 20012 KB Output is correct
2 Correct 298 ms 27536 KB Output is correct
3 Correct 334 ms 25176 KB Output is correct
4 Correct 230 ms 26452 KB Output is correct
5 Correct 747 ms 18516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 320 ms 19796 KB Output is correct
2 Correct 258 ms 20564 KB Output is correct
3 Correct 560 ms 18768 KB Output is correct
4 Correct 353 ms 19536 KB Output is correct
5 Correct 235 ms 26448 KB Output is correct
6 Correct 782 ms 20708 KB Output is correct