Problem p03641 (C++)

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

#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define repd(i, r, l) for (register int i = r; i >= l; i--)
#define rvc(i, S) for (register int i = 0; i < (int)S.size(); i++)
#define rvcd(i, S) for (register int i = ((int)S.size()) - 1; i >= 0; i--)
#define fore(i, x) for (register int i = head[x]; i; i = e[i].next)
#define forup(i, l, r) for (register int i = l; i <= r; i += lowbit(i))
#define fordown(i, id) for (register int i = id; i; i -= lowbit(i))
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define lowbit(x) (x & (-x))

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pr;

const ll MAX = 2e18;
const ld inf = 2e18;
const int N = 3e6 + 10;
const int maxn = 200020;
const ll mod = 1e9 + 7;

int a[maxn], n, b[maxn], tot;
struct node {
  int x, y, l, r;
  bool operator<(node c) const { return a[x] > a[c.x]; }
};
priority_queue<node> heap;

namespace Seg {
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
const int M = 1e5 + 20;
int id[2][M << 2];

inline void update(int x) {
  rep(t, 0, 1) {
    if (a[id[t][ls(x)]] > a[id[t][rs(x)]])
      id[t][x] = id[t][rs(x)];
    else
      id[t][x] = id[t][ls(x)];
  }
}
void build(int x, int l, int r) {
  if (l == r) {
    id[l & 1][x] = l;
    return;
  }
  int mid = (l + r) >> 1;
  build(ls(x), l, mid), build(rs(x), mid + 1, r);
  update(x);
}
int query(int x, int l, int r, int L, int R, int t) {
  if (L > R)
    return 0;
  if (L <= l && R >= r)
    return id[t][x];
  int mid = (l + r) >> 1;
  int lres = 0, rres = 0;
  if (L <= mid)
    lres = query(ls(x), l, mid, L, R, t);
  if (R > mid)
    rres = query(rs(x), mid + 1, r, L, R, t);
  if (a[lres] < a[rres])
    return lres;
  return rres;
}
} // namespace Seg
using namespace Seg;

void init() {
  a[0] = n + 1;
  build(1, 1, n);
  int t = 1;
  int x = query(1, 1, n, 1, n, t);
  int y = query(1, 1, n, x + 1, n, t ^ 1);
  heap.push((node){x, y, 1, n});
}
void insert(int l, int r) {
  if (l > r)
    return;
  int t = l & 1;
  int x = query(1, 1, n, l, r, t);
  int y = query(1, 1, n, x + 1, r, t ^ 1);
  heap.push((node){x, y, l, r});
}
int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", &a[i]);
  init();
  while (heap.size()) {
    node cur = heap.top();
    heap.pop();
    int l = cur.l, r = cur.r, x = cur.x, y = cur.y;
    //	cout<<l<<" "<<r<<" "<<x<<" "<<y<<endl;
    //	system("pause");
    b[++tot] = a[x], b[++tot] = a[y];
    insert(l, x - 1);
    insert(x + 1, y - 1);
    insert(y + 1, r);
  }
  rep(i, 1, tot) printf("%d ", b[i]);
  puts("");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define repd(i, r, l) for (register int i = r; i >= l; i--)
#define rvc(i, S) for (register int i = 0; i < (int)S.size(); i++)
#define rvcd(i, S) for (register int i = ((int)S.size()) - 1; i >= 0; i--)
#define fore(i, x) for (register int i = head[x]; i; i = e[i].next)
#define forup(i, l, r) for (register int i = l; i <= r; i += lowbit(i))
#define fordown(i, id) for (register int i = id; i; i -= lowbit(i))
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define lowbit(x) (x & (-x))

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pr;

const ll MAX = 2e18;
const ld inf = 2e18;
const int N = 3e6 + 10;
const int maxn = 200020;
const ll mod = 1e9 + 7;

int a[maxn], n, b[maxn], tot;
struct node {
  int x, y, l, r;
  bool operator<(node c) const { return a[x] > a[c.x]; }
};
priority_queue<node> heap;

namespace Seg {
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
const int M = 2e5 + 20;
int id[2][M << 2];

inline void update(int x) {
  rep(t, 0, 1) {
    if (a[id[t][ls(x)]] > a[id[t][rs(x)]])
      id[t][x] = id[t][rs(x)];
    else
      id[t][x] = id[t][ls(x)];
  }
}
void build(int x, int l, int r) {
  if (l == r) {
    id[l & 1][x] = l;
    return;
  }
  int mid = (l + r) >> 1;
  build(ls(x), l, mid), build(rs(x), mid + 1, r);
  update(x);
}
int query(int x, int l, int r, int L, int R, int t) {
  if (L > R)
    return 0;
  if (L <= l && R >= r)
    return id[t][x];
  int mid = (l + r) >> 1;
  int lres = 0, rres = 0;
  if (L <= mid)
    lres = query(ls(x), l, mid, L, R, t);
  if (R > mid)
    rres = query(rs(x), mid + 1, r, L, R, t);
  if (a[lres] < a[rres])
    return lres;
  return rres;
}
} // namespace Seg
using namespace Seg;

void init() {
  a[0] = n + 1;
  build(1, 1, n);
  int t = 1;
  int x = query(1, 1, n, 1, n, t);
  int y = query(1, 1, n, x + 1, n, t ^ 1);
  heap.push((node){x, y, 1, n});
}
void insert(int l, int r) {
  if (l > r)
    return;
  int t = l & 1;
  int x = query(1, 1, n, l, r, t);
  int y = query(1, 1, n, x + 1, r, t ^ 1);
  heap.push((node){x, y, l, r});
}
int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", &a[i]);
  init();
  while (heap.size()) {
    node cur = heap.top();
    heap.pop();
    int l = cur.l, r = cur.r, x = cur.x, y = cur.y;
    //	cout<<l<<" "<<r<<" "<<x<<" "<<y<<endl;
    //	system("pause");
    b[++tot] = a[x], b[++tot] = a[y];
    insert(l, x - 1);
    insert(x + 1, y - 1);
    insert(y + 1, r);
  }
  rep(i, 1, tot) printf("%d ", b[i]);
  puts("");
}