Submission #1354054

#TimeUsernameProblemLanguageResultExecution timeMemory
1354054kl0989eSwap (BOI16_swap)C++20
100 / 100
273 ms67248 KiB
#include <bits/stdc++.h>
using namespace std;

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2,fma")

#define ll long long
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define pi pair<int, int>
#define pl pair<ll,ll>
#define all(x) (x).begin(),(x).end()

int n;
vi a;
vector<vi> best;
vector<vi> memo;
vector<vi> memo2;
vector<vi> memo3;

void merge(int i) {
	int j=0;
	int add=1;
	while (j<best[2*i].size() || j<best[2*i+1].size()) {
		int tj=j;
		while (tj<best[2*i].size() && tj<j+add) {
			best[i].pb(best[2*i][tj++]);
		}
		tj=j;
		while (tj<best[2*i+1].size() && tj<j+add) {
			best[i].pb(best[2*i+1][tj++]);
		}
		j+=add;
		add*=2;
	}
}

void dfs(int i, int val) {
	best[i].clear();
	if (val==a[i] && memo[i].size()) {
		best[i]=memo[i];
		return;
	}
	if (i!=1 && val==a[i/2] && memo2[i].size()) {
		best[i]=memo2[i];
		return;
	}
	if ((i^1)<=n && i!=1 && val==a[i^1] && memo3[i].size()) {
		best[i]=memo3[i];
		return;
	}
	if (2*i>n) {
		best[i].pb(val);
		if (val==a[i]) {
			memo[i]=best[i];
		}
		if (i!=1 && val==a[i/2]) {
			memo2[i]=best[i];
		}
		if ((i^1)<=n && i!=1 && val==a[i^1]) {
			memo3[i]=best[i];
		}
		return;
	}
	if (2*i+1>n) {
		best[i].pb(min(val,a[2*i]));
		best[i].pb(max(val,a[2*i]));
		if (val==a[i]) {
			memo[i]=best[i];
		}
		if (i!=1 && val==a[i/2]) {
			memo2[i]=best[i];
		}
		if ((i^1)<=n && i!=1 && val==a[i^1]) {
			memo3[i]=best[i];
		}
		return;
	}
	best[i].pb(min(val,min(a[2*i],a[2*i+1])));
	if (best[i][0]==val) {
		dfs(2*i,a[2*i]);
		dfs(2*i+1,a[2*i+1]);
		merge(i);
		if (val==a[i]) {
			memo[i]=best[i];
		}
		if (i!=1 && val==a[i/2]) {
			memo2[i]=best[i];
		}
		if ((i^1)<=n && i!=1 && val==a[i^1]) {
			memo3[i]=best[i];
		}
		return;
	}
	if (best[i][0]==a[2*i]) {
		dfs(2*i,val);
		dfs(2*i+1,a[2*i+1]);
		merge(i);
		if (val==a[i]) {
			memo[i]=best[i];
		}
		if (i!=1 && val==a[i/2]) {
			memo2[i]=best[i];
		}
		if ((i^1)<=n && i!=1 && val==a[i^1]) {
			memo3[i]=best[i];
		}
		return;
	}
	dfs(2*i,val);
	dfs(2*i+1,a[2*i]);
	merge(i);
	vi t={min(val,min(a[2*i],a[2*i+1]))};
	swap(t,best[i]);
	dfs(2*i,a[2*i]);
	dfs(2*i+1,val);
	merge(i);
	for (int j=0; j<t.size(); j++) {
		if (t[j]!=best[i][j]) {
			if (t[j]<best[i][j]) {
				swap(t,best[i]);
			}
			break;
		}
	}
	if (val==a[i]) {
		memo[i]=best[i];
	}
	if (i!=1 && val==a[i/2]) {
		memo2[i]=best[i];
	}
	if ((i^1)<=n && i!=1 && val==a[i^1]) {
		memo3[i]=best[i];
	}
}

int32_t main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
	cin >> n;
	a.resize(n+1);
	best.resize(n+1);
	memo.resize(n+1);
	memo2.resize(n+1);
	memo3.resize(n+1);
	for (int i=1; i<=n; i++) {
		cin >> a[i];
	}
	dfs(1,a[1]);
	for (int i=0; i<n; i++) {
		cout << best[1][i] << " \n"[i==n];
	}
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...