Language: C++

#include <iostream>
#include <vector>
#include <memory.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <numeric>
#include <queue>
#pragma comment(linker, "/STACK:1000000000")
using namespace std;
typedef long long LL;
typedef double D;

struct node
{
	int val, pos;
};

struct type
{
	int from, to;
 	node data;
};

bool compare(type a, type b)
{
	return a.data.val > b.data.val;
}

priority_queue<type, vector<type>, function<bool(type, type)>> q(compare);

const int N = 2e5 + 2, inf = 1e9;
node arr[2][N << 1];

node MIN(node a, node b)
{
	return a.val < b.val ? a : b;
}

node min_q(int l, int r)
{
	bool c = l & 1;
	node res = {inf, -1};
	for (l += N, r += N; l < r; l >>= 1, r >>= 1)
	{
		if (l & 1) res = MIN(res, arr[c][l++]);
		if (r & 1) res = MIN(res, arr[c][--r]);
	}
	return res;
}

int main()
{
	ios::sync_with_stdio(0);
	int n;
	cin >> n;
	memset(arr, 1, sizeof(arr));
	for (int i = 0; i < n; ++i)
	{
		arr[0][N + i].pos = arr[1][N + i].pos = i;
		cin >> arr[i & 1][N + i].val;
	}
	
	for (int i = N - 1; i > 0; --i)
	{
		arr[0][i] = MIN(arr[0][i << 1], arr[0][i << 1 ^ 1]);
		arr[1][i] = MIN(arr[1][i << 1], arr[1][i << 1 ^ 1]);
	}
	
	q.push({0, n, min_q(0, n)});
	
	while (!q.empty())
	{
		type v = q.top();
		q.pop();
		
		node a = v.data, b = min_q(a.pos + 1, v.to);
		cout << a.val << ' ' << b.val << ' ';
		
		if (a.pos - v.from >= 2)
			q.push({v.from, a.pos, min_q(v.from, a.pos)});
		if (b.pos - a.pos > 2)
			q.push({a.pos + 1, b.pos, min_q(a.pos + 1, b.pos)});
		if (v.to - b.pos > 2)
			q.push({b.pos + 1, v.to, min_q(b.pos + 1, v.to)});
	}
}

Compiler output:
a.cc:30:36: error: 'function' was not declared in this scope
   30 | priority_queue<type, vector<type>, function<bool(type, type)>> q(compare);
      |                                    ^~~~~~~~
a.cc:9:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
    8 | #include <queue>
  +++ |+#include <functional>
    9 | #pragma comment(linker, "/STACK:1000000000")
a.cc:30:60: error: expression list treated as compound expression in functional cast [-fpermissive]
   30 | priority_queue<type, vector<type>, function<bool(type, type)>> q(compare);
      |                                                            ^
a.cc:30:61: error: template argument 3 is invalid
   30 | priority_queue<type, vector<type>, function<bool(type, type)>> q(compare);
      |                                                             ^~
a.cc:30:73: error: invalid conversion from 'bool (*)(type, type)' to 'int' [-fpermissive]
   30 | priority_queue<type, vector<type>, function<bool(type, type)>> q(compare);
      |                                                                         ^
      |                                                                         |
      |                                                                         bool (*)(type, type)
a.cc: In function 'int main()':
a.cc:70:11: error: request for member 'push' in 'q', which is of non-class type 'int'
   70 |         q.push({0, n, min_q(0, n)});
      |           ^~~~
a.cc:72:19: error: request for member 'empty' in 'q', which is of non-class type 'int'
   72 |         while (!q.empty())
      |                   ^~~~~
a.cc:74:28: error: request for member 'top' in 'q', which is of non-class type 'int'
   74 |                 type v = q.top();
      |                            ^~~
a.cc:75:19: error: request for member 'pop' in 'q', which is of non-class type 'int'
   75 |                 q.pop();
      |                   ^~~
a.cc:81:27: error: request for member 'push' in 'q', which is of non-class type 'int'
   81 |                         q.push({v.from, a.pos, min_q(v.from, a.pos)});
      |                           ^~~~
a.cc:83:27: error: request for member 'push' in 'q', which is of non-class type 'int'
   83 |                         q.push({a.pos + 1, b.pos, min_q(a.pos + 1, b.pos)});
      |                           ^~~~
a.cc:85:27: error: request for member 'push' in 'q', which is of non-class type 'int'
   85 |                         q.push({b.pos + 1, v.to, min_q(b.pos + 1, v.to)});
      |                           ^~~~