Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define cst()                                                                  \
  int __t;                                                                     \
  scanf("%d", &__t);                                                           \
  while (__t--)
#define rep(i, s, t) for (int i = s; i < t; ++i)
#define repb(i, s, t) for (int i = s; i <= t; ++i)
#define lepb(i, s, t) for (int i = s; i >= t; --i)
#define mst(a, b) memset(a, b, sizeof(a))
#define mcpy(des, sor) memcpy(des, sor, sizeof(sor))
#define pt(x) cout << #x << "=" << x << " "
#define ptl(x) cout << #x << "=" << x << endl
#define sz(x) (int)(x.size())
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define lson n << 1, L, M
#define rson n << 1 | 1, M + 1, R
template <class T> void Max(T &a, T b) { a < b ? a = b : 0; }
template <class T> void Min(T &a, T b) { a > b ? a = b : 0; }
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const ll mod = 998244353;
#define maxn 20006

int a[maxn];
pii T[2][maxn * 4];

void update(int o, int n, int L, int R, pii vp) {
  if (L == R) {
    T[o][n] = vp;
    return;
  }
  int M = (L + R) / 2;
  if (vp.se <= M)
    update(o, lson, vp);
  else
    update(o, rson, vp);
  T[o][n] = min(T[o][n << 1], T[o][n << 1 | 1]);
}

pii query(int o, int n, int L, int R, int l, int r) {
  if (l <= L && R <= r)
    return T[o][n];
  int M = (L + R) / 2;
  pii ret = mp(inf, inf);
  if (l <= M)
    ret = min(ret, query(o, lson, l, r));
  if (r > M)
    ret = min(ret, query(o, rson, l, r));
  return ret;
}

struct itv {
  itv(){};
  int l, r;
  vector<int> xy;
  bool operator<(const itv &i) const { return a[xy[0]] > a[i.xy[0]]; }
};

