Language: C++

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <utility>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <functional>
#include <stdlib.h>
using namespace std;
const int maxn = 1 << 17;
int n, dat[2 * maxn - 1],dat2[2*maxn-1],n2;
void init(int n_) {
	n = 1;
	while (n < n_)n *= 2;
	for (int i = 0; i < 2 * n - 1; i++) {
		dat[i] = INT_MAX;
	}
}
void init2(int n_) {
	n2 = 1;
	while (n2 < n_)n2 *= 2;
	for (int i = 0; i < 2 * n2 - 1; i++) {
		dat2[i] = INT_MAX;
	}
}
void update(int k, int a) {
	k += n - 1;
	dat[k] = a;
	while (k > 0) {
		k = (k - 1) / 2;
		dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
	}
}
void update2(int k, int a) {
	k += n2 - 1;
	dat2[k] = a;
	while (k > 0) {
		k = (k - 1) / 2;
		dat2[k] = min(dat2[k * 2 + 1], dat2[k * 2 + 2]);
	}
}
int query(int a, int b, int k, int l, int r) {
	if (r <= a || b <= l)return INT_MAX;
	if (a <= l&&r <= b)return dat[k];
	else {
		int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
		int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
		return min(vl, vr);
	}
}
int query2(int a, int b, int k, int l, int r) {
	if (r <= a || b <= l)return INT_MAX;
	if (a <= l&&r <= b)return dat2[k];
	else {
		int vl = query2(a, b, k * 2 + 1, l, (l + r) / 2);
		int vr = query2(a, b, k * 2 + 2, (l + r) / 2, r);
		return min(vl, vr);
	}
}
typedef pair<int, int> P;
P b[200005];
int nn;
int binary(int k) {
	int lb = -1,ub = nn;
	while (ub - lb > 1) {
		int mid = (ub + lb) / 2;
		if (b[mid].first >= k)ub = mid;
		else lb = mid;
	}
	return b[ub].second;
}
signed main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int a[200005];
	cin >> nn;
	for (int i = 0; i < nn; i++) {
		cin >> a[i];
	}
	init(nn);
	init2(nn );
	for (int i = 0; i < nn; i++) {
		b[i] = P(a[i], i);
	}
	sort(b, b + nn);
	for (int i = 0; i < nn; i+=2) {
		update(i, a[i]);
	}
	for (int i = 1; i < nn; i += 2) {
		update2(i, a[i]);
	}
	vector<int> ans;
	priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> que;
	que.push(make_tuple(query(0,nn,0,0,nn), 0, nn));
	while (!que.empty()) {
		tuple<int, int, int> now;
		now = que.top();
		que.pop();
		int mi, a, b;
		mi = get<0>(now);
		a = get<1>(now);
		b = get<2>(now);
		int pomi;
		pomi = binary(mi);
		int ma;
		if(pomi%2==0)
		 ma = query2(a, b, 0, 0, nn);
		else ma = query(a, b, 0, 0, nn);
		ans.push_back(mi);
		ans.push_back(ma);
		int poma;
		poma = binary(ma);
		if (pomi > a) {
			if(a%2==0)
			que.push(make_tuple(query(a, pomi, 0, 0, nn), a, pomi));
			else
				que.push(make_tuple(query2(a, pomi, 0, 0, nn), a, pomi));
		}
		if (poma-1 > pomi + 1) {
			if((pomi+1)%2==0)
			que.push(make_tuple(query(pomi + 1, poma, 0, 0, nn), pomi + 1, poma));
			else
				que.push(make_tuple(query2(pomi + 1, poma, 0, 0, nn), pomi + 1, poma));
		}
		if (b-1 > poma) {
			if((poma+1)%2==0)
			que.push(make_tuple(query(poma + 1, b, 0, 0, nn), poma + 1, b));
			else
				que.push(make_tuple(query2(poma + 1, b, 0, 0, nn), poma + 1, b));
		}
	}
	for (int i = 0; i < n; i++) {
		cout << ans[i]<<" ";
	}
	return 0;
}

Compiler output:
a.cc: In function 'void init(int)':
a.cc:18:26: error: 'INT_MAX' was not declared in this scope
   18 |                 dat[i] = INT_MAX;
      |                          ^~~~~~~
a.cc:11:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   10 | #include <stdlib.h>
  +++ |+#include <climits>
   11 | using namespace std;
a.cc: In function 'void init2(int)':
a.cc:25:27: error: 'INT_MAX' was not declared in this scope
   25 |                 dat2[i] = INT_MAX;
      |                           ^~~~~~~
a.cc:25:27: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc: In function 'int query(int, int, int, int, int)':
a.cc:45:37: error: 'INT_MAX' was not declared in this scope
   45 |         if (r <= a || b <= l)return INT_MAX;
      |                                     ^~~~~~~
a.cc:45:37: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc: In function 'int query2(int, int, int, int, int)':
a.cc:54:37: error: 'INT_MAX' was not declared in this scope
   54 |         if (r <= a || b <= l)return INT_MAX;
      |                                     ^~~~~~~
a.cc:54:37: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'