Language: C++

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#define mp make_pair
#define ft first
#define sc second
using namespace std;
const int Inf = 1e9 + 7;
int w[200010], hash[200010], Min[800010][2], ans[200010];

void build(int l, int r, int cnt) {
	if (l == r) {
		scanf("%d", &w[l]), hash[w[l]] = l;
		Min[cnt][l & 1] = w[l], Min[cnt][1 ^ (l & 1)] = Inf;
		return ;
	}
	build(l, (l + r) / 2, cnt * 2); build((l + r) / 2 + 1, r, cnt * 2 + 1);
	Min[cnt][l & 1] = min(Min[cnt * 2][l & 1], Min[cnt * 2 + 1][l & 1]);
	Min[cnt][1 ^ (l & 1)] = min(Min[cnt * 2][1 ^ (l & 1)], Min[cnt * 2 + 1][1 ^ (l & 1)]);
}

int ask(int x, int y, int l, int r, int cnt, int bit) {
	
	if (x == l && y == r) return Min[cnt][bit & 1];
	int mid = (l + r) / 2;
	if (mid >= y) return ask(x, y, l, mid, cnt * 2, bit);
	else if (mid < x) return ask(x, y, mid + 1, r, cnt * 2 + 1, bit);
	else return min(ask(x, mid, l, mid, cnt * 2, bit), ask(mid + 1, y, mid + 1, r, cnt * 2 + 1, bit));
	
}

priority_queue < pair< int, pair<int, int> > > q;

int main() {

//	freopen("B.in", "r", stdin);
//	freopen("B.out", "w", stdout);

	int n, tot = 0; scanf("%d", &n);
	build(1, n, 1);
	q.push(mp(-ask(1, n, 1, n, 1, 1), mp(1, n)));
	while (!q.empty()) {
		pair< int, pair<int, int> > x = q.top(); q.pop();
		if (x.sc.sc - x.sc.ft == 1) {
			ans[++tot] = w[x.sc.ft], ans[++tot] = w[x.sc.sc];
			continue;
		}
		int Min1 = ask(x.sc.ft, x.sc.sc, 1, n, 1, x.sc.ft);
		int place1 = hash[Min1];
		if (place1 - 1 > x.sc.ft) q.push(mp(-ask(x.sc.ft, place1 - 1, 1, n, 1, x.sc.ft), mp(x.sc.ft, place1 - 1)));
		int Min2 = ask(place1 + 1, x.sc.sc, 1, n, 1, place1 + 1);
		int place2 = hash[Min2];
		if (place2 - 1 > place1 + 1) q.push(mp(-ask(place1 + 1, place2 - 1, 1, n, 1, place1 + 1), mp(place1 + 1, place2 - 1)));
		if (x.sc.sc > place2 + 1) q.push(mp(-ask(place2 + 1, x.sc.sc, 1, n, 1, place2 + 1), mp(place2 + 1, x.sc.sc)));
		ans[++tot] = Min1, ans[++tot] = Min2;
	}
	for (int i = 1; i <= tot; ++i)
		printf("%d ", ans[i]);
	return 0;
	
}

Compiler output:
a.cc: In function 'void build(int, int, int)':
a.cc:15:37: error: reference to 'hash' is ambiguous
   15 |                 scanf("%d", &w[l]), hash[w[l]] = l;
      |                                     ^~~~
In file included from /usr/include/c++/14/string_view:50,
                 from /usr/include/c++/14/bits/basic_string.h:47,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:11:16: note:                 'int hash [200010]'
   11 | int w[200010], hash[200010], Min[800010][2], ans[200010];
      |                ^~~~
a.cc: In function 'int main()':
a.cc:51:30: error: reference to 'hash' is ambiguous
   51 |                 int place1 = hash[Min1];
      |                              ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:11:16: note:                 'int hash [200010]'
   11 | int w[200010], hash[200010], Min[800010][2], ans[200010];
      |                ^~~~
a.cc:54:30: error: reference to 'hash' is ambiguous
   54 |                 int place2 = hash[Min2];
      |                              ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:11:16: note:                 'int hash [200010]'
   11 | int w[200010], hash[200010], Min[800010][2], ans[200010];
      |                ^~~~