Language: C++

#include<iostream>
#include<vector>
#include<math.h>
#include<map>
#include<queue>
#include<algorithm>
#include<functional>
using namespace std;
#define INF int(1e9)
#define N 100000
#define MP make_pair
typedef pair<int, int> P;
typedef pair<pair<P, int>, P>PP;
int n, a, b;
P *mia, *mib;
P seg1[4 * (2 * N + 1)], seg2[4 * (2 * N + 1)];
priority_queue<PP, vector<PP>, greater<PP> > pq;

void init(P aa[], int nn)
{
	n = 1;
	while (n < nn) n *= 2;

	for (int i = 0; i < n * 2 - 1; i++)
		aa[i] = MP(INF,INF);
}


void update(P aa[], int k, int c, int nn)
{
	int num = k;
	k += nn - 1;
	aa[k] = MP(c, num);
	int cc = nn;
	while (k != 0)
	{
		k = (k - 1) / 2;
		if (aa[k].first <= c)
			break;
		aa[k] = MP(c, num);
	}
}

P* query(int s, int e, int k, int l, int r, P aa[])
{
	if (r <= s || l >= e) return NULL;
	if (r <= e && s <= l) 
	{
		return aa + k;
	}
	else
	{
		P* vl = query(s, e, k * 2 + 1, l, (l + r) / 2, aa);
		P* vr = query(s, e, k * 2 + 2, (l + r) / 2, r, aa);
		if (vl == NULL)
		{
			if (vr == NULL)
			{
				return NULL;
			}
			else
			{
				return vr;
			}
		}
		if (vr == NULL)
		{
			return vl;
		}
		return *vl < *vr ? vl : vr;
	}
}
bool solve2(int s, int e)
{
	if (e - s <= 0)
	{
		return false;
	}
	if (s % 2) 
	{
		mia = query(s, e, 0, 0, n, seg2);
		if ((*mia).first == INF)
		{
			return false;
		}
		mib = query((*mia).second, e, 0, 0, n, seg1);
	}
	else
	{
		mia = query(s , e, 0, 0, n, seg1);
		if ((*mia).first == INF)
		{
			return false;
		}
		mib = query((*mia).second, e, 0, 0, n, seg2);
	}
	return true;
}

void solve()
{
	
	if (pq.empty())
	{
		return;
	}
	cout << pq.top().first.first.first << " " << pq.top().first.second << " ";
	int s = pq.top().second.first, e = pq.top().second.second, se = pq.top().first.first.second, es = pq.top().first.second;
	pq.pop();
	if (solve2(s, se))
	{
		pq.push(MP(MP(*mia, (*mib).second), MP(s, se)));
	}
	if (solve2(se + 1, es))
	{
		pq.push(MP(MP(*mia, (*mib).second), MP(se + 1, es)));
	}
	if (solve2(es + 1, e))
	{
		pq.push(MP(MP(*mia, (*mib).second), MP(es + 1, e)));
	}
	solve();
}

int main() 
{
	cin >> n;
	int nn = n;
	init(seg1, n);
	init(seg2, n);
	for (int i = 0; i < nn; i+=2)
	{
		cin >> a >> b;
		update(seg1, i, a, n);
		update(seg2, i + 1, b, n);
	}
	if (solve2(0, n))
	{
		pq.push(MP(MP(*mia, *mib), MP(0, n)));
	}
	solve();
	cout << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:139:24: error: no matching function for call to 'std::priority_queue<std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> >, std::vector<std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> > >, std::greater<std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> > > >::push(std::pair<std::pair<std::pair<int, int>, std::pair<int, int> >, std::pair<int, int> >)'
  139 |                 pq.push(MP(MP(*mia, *mib), MP(0, n)));
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/queue:66,
                 from a.cc:5:
/usr/include/c++/14/bits/stl_queue.h:736:7: note: candidate: 'void std::priority_queue<_Tp, _Sequence, _Compare>::push(const value_type&) [with _Tp = std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> >; _Sequence = std::vector<std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> > >; _Compare = std::greater<std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> > >; value_type = std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> >]'
  736 |       push(const value_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:736:30: note:   no known conversion for argument 1 from 'std::pair<std::pair<std::pair<int, int>, std::pair<int, int> >, std::pair<int, int> >' to 'const std::priority_queue<std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> >, std::vector<std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> > >, std::greater<std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> > > >::value_type&' {aka 'const std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> >&'}
  736 |       push(const value_type& __x)
      |            ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_queue.h:744:7: note: candidate: 'void std::priority_queue<_Tp, _Sequence, _Compare>::push(value_type&&) [with _Tp = std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> >; _Sequence = std::vector<std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> > >; _Compare = std::greater<std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> > >; value_type = std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> >]'
  744 |       push(value_type&& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:744:25: note:   no known conversion for argument 1 from 'std::pair<std::pair<std::pair<int, int>, std::pair<int, int> >, std::pair<int, int> >' to 'std::priority_queue<std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> >, std::vector<std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> > >, std::greater<std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> > > >::value_type&&' {aka 'std::pair<std::pair<std::pair<int, int>, int>, std::pair<int, int> >&&'}
  744 |       push(value_type&& __x)
      |            ~~~~~~~~~~~~~^~~