Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>

#define pb push_back
#define mp make_pair
#define taskname "A"

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int n, a[maxn];
struct IT {
  ii s[maxn * 4];
  void Build(int x, int l, int r, bool delta) {
    if (l == r) {
      s[x] = mp(a[l], l);
      if ((l & 1) != delta)
        s[x].first = 1e9;
      return;
    }
    int mid = l + r >> 1;
    Build(x * 2, l, mid, delta);
    Build(x * 2 + 1, mid + 1, r, delta);
    s[x] = min(s[x * 2], s[x * 2 + 1]);
  }
  ii Query(int x, int l, int r, int L, int R) {
    if (L > r || l > R) {
      return mp(1e9, 0);
    }
    if (L <= l && r <= R)
      return s[x];
    int mid = l + r >> 1;
    return min(Query(x * 2, l, mid, L, R), Query(x * 2 + 1, mid + 1, r, L, R));
  }
} s[2];
int id[maxn];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen(taskname ".INP", "r")) {
    freopen(taskname ".INP", "r", stdin);
    freopen(taskname ".OUT", "w  ", stdout);
  }
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    // cout << a[i] << endl;
    id[a[i]] = i;
  }
  s[0].Build(1, 1, n, 0);
  s[1].Build(1, 1, n, 1);
  // return 0;
  function<pair<ii, ii>(int, int)> Cal = [&](int l, int r) {
    assert(!((r - l + 1) & 1));
    if (l > r)
      return mp(mp((int)1e9, (int)1e9), mp(l, r));
    auto tmp = s[(l & 1)].Query(1, 1, n, l, r);
    return mp(mp(tmp.first, s[!(l & 1)].Query(1, 1, n, tmp.second, r).first),
              mp(l, r));
  };
  priority_queue<pair<ii, ii>, vector<pair<ii, ii>>, greater<pair<ii, ii>>> pq;
  pq.push(Cal(1, n));
  for (int i = 1; i <= n / 2; ++i) {
    auto u = pq.top().second;
    auto v = pq.top().first;
    pq.pop();
    cout << v.first << " " << v.second << " ";
    pq.push(Cal(u.first, id[v.first] - 1));
    pq.push(Cal(id[v.first] + 1, id[v.second] - 1));
    pq.push(Cal(id[v.second] + 1, u.second));
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>

#define pb push_back
#define mp make_pair
#define taskname "A"

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
int n, a[maxn];
struct IT {
  ii s[maxn * 4];
  void Build(int x, int l, int r, bool delta) {
    if (l == r) {
      s[x] = mp(a[l], l);
      if ((l & 1) != delta)
        s[x].first = 1e9;
      return;
    }
    int mid = l + r >> 1;
    Build(x * 2, l, mid, delta);
    Build(x * 2 + 1, mid + 1, r, delta);
    s[x] = min(s[x * 2], s[x * 2 + 1]);
  }
  ii Query(int x, int l, int r, int L, int R) {
    if (L > r || l > R) {
      return mp(1e9, 0);
    }
    if (L <= l && r <= R)
      return s[x];
    int mid = l + r >> 1;
    return min(Query(x * 2, l, mid, L, R), Query(x * 2 + 1, mid + 1, r, L, R));
  }
} s[2];
int id[maxn];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen(taskname ".INP", "r")) {
    freopen(taskname ".INP", "r", stdin);
    freopen(taskname ".OUT", "w  ", stdout);
  }
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    // cout << a[i] << endl;
    id[a[i]] = i;
  }
  s[0].Build(1, 1, n, 0);
  s[1].Build(1, 1, n, 1);
  // return 0;
  function<pair<ii, ii>(int, int)> Cal = [&](int l, int r) {
    assert(!((r - l + 1) & 1));
    if (l > r)
      return mp(mp((int)1e9, (int)1e9), mp(l, r));
    auto tmp = s[(l & 1)].Query(1, 1, n, l, r);
    return mp(mp(tmp.first, s[!(l & 1)].Query(1, 1, n, tmp.second, r).first),
              mp(l, r));
  };
  priority_queue<pair<ii, ii>, vector<pair<ii, ii>>, greater<pair<ii, ii>>> pq;
  pq.push(Cal(1, n));
  for (int i = 1; i <= n / 2; ++i) {
    auto u = pq.top().second;
    auto v = pq.top().first;
    pq.pop();
    cout << v.first << " " << v.second << " ";
    pq.push(Cal(u.first, id[v.first] - 1));
    pq.push(Cal(id[v.first] + 1, id[v.second] - 1));
    pq.push(Cal(id[v.second] + 1, u.second));
  }
}