int main() {
  //	freopen("1.txt","r",stdin);
  int n;
  pii l, r;
  itv t, s;
  vector<int> ans;
  mst(T, inf);
  priority_queue<itv> q;
  cin >> n;
  repb(i, 1, n) {
    cin >> a[i];
    update(i & 1, 1, 1, n, mp(a[i], i));
  }
  l = query(1, 1, 1, n, 1, n);
  r = query(0, 1, 1, n, l.se + 1, n);
  t.l = 1;
  t.r = n;
  t.xy.pb(l.se);
  t.xy.pb(r.se);
  q.push(t);
  while (!q.empty()) {
    t = q.top();
    q.pop();
    ans.pb(t.xy[0]);
    ans.pb(t.xy[1]);
    if (t.l != t.xy[0]) {
      s.xy.clear();
      s.l = t.l;
      s.r = t.xy[0] - 1;
      l = query(s.l & 1, 1, 1, n, s.l, s.r);
      r = query(!(s.l & 1), 1, 1, n, l.se + 1, s.r);
      s.xy.pb(l.se);
      s.xy.pb(r.se);
      q.push(s);
    }
    if (t.xy[0] + 1 != t.xy[1]) {
      s.xy.clear();
      s.l = t.xy[0] + 1;
      s.r = t.xy[1] - 1;
      l = query(s.l & 1, 1, 1, n, s.l, s.r);
      r = query(!(s.l & 1), 1, 1, n, l.se + 1, s.r);
      s.xy.pb(l.se);
      s.xy.pb(r.se);
      q.push(s);
    }
    if (t.xy[1] != t.r) {
      s.xy.clear();
      s.l = t.xy[1] + 1;
      s.r = t.r;
      l = query(s.l & 1, 1, 1, n, s.l, s.r);
      r = query(!(s.l & 1), 1, 1, n, l.se + 1, s.r);
      s.xy.pb(l.se);
      s.xy.pb(r.se);
      q.push(s);
    }
  }
  rep(i, 0, sz(ans)) cout << a[ans[i]] << " ";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define cst()                                                                  \
  int __t;                                                                     \
  scanf("%d", &__t);                                                           \
  while (__t--)
#define rep(i, s, t) for (int i = s; i < t; ++i)
#define repb(i, s, t) for (int i = s; i <= t; ++i)
#define lepb(i, s, t) for (int i = s; i >= t; --i)
#define mst(a, b) memset(a, b, sizeof(a))
#define mcpy(des, sor) memcpy(des, sor, sizeof(sor))
#define pt(x) cout << #x << "=" << x << " "
#define ptl(x) cout << #x << "=" << x << endl
#define sz(x) (int)(x.size())
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define lson n << 1, L, M
#define rson n << 1 | 1, M + 1, R
template <class T> void Max(T &a, T b) { a < b ? a = b : 0; }
template <class T> void Min(T &a, T b) { a > b ? a = b : 0; }
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const ll mod = 998244353;
#define maxn 300006

int a[maxn];
pii T[2][maxn * 4];

void update(int o, int n, int L, int R, pii vp) {
  if (L == R) {
    T[o][n] = vp;
    return;
  }
  int M = (L + R) / 2;
  if (vp.se <= M)
    update(o, lson, vp);
  else
    update(o, rson, vp);
  T[o][n] = min(T[o][n << 1], T[o][n << 1 | 1]);
}

pii query(int o, int n, int L, int R, int l, int r) {
  if (l <= L && R <= r)
    return T[o][n];
  int M = (L + R) / 2;
  pii ret = mp(inf, inf);
  if (l <= M)
    ret = min(ret, query(o, lson, l, r));
  if (r > M)
    ret = min(ret, query(o, rson, l, r));
  return ret;
}

struct itv {
  itv(){};
  int l, r;
  vector<int> xy;
  bool operator<(const itv &i) const { return a[xy[0]] > a[i.xy[0]]; }
};

int main() {
  //	freopen("1.txt","r",stdin);
  int n;
  pii l, r;
  itv t, s;
  vector<int> ans;
  mst(T, inf);
  priority_queue<itv> q;
  cin >> n;
  repb(i, 1, n) {
    cin >> a[i];
    update(i & 1, 1, 1, n, mp(a[i], i));
  }
  l = query(1, 1, 1, n, 1, n);
  r = query(0, 1, 1, n, l.se + 1, n);
  t.l = 1;
  t.r = n;
  t.xy.pb(l.se);
  t.xy.pb(r.se);
  q.push(t);
  while (!q.empty()) {
    t = q.top();
    q.pop();
    ans.pb(t.xy[0]);
    ans.pb(t.xy[1]);
    if (t.l != t.xy[0]) {
      s.xy.clear();
      s.l = t.l;
      s.r = t.xy[0] - 1;
      l = query(s.l & 1, 1, 1, n, s.l, s.r);
      r = query(!(s.l & 1), 1, 1, n, l.se + 1, s.r);
      s.xy.pb(l.se);
      s.xy.pb(r.se);
      q.push(s);
    }
    if (t.xy[0] + 1 != t.xy[1]) {
      s.xy.clear();
      s.l = t.xy[0] + 1;
      s.r = t.xy[1] - 1;
      l = query(s.l & 1, 1, 1, n, s.l, s.r);
      r = query(!(s.l & 1), 1, 1, n, l.se + 1, s.r);
      s.xy.pb(l.se);
      s.xy.pb(r.se);
      q.push(s);
    }
    if (t.xy[1] != t.r) {
      s.xy.clear();
      s.l = t.xy[1] + 1;
      s.r = t.r;
      l = query(s.l & 1, 1, 1, n, s.l, s.r);
      r = query(!(s.l & 1), 1, 1, n, l.se + 1, s.r);
      s.xy.pb(l.se);
      s.xy.pb(r.se);
      q.push(s);
    }
  }
  rep(i, 0, sz(ans)) cout << a[ans[i]] << " ";
}