Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <unordered_map>
#include <vector>
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define ll long long
#define INF 1000000000000000000
#define MOD 1000000007
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define uid uniform_int_distribution<int>
using namespace std;
// Read the question carefully and see all the given sample tests
// Think about the correctness before implementing

/*
 * 4 6 3 2 8 5 7 1
 *
 *
 * */
using tii = tuple<int, int, int, int>;
using PII = pair<int, int>;
const int N = 100 * 1000 + 5;
vector<int> A(N);
vector<PII> tree(4 * N, {MOD, MOD});
void build(int l, int r, int i) {
  if (l == r) {
    if (l % 2 == 1) {
      tree[i].ff = A[l];
    } else {
      tree[i].ss = A[l];
    }
    return;
  }
  int mid = (l + r) / 2;
  build(l, mid, 2 * i);
  build(mid + 1, r, 2 * i + 1);
  tree[i].ff = min(tree[2 * i].ff, tree[2 * i + 1].ff);
  tree[i].ss = min(tree[2 * i].ss, tree[2 * i + 1].ss);
}
PII query(int ql, int qr, int l, int r, int i) {
  if (ql > r || qr < l)
    return {MOD, MOD};
  if (ql <= l && r <= qr)
    return tree[i];
  int mid = (l + r) / 2;
  PII a = query(ql, qr, l, mid, 2 * i);
  PII b = query(ql, qr, mid + 1, r, 2 * i + 1);
  return {min(a.ff, b.ff), min(a.ss, b.ss)};
}
int main() {
  int n;
  cin >> n;
  vector<int> rm(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    rm[A[i]] = i;
  }
  build(1, n, 1);
  priority_queue<tii, vector<tii>, greater<tii>> pq;
  // pq.push(make_tuple(1, 1, 1, 1));
  auto ins = [&](int l, int r) {
    if (l % 2 == 1) {
      int val1 = query(l, r, 1, n, 1).ff;
      int val2 = query(rm[val1], r, 1, n, 1).ss;
      pq.push(make_tuple(val1, val2, l, r));
    } else {
      int val1 = query(l, r, 1, n, 1).ss;
      int val2 = query(rm[val1], r, 1, n, 1).ff;
      pq.push(make_tuple(val1, val2, l, r));
    }
  };
  auto decompose = [&](int l, int p1, int p2, int r) {
    if (l <= p1 - 1) {
      ins(l, p1 - 1);
    }
    if (p1 + 1 <= p2 - 1) {
      ins(p1 + 1, p2 - 1);
    }
    if (p2 + 1 <= r) {
      ins(p2 + 1, r);
    }
  };
  ins(1, n);
  vector<int> ans;
  while (!pq.empty()) {
    int val1, val2, l, r;
    tie(val1, val2, l, r) = pq.top();
    pq.pop();
    ans.push_back(val1);
    ans.push_back(val2);
    decompose(l, rm[val1], rm[val2], r);
  }
  for (auto g : ans)
    cout << g << " ";
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <unordered_map>
#include <vector>
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define ll long long
#define INF 1000000000000000000
#define MOD 1000000007
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define uid uniform_int_distribution<int>
using namespace std;
// Read the question carefully and see all the given sample tests
// Think about the correctness before implementing

/*
 * 4 6 3 2 8 5 7 1
 *
 *
 * */
using tii = tuple<int, int, int, int>;
using PII = pair<int, int>;
const int N = 200 * 1000 + 5;
vector<int> A(N);
vector<PII> tree(4 * N, {MOD, MOD});
void build(int l, int r, int i) {
  if (l == r) {
    if (l % 2 == 1) {
      tree[i].ff = A[l];
    } else {
      tree[i].ss = A[l];
    }
    return;
  }
  int mid = (l + r) / 2;
  build(l, mid, 2 * i);
  build(mid + 1, r, 2 * i + 1);
  tree[i].ff = min(tree[2 * i].ff, tree[2 * i + 1].ff);
  tree[i].ss = min(tree[2 * i].ss, tree[2 * i + 1].ss);
}
PII query(int ql, int qr, int l, int r, int i) {
  if (ql > r || qr < l)
    return {MOD, MOD};
  if (ql <= l && r <= qr)
    return tree[i];
  int mid = (l + r) / 2;
  PII a = query(ql, qr, l, mid, 2 * i);
  PII b = query(ql, qr, mid + 1, r, 2 * i + 1);
  return {min(a.ff, b.ff), min(a.ss, b.ss)};
}
int main() {
  int n;
  cin >> n;
  vector<int> rm(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    rm[A[i]] = i;
  }
  build(1, n, 1);
  priority_queue<tii, vector<tii>, greater<tii>> pq;
  // pq.push(make_tuple(1, 1, 1, 1));
  auto ins = [&](int l, int r) {
    if (l % 2 == 1) {
      int val1 = query(l, r, 1, n, 1).ff;
      int val2 = query(rm[val1], r, 1, n, 1).ss;
      pq.push(make_tuple(val1, val2, l, r));
    } else {
      int val1 = query(l, r, 1, n, 1).ss;
      int val2 = query(rm[val1], r, 1, n, 1).ff;
      pq.push(make_tuple(val1, val2, l, r));
    }
  };
  auto decompose = [&](int l, int p1, int p2, int r) {
    if (l <= p1 - 1) {
      ins(l, p1 - 1);
    }
    if (p1 + 1 <= p2 - 1) {
      ins(p1 + 1, p2 - 1);
    }
    if (p2 + 1 <= r) {
      ins(p2 + 1, r);
    }
  };
  ins(1, n);
  vector<int> ans;
  while (!pq.empty()) {
    int val1, val2, l, r;
    tie(val1, val2, l, r) = pq.top();
    pq.pop();
    ans.push_back(val1);
    ans.push_back(val2);
    decompose(l, rm[val1], rm[val2], r);
  }
  for (auto g : ans)
    cout << g << " ";
}