Language: C++

#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EPS 1e-10
#define set0(x) memset((x),0,sizeof(x))
#define setINF(x) memset((x),63,sizeof(x))
#define MP make_pair
#define X first
#define L second.first
#define R second.second
using namespace std;
int n,a[2][100005],pos[100005],mi[2][100005][20];
typedef pair<int,pair<int,int> > pip;
priority_queue<pip,vector<pip>,greater<pip> > pq;
inline int query(int k,int l,int r){
	int lb=0,ub=lg+1;
        while(ub-lb>1){
            int mid=(lb+ub)>>1;
            if(r-l+1>=(1<<mid))lb=mid;
            else ub=mid;
        }
	return min(mi[k][l][lb],mi[k][r-(1<<lb)+1][lb]);
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	for(int i=0;i<n;i++){
		int x;
		cin>>x;
		pos[x]=i;
		a[i&1][i]=x;
		a[(i&1)^1][i]=INF;
	}
	//RMQ Init
	for(int k=0;k<2;k++){
		for(int i=0;i<n;i++){
			mi[k][i][0]=a[k][i];
		}
		for(int j=1;(1<<j)<=n;j++){
			for(int i=0;(i+(1<<j)-1)<n;i++){
				mi[k][i][j]=min(mi[k][i][j-1],mi[k][i+(1<<(j-1))][j-1]);
			}
		}
	}
	pq.push(MP(query(0,0,n-1),MP(0,n-1)));
	while(!pq.empty()){
		pip u=pq.top();
		pq.pop();
		int q1=u.X,q2=query((pos[u.X]&1)^1,pos[u.X]+1,u.R);
		cout<<q1<<' '<<q2<<' ';
		if(u.L<pos[q1])pq.push(MP(query(u.L&1,u.L,pos[q1]-1),MP(u.L,pos[q1]-1)));
		if(pos[q1]+1<pos[q2])pq.push(MP(query((pos[q1]+1)&1,pos[q1]+1,pos[q2]-1),MP(pos[q1]+1,pos[q2]-1)));
		if(pos[q2]<u.R)pq.push(MP(query((pos[q2]+1)&1,pos[q2]+1,u.R),MP(pos[q2]+1,u.R)));
	}
	return 0;
}

Compiler output:
a.cc: In function 'int query(int, int, int)':
a.cc:95:21: error: 'lg' was not declared in this scope; did you mean 'lb'?
   95 |         int lb=0,ub=lg+1;
      |                     ^~
      |                     lb