Language: C++

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cout << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define int long long

template<typename T>
struct SegmentTree{
private:
    int n;
    T E;
    vector<T> node;
    inline void updatef(T& x,T& y){
        x = y;
        //x += y;
        //x = max(x,y);
        //x = min(x,y);
    }
    inline T queryf(T& x,T& y){
        return min(x,y);
        //return x+y;
        //return max(x,y);
    }

public:
    SegmentTree() {}
    
    void init(int sz, T E_) {
        E = E_;
        n=1;
        while(n<sz)n<<=1;
        node.resize(2*n-1,E);
    }

    SegmentTree(int sz,T E_):E(E_){
        n=1;
        while(n<sz)n<<=1;
        node.resize(2*n-1,E);
    }

    SegmentTree(vector<T>& A,T E_):E(E_){
        int sz=A.size();
        n=1;
        while(n<sz)n<<=1;
        node.resize(2*n-1,E);
        rep(i,sz)node[i+n-1]=A[i];
        for(int i=n-2;i>=0;--i){
            node[i]=queryf(node[2*i+1], node[2*i+2]);
        }
    }
    void update(int k,T x){
        k+=n-1;
        updatef(node[k],x);
        while(k>0){
            k=(k-1)/2;
            node[k]=queryf(node[2*k+1], node[2*k+2]);
        }
    }
       //[a,b)での値を返す
    T get(int a,int b,int k=0,int l=0,int r=-1){
        if(r<0)r=n;
        if(r<=a||b<=l)return E;
        if(a<=l&&r<=b)return node[k];
        T xl=get(a,b,2*k+1,l,(l+r)/2);
        T xr=get(a,b,2*k+2,(l+r)/2,r);
        return queryf(xl, xr);
    }
};

int n;
vector<int> p;
SegmentTree<P> even, odd;
priority_queue<PP, vector<PP>, greater<PP>> pque;

vector<int> pa;

// void dfs(int l, int r) {
//     if(l + 1 >= r) return;

//     P x, y;
//     if(l % 2 == 0) {
//         x = even.get(l, r);
//         y = odd.get(l, r);
//     } else {
//         x = odd.get(l, r);
//         y = even.get(l, r);
//     }
//     pque.push(make_pair(x.fr, make_pair(l, r)));
//     pa[x.fr] = y.fr;

//     int a = x.sc, b = y.sc;

//     dfs(l, a);
//     dfs(a + 1, b);
//     dfs(b + 1, r);
// }

PP f(int l, int r) {
    P x;
    if(l % 2 == 0) {
        x = even.get(l, r);
        y = odd.get(l, r);
    } else {
        x = odd.get(l, r);
        y = even.get(l, r);
    }
    pa[x.fr] = pa[y.fr];
    return make_pair(x.fr, make_pair(l, r));
}

void solve() {
	cin >> n;
    p.resize(n);
    pa.resize(n); 
    rep(i, n) {
        cin >> p[i];
    }
    even.init(n, make_pair(INF, INF));
    odd.init(n, make_pair(INF, INF));
    rep(i, n) {
        if(i % 2 == 0) {
            even.update(i, make_pair(p[i], i));
        } else {
            odd.update(i, make_pair(p[i], i));
        }
    }
    vector<int> ans;
    
    P x, y;
    x = even.get(0, n);
    y = odd.get(0, n);
    pque.push(make_pair(x.fr, make_pair(0, n)));
    pa[x.fr] = y.fr;

    while(!pque.empty()) {
        PP q = pque.top(); pque.pop();
        int a = q.fr;
        ans.push_back(a);
        ans.push_back(pa[a]);

        int l = q.sc.fr, r = q.sc.sc;
        if(l % 2 == 0) {
            x = even.get(l, r);
            y = odd.get(l, r);
        } else {
            x = odd.get(l, r);
            y = even.get(l, r);
        }

        int u = x.sc, v = y.sc;
        if(l + 1 < u) pque.push(f(l, u));
        if(u + 1 < v) pque.push(f(u + 1, v));
        if(v + 1 < r) pque.push(f(v + 1, r));
    }

    rep(i, n) {
        cout << ans[i] << " ";
    }
    cout << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init();
	solve();
	//cout << "finish" << endl;
    return 0;
}

Compiler output:
a.cc: In function 'PP f(long long int, long long int)':
a.cc:148:9: error: 'y' was not declared in this scope
  148 |         y = odd.get(l, r);
      |         ^
a.cc:151:9: error: 'y' was not declared in this scope
  151 |         y = even.get(l, r);
      |         ^
a.cc:153:19: error: 'y' was not declared in this scope
  153 |     pa[x.fr] = pa[y.fr];
      |                   ^