답안 #765993

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765993 2023-06-25T08:33:02 Z vjudge1 Tree Rotations (POI11_rot) C++17
0 / 100
34 ms 65536 KB
#include <bits/stdc++.h>
using namespace std;
 
// #pragma comment(linker, "/stack:2000000000")
// #pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
 
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
 
#define precise(a) cout<<fixed<<setprecision(a)
#define sz size()
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
// t1
const ll mod = ll(1e9)+7; //(b + (a%b)) % b (to mod -1%(10^9+7) correctly in c++ its -1 but its suppose to be 10^9+6
const ll MOD = 998244353;  // (a%mod)*(binpow(b,mod-2,mod) = (a/b)%mod
const ll N = ll(1e6)+10;
const ll inf = 1e18;
const ld eps = 1e-15L;
const ll B = 400;
// const ld pie = acos(-1.0);
 
ll lcm(ll a, ll b){ return (a / __gcd(a,b))*b; }
ll binpow(ll a, ll b, ll m){ ll res=1;a%=m; while(b>0){ if(b&1)res=(res*a)%m; a=(a*a)%m; b/=2; } return res%m;}
 
void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }

int n, lc[N*20], rc[N*20], node, ans[N], a[N], t[N*4], siz[N];
vector<int> g[N];
deque<int> val[N];

int read() {
	++node;
	int root = node;
	siz[root] = 1;
	int v; cin>>v;
	if(v == 0) {
		lc[root] = read();
		rc[root] = read();
		siz[root] = siz[lc[root]] + siz[rc[root]];
 		return root;
	}
	else {
		a[root] = v;
		val[root].pb(v);
		lc[root] = rc[root] = -1;
		return root;
	}
}

void update(int v, int tl, int tr, int p, int x) {
	if(tl == tr) {
		t[v] += x;
		return;
	}
	int tm = (tl + tr) / 2;
	if(p <= tm) update(v+v, tl, tm, p, x);
	else update(v+v+1, tm + 1, tr, p, x);
	t[v] = t[v+v] + t[v+v+1];
}

ll get(int v, int tl, int tr, int l, int r) {
	if(tl > r || l > tr) return 0;
	if(l <= tl && tr <= r) return t[v];
	int tm = (tl + tr) / 2;
	return get(v+v, tl, tm, l, r) + get(v+v+1, tm+1, tr, l, r);
}




void dfs(int v, bool clear) {
	if(lc[v] == -1) {
		if(!clear) {
			for(auto x : val[v]) update(1, 1, n, x, 1);
		}
		return;
	}
	if(siz[lc[v]] < siz[rc[v]]) {
		dfs(lc[v], 1);
		dfs(rc[v], 0);
		
		// light ... heavy
		int ex = 0;
		for(auto x : val[lc[v]]) ex += get(1, 1, n, 1, x-1);
		int ans1 = ans[lc[v]] + ans[rc[v]] + ex;
		ex = 0;
		// heavy light
		for(auto x : val[lc[v]]) ex += get(1, 1, n, x+1, n);
		int ans2 = ans[rc[v]] + ans[lc[v]] + ex;
		if(ans1 < ans2) {
			ans[v] = ans1;
			val[v].swap(val[rc[v]]);
			for(int j = val[lc[v]].sz-1; j>=0; j--) {
				int x = val[lc[v]][j];
				val[v].push_front(x);
			}
		}
		else {
			ans[v] = ans2;
			val[v].swap(val[rc[v]]);
			for(auto x : val[lc[v]]) val[v].pb(x);
		}
		if(!clear) for(auto x : val[lc[v]]) update(1, 1, n, x, 1);
	}
	else {
		dfs(rc[v], 1);
		dfs(lc[v], 0);
		// light ... heavy
		int ex = 0;
		for(auto x : val[rc[v]]) ex += get(1, 1, n, 1, x-1);
		int ans1 = ans[rc[v]] + ans[lc[v]] + ex;
		ex = 0;
		// heavy light
		for(auto x : val[rc[v]]) ex += get(1, 1, n, x+1, n);
		int ans2 = ans[lc[v]] + ans[rc[v]] + ex;
		if(ans1 < ans2) {
			ans[v] = ans1;
			val[v].swap(val[lc[v]]);
			for(int j = val[rc[v]].sz-1; j>=0; j--) {
				int x = val[rc[v]][j];
				val[v].push_front(x);
			}
		}
		else {
			ans[v] = ans2;
			val[v].swap(val[lc[v]]);
			for(auto x : val[rc[v]]) val[v].pb(x);
		}
		if(!clear) for(auto x : val[rc[v]]) update(1, 1, n, x, 1);
	}
//	cout<<v<<"\n";
//	for(auto x : val[v]) cout<<x<<" ";
//	cout<<"\n";
	
	
}

void Baizho()
{
	cin>>n;
	read();
	dfs(1, 0);
	cout<<ans[1];
}

 
int main() {
 
//     Freopen("disrupt");
    ios_base::sync_with_stdio(false);   
    cin.tie(0);cout.tie(0); 
//    precalc();
   
    int ttt = 1;
//    cin>>ttt;

    for(int i=1; i<=ttt; i++) {Baizho(); }
}

Compilation message

rot.cpp: In function 'void Freopen(std::string)':
rot.cpp:33:34: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
      |                           ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:33:76: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
      |                                                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 29 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 25 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 28 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 28 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 34 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 29 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 29 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 29 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 34 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 29 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 32 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -