Language: C++

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
priority_queue<int,vector<int>,greater<int> >Heap;
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
int v[200005],first[200005],next[200005],e;
void AddEdge(int U,int V){
	v[++e]=V;
	next[e]=first[U];
	first[U]=e;
}
int a[200005],cnt,ma[200005],pai[200005];
int minv[2][100005],n,pos[200005];
void update(int o,int p,int v,int rt,int l,int r)
{
    if(l==r)
      {
        minv[o][rt]+=v;
        return;
      }
    int m=(l+r>>1);
    if(p<=m) update(o,p,v,lson);
    else update(o,p,v,rson);
    minv[o][rt]=min(minv[o][rt<<1],minv[o][rt<<1|1]);
}
int query(int o,int ql,int qr,int rt,int l,int r)
{
    if(ql<=l&&r<=qr) return minv[o][rt];
    int m=(l+r>>1);
    int res=2147483647;
    if(ql<=m) res=min(res,query(o,ql,qr,lson));
    if(m<qr) res=min(res,query(o,ql,qr,rson));
    return res;
}
void work(int l,int r,int f){
	if(l>=r){
		return;
	}
	int L=pos[query(l&1,ma[l],(l%2 == r%2) ? ma[r] : ma[r-1],1,1,n/2)];
	int R=pos[query((L+1)&1,ma[L+1],((L+1)%2 == r%2) ? ma[r] : ma[r-1],1,1,n/2)];
	pai[a[L]]=a[R];
	AddEdge(a[f],a[L]);
	work(l,L-1,f);
	work(L+1,R-1,L);
	work(R+1,r,f);
}
int main(){
	scanf("%d",&n);
	if(n==2){
		puts("1 2");
		return 0;
	}
	for(int i=1;i<=n;++i){
		scanf("%d",&a[i]);
		pos[a[i]]=i;
		update(i&1,i/2+(i&1),a[i],1,1,n/2);
		ma[i]=i/2+(i&1);
	}
	work(1,n,0);
	for(int i=first[0];i;i=next[i]){
		Heap.push(v[i]);
	}
	while(!Heap.empty()){
		int U=Heap.top(); Heap.pop();
		cnt+=2;
		printf("%d %d%c",U,pai[U],cnt==n ? '\n' : ' ');
		for(int i=first[U];i;i=next[i]){
			Heap.push(v[i]);
		}
	}
	return 0;
}

Compiler output:
a.cc: In function 'void AddEdge(int, int)':
a.cc:12:9: error: reference to 'next' is ambiguous
   12 |         next[e]=first[U];
      |         ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:9:29: note:                 'int next [200005]'
    9 | int v[200005],first[200005],next[200005],e;
      |                             ^~~~
a.cc: In function 'int main()':
a.cc:63:32: error: reference to 'next' is ambiguous
   63 |         for(int i=first[0];i;i=next[i]){
      |                                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:9:29: note:                 'int next [200005]'
    9 | int v[200005],first[200005],next[200005],e;
      |                             ^~~~
a.cc:70:40: error: reference to 'next' is ambiguous
   70 |                 for(int i=first[U];i;i=next[i]){
      |                                        ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:9:29: note:                 'int next [200005]'
    9 | int v[200005],first[200005],next[200005],e;
      |                             ^~~~