Problem p03641 (C++)

Buggy submission (status: Runtime Error):
// This is getting accepted!
//  I HATE BUG
//  God Of The Bugs
//  12/11/2016
#include <bits/stdc++.h>

using namespace std;

#define ms(s, n) memset(s, n, sizeof(s))
#define FI first
#define SE second
#define pb push_back
#define mp make_pair
#define ll long long
#define sz(a) ((int)(a).size())
#define __builtin_popcount __builtin_popcounll
#define ld long double

typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> ppi;

const double PI = acos(0) * 2;
const double EPS = 1e-8;
const ll MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;
const int oo = 1e9;
const double foo = 1e30;

template <class T> int getbit(T s, int i) { return (s >> i) & 1; }
template <class T> T onbit(T s, int i) { return s | (T(1) << i); }
template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template <class T> int cntbit(T s) { return __builtin_popcounll(s); }
template <class T> T sqr(T x) { return x * x; }

inline void addmod(int &a, int val, int p = MOD) {
  if ((a = (a + val)) >= p)
    a -= p;
}
inline void submod(int &a, int val, int p = MOD) {
  if ((a = (a - val)) < 0)
    a += p;
}
inline int mult(int a, int b, int p = MOD) { return (ll)a * b % p; }

int M[2][19][MAXN], a[MAXN], pos[MAXN], n;
vector<int> v[2];
priority_queue<pair<int, pii>> st;

int getMin(int t, int u, int v) {
  int k = log2(v - u + 1);
  return min(M[t][u][k], M[t][v - (1 << k) + 1][k]);
}

int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("inp.txt", "r", stdin);
  ////    freopen("out.txt", "w", stdout);
  // #endif

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    pos[a[i]] = i;
  }

  for (int i = 0; i < n; i++) {
    v[i % 2].pb(a[i]);
  }

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < sz(v[i]); j++) {
      M[i][j][0] = v[i][j];
    }
    for (int k = 1; (1 << k) <= sz(v[i]); k++) {
      for (int j = 0; j + (1 << k) - 1 < sz(v[i]); j++) {
        M[i][j][k] = min(M[i][j][k - 1], M[i][j + (1 << (k - 1))][k - 1]);
      }
    }
  }

  st.push(mp(-getMin(0, 0, (n - 1) / 2), mp(0, n - 1)));

  while (!st.empty()) {
    pair<int, pii> cur = st.top();
    st.pop();
    cur.FI = -cur.FI;

    int L = cur.SE.FI, R = cur.SE.SE;

    int now = pos[cur.FI];

    int gg = getMin((now % 2) ^ 1, (now + 1) / 2, R / 2);

    int nxt = pos[gg];

    printf("%d %d ", cur.FI, gg);

    if (now + 1 < nxt - 1) {
      gg = getMin((now + 1) % 2, (now + 1) / 2, (nxt - 2) / 2);
      st.push(mp(-gg, mp(now + 1, nxt - 1)));
    }
    if (L < now - 1) {
      gg = getMin(L % 2, L / 2, (now - 2) / 2);
      st.push(mp(-gg, mp(L, now - 1)));
    }
    if (nxt + 1 < R) {
      gg = getMin((nxt + 1) % 2, (nxt + 1) / 2, (R - 1) / 2);
      st.push(mp(-gg, mp(nxt + 1, R)));
    }
  }

  return 0;
}


Fixed submission:
// This is getting accepted!
//  I HATE BUG
//  God Of The Bugs
//  12/11/2016
#include <bits/stdc++.h>

using namespace std;

#define ms(s, n) memset(s, n, sizeof(s))
#define FI first
#define SE second
#define pb push_back
#define mp make_pair
#define ll long long
#define sz(a) ((int)(a).size())
#define __builtin_popcount __builtin_popcounll
#define ld long double

typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> ppi;

const double PI = acos(0) * 2;
const double EPS = 1e-8;
const ll MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;
const int oo = 1e9;
const double foo = 1e30;

template <class T> int getbit(T s, int i) { return (s >> i) & 1; }
template <class T> T onbit(T s, int i) { return s | (T(1) << i); }
template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template <class T> int cntbit(T s) { return __builtin_popcounll(s); }
template <class T> T sqr(T x) { return x * x; }

inline void addmod(int &a, int val, int p = MOD) {
  if ((a = (a + val)) >= p)
    a -= p;
}
inline void submod(int &a, int val, int p = MOD) {
  if ((a = (a - val)) < 0)
    a += p;
}
inline int mult(int a, int b, int p = MOD) { return (ll)a * b % p; }

int M[2][MAXN][19], a[MAXN], pos[MAXN], n;
vector<int> v[2];
priority_queue<pair<int, pii>> st;

int getMin(int t, int u, int v) {
  int k = log2(v - u + 1);
  return min(M[t][u][k], M[t][v - (1 << k) + 1][k]);
}

int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("inp.txt", "r", stdin);
  ////    freopen("out.txt", "w", stdout);
  // #endif

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    pos[a[i]] = i;
  }

  for (int i = 0; i < n; i++) {
    v[i % 2].pb(a[i]);
  }

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < sz(v[i]); j++) {
      M[i][j][0] = v[i][j];
    }
    for (int k = 1; (1 << k) <= sz(v[i]); k++) {
      for (int j = 0; j + (1 << k) - 1 < sz(v[i]); j++) {
        M[i][j][k] = min(M[i][j][k - 1], M[i][j + (1 << (k - 1))][k - 1]);
      }
    }
  }

  st.push(mp(-getMin(0, 0, (n - 1) / 2), mp(0, n - 1)));

  while (!st.empty()) {
    pair<int, pii> cur = st.top();
    st.pop();
    cur.FI = -cur.FI;

    int L = cur.SE.FI, R = cur.SE.SE;

    int now = pos[cur.FI];

    int gg = getMin((now % 2) ^ 1, (now + 1) / 2, R / 2);

    int nxt = pos[gg];

    printf("%d %d ", cur.FI, gg);

    if (now + 1 < nxt - 1) {
      gg = getMin((now + 1) % 2, (now + 1) / 2, (nxt - 2) / 2);
      st.push(mp(-gg, mp(now + 1, nxt - 1)));
    }
    if (L < now - 1) {
      gg = getMin(L % 2, L / 2, (now - 2) / 2);
      st.push(mp(-gg, mp(L, now - 1)));
    }
    if (nxt + 1 < R) {
      gg = getMin((nxt + 1) % 2, (nxt + 1) / 2, (R - 1) / 2);
      st.push(mp(-gg, mp(nxt + 1, R)));
    }
  }

  return 0;
}