Language: C++

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include<bitset>
#include<map>

#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define fi first
#define se second

using namespace std;

typedef long long LL;
typedef double db;
typedef unsigned int ui;
typedef pair<int,int> pir;

int get(){
	char ch;
	while(ch=getchar(),(ch<'0'||ch>'9')&&ch!='-');
	if (ch=='-'){
		int s=0;
		while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0';
		return -s;
	}
	int s=ch-'0';
	while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0';
	return s;
}

ui seed;
ui random(){seed^=seed<<13;seed^=seed>>17;seed^=seed<<5;return seed;}

const int N = 2e+5+5;

int n;
struct node{
	int l,r,val,key,tot;
	int v[2];
}tree[N];
int fa[N];
int p[N],q[N];
bool bz[N];

int getrt(int x){for(;fa[x];x=fa[x]);return x;}

void update(int x){
	tree[x].v[0]=min(tree[tree[x].l].v[0],tree[tree[x].r].v[0]);
	tree[x].v[1]=min(tree[tree[x].l].v[1],tree[tree[x].r].v[1]);
	tree[x].v[x&1]=min(tree[x].v[x&1],tree[x].key);
	tree[x].tot=tree[tree[x].l].tot+tree[tree[x].r].tot+1;
}

int merge(int x,int y){
	if (!x||!y)return x^y;
	if (tree[x].val<tree[y].val){
		y=merge(tree[x].r,y);
		fa[y]=x,tree[x].r=y;
		update(x);
		return x;
	}
	x=merge(x,tree[y].l);
	fa[x]=y,tree[y].l=x;
	update(y);
	return y;
}

int findth(int x,int y){
	if (x==y)return tree[tree[x].l].tot+1;
	if (y<x)return findth(tree[x].l,y);
	return tree[tree[x].l].tot+1+findth(tree[x].r,y);
}

pir split(int x,int k){
	fa[x]=0;
	if (!k)return make_pair(0,k);
	if (k>=tree[x].tot)return make_pair(k,0);
	if (k<=tree[tree[x].l].tot){
		pir u=split(tree[x].l,k);
		tree[x].l=u.se;
		if (u.se)fa[u.se]=x;
		update(x);
		return make_pair(u.fi,x);
	}
	pir u=split(tree[x].r,k-tree[tree[x].l].tot-1);
	tree[x].r=u.fi;
	if (u.fi)fa[u.fi]=x;
	update(x);
	return make_pair(x,u.se);
}

void del(int x){
	int rt=getrt(x);
	int k=findth(rt,x);
	pir u=split(rt,k);
	if (k)split(u.fi,k-1);
}

int main(){
	n=get();
	seed=16792369;
	fo(i,1,n){p[i]=get();q[p[i]]=i;}
	int rt=0;
	tree[0].v[0]=tree[0].v[1]=1E+9;
	fo(i,1,n){
		tree[i].val=random();
		tree[i].key=tree[i].v[i&1]=p[i];
		tree[i].v[(i+1)&1]=1e+9;
		tree[i].tot=1;
		rt=merge(rt,i);
	}
	fo(i,1,n)
	if (!bz[i]){
		int rt=getrt(q[i]);
		int x=i,y=tree[rt].v[(q[i]+1)&1];
		if (q[x]<q[y])printf("%d %d ",x,y);
		else printf("%d %d ",y,x);
		del(q[x]),del(q[y]);
		bz[y]=1;
	}
	putchar('\n');
	return 0;
}

Compiler output:
a.cc:36:4: error: ambiguating new declaration of 'ui random()'
   36 | ui random(){seed^=seed<<13;seed^=seed>>17;seed^=seed<<5;return seed;}
      |    ^~~~~~
In file included from /usr/include/c++/14/cstdlib:79,
                 from /usr/include/c++/14/ext/string_conversions.h:43,
                 from /usr/include/c++/14/bits/basic_string.h:4154,
                 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/stdlib.h:521:17: note: old declaration 'long int random()'
  521 | extern long int random (void) __THROW;
      |                 ^~~~~~