Submission #862029

# Submission time Handle Problem Language Result Execution time Memory
862029 2023-10-17T11:52:30 Z vjudge1 Tree Rotations (POI11_rot) C++17
0 / 100
164 ms 49752 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 = 2e5 + 7;
const int P = 911;
const ll INF = 1e18;

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

vector <int> g[N];
int sz[N], res[N], ans, cnt[N], a[N], n, t[N << 2];
int id[N];
vector <int> vc[N];

void upd(int idx, int val, int v = 1, int tl = 0, int tr = n + 1) {
	if (tl == tr) {
		t[v] += val;
		return;
	}
	if (idx <= tm) upd(idx, val, ls);
	else upd(idx, val, rs);
	t[v] = t[v << 1] + t[v << 1 | 1];
}

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

int get2(int r, int v = 1, int tl = 0, int tr = n + 1) {
	if (tr <= r) return t[v];
	if (tm == r) return t[v << 1];
	if (tm <= r) return get2(r, rs) + t[v << 1];
	if (tl <= r) return get2(r, ls);
	return 0;
}

void calc(int v = 1, int pr = 0) {
	id[v] = v;
	sz[v] = 1;
	for (int u: g[v]) if (u != pr) {
		calc(u, v);
		sz[v] += sz[u];
	}
}

void add(int v) {
	if (!a[v]) return;
	upd(a[v], 1);
}

void clr(int v) {
	if (!a[v]) return;
	upd(a[v], -1);
}

void dadd(int v) {
	add(v);
	for (int u: g[v]) {
		dadd(u);
	}
}

void dclr(int v) {
	clr(v);
	for (int u: g[v]) {
		dclr(u);
	}
}


void dfs(int v = 1, bool act = 0) {
	int to = 0;
	for (int u: g[v]) {
//		cout << "dd " << v << ' ' << u << '\n'; 
		if (sz[u] > sz[to]) {
			to = u;
		}
	}
	for (int u: g[v]) {
		if (u != to) {
			dfs(u, 1);
			id[v] = id[u];
		}
	}
	if (to) dfs(to, 0);
	int cur1 = 0, cur2 = 0;
//	cout << "ans " << v << '\n';
	for (int it: vc[id[v]]) {
		cur1 += get1(it + 1);
		cur2 += get2(it - 1);
		vc[id[to]].pb(it);
//		cout << it << ' ' << cur1 << ' ' << cur2 << '\n';
	}
	if (to) id[v] = id[to];
	ans += min(cur1, cur2);
	if (a[v]) {
//		cout << v << ' ' << a[v] << ' ' << id[v] << '\n';
		vc[id[v]].pb(a[v]);
	}
	add(v);
	if (act) {
		dclr(v);
		return;
	}
	for (int u: g[v]) if (u != to) {
		dadd(u);
	}
}

int temp = 1;
set <int> st;
map <int, int> mp;

void input(int v) {
	int x;
	cin >> x;
	if (x) {
		st.insert(x);
		a[v] = x;
		return;
	}
	temp++;
	g[v].pb(temp);
	input(temp);
	temp++;
	g[v].pb(temp);
	input(temp);
}

void GazizMadi() {
	cin >> n;
	input(1);
	for (int it: st) {
		mp[it] = len(mp) + 1;
	}
	for (int i = 1; i <= temp; i++) {
		a[i] = mp[a[i]];
	}
	calc();
	dfs();
	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 upd(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:54:13: note: in expansion of macro 'tm'
   54 |  if (idx <= tm) upd(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:54:31: note: in expansion of macro 'ls'
   54 |  if (idx <= tm) upd(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:55:21: note: in expansion of macro 'rs'
   55 |  else upd(idx, val, rs);
      |                     ^~
rot.cpp: In function 'long long int get1(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:61:11: note: in expansion of macro 'tm'
   61 |  if (l == tm + 1) return t[v << 1 | 1];
      |           ^~
rot.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 | #define tm (tl + tr >> 1)
      |             ~~~^~~~
rot.cpp:62:11: note: in expansion of macro 'tm'
   62 |  if (l <= tm) return get1(l, ls) + t[v << 1 | 1];
      |           ^~
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:62:30: note: in expansion of macro 'ls'
   62 |  if (l <= tm) return get1(l, ls) + t[v << 1 | 1];
      |                              ^~
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:63:30: note: in expansion of macro 'rs'
   63 |  if (l <= tr) return get1(l, rs);
      |                              ^~
rot.cpp: In function 'long long int get2(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:69:6: note: in expansion of macro 'tm'
   69 |  if (tm == r) return t[v << 1];
      |      ^~
rot.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 | #define tm (tl + tr >> 1)
      |             ~~~^~~~
rot.cpp:70:6: note: in expansion of macro 'tm'
   70 |  if (tm <= r) return get2(r, rs) + t[v << 1];
      |      ^~
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:70:30: note: in expansion of macro 'rs'
   70 |  if (tm <= r) return get2(r, rs) + t[v << 1];
      |                              ^~
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:71:30: note: in expansion of macro 'ls'
   71 |  if (tl <= r) return get2(r, ls);
      |                              ^~
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 17752 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 17756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 17752 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 18952 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 23384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 164 ms 31844 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 146 ms 45364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 64 ms 48664 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 49 ms 44344 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 62 ms 49752 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 61 ms 49600 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -