Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
using Pi = pair<P, int>;
#define rep(i, n) for (int i = 0; i < n; i++)
const int mx = 200005;
int n, q[mx], p, c;
vector<int> ch[mx];
P ans[mx];
priority_queue<Pi, vector<Pi>, greater<Pi>> qu;
struct SegTree {
  int sz;
  P ide = P(1e9, 0);
  vector<P> dat;
  SegTree(int n) {
    for (sz = 2; sz < n; sz *= 2)
      ;
    dat.assign(sz * 2, ide);
  }
  void set(int x, P n) {
    dat[x += sz] = n;
    while (x /= 2)
      dat[x] = min(dat[x * 2], dat[x * 2 + 1]);
  }
  P query(int a, int b, int k = 1, int l = 0, int r = -1) {
    if (r == -1)
      r = sz;
    if (r <= a || b <= l)
      return ide;
    if (a <= l && r <= b)
      return dat[k];
    return min(query(a, b, k * 2, l, (l + r) / 2),
               query(a, b, k * 2 + 1, (l + r) / 2, r));
  }
} sg[2] = {SegTree(mx / 2), SegTree(mx / 2)};

void solve(int s, int v, int pr) {
  if (s == v)
    return;
  P ss = sg[s & 1].query(s, v);
  P vv = sg[s & 1 ^ 1].query(ss.second + 1, v + 1);
  if (c)
    ch[pr].push_back(c);
  ans[pr = c++] = P(ss.first, vv.first);
  solve(s, ss.second, pr);
  solve(ss.second + 1, vv.second, pr);
  solve(vv.second + 1, v, pr);
}
int main() {
  scanf("%d", &n);
  rep(i, n) {
    scanf("%d", &p);
    sg[i & 1].set(i, P(p, i));
  }
  solve(0, n, 0);
  qu.push(Pi(ans[0], 0));
  while (!qu.empty()) {
    auto it = qu.top();
    qu.pop();
    printf("%s%d %d", (it.second ? " " : ""), it.first.first, it.first.second);
    for (auto t : ch[it.second])
      qu.push(Pi(ans[t], t));
  }
  puts("");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
using Pi = pair<P, int>;
#define rep(i, n) for (int i = 0; i < n; i++)
const int mx = 200005;
int n, q[mx], p, c;
vector<int> ch[mx];
P ans[mx];
priority_queue<Pi, vector<Pi>, greater<Pi>> qu;
struct SegTree {
  int sz;
  P ide = P(1e9, 0);
  vector<P> dat;
  SegTree(int n) {
    for (sz = 2; sz < n; sz *= 2)
      ;
    dat.assign(sz * 2, ide);
  }
  void set(int x, P n) {
    dat[x += sz] = n;
    while (x /= 2)
      dat[x] = min(dat[x * 2], dat[x * 2 + 1]);
  }
  P query(int a, int b, int k = 1, int l = 0, int r = -1) {
    if (r == -1)
      r = sz;
    if (r <= a || b <= l)
      return ide;
    if (a <= l && r <= b)
      return dat[k];
    return min(query(a, b, k * 2, l, (l + r) / 2),
               query(a, b, k * 2 + 1, (l + r) / 2, r));
  }
} sg[2] = {SegTree(mx), SegTree(mx)};

void solve(int s, int v, int pr) {
  if (s == v)
    return;
  P ss = sg[s & 1].query(s, v);
  P vv = sg[s & 1 ^ 1].query(ss.second + 1, v + 1);
  if (c)
    ch[pr].push_back(c);
  ans[pr = c++] = P(ss.first, vv.first);
  solve(s, ss.second, pr);
  solve(ss.second + 1, vv.second, pr);
  solve(vv.second + 1, v, pr);
}
int main() {
  scanf("%d", &n);
  rep(i, n) {
    scanf("%d", &p);
    sg[i & 1].set(i, P(p, i));
  }
  solve(0, n, 0);
  qu.push(Pi(ans[0], 0));
  while (!qu.empty()) {
    auto it = qu.top();
    qu.pop();
    printf("%s%d %d", (it.second ? " " : ""), it.first.first, it.first.second);
    for (auto t : ch[it.second])
      qu.push(Pi(ans[t], t));
  }
  puts("");
}