Language: C++

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

template <class T>
class SegTree {
    using F = function<T(T, T)>;
    int n;
    vector<T> data;
    const F f;
    const T e;

public:
    SegTree(const vector<T>& as, const F f, const T& e) : f(f), e(e) {
        n = 1;
        while (n < as.size()) n <<= 1;
        data.assign(2 * n, e);
        for (int i = 0; i < as.size(); i++) {
            data[n + i] = as[i];
        }
        for (int i = n - 1; i > 0; i--) {
            data[i] = f(data[2 * i], data[2 * i + 1]);
        }
    }

    void update(int k, const T& x) {
        k += n;
        data[k] = x;
        while (k >>= 1) {
            data[k] = f(data[2 * k], data[2 * k + 1]);
        }
    }

    T query(int a, int b) {
        T L = e, R = e;
        for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
            if (a & 1) {
                L = f(L, data[a++]);
            }
            if (b & 1) {
                R = f(data[--b], R);
            }
        }
        return f(L, R);
    }
};

using ii = pair<int, int>;
struct range {
    int l, r;
    ii mi;
    bool even;
    bool operator<(const range& rhs) const {
        return mi.first > rhs.mi.first;
    }
};

void printvec(vi& x, int size = 0) {
    if (x.size() == 0) {
        cout << endl;
        return;
    }
    cout << x.front();
    for (int i = 1; i < (size == 0 ? x.size() : size); i++) {
        cout << " " << x[i];
    }
    cout << endl;
}

using ii = pair<int, int>;
struct range {
    int l, r;
    ii mi;
    bool even;
    bool operator<(const range& rhs) const {
        return mi.first > rhs.mi.first;
    }
};

void printvec(vi& x, int size = 0) {
    if (x.size() == 0) {
        cout << endl;
        return;
    }
    cout << x.front();
    for (int i = 1; i < (size == 0 ? x.size() : size); i++) {
        cout << " " << x[i];
    }
    cout << endl;
}

int main() {
    int n;
    cin >> n;
    vector<ii> es, os;
    for (int i = 0; i < n; i++) {
        int p;
        cin >> p;
        if (i % 2 == 0) {
            es.emplace_back(p, i);
        } else {
            os.emplace_back(p, i);
        }
    }
    SegTree<ii> et(es, [](ii a, ii b) {return min(a, b);}, ii(1e9, 0));
    SegTree<ii> ot(os, [](ii a, ii b) {return min(a, b);}, ii(1e9, 0));
    priority_queue<range> er;
    range orr = {-1, -1, ii(-1, -1), true};
    er.push({0, n, et.query(0, n / 2), true});
    vi ans;
    while (er.size() || orr.l != -1) {
        if (orr.l != -1) {
            ans.push_back(orr.mi.first);
            if (orr.mi.second != orr.l) {
                er.push({orr.l, orr.mi.second, (orr.even ? et : ot).query((orr.l + orr.even) / 2, (orr.mi.second + orr.even) / 2), orr.even});
            }
            if (orr.mi.second + 1 != orr.r) {
                er.push({orr.mi.second + 1, orr.r, (!orr.even ? et : ot).query((orr.mi.second + 1 + !orr.even) / 2, (orr.r + !orr.even) / 2), !orr.even});
            }
            orr = {-1, -1, ii(-1, -1), true};
            continue;
        }
        
        range rem = er.top();
        ans.push_back(rem.mi.first);
        if (rem.l != rem.mi.second) {
            er.push({rem.l, rem.mi.second, (rem.even ? et : ot).query((rem.l + rem.even) / 2, (rem.mi.second + rem.even) / 2), rem.even});
        }
        orr = {rem.mi.second + 1, rem.r, (!rem.even ? et : ot).query((rem.mi.second + 1 + !rem.even) / 2, (rem.r + !rem.even) / 2), !rem.even};
        er.pop();
    }
    
    printvec(ans);
}


Compiler output:
a.cc:75:8: error: redefinition of 'struct range'
   75 | struct range {
      |        ^~~~~
a.cc:53:8: note: previous definition of 'struct range'
   53 | struct range {
      |        ^~~~~
a.cc:84:6: error: redefinition of 'void printvec(vi&, int)'
   84 | void printvec(vi& x, int size = 0) {
      |      ^~~~~~~~
a.cc:62:6: note: 'void printvec(vi&, int)' previously defined here
   62 | void printvec(vi& x, int size = 0) {
      |      ^~~~~~~~