Submission #765953

# Submission time Handle Problem Language Result Execution time Memory
765953 2023-06-25T08:07:22 Z vjudge1 Tree Rotations (POI11_rot) C++17
27 / 100
109 ms 65536 KB
#include <bits/stdc++.h>
 
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(a) a.begin() , a.end()
#define F first
#define S second
 
using namespace std;
using ll = long long;
 
const ll N = 5e5+5 , inf = 2e9 + 7;
const ll INF = 1e18 ,   mod = 1e9+7;	

ll a[N] , ans[N] , n , mxson[N] , sz[N] , len , t[N*5] , m; 
vector<ll> g[N] , vec[N];

void precalc(ll v , ll pr){
	if(v <= n) sz[v] = 1;
	for(ll to : g[v]){
		if(to != pr){
			precalc(to,v);
			sz[v] += sz[to];
			if(sz[to] > sz[mxson[v]]) mxson[v] = to;
		}
	}
}

void upd(ll p, ll value) { 
  	for (t[p += m] = value; p > 1; p >>= 1) t[p>>1] = t[p] + t[p^1];
}

ll get(ll l, ll r) {
	r++;
 	ll res = 0;
  	for (l += m, r += m; l < r; l >>= 1, r >>= 1) {
    	if (l&1) res += t[l++];
   		if (r&1) res += t[--r];
  	}
  	return res;
}

void dfs(ll v , ll pr){
	for(ll to : g[v]){
		if(to != pr && to != mxson[v]){
			dfs(to,v);
			for(ll x : vec[to]) upd(x,0);
		}
	}
	if(mxson[v]) {
		dfs(mxson[v],v);
		vec[v] = vec[mxson[v]];
		ans[v] += ans[mxson[v]];
	}
	ll res = 0 , res1 = 0;
	for(ll to : g[v]){
		ll ob = t[1];
		if(to != pr && to != mxson[v]) {
			for(ll x : vec[to]) {
				ll num = get(x,n-1);
				res += num;
				res1 += ob-num;
			}
			for(ll x : vec[to]) {
				upd(x,1);
				vec[v].push_back(x);
			}
			ans[v] += ans[to];
		}
	}
	if(v <= n) {
		vec[v].push_back(v-1);
		upd(v-1,1);
	}
	else {
		ans[v] += min(res,res1);
	}
} 
ll NUM;
void go(ll v){
	ll x;
	cin >> x;
	if(x == 0){
		ll to = ++NUM;
		g[v].push_back(to);
		g[to].push_back(v);
		go(to);
	} else {
		g[v].push_back(x);
		g[x].push_back(v);
	}
	cin >> x;
	if(x == 0){
		ll to = ++NUM;
		g[v].push_back(to);
		g[to].push_back(v);
		go(to);
	} else {
		g[v].push_back(x);
		g[x].push_back(v);
	}
}
void solve(){
	cin >> n;
	m = n;
	NUM = n+1;
	ll a;
	cin >> a;
	go(n+1);
	precalc(n+1,0);
	len = 450;
	dfs(n+1,0);
	cout << ans[n+1] << "\n";
}
/*

*/
signed main(){
	ios;
	solve();
	return 0;
} 	
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 14 ms 23720 KB Output is correct
3 Correct 13 ms 23824 KB Output is correct
4 Correct 12 ms 23816 KB Output is correct
5 Correct 15 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23820 KB Output is correct
2 Correct 11 ms 23892 KB Output is correct
3 Correct 14 ms 23884 KB Output is correct
4 Correct 13 ms 23892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 24204 KB Output is correct
2 Correct 14 ms 24276 KB Output is correct
3 Correct 12 ms 24020 KB Output is correct
4 Correct 13 ms 27020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 27 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 31 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 88 ms 51536 KB Output is correct
2 Runtime error 43 ms 65536 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 71 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 75 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 103 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 109 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 101 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -