Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7; // 998244353;// ;
constexpr long long INF = numeric_limits<LL>::max() / 2;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourd;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
struct Edge {
  LL to, cost;
};
struct edge {
  LL from, to, cost;
};
vector<vector<LL>> g;
vector<edge> edges;
vector<LL> v;
map<LL, LL> ma;
set<Pll> st;
LL h, w, n, m, k, t, s, p, q, last, cnt, sum, ans[210000], dp[210000],
    a[210000], b[210000];
string str, ss;
bool f;
char c;
struct Seg {
  LL n;
  vector<LL> node;
  Seg(LL n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    node.resize(n * 2 - 1);
    rep(i, n * 2 - 1) node[i] = INF;
  }
  void update(LL i, LL x) {
    i = i + n - 1;
    node[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
    }
  }
  LL find(LL l, LL r) { return find(0, 0, n - 1, l, r); }
  LL find(LL i, LL a, LL b, LL l, LL r) {
    if (b < l || r < a)
      return INF;
    if (l <= a && b <= r)
      return node[i];
    return min(find(i * 2 + 1, a, (a + b) / 2, l, r),
               find(i * 2 + 2, (a + b + 1) / 2, b, l, r));
  }
};
Seg seg1(210000), seg2(8);
Pll node[210000];
int main() {
  cin >> n;
  g.resize(n / 2);
  rep(i, n) {
    cin >> a[i];
    b[a[i]] = i;
    if (i % 2 == 0)
      seg1.update(i, a[i]);
    else
      seg2.update(i, a[i]);
  }
  queue<Ppll> qu;
  qu.push(Ppll(0, Pll(n - 1, -1)));
  while (!qu.empty()) {
    Pll cur = Pll(qu.front().first, qu.front().second.first);
    LL u = qu.front().second.second;
    qu.pop();
    LL l, r;
    if (cur.first % 2 == 0) {
      l = seg1.find(cur.first, cur.second);
      r = seg2.find(b[l], cur.second);
    } else {
      l = seg2.find(cur.first, cur.second);
      r = seg1.find(b[l], cur.second);
    }
    node[cnt] = Pll(l, r);
    if (u != -1) {
      g[u].push_back(cnt);
    }
    l = b[l];
    r = b[r];
    if (cur.first != l)
      qu.push(Ppll(cur.first, Pll(l - 1, cnt)));
    if (l + 1 < r - 1)
      qu.push(Ppll(l + 1, Pll(r - 1, cnt)));
    if (cur.second != r)
      qu.push(Ppll(r + 1, Pll(cur.second, cnt)));
    cnt++;
  }
  priority_queue<Pll, vector<Pll>, greater<Pll>> pq;
  pq.push(Pll(node[0].first, 0));
  vector<LL> V;
  while (!pq.empty()) {
    Pll cur = pq.top();
    LL v = cur.second;
    pq.pop();
    V.push_back(node[v].first);
    V.push_back(node[v].second);
    rep(i, g[v].size()) { pq.push(Pll(node[g[v][i]].first, g[v][i])); }
  }
  rep(i, n) {
    if (i)
      cout << " ";
    cout << V[i];
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7; // 998244353;// ;
constexpr long long INF = numeric_limits<LL>::max() / 2;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourd;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
struct Edge {
  LL to, cost;
};
struct edge {
  LL from, to, cost;
};
vector<vector<LL>> g;
vector<edge> edges;
vector<LL> v;
map<LL, LL> ma;
set<Pll> st;
LL h, w, n, m, k, t, s, p, q, last, cnt, sum, ans[210000], dp[210000],
    a[210000], b[210000];
string str, ss;
bool f;
char c;
struct Seg {
  LL n;
  vector<LL> node;
  Seg(LL n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    node.resize(n * 2 - 1);
    rep(i, n * 2 - 1) node[i] = INF;
  }
  void update(LL i, LL x) {
    i = i + n - 1;
    node[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
    }
  }
  LL find(LL l, LL r) { return find(0, 0, n - 1, l, r); }
  LL find(LL i, LL a, LL b, LL l, LL r) {
    if (b < l || r < a)
      return INF;
    if (l <= a && b <= r)
      return node[i];
    return min(find(i * 2 + 1, a, (a + b) / 2, l, r),
               find(i * 2 + 2, (a + b + 1) / 2, b, l, r));
  }
};
Seg seg1(210000), seg2(210000);
Pll node[210000];
int main() {
  cin >> n;
  g.resize(n / 2);
  rep(i, n) {
    cin >> a[i];
    b[a[i]] = i;
    if (i % 2 == 0)
      seg1.update(i, a[i]);
    else
      seg2.update(i, a[i]);
  }
  queue<Ppll> qu;
  qu.push(Ppll(0, Pll(n - 1, -1)));
  while (!qu.empty()) {
    Pll cur = Pll(qu.front().first, qu.front().second.first);
    LL u = qu.front().second.second;
    qu.pop();
    LL l, r;
    if (cur.first % 2 == 0) {
      l = seg1.find(cur.first, cur.second);
      r = seg2.find(b[l], cur.second);
    } else {
      l = seg2.find(cur.first, cur.second);
      r = seg1.find(b[l], cur.second);
    }
    node[cnt] = Pll(l, r);
    if (u != -1) {
      g[u].push_back(cnt);
    }
    l = b[l];
    r = b[r];
    if (cur.first != l)
      qu.push(Ppll(cur.first, Pll(l - 1, cnt)));
    if (l + 1 < r - 1)
      qu.push(Ppll(l + 1, Pll(r - 1, cnt)));
    if (cur.second != r)
      qu.push(Ppll(r + 1, Pll(cur.second, cnt)));
    cnt++;
  }
  priority_queue<Pll, vector<Pll>, greater<Pll>> pq;
  pq.push(Pll(node[0].first, 0));
  vector<LL> V;
  while (!pq.empty()) {
    Pll cur = pq.top();
    LL v = cur.second;
    pq.pop();
    V.push_back(node[v].first);
    V.push_back(node[v].second);
    rep(i, g[v].size()) { pq.push(Pll(node[g[v][i]].first, g[v][i])); }
  }
  rep(i, n) {
    if (i)
      cout << " ";
    cout << V[i];
  }
  cout << endl;
  return 0;
}