Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define ALL(x) (x).begin(), (x).end()
#define UNQ(x) (x).erase(unique(ALL(x)), (x).end())
#define lwb(x, y) lower_bound(ALL(x), (y)) - (x).begin()
#define upb(x, y) upper_bound(ALL(x), (y)) - (x).begin()
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;

int n, m, arr[100010];
ii st[400010];

struct ev {
  int val, x, y, ok;
  bool operator<(const ev &a) const {
    if (a.ok != ok)
      return a.ok < ok;
    return arr[a.val] < arr[val];
  }
};

priority_queue<ev> djk;

void build(int node, int f, int l) {
  if (f == l) {
    if (f & 1)
      st[node] = {n + 1, f};
    else
      st[node] = {l, n + 1};
    return;
  }
  build(2 * node, f, (f + l) / 2);
  build(2 * node + 1, (f + l) / 2 + 1, l);
  if (arr[st[2 * node + 1].fi] < arr[st[2 * node].fi])
    st[node].fi = st[2 * node + 1].fi;
  else
    st[node].fi = st[2 * node].fi;
  if (arr[st[2 * node + 1].se] < arr[st[2 * node].se])
    st[node].se = st[2 * node + 1].se;
  else
    st[node].se = st[2 * node].se;
}

ii qr(int node, int f, int l, int x, int y) {
  if (f > y || l < x)
    return {n + 1, n + 1};
  if (x <= f && l <= y)
    return st[node];
  ii r1 = qr(2 * node, f, (f + l) / 2, x, y),
     r2 = qr(2 * node + 1, (f + l) / 2 + 1, l, x, y), res;

  if (arr[r1.fi] < arr[r2.fi])
    res.fi = r1.fi;
  else
    res.fi = r2.fi;
  if (arr[r1.se] < arr[r2.se])
    res.se = r1.se;
  else
    res.se = r2.se;
  return res;
}

void find_ans(vii &srt) {
  djk.push({0, 1, n, 0});
  int f, l, mrk, par;
  set<ev>::iterator it;
  ev tmp;
  while (!djk.empty()) {

    tmp = djk.top();
    djk.pop();

    f = tmp.x;
    l = tmp.y;
    mrk = tmp.ok;
    par = tmp.val;
    if (f > l)
      continue;
    // printf("%d %d %d %d\n" , f , l , mrk , par) ;
    ii rs;
    rs = qr(1, 1, n, f, l);
    int use, p;
    if (mrk) {
      if (f & 1)
        use = rs.se;
      else
        use = rs.fi;
      srt.push_back(ii(arr[par], arr[use]));
      // printf("%d %d\n" , arr[par] , arr[use] ) ;
      djk.push({0, f, use - 1, 0});
      djk.push({0, use + 1, l, 0});
    } else {
      if (f & 1)
        use = rs.se;
      else
        use = rs.fi;
      p = use;
      djk.push({0, f, use - 1, 0});
      djk.push({p, use + 1, l, 1});
    }
  }
}

int main() {
  // freopen("in.txt" , "r" , stdin);
  // freopen("out.txt" , "w" , stdout) ;
  scanf("%d", &n);
  arr[n + 1] = 1e9;
  for (int i = 1; i <= n; i++) {
    scanf("%d", arr + i);
  }
  build(1, 1, n);
  vii res;
  res.clear();
  find_ans(res);
  for (ii v : res)
    printf("%d %d ", v.fi, v.se);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define ALL(x) (x).begin(), (x).end()
#define UNQ(x) (x).erase(unique(ALL(x)), (x).end())
#define lwb(x, y) lower_bound(ALL(x), (y)) - (x).begin()
#define upb(x, y) upper_bound(ALL(x), (y)) - (x).begin()
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;

int n, m, arr[200010];
ii st[800010];

struct ev {
  int val, x, y, ok;
  bool operator<(const ev &a) const {
    if (a.ok != ok)
      return a.ok < ok;
    return arr[a.val] < arr[val];
  }
};

priority_queue<ev> djk;

void build(int node, int f, int l) {
  if (f == l) {
    if (f & 1)
      st[node] = {n + 1, f};
    else
      st[node] = {l, n + 1};
    return;
  }
  build(2 * node, f, (f + l) / 2);
  build(2 * node + 1, (f + l) / 2 + 1, l);
  if (arr[st[2 * node + 1].fi] < arr[st[2 * node].fi])
    st[node].fi = st[2 * node + 1].fi;
  else
    st[node].fi = st[2 * node].fi;
  if (arr[st[2 * node + 1].se] < arr[st[2 * node].se])
    st[node].se = st[2 * node + 1].se;
  else
    st[node].se = st[2 * node].se;
}

ii qr(int node, int f, int l, int x, int y) {
  if (f > y || l < x)
    return {n + 1, n + 1};
  if (x <= f && l <= y)
    return st[node];
  ii r1 = qr(2 * node, f, (f + l) / 2, x, y),
     r2 = qr(2 * node + 1, (f + l) / 2 + 1, l, x, y), res;

  if (arr[r1.fi] < arr[r2.fi])
    res.fi = r1.fi;
  else
    res.fi = r2.fi;
  if (arr[r1.se] < arr[r2.se])
    res.se = r1.se;
  else
    res.se = r2.se;
  return res;
}

void find_ans(vii &srt) {
  djk.push({0, 1, n, 0});
  int f, l, mrk, par;
  set<ev>::iterator it;
  ev tmp;
  while (!djk.empty()) {

    tmp = djk.top();
    djk.pop();

    f = tmp.x;
    l = tmp.y;
    mrk = tmp.ok;
    par = tmp.val;
    if (f > l)
      continue;
    // printf("%d %d %d %d\n" , f , l , mrk , par) ;
    ii rs;
    rs = qr(1, 1, n, f, l);
    int use, p;
    if (mrk) {
      if (f & 1)
        use = rs.se;
      else
        use = rs.fi;
      srt.push_back(ii(arr[par], arr[use]));
      // printf("%d %d\n" , arr[par] , arr[use] ) ;
      djk.push({0, f, use - 1, 0});
      djk.push({0, use + 1, l, 0});
    } else {
      if (f & 1)
        use = rs.se;
      else
        use = rs.fi;
      p = use;
      djk.push({0, f, use - 1, 0});
      djk.push({p, use + 1, l, 1});
    }
  }
}

int main() {
  // freopen("in.txt" , "r" , stdin);
  // freopen("out.txt" , "w" , stdout) ;
  scanf("%d", &n);
  arr[n + 1] = 1e9;
  for (int i = 1; i <= n; i++) {
    scanf("%d", arr + i);
  }
  build(1, 1, n);
  vii res;
  res.clear();
  find_ans(res);
  for (ii v : res)
    printf("%d %d ", v.fi, v.se);
  return 0;
}