Problem p03641 (C++)

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

const int MAX_N = 131072;
int o[MAX_N * 2 - 1], e[MAX_N * 2 - 1];

void update(int *arr, int n, int i, int x) {
  i += MAX_N - 1;
  arr[i] = x;
  while (i > 0) {
    i = (i - 1) / 2;
    arr[i] = std::min(arr[i * 2 + 1], arr[i * 2 + 2]);
  }
}

// minimum value of [a, b)
int query(int *arr, int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INT_MAX;
  if (a <= l && r <= b)
    return arr[k];
  else {
    int vl = query(arr, a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(arr, a, b, k * 2 + 2, (l + r) / 2, r);
    return std::min(vl, vr);
  }
}

int main() {
  int n;
  std::cin >> n;
  std::vector<int> p(n + 1), index(n + 1);
  for (int i = 0; i < 2 * MAX_N - 1; i++)
    o[i] = e[i] = INT_MAX;
  for (int i = 0; i < n; i++) {
    std::cin >> p[i];
    index[p[i]] = i;
    if (i % 2)
      update(o, MAX_N, i, p[i]);
    else
      update(e, MAX_N, i, p[i]);
  }

  // (minimum even-indexed value, left index, right index)
  std::priority_queue<std::tuple<int, int, int>,
                      std::vector<std::tuple<int, int, int>>, std::greater<>>
      que;
  que.emplace(query(e, 0, n, 0, 0, MAX_N), 0, n);
  std::deque<int> q;

  while (!que.empty()) {
    auto curr = que.top();
    auto v = std::get<0>(curr), l = std::get<1>(curr), r = std::get<2>(curr);
    // std::cout << v << " " << l << " " << r << std::endl;
    que.pop();
    if (r - l < 2)
      continue;
    q.push_back(v);
    auto s = query((l % 2 ? e : o), index[v], r, 0, 0, MAX_N);
    q.push_back(s);
    // std::cout << "\t" << index[v] << " " << index[s] << std::endl;

    que.emplace(query((l % 2 ? o : e), l, index[v], 0, 0, MAX_N), l, index[v]);
    que.emplace(
        query((index[v] % 2 ? e : o), index[v] + 1, index[s], 0, 0, MAX_N),
        index[v] + 1, index[s]);
    que.emplace(query((index[s] % 2 ? e : o), index[s] + 1, r, 0, 0, MAX_N),
                index[s] + 1, r);
  }

  for (auto &el : q)
    std::cout << el << " ";
  std::cout << std::endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

const int MAX_N = 2 * 131072;
int o[MAX_N * 2 - 1], e[MAX_N * 2 - 1];

void update(int *arr, int n, int i, int x) {
  i += MAX_N - 1;
  arr[i] = x;
  while (i > 0) {
    i = (i - 1) / 2;
    arr[i] = std::min(arr[i * 2 + 1], arr[i * 2 + 2]);
  }
}

// minimum value of [a, b)
int query(int *arr, int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INT_MAX;
  if (a <= l && r <= b)
    return arr[k];
  else {
    int vl = query(arr, a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(arr, a, b, k * 2 + 2, (l + r) / 2, r);
    return std::min(vl, vr);
  }
}

int main() {
  int n;
  std::cin >> n;
  std::vector<int> p(n + 1), index(n + 1);
  for (int i = 0; i < 2 * MAX_N - 1; i++)
    o[i] = e[i] = INT_MAX;
  for (int i = 0; i < n; i++) {
    std::cin >> p[i];
    index[p[i]] = i;
    if (i % 2)
      update(o, MAX_N, i, p[i]);
    else
      update(e, MAX_N, i, p[i]);
  }

  // (minimum even-indexed value, left index, right index)
  std::priority_queue<std::tuple<int, int, int>,
                      std::vector<std::tuple<int, int, int>>, std::greater<>>
      que;
  que.emplace(query(e, 0, n, 0, 0, MAX_N), 0, n);
  std::deque<int> q;

  while (!que.empty()) {
    auto curr = que.top();
    auto v = std::get<0>(curr), l = std::get<1>(curr), r = std::get<2>(curr);
    // std::cout << v << " " << l << " " << r << std::endl;
    que.pop();
    if (r - l < 2)
      continue;
    q.push_back(v);
    auto s = query((l % 2 ? e : o), index[v], r, 0, 0, MAX_N);
    q.push_back(s);
    // std::cout << "\t" << index[v] << " " << index[s] << std::endl;

    que.emplace(query((l % 2 ? o : e), l, index[v], 0, 0, MAX_N), l, index[v]);
    que.emplace(
        query((index[v] % 2 ? e : o), index[v] + 1, index[s], 0, 0, MAX_N),
        index[v] + 1, index[s]);
    que.emplace(query((index[s] % 2 ? e : o), index[s] + 1, r, 0, 0, MAX_N),
                index[s] + 1, r);
  }

  for (auto &el : q)
    std::cout << el << " ";
  std::cout << std::endl;

  return 0;
}