Language: C++

#include<ctime>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath> 
#include<cstring> 
#include<cassert>
#include<string>
#include<sstream>
#include<fstream>
#include<deque>
#include<queue>
#include<vector>
#include<map>
#include<list>
#include<stack>
#include<set>
#include<bitset>
#include<iomanip>
#include<utility>
#include<functional>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cwchar>
#include<cwctype>
#include<exception>
#include<locale>
#include<numeric>
#include<new>
#include<stdexcept>
#include<limits>
using namespace std;

#define ll long long
#define INF 1e9
#define rep(i,n) for(int (i)=0;(i)<n;i++)
#define REP(i,n) for(int (i)=1;(i)<=n;i++)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
#define pii pair<int,int>
#define sz(s) s.size()
#define all(s) (s.begin(),s.end())

const int maxn=200005;
int n;
int tree0[maxn*4],tree1[maxn*4],a[maxn],tag[maxn*4],leaf[maxn],next[maxn*4];
vector<int>ans;
bool debug;

int MIN(int c,int d){
	if(c==INF)return d;
	if(d==INF)return c;
	return a[c]<a[d]?c:d;
}

void build(int l,int r,int node){
	if(l==r){
		if(l&1)tree1[node]=l,tree0[node]=INF;
		else tree1[node]=INF,tree0[node]=l;
		leaf[l]=node;
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,node*2);
	build(mid+1,r,node*2+1);
	tree0[node]=MIN(tree0[node*2],tree0[node*2+1]);
	tree1[node]=MIN(tree1[node*2],tree1[node*2+1]);
}

void Push(int node){
	if(tag[node]){
		swap(tree0[node],tree1[node]);
		tag[node*2]^=tag[node];
		tag[node*2+1]^=tag[node];
		tag[node]=0;
	}
}

void update(int l,int r,int vl,int vr,int node){
	if(vl>vr)return;
	if(l>vr||r<vl)return;
	if(l>=vl&&r<=vr){
		tag[node]^=1;
		Push(node);
		return;
	}
	int mid=(l+r)>>1;
	Push(node);
	update(l,mid,vl,vr,node*2);
	update(mid+1,r,vl,vr,node*2+1);
	tree0[node]=MIN(tree0[node*2],tree0[node*2+1]);
	tree1[node]=MIN(tree1[node*2],tree1[node*2+1]); 
}

int query_0(int l,int r,int vl,int vr,int node){
	if(l>vr||r<vl)return INF;
	if(l>=vl&&r<=vr)return tag[node]?tree1[node]:tree0[node];
	Push(node);
	int mid=(l+r)>>1;
	return MIN(query_0(l,mid,vl,vr,node*2),query_0(mid+1,r,vl,vr,node*2+1));
}

int query_1(int l,int r,int vl,int vr,int node){
	if(l>vr||r<vl)return INF;
	if(l>=vl&&r<=vr)return tag[node]?tree0[node]:tree1[node];
	Push(node);
	int mid=(l+r)>>1;
	return MIN(query_1(l,mid,vl,vr,node*2),query_1(mid+1,r,vl,vr,node*2+1));
}

int Find_next(int l,int r,int vl,int vr,int node){
	if(l>vr||r<vl)return INF;
	if(l>=vl&&r<=vr)return next[node];
	int mid=(l+r)>>1;
	return min(Find_next(l,mid,vl,vr,node*2),Find_next(mid+1,r,vl,vr,node*2+1));
}

void del(int pos){
	int node=leaf[pos];
	tree0[node]=tree1[node]=INF;
	while(node){
		node>>=1;
		tree0[node]=MIN(tree0[node*2],tree0[node*2+1]);
		tree1[node]=MIN(tree1[node*2],tree1[node*2+1]); 
	}
}

void add(int pos,int val){
	int node=leaf[pos];
	next[node]=val;
	while(node){
		node>>=1;
		next[node]=min(next[node*2],next[node*2+1]);
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	REP(i,n)cin>>a[i];
	build(1,n,1);
	REP(i,n)add(i,n+1);
	REP(i,n/2){
		int pos=query_1(1,n,1,n,1);
		ans.push_back(a[pos]);
		int cur=Find_next(1,n,pos+1,n,1);
		int pos2=query_0(1,n,pos+1,cur-1,1);
//		cout<<pos<<" "<<pos2<<endl;
//		cout<<query_0(1,n,6,6,1)<<endl;
		ans.push_back(a[pos2]);
		del(pos);
		del(pos2);
		add(pos,pos);
		add(pos2,pos2);
		update(1,n,pos+1,pos2-1,1);
	}
	rep(i,ans.size())cout<<ans[i]<<" ";
	return 0;
}

Compiler output:
a.cc: In function 'int Find_next(int, int, int, int, int)':
a.cc:124:32: error: reference to 'next' is ambiguous
  124 |         if(l>=vl&&r<=vr)return next[node];
      |                                ^~~~
In file included from /usr/include/c++/14/string:47,
                 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: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:57:64: note:                 'int next [800020]'
   57 | int tree0[maxn*4],tree1[maxn*4],a[maxn],tag[maxn*4],leaf[maxn],next[maxn*4];
      |                                                                ^~~~
a.cc: In function 'void add(int, int)':
a.cc:141:9: error: reference to 'next' is ambiguous
  141 |         next[node]=val;
      |         ^~~~
/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:57:64: note:                 'int next [800020]'
   57 | int tree0[maxn*4],tree1[maxn*4],a[maxn],tag[maxn*4],leaf[maxn],next[maxn*4];
      |                                                                ^~~~
a.cc:144:17: error: reference to 'next' is ambiguous
  144 |                 next[node]=min(next[node*2],next[node*2+1]);
      |                 ^~~~
/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:57:64: note:                 'int next [800020]'
   57 | int tree0[maxn*4],tree1[maxn*4],a[maxn],tag[maxn*4],leaf[maxn],next[maxn*4];
      |                                                                ^~~~
a.cc:144:32: error: reference to 'next' is ambiguous
  144 |                 next[node]=min(next[node*2],next[node*2+1]);
      |                                ^~~~
/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:57:64: note:                 'int next [800020]'
   57 | int tree0[maxn*4],tree1[maxn*4],a[maxn],tag[maxn*4],leaf[maxn],next[maxn*4];
      |                                                                ^~~~
a.cc:144:45: error: reference to 'next' is ambiguous
  144 |                 next[node]=min(next[node*2],next[node*2+1]);
      |                                             ^~~~
/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:57:64: note:                 'int next [800020]'
   57 | int tree0[maxn*4],tree1[maxn*4],a[maxn],tag[maxn*4],leaf[maxn],next[maxn*4];
      |                                                                ^~~~