Problem p03641 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int n, A[200050];
typedef pair<int, int> pi;
typedef pair<pi, pi> ppi;
pi stOdd[200050][20], stEven[200050][20];
priority_queue<ppi, vector<ppi>, greater<ppi>> pq;
pi mmin(pi a, pi b) { return a.first < b.first ? a : b; }
int precomp() {
  for (int i = 0; i < n; i++)
    if (i % 2 == 0)
      stEven[i][0] = pi(A[i], i), stOdd[i][0] = pi(INT_MAX, 0);
    else
      stEven[i][0] = pi(INT_MAX, 0), stOdd[i][0] = pi(A[i], i);
  for (int h = 1; h < 15; h++) {
    for (int i = 0; i + (1 << h) <= n; i++) {
      stEven[i][h] = mmin(stEven[i][h - 1], stEven[i + (1 << (h - 1))][h - 1]);
      stOdd[i][h] = mmin(stOdd[i][h - 1], stOdd[i + (1 << (h - 1))][h - 1]);
    }
  }
}
pi rmqOdd(int x, int y) { // [x,y]
  int k = 31 - __builtin_clz(y - x + 1);
  return mmin(stOdd[x][k], stOdd[y - (1 << k) + 1][k]);
}
pi rmqEven(int x, int y) {
  int k = 31 - __builtin_clz(y - x + 1);
  return mmin(stEven[x][k], stEven[y - (1 << k) + 1][k]);
}
pi rmq(int x, int y, bool odd) { return odd ? rmqOdd(x, y) : rmqEven(x, y); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  precomp();
  pq.push(ppi(rmq(0, n - 1, 0), pi(0, n - 1)));
  while (!pq.empty()) {
    cout << pq.top().first.first << " ";
    int x = pq.top().second.first, y = pq.top().second.second;
    int p = pq.top().first.second;
    pq.pop();
    pi cur = rmq(p + 1, y, 1 - x % 2);
    int a = cur.first, b = cur.second;
    cout << a << " ";
    // [x, p-1], [p+1, b-1], [b+1, y]
    //		printf("[%d, %d], [%d, %d], [%d, %d]\n", x, p-1, p+1, b-1, b+1,
    //y);
    if (x < p - 1)
      pq.push(ppi(rmq(x, p - 1, x % 2), pi(x, p - 1)));
    if (p + 1 < b - 1)
      pq.push(ppi(rmq(p + 1, b - 1, (p + 1) % 2), pi(p + 1, b - 1)));
    if (b + 1 < y)
      pq.push(ppi(rmq(b + 1, y, (b + 1) % 2), pi(b + 1, y)));
  }
}


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

using namespace std;

int n, A[200050];
typedef pair<int, int> pi;
typedef pair<pi, pi> ppi;
pi stOdd[200050][20], stEven[200050][20];
priority_queue<ppi, vector<ppi>, greater<ppi>> pq;
pi mmin(pi a, pi b) { return a.first < b.first ? a : b; }
int precomp() {
  for (int i = 0; i < n; i++)
    if (i % 2 == 0)
      stEven[i][0] = pi(A[i], i), stOdd[i][0] = pi(INT_MAX, 0);
    else
      stEven[i][0] = pi(INT_MAX, 0), stOdd[i][0] = pi(A[i], i);
  for (int h = 1; h < 18; h++) {
    for (int i = 0; i + (1 << h) <= n; i++) {
      stEven[i][h] = mmin(stEven[i][h - 1], stEven[i + (1 << (h - 1))][h - 1]);
      stOdd[i][h] = mmin(stOdd[i][h - 1], stOdd[i + (1 << (h - 1))][h - 1]);
    }
  }
}
pi rmqOdd(int x, int y) { // [x,y]
  int k = 31 - __builtin_clz(y - x + 1);
  return mmin(stOdd[x][k], stOdd[y - (1 << k) + 1][k]);
}
pi rmqEven(int x, int y) {
  int k = 31 - __builtin_clz(y - x + 1);
  return mmin(stEven[x][k], stEven[y - (1 << k) + 1][k]);
}
pi rmq(int x, int y, bool odd) { return odd ? rmqOdd(x, y) : rmqEven(x, y); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  precomp();
  pq.push(ppi(rmq(0, n - 1, 0), pi(0, n - 1)));
  while (!pq.empty()) {
    cout << pq.top().first.first << " ";
    int x = pq.top().second.first, y = pq.top().second.second;
    int p = pq.top().first.second;
    pq.pop();
    pi cur = rmq(p + 1, y, 1 - x % 2);
    int a = cur.first, b = cur.second;
    cout << a << " ";
    // [x, p-1], [p+1, b-1], [b+1, y]
    //		printf("[%d, %d], [%d, %d], [%d, %d]\n", x, p-1, p+1, b-1, b+1,
    //y);
    if (x < p - 1)
      pq.push(ppi(rmq(x, p - 1, x % 2), pi(x, p - 1)));
    if (p + 1 < b - 1)
      pq.push(ppi(rmq(p + 1, b - 1, (p + 1) % 2), pi(p + 1, b - 1)));
    if (b + 1 < y)
      pq.push(ppi(rmq(b + 1, y, (b + 1) % 2), pi(b + 1, y)));
  }
}