Submission #863652

# Submission time Handle Problem Language Result Execution time Memory
863652 2023-10-20T16:35:07 Z vjudge1 Tree Rotations (POI11_rot) C++17
63 / 100
194 ms 65536 KB
//gm  --- akezhon
#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #define int long long
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define tm (tl+tr)/2
#define TL v+v, tl, tm
#define TR v+v+1, tm+1, tr
#define DA l <= tl && tr <= r
#define NET r < tl || tr < l
#define double long double
using namespace std;
const int N=3e5+7;
const int M=1e9+7;
const int inf=1e9;
int c[N], tin[N], tout[N], eul[N], sz[N], big[N], t[N*4], l[N], r[N], szz;
vector <pair<int*, int>> rb;
vector <int> g[N];
int n, timer, cur, ans;
void roll(int &x){
	rb.push_back({&x, x});
}
void tupd(int pos, int v=1, int tl=1, int tr=n){
	roll(t[v]);
	if(tl == tr){
		t[v]++;
		return;
	}
	if(pos <= tm)tupd(pos, TL);
	else tupd(pos, TR);
	t[v] = t[v+v] + t[v+v+1];
}
int get(int l, int r, int v=1, int tl=1, int tr=n){
	if(NET)return 0;
	if(DA)return t[v];
	return get(l, r, TL) + get(l, r, TR);
}
void dfs(int v=1, int p=0){
	tin[v] = ++timer;
	eul[timer] = v;
	sz[v] = 1;
	for(int to : g[v]){
		if(to == p)continue;
		dfs(to, v);
		sz[v] += sz[to];
		if(sz[to] > sz[big[v]])big[v] = to;
	}
	tout[v] = timer;
}
void go(int v=1, int cl=0){
	int temp = rb.size();
	if(!c[v]){
		if(big[v] == l[v])go(r[v], 1);
		else go(l[v], 1);
		
		go(big[v], 0);
		int cntkeep=0, cntswap=0;
		if(big[v] == l[v]){
			for(int i=tin[r[v]]; i <= tout[r[v]]; i++){
				int f = eul[i];
				if(c[f]){
					cntkeep += get(c[f], n);
					cntswap += get(1, c[f]);
				}
			}
			ans += min(cntkeep, cntswap);
			if(!cl) {
				for(int i=tin[r[v]]; i <= tout[r[v]]; i++){
					int f = eul[i];
					if(c[f]){
						tupd(c[f]);
					}
				}
			}
		}
		else {
			for(int i=tin[l[v]]; i <= tout[l[v]]; i++){
				int f = eul[i];
				if(c[f]){
					cntkeep += get(1, c[f]);
					cntswap += get(c[f], n);
				}
			}
			ans += min(cntkeep, cntswap);
			if(!cl){
				for(int i=tin[l[v]]; i <= tout[l[v]]; i++){
					int f = eul[i];
					if(c[f]){
						tupd(c[f]);
					}
				}
			}
		}
		if(cl){
			while(temp < (int)rb.size()){
				*(rb.back().F) = rb.back().S;
				rb.pop_back();
			}
		}
	}
	else {
		if(!cl)tupd(c[v]);
	}
}
void rec(){
	cin >> c[szz];
	if(!c[szz]){
		int cur = szz;
		
		szz++;
		l[cur] = szz;
		g[cur].pb(szz);
		rec();
		
		szz++;
		r[cur] = szz;
		g[cur].pb(szz);
		rec();
	}
}
void AlemAmenov(){
	
	cin >> n;
	szz=1;
	rec();
	dfs();	
	go();
	cout << ans;
}
signed main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	// freopen("promote.in", "r", stdin);
	// freopen("promote.out", "w", stdout);
	int RealName=1;
	// cin >> RealName;
	// int C=0;
	while(RealName--){
		// cout << "Case " << ++C << ":\n";
		AlemAmenov();
	}

return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 2 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 15196 KB Output is correct
2 Correct 4 ms 15452 KB Output is correct
3 Correct 4 ms 15452 KB Output is correct
4 Correct 3 ms 15368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 16604 KB Output is correct
2 Correct 10 ms 16308 KB Output is correct
3 Correct 7 ms 16604 KB Output is correct
4 Correct 6 ms 16600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21776 KB Output is correct
2 Correct 31 ms 19840 KB Output is correct
3 Correct 98 ms 27660 KB Output is correct
4 Correct 23 ms 22216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 157 ms 35760 KB Output is correct
2 Correct 83 ms 39568 KB Output is correct
3 Correct 95 ms 39368 KB Output is correct
4 Correct 90 ms 39348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 63508 KB Output is correct
2 Correct 136 ms 59052 KB Output is correct
3 Correct 172 ms 55992 KB Output is correct
4 Correct 123 ms 55724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 194 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 48 ms 39536 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 64 ms 37640 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 44 ms 37208 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -