Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
namespace ZheCaoZuoZhenShiBaoZhaAAAA {
const int N = 100100;
struct interval {
  int L, R, w;
  interval(int a = 0, int b = 0, int c = 0) { L = a, R = b, w = c; }
};
bool operator<(interval a, interval b) { return a.w > b.w; }
std::priority_queue<interval> Q;
int s[N], ds[N], t[N];
int ef[N][22], of[N][22];
int n, dn, tot;
void initialize() {
  scanf("%d", &n), dn = n >> 1;
  for (int i = 1; i <= n; i++)
    scanf("%d", s + i), ds[s[i]] = i;
  for (int i = 1; i <= dn; i++)
    ef[i][0] = s[i * 2], of[i][0] = s[i * 2 - 1];
  for (int k = 1; k <= 20; k++)
    for (int i = 1; i <= dn; i++)
      if (i + (1 << k) - 1 <= dn) {
        ef[i][k] = std::min(ef[i][k - 1], ef[i + (1 << (k - 1))][k - 1]);
        of[i][k] = std::min(of[i][k - 1], of[i + (1 << (k - 1))][k - 1]);
      }
  //		printf("fuck %d %d %d\n",of[1][0],of[2][0],of[1][1]);
  while (!Q.empty())
    Q.pop();
}
int query_odd(int L, int R) {
  //		printf("query : %d %d\n",L,R);
  int x = 1, k = 0;
  while (x * 2 <= (R - L + 1))
    x <<= 1, k++;
  //		printf("%d , %d : %d %d\n",L,R-x+1,of[L][k],of[R-x+1][k]);
  return std::min(of[L][k], of[R - x + 1][k]);
}
int query_even(int L, int R) {
  int x = 1, k = 0;
  while (x * 2 <= (R - L + 1))
    x <<= 1, k++;
  return std::min(ef[L][k], ef[R - x + 1][k]);
}
inline int ev(int L, int R) { return query_even((L + 1) >> 1, R >> 1); }
inline int od(int L, int R) { return query_odd((L >> 1) + 1, (R + 1) >> 1); }
void solve() {
  initialize();
  Q.push(interval(1, n, od(1, n)));
  tot = 0;
  for (int p, q; !Q.empty();) {
    interval H = Q.top();
    Q.pop();
    //			printf("%d -- %d : %d\n",H.L,H.R,H.w);
    p = ds[H.w];
    if (p & 1)
      q = ds[ev(p + 1, H.R)];
    else
      q = ds[od(p + 1, H.R)];
    //			printf("%d , %d\n",p,q);
    t[++tot] = s[p], t[++tot] = s[q];

    if (p != H.L)
      Q.push(interval(H.L, p - 1, H.L & 1 ? od(H.L, p - 1) : ev(H.L, p - 1)));
    if (p + 1 != q)
      Q.push(interval(p + 1, q - 1,
                      (p + 1) & 1 ? od(p + 1, q - 1) : ev(p + 1, q - 1)));
    if (q != H.R)
      Q.push(
          interval(q + 1, H.R, (q + 1) & 1 ? od(q + 1, H.R) : ev(q + 1, H.R)));
  }
  for (int i = 1; i <= n; i++)
    printf("%d ", t[i]);
  printf("\n");
}
} // namespace ZheCaoZuoZhenShiBaoZhaAAAA
int main() {
  ZheCaoZuoZhenShiBaoZhaAAAA::solve();
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
namespace ZheCaoZuoZhenShiBaoZhaAAAA {
const int N = 200100;
struct interval {
  int L, R, w;
  interval(int a = 0, int b = 0, int c = 0) { L = a, R = b, w = c; }
};
bool operator<(interval a, interval b) { return a.w > b.w; }
std::priority_queue<interval> Q;
int s[N], ds[N], t[N];
int ef[N][22], of[N][22];
int n, dn, tot;
void initialize() {
  scanf("%d", &n), dn = n >> 1;
  for (int i = 1; i <= n; i++)
    scanf("%d", s + i), ds[s[i]] = i;
  for (int i = 1; i <= dn; i++)
    ef[i][0] = s[i * 2], of[i][0] = s[i * 2 - 1];
  for (int k = 1; k <= 20; k++)
    for (int i = 1; i <= dn; i++)
      if (i + (1 << k) - 1 <= dn) {
        ef[i][k] = std::min(ef[i][k - 1], ef[i + (1 << (k - 1))][k - 1]);
        of[i][k] = std::min(of[i][k - 1], of[i + (1 << (k - 1))][k - 1]);
      }
  //		printf("fuck %d %d %d\n",of[1][0],of[2][0],of[1][1]);
  while (!Q.empty())
    Q.pop();
}
int query_odd(int L, int R) {
  //		printf("query : %d %d\n",L,R);
  int x = 1, k = 0;
  while (x * 2 <= (R - L + 1))
    x <<= 1, k++;
  //		printf("%d , %d : %d %d\n",L,R-x+1,of[L][k],of[R-x+1][k]);
  return std::min(of[L][k], of[R - x + 1][k]);
}
int query_even(int L, int R) {
  int x = 1, k = 0;
  while (x * 2 <= (R - L + 1))
    x <<= 1, k++;
  return std::min(ef[L][k], ef[R - x + 1][k]);
}
inline int ev(int L, int R) { return query_even((L + 1) >> 1, R >> 1); }
inline int od(int L, int R) { return query_odd((L >> 1) + 1, (R + 1) >> 1); }
void solve() {
  initialize();
  Q.push(interval(1, n, od(1, n)));
  tot = 0;
  for (int p, q; !Q.empty();) {
    interval H = Q.top();
    Q.pop();
    //			printf("%d -- %d : %d\n",H.L,H.R,H.w);
    p = ds[H.w];
    if (p & 1)
      q = ds[ev(p + 1, H.R)];
    else
      q = ds[od(p + 1, H.R)];
    //			printf("%d , %d\n",p,q);
    t[++tot] = s[p], t[++tot] = s[q];

    if (p != H.L)
      Q.push(interval(H.L, p - 1, H.L & 1 ? od(H.L, p - 1) : ev(H.L, p - 1)));
    if (p + 1 != q)
      Q.push(interval(p + 1, q - 1,
                      (p + 1) & 1 ? od(p + 1, q - 1) : ev(p + 1, q - 1)));
    if (q != H.R)
      Q.push(
          interval(q + 1, H.R, (q + 1) & 1 ? od(q + 1, H.R) : ev(q + 1, H.R)));
  }
  for (int i = 1; i <= n; i++)
    printf("%d ", t[i]);
  printf("\n");
}
} // namespace ZheCaoZuoZhenShiBaoZhaAAAA
int main() {
  ZheCaoZuoZhenShiBaoZhaAAAA::solve();
  return 0;
}