Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define forn(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define ford(i, a, n) for (int i = (int)(n - 1); i >= (int)(a); --i)
#define fore(i, a, n) for (int i = (int)(a); i <= (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define fs first
#define sn second
#define trace(a)                                                               \
  for (auto i : a)                                                             \
    cerr << i << ' ';                                                          \
  cerr << '\n'
#define eb emplace_back

#ifndef M_PI
const ld M_PI = acos(-1.0);
#endif

template <typename T> inline void _setmax(T &a, T b) {
  if (a < b)
    a = b;
}

template <typename T> inline void _setmin(T &a, T b) {
  if (a > b)
    a = b;
}

const ld eps = 1e-9;
const int INF = 2000000000;
const ll LINF = 1ll * INF * INF;
const ll MOD = 1000000007;

const int N = 1 << 17;

int e[2 * N], o[2 * N];

void fill(int *t) { forn(i, 0, 2 * N) t[i] = INF; }

void build(int *t) { ford(i, 1, N) t[i] = min(t[2 * i], t[2 * i + 1]); }

void _set(int *t, int pos, int val) {
  pos += N;
  t[pos] = val;
  pos /= 2;
  while (pos) {
    t[pos] = min(t[2 * pos], t[2 * pos + 1]);
    pos /= 2;
  }
}

int get(int *t, int l, int r) {
  l += N, r += N;
  int ans = INF;
  while (l < r) {
    // cerr << l << ' ' << r << '\n';
    if (l % 2) {
      _setmin(ans, t[l]);
      ++l;
    }
    if (r % 2) {
      --r;
      _setmin(ans, t[r]);
    }
    l /= 2;
    r /= 2;
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  fill(o), fill(e);
  int n;
  cin >> n;
  vector<int> a(n);
  forn(i, 0, n) cin >> a[i];
  set<int> bounds;
  bounds.insert(n);
  bounds.insert(-1);
  vector<int> pos(n + 1);
  forn(i, 0, n) pos[a[i]] = i;
  vector<int> ans;
  forn(i, 0, n)(i % 2 ? o : e)[i + N] = a[i];
  build(o), build(e);
  set<int> vars;
  vars.insert(get(e, 0, n - 1));
  forn(_, 0, n / 2) {
    int x = *vars.begin();
    vars.erase(vars.begin());
    int xpos = pos[x];
    auto it = bounds.lower_bound(xpos);
    int r = *it;
    --it;
    int l = *it;
    int y = get(xpos % 2 ? e : o, xpos, r);
    int ypos = pos[y];
    ans.eb(x);
    ans.eb(y);
    bounds.insert(xpos);
    bounds.insert(ypos);
    int val1 = get(l % 2 ? e : o, l, xpos - 1);
    int val2 = get(xpos % 2 ? e : o, xpos, ypos - 1);
    int val3 = get(ypos % 2 ? e : o, ypos, r - 1);
    vars.insert(val1);
    vars.insert(val2);
    vars.insert(val3);
  }
  forn(i, 0, n) cout << ans[i] << ' ';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define forn(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define ford(i, a, n) for (int i = (int)(n - 1); i >= (int)(a); --i)
#define fore(i, a, n) for (int i = (int)(a); i <= (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define fs first
#define sn second
#define trace(a)                                                               \
  for (auto i : a)                                                             \
    cerr << i << ' ';                                                          \
  cerr << '\n'
#define eb emplace_back

#ifndef M_PI
const ld M_PI = acos(-1.0);
#endif

template <typename T> inline void _setmax(T &a, T b) {
  if (a < b)
    a = b;
}

template <typename T> inline void _setmin(T &a, T b) {
  if (a > b)
    a = b;
}

const ld eps = 1e-9;
const int INF = 2000000000;
const ll LINF = 1ll * INF * INF;
const ll MOD = 1000000007;

const int N = 1 << 18;

int e[2 * N], o[2 * N];

void fill(int *t) { forn(i, 0, 2 * N) t[i] = INF; }

void build(int *t) { ford(i, 1, N) t[i] = min(t[2 * i], t[2 * i + 1]); }

void _set(int *t, int pos, int val) {
  pos += N;
  t[pos] = val;
  pos /= 2;
  while (pos) {
    t[pos] = min(t[2 * pos], t[2 * pos + 1]);
    pos /= 2;
  }
}

int get(int *t, int l, int r) {
  l += N, r += N;
  int ans = INF;
  while (l < r) {
    // cerr << l << ' ' << r << '\n';
    if (l % 2) {
      _setmin(ans, t[l]);
      ++l;
    }
    if (r % 2) {
      --r;
      _setmin(ans, t[r]);
    }
    l /= 2;
    r /= 2;
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  fill(o), fill(e);
  int n;
  cin >> n;
  vector<int> a(n);
  forn(i, 0, n) cin >> a[i];
  set<int> bounds;
  bounds.insert(n);
  bounds.insert(-1);
  vector<int> pos(n + 1);
  forn(i, 0, n) pos[a[i]] = i;
  vector<int> ans;
  forn(i, 0, n)(i % 2 ? o : e)[i + N] = a[i];
  build(o), build(e);
  set<int> vars;
  vars.insert(get(e, 0, n - 1));
  forn(_, 0, n / 2) {
    int x = *vars.begin();
    vars.erase(vars.begin());
    int xpos = pos[x];
    auto it = bounds.lower_bound(xpos);
    int r = *it;
    --it;
    int l = *it;
    int y = get(xpos % 2 ? e : o, xpos, r);
    int ypos = pos[y];
    ans.eb(x);
    ans.eb(y);
    bounds.insert(xpos);
    bounds.insert(ypos);
    int val1 = get(l % 2 ? e : o, l, xpos - 1);
    int val2 = get(xpos % 2 ? e : o, xpos, ypos - 1);
    int val3 = get(ypos % 2 ? e : o, ypos, r - 1);
    vars.insert(val1);
    vars.insert(val2);
    vars.insert(val3);
  }
  forn(i, 0, n) cout << ans[i] << ' ';
}