Language: C++

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;
typedef pair<pll, pll> P;
const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS = 1e-9;
const bool debug = 0;
//---------------------------------//

template <typename T>
struct SegTree {
	T init_val;
	int n;
	vector<T> dat;
	
	SegTree() {}
	SegTree(int _n, T init_val) : init_val(init_val) {
		n = 1;
		while (n < _n) n *= 2;
		dat.resize(n * 2 - 1, init_val);
	}
	
	void update(int i, T x) {
		i += n - 1;
		dat[i] = min(dat[i], x);
		while (i > 0) {
			i = (i - 1) / 2;
			dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
		}
	}
	
	void set(int i, T x) {
		dat[i + n - 1] = x;
		update(i, x);
	}
	
	// 探索範囲[a,b), 現在見ているkノード[l,r)
	T query(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r < 0) r = n;
		
		if (r <= a || b <= l) return init_val;
		if (a <= l && r <= b) return dat[k];
		return min( query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r) );
	}
};

int N;
int p[212345], ans[212345];
SegTree<pii> segodd, segeven, segall;

// [l, r)
vector<int> dfs(int l, int r) {
	if (l >= r) continue;
	vector<int> res;
	if (r - l == 2) {
		res.push_back(p[l]);
		res.push_back(p[l + 1]);
		return res;
	}
	
	int miidx = segall.query(l, r).se;
	vector<int> v[2];
	
	if (miidx % 2 == 0) {
		res.push_back(p[miidx]);
		res.push_back(p[miidx + 1]);
		
		if (l != miidx) v[0] = dfs(l, miidx);
		if (r != miidx + 2) v[1] = dfs(miidx + 2, r);
	}
	else {
		int nidx = -1;
		if (r & 1) nidx = segodd.query(l, r - 3).se;
		else nidx = segeven.query(l, r - 3).se;
		
		if (p[nidx] >= p[miidx - 1]) {
			res.push_back(p[miidx - 1]);
			res.push_back(p[miidx]);
			
			if (l < miidx - 1) v[0] = dfs(l, miidx - 1);
			if (miidx + 1 < r) v[1] = dfs(miidx + 1, r);
		}
		else {
		
			res.push_back(p[nidx]);
			res.push_back(p[r - 1]);
			
			if (l != nidx) v[0] = dfs(l, nidx);
			if (nidx + 1 < r - 1) v[1] = dfs(nidx + 1, r - 1);
		}
	}
	
	// 結合処理
	int p1 = 0, p2 = 0;
	while (p1 < v[0].size() && p2 < v[1].size()) {
		if (v[0][p1] < v[1][p2]) {
			res.push_back(v[0][p1++]);
			res.push_back(v[0][p1++]);
		}
		else {
			res.push_back(v[1][p2++]);
			res.push_back(v[1][p2++]);
		}
	}
	
	while (p1 < v[0].size()) res.push_back(v[0][p1++]);
	while (p2 < v[1].size()) res.push_back(v[1][p2++]);
	
	return res;
}

int main() {
	cin >> N;
	REP(i, N) scanf("%d", p + i);
	
	segodd = SegTree<pii>(N, pii(INF, -1));
	segeven = SegTree<pii>(N, pii(INF, -1));
	segall = SegTree<pii>(N, pii(INF, -1));
	
	REP(i, N) {
		if (i & 1) segodd.update(i, pii(p[i], i));
		else segeven.update(i, pii(p[i], i));
		segall.update(i, pii(p[i], i));
	}
	
	vector<int> ans = dfs(0, N);
	REP(i, N) printf("%d%c", ans[i], i == N - 1 ? '\n' : ' ');
	
	return 0;
}

/*
6
3 1 5 2 6 4
*/

Compiler output:
a.cc: In function 'std::vector<int> dfs(int, int)':
a.cc:64:21: error: continue statement not within a loop
   64 |         if (l >= r) continue;
      |                     ^~~~~~~~