Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long LL;
struct node {
  int val;
  node *left, *right;
  node(int a, node *b, node *c) {
    val = a;
    left = b;
    right = c;
  }
};
node *create(int *arr, int L, int R) {
  if (L == R)
    return new node(arr[L], NULL, NULL);
  node *x = create(arr, L, (L + R) / 2);
  node *y = create(arr, (L + R) / 2 + 1, R);
  return new node(min(x->val, y->val), x, y);
}
int ask(node *p, int L, int R, int i, int j) {
  if (i > R || j < L)
    return 1e9;
  if (i <= L && R <= j)
    return p->val;
  int x = ask(p->left, L, (L + R) / 2, i, j);
  int y = ask(p->right, (L + R) / 2 + 1, R, i, j);
  return min(x, y);
}
void update(node *p, int L, int R, int dat, int to) {
  if (L > dat || R < dat)
    return;
  if (L == R) {
    p->val = to;
    return;
  }
  update(p->left, L, (L + R) / 2, dat, to);
  update(p->right, (L + R) / 2 + 1, R, dat, to);
  p->val = min(p->left->val, p->right->val);
}
int arr[100005];
int arr2[100005];
int mapper[100005];
struct chai {
  int val;
  int left, right;
  bool operator<(chai other) const { return val > other.val; }
  chai(int a, int b, int c) {
    val = a;
    left = b;
    right = c;
  }
};
node *root1, *root2;
int n;
int getMin(int a, int b) {
  if (a % 2)
    return ask(root1, 1, n, a, b);
  return ask(root2, 1, n, a, b);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", arr + i);
    mapper[arr[i]] = i;
    arr2[i] = 1e9;
  }
  for (int i = 2; i <= n; i += 2) {
    arr2[i] = arr[i];
    arr[i] = 1e9;
  }
  root1 = create(arr, 1, n);
  root2 = create(arr2, 1, n);
  vi res;
  priority_queue<chai> pq;
  pq.push(chai(getMin(1, n), 1, n));
  for (int i = 2; i <= n; i += 2) {
    chai here = pq.top();
    pq.pop();
    int x = here.val;
    int L = here.left, R = here.right;
    // printf("%d: [%d, %d]\n", x, L, R);
    int add = mapper[x];
    int y;
    if (L % 2)
      y = ask(root2, 1, n, add + 1, R);
    else
      y = ask(root1, 1, n, add + 1, R);
    // printf("%d\n", y);
    int add2 = mapper[y];
    res.pb(x);
    res.pb(y);
    if (L <= add - 1)
      pq.push(chai(getMin(L, add - 1), L, add - 1));
    if (add + 1 <= add2 - 1)
      pq.push(chai(getMin(add + 1, add2 - 1), add + 1, add2 - 1));
    if (add2 + 1 <= R)
      pq.push(chai(getMin(add2 + 1, R), add2 + 1, R));
  }
  for (auto x : res)
    printf("%d ", x);
  printf("\n");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long LL;
struct node {
  int val;
  node *left, *right;
  node(int a, node *b, node *c) {
    val = a;
    left = b;
    right = c;
  }
};
node *create(int *arr, int L, int R) {
  if (L == R)
    return new node(arr[L], NULL, NULL);
  node *x = create(arr, L, (L + R) / 2);
  node *y = create(arr, (L + R) / 2 + 1, R);
  return new node(min(x->val, y->val), x, y);
}
int ask(node *p, int L, int R, int i, int j) {
  if (i > R || j < L)
    return 1e9;
  if (i <= L && R <= j)
    return p->val;
  int x = ask(p->left, L, (L + R) / 2, i, j);
  int y = ask(p->right, (L + R) / 2 + 1, R, i, j);
  return min(x, y);
}
void update(node *p, int L, int R, int dat, int to) {
  if (L > dat || R < dat)
    return;
  if (L == R) {
    p->val = to;
    return;
  }
  update(p->left, L, (L + R) / 2, dat, to);
  update(p->right, (L + R) / 2 + 1, R, dat, to);
  p->val = min(p->left->val, p->right->val);
}
int arr[200005];
int arr2[200005];
int mapper[200005];
struct chai {
  int val;
  int left, right;
  bool operator<(chai other) const { return val > other.val; }
  chai(int a, int b, int c) {
    val = a;
    left = b;
    right = c;
  }
};
node *root1, *root2;
int n;
int getMin(int a, int b) {
  if (a % 2)
    return ask(root1, 1, n, a, b);
  return ask(root2, 1, n, a, b);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", arr + i);
    mapper[arr[i]] = i;
    arr2[i] = 1e9;
  }
  for (int i = 2; i <= n; i += 2) {
    arr2[i] = arr[i];
    arr[i] = 1e9;
  }
  root1 = create(arr, 1, n);
  root2 = create(arr2, 1, n);
  vi res;
  priority_queue<chai> pq;
  pq.push(chai(getMin(1, n), 1, n));
  for (int i = 2; i <= n; i += 2) {
    chai here = pq.top();
    pq.pop();
    int x = here.val;
    int L = here.left, R = here.right;
    // printf("%d: [%d, %d]\n", x, L, R);
    int add = mapper[x];
    int y;
    if (L % 2)
      y = ask(root2, 1, n, add + 1, R);
    else
      y = ask(root1, 1, n, add + 1, R);
    // printf("%d\n", y);
    int add2 = mapper[y];
    res.pb(x);
    res.pb(y);
    if (L <= add - 1)
      pq.push(chai(getMin(L, add - 1), L, add - 1));
    if (add + 1 <= add2 - 1)
      pq.push(chai(getMin(add + 1, add2 - 1), add + 1, add2 - 1));
    if (add2 + 1 <= R)
      pq.push(chai(getMin(add2 + 1, R), add2 + 1, R));
  }
  for (auto x : res)
    printf("%d ", x);
  printf("\n");
  return 0;
}