Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define ull unsigned long long
#define db long double
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define mp make_pair
#define all(x) (x).begin(), (x).end()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int N = 2e5 + 123, K = 18;
int n, p[N], a[2][N], mn[2][K][N], lg[N];
int ans[N], k;

int getmin(int c, int x, int y) {
  if (a[c][x] < a[c][y]) {
    return x;
  }
  return y;
}

int get(int c, int l, int r) {
  int len = lg[r - l + 1];
  return getmin(c, mn[c][len][l], mn[c][len][r - (1 << len) + 1]);
}

int m;
pii seg[N];

pii getpos(int x) {
  int l = seg[x].F, r = seg[x].S;
  int p1 = get(l & 1, l, r);
  int p2 = get((l & 1) ^ 1, p1 + 1, r);
  return {p1, p2};
}

int getans(int x) {
  int l = seg[x].F, r = seg[x].S;
  int p1 = get(l & 1, l, r);
  return p[p1];
}

void dijkstra(int L, int R) {
  seg[++m] = {L, R};
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push({getans(m), m});
  while (!pq.empty()) {
    pii cur = pq.top();
    pq.pop();

    int l = seg[cur.S].F, r = seg[cur.S].S;
    if (r - l + 1 >= 2) {
      pii pos = getpos(cur.S);
      ans[++k] = p[pos.F];
      ans[++k] = p[pos.S];

      seg[++m] = {l, pos.F - 1};
      pq.push({getans(m), m});
      seg[++m] = {pos.F + 1, pos.S - 1};
      pq.push({getans(m), m});
      seg[++m] = {pos.S + 1, r};
      pq.push({getans(m), m});
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  p[N - 1] = N;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
    a[i & 1][i] = p[i];
    a[(i & 1) ^ 1][i] = N;
    mn[0][0][i] = mn[1][0][i] = i;
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 1; j < K; j++) {
      for (int k = 1; k + (1 << j) - 1 <= n; k++) {
        mn[i][j][k] =
            getmin(i, mn[i][j - 1][k], mn[i][j - 1][k + (1 << (j - 1))]);
      }
    }
  }
  for (int i = 2; i < N; i++) {
    lg[i] = lg[i / 2] + 1;
  }
  dijkstra(1, n);
  for (int i = 1; i <= k; i++) {
    cout << ans[i] << ' ';
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define ull unsigned long long
#define db long double
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define mp make_pair
#define all(x) (x).begin(), (x).end()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int N = 2e5 + 123, K = 18;
int n, p[N], a[2][N], mn[2][K][N], lg[N];
int ans[N], k;

int getmin(int c, int x, int y) {
  if (a[c][x] < a[c][y]) {
    return x;
  }
  return y;
}

int get(int c, int l, int r) {
  int len = lg[r - l + 1];
  return getmin(c, mn[c][len][l], mn[c][len][r - (1 << len) + 1]);
}

int m;
pii seg[3 * N];

pii getpos(int x) {
  int l = seg[x].F, r = seg[x].S;
  int p1 = get(l & 1, l, r);
  int p2 = get((l & 1) ^ 1, p1 + 1, r);
  return {p1, p2};
}

int getans(int x) {
  int l = seg[x].F, r = seg[x].S;
  int p1 = get(l & 1, l, r);
  return p[p1];
}

void dijkstra(int L, int R) {
  seg[++m] = {L, R};
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push({getans(m), m});
  while (!pq.empty()) {
    pii cur = pq.top();
    pq.pop();

    int l = seg[cur.S].F, r = seg[cur.S].S;
    if (r - l + 1 >= 2) {
      pii pos = getpos(cur.S);
      ans[++k] = p[pos.F];
      ans[++k] = p[pos.S];

      seg[++m] = {l, pos.F - 1};
      pq.push({getans(m), m});
      seg[++m] = {pos.F + 1, pos.S - 1};
      pq.push({getans(m), m});
      seg[++m] = {pos.S + 1, r};
      pq.push({getans(m), m});
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  p[N - 1] = N;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
    a[i & 1][i] = p[i];
    a[(i & 1) ^ 1][i] = N;
    mn[0][0][i] = mn[1][0][i] = i;
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 1; j < K; j++) {
      for (int k = 1; k + (1 << j) - 1 <= n; k++) {
        mn[i][j][k] =
            getmin(i, mn[i][j - 1][k], mn[i][j - 1][k + (1 << (j - 1))]);
      }
    }
  }
  for (int i = 2; i < N; i++) {
    lg[i] = lg[i / 2] + 1;
  }
  dijkstra(1, n);
  for (int i = 1; i <= k; i++) {
    cout << ans[i] << ' ';
  }
}