Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <queue>

using namespace std;

const int MAX_N = 1e5 + 10;

int n;
int p[MAX_N];
int id[MAX_N];
int tree[2][MAX_N * 4];

int ans[MAX_N];

priority_queue<pair<pair<int, int>, pair<int, int>>> pq;

void update(int pa, int idx, int l, int r, int q, int u) {
  if (l == r) {
    tree[pa][idx] = u;
    return;
  }

  int mid = (l + r) >> 1;

  if (q <= mid)
    update(pa, idx * 2, l, mid, q, u);
  else
    update(pa, idx * 2 + 1, mid + 1, r, q, u);

  if (tree[pa][idx * 2] < tree[pa][idx * 2 + 1])
    tree[pa][idx] = tree[pa][idx * 2];
  else
    tree[pa][idx] = tree[pa][idx * 2 + 1];
}

int find(int pa, int idx, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr)
    return tree[pa][idx];

  int mid = (l + r) >> 1;

  int la, ra;

  if (ql <= mid)
    la = find(pa, idx * 2, l, mid, ql, qr);
  else
    la = (1 << 30);

  if (mid < qr)
    ra = find(pa, idx * 2 + 1, mid + 1, r, ql, qr);
  else
    ra = (1 << 30);

  return (la < ra) ? la : ra;
}

pair<int, int> get(int l, int r) {
  pair<int, int> f;
  f.first = -find(l & 1, 1, 1, n, l, r);
  f.second = -find((l & 1) ^ 1, 1, 1, n, id[-f.first], r);
  return f;
}

void add(int l, int r) {
  if (r < l)
    return;
  pair<int, int> f = get(l, r);
  /// printf ("%d %d = %d %d\n", l, r, -f.first, -f.second);

  pq.push({f, {l, r}});
}

int main() {
  int i;

  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    id[p[i]] = i;
    update(i & 1, 1, 1, n, i, p[i]);
    update((i & 1) ^ 1, 1, 1, n, i, (1 << 30));
  }

  pq.push({{get(1, n)}, {1, n}});

  for (i = 0; i < n; i += 2) {
    pair<pair<int, int>, pair<int, int>> pqt = pq.top();
    pair<int, int> can = pqt.first;
    pair<int, int> itr = pqt.second;

    pq.pop();

    /// printf ("%d %d %d %d\n", -can.first, -can.second, itr.first,
    /// itr.second);

    ans[i] = -can.first;
    ans[i + 1] = -can.second;

    int x = id[ans[i]], y = id[ans[i + 1]];

    add(itr.first, x - 1);
    add(x + 1, y - 1);
    add(y + 1, itr.second);
  }

  for (i = 0; i < n; i++)
    printf("%d ", ans[i]);
  printf("\n");

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <queue>

using namespace std;

const int MAX_N = 2e5 + 10;

int n;
int p[MAX_N];
int id[MAX_N];
int tree[2][MAX_N * 4];

int ans[MAX_N];

priority_queue<pair<pair<int, int>, pair<int, int>>> pq;

void update(int pa, int idx, int l, int r, int q, int u) {
  if (l == r) {
    tree[pa][idx] = u;
    return;
  }

  int mid = (l + r) >> 1;

  if (q <= mid)
    update(pa, idx * 2, l, mid, q, u);
  else
    update(pa, idx * 2 + 1, mid + 1, r, q, u);

  if (tree[pa][idx * 2] < tree[pa][idx * 2 + 1])
    tree[pa][idx] = tree[pa][idx * 2];
  else
    tree[pa][idx] = tree[pa][idx * 2 + 1];
}

int find(int pa, int idx, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr)
    return tree[pa][idx];

  int mid = (l + r) >> 1;

  int la, ra;

  if (ql <= mid)
    la = find(pa, idx * 2, l, mid, ql, qr);
  else
    la = (1 << 30);

  if (mid < qr)
    ra = find(pa, idx * 2 + 1, mid + 1, r, ql, qr);
  else
    ra = (1 << 30);

  return (la < ra) ? la : ra;
}

pair<int, int> get(int l, int r) {
  pair<int, int> f;
  f.first = -find(l & 1, 1, 1, n, l, r);
  f.second = -find((l & 1) ^ 1, 1, 1, n, id[-f.first], r);
  return f;
}

void add(int l, int r) {
  if (r < l)
    return;
  pair<int, int> f = get(l, r);
  /// printf ("%d %d = %d %d\n", l, r, -f.first, -f.second);

  pq.push({f, {l, r}});
}

int main() {
  int i;

  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    id[p[i]] = i;
    update(i & 1, 1, 1, n, i, p[i]);
    update((i & 1) ^ 1, 1, 1, n, i, (1 << 30));
  }

  pq.push({{get(1, n)}, {1, n}});

  for (i = 0; i < n; i += 2) {
    pair<pair<int, int>, pair<int, int>> pqt = pq.top();
    pair<int, int> can = pqt.first;
    pair<int, int> itr = pqt.second;

    pq.pop();

    /// printf ("%d %d %d %d\n", -can.first, -can.second, itr.first,
    /// itr.second);

    ans[i] = -can.first;
    ans[i + 1] = -can.second;

    int x = id[ans[i]], y = id[ans[i + 1]];

    add(itr.first, x - 1);
    add(x + 1, y - 1);
    add(y + 1, itr.second);
  }

  for (i = 0; i < n; i++)
    printf("%d ", ans[i]);
  printf("\n");

  return 0;
}