Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define pi pair<int, int>
#define pl pair<long long, long long>
#define lg length()
#define pb push_back
ifstream in("file.in");
ofstream out("file.out");
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005

int n, x, y;
pi b1[400005], b2[400005];

map<pi, vector<pi>> m;

set<pi> q;

pi mn(pi a, pi b) {
  if (a.x < b.x)
    return a;
  else
    return b;
}

void Upd1(int nod, int l, int r, int pos, int val) {
  if (l > pos || r < pos)
    return;
  if (l == r) {
    b1[nod] = {val, pos};
    return;
  }
  int mid = (l + r) / 2;
  Upd1(2 * nod, l, mid, pos, val);
  Upd1(2 * nod + 1, mid + 1, r, pos, val);
  b1[nod] = mn(b1[2 * nod], b1[2 * nod + 1]);
}

pi Qry1(int nod, int l, int r, int lt, int rt) {
  if (l > rt || r < lt)
    return {1e9, 1e9};
  if (l >= lt && r <= rt)
    return b1[nod];
  int mid = (l + r) / 2;
  return mn(Qry1(2 * nod, l, mid, lt, rt),
            Qry1(2 * nod + 1, mid + 1, r, lt, rt));
}

void Upd2(int nod, int l, int r, int pos, int val) {
  if (l > pos || r < pos)
    return;
  if (l == r) {
    b2[nod] = {val, pos};
    return;
  }
  int mid = (l + r) / 2;
  Upd2(2 * nod, l, mid, pos, val);
  Upd2(2 * nod + 1, mid + 1, r, pos, val);
  b2[nod] = mn(b2[2 * nod], b2[2 * nod + 1]);
}

pi Qry2(int nod, int l, int r, int lt, int rt) {
  if (l > rt || r < lt)
    return {1e9, 1e9};
  if (l >= lt && r <= rt)
    return b2[nod];
  int mid = (l + r) / 2;
  return mn(Qry2(2 * nod, l, mid, lt, rt),
            Qry2(2 * nod + 1, mid + 1, r, lt, rt));
}

pi Find(int l, int r, int p) {
  if (p) {
    pi x = Qry1(1, 1, n, l, r);
    pi y = Qry2(1, 1, n, x.second + 1, r);
    pi t, g = {x.first, y.first};
    if (x.second - 1 >= l)
      t = Find(l, x.second - 1, p), m[g].push_back(t);
    if (y.second - 1 >= x.second + 1)
      t = Find(x.second + 1, y.second - 1, !p), m[g].push_back(t);
    if (r >= y.second + 1)
      t = Find(y.second + 1, r, p), m[g].push_back(t);
    return g;
  } else {
    pi x = Qry2(1, 1, n, l, r);
    pi y = Qry1(1, 1, n, x.second + 1, r);
    pi t, g = {x.first, y.first};
    if (x.second - 1 >= l)
      t = Find(l, x.second - 1, p), m[g].push_back(t);
    if (y.second - 1 >= x.second + 1)
      t = Find(x.second + 1, y.second - 1, !p), m[g].push_back(t);
    if (r >= y.second + 1)
      t = Find(y.second + 1, r, p), m[g].push_back(t);
    return g;
  }
}

int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    if (i % 2)
      Upd1(1, 1, n, i, x), Upd2(1, 1, n, i, 1e9);
    else
      Upd1(1, 1, n, i, 1e9), Upd2(1, 1, n, i, x);
  }
  pi t = Find(1, n, 1);
  q.insert(t);
  while (q.size()) {
    t = *(q.begin());
    q.erase(q.begin());
    for (pi i : m[t]) {
      q.insert(i);
    }
    cout << t.first << ' ' << t.second << ' ';
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define pi pair<int, int>
#define pl pair<long long, long long>
#define lg length()
#define pb push_back
ifstream in("file.in");
ofstream out("file.out");
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005

int n, x, y;
pi b1[800005], b2[800005];

map<pi, vector<pi>> m;

set<pi> q;

pi mn(pi a, pi b) {
  if (a.x < b.x)
    return a;
  else
    return b;
}

void Upd1(int nod, int l, int r, int pos, int val) {
  if (l > pos || r < pos)
    return;
  if (l == r) {
    b1[nod] = {val, pos};
    return;
  }
  int mid = (l + r) / 2;
  Upd1(2 * nod, l, mid, pos, val);
  Upd1(2 * nod + 1, mid + 1, r, pos, val);
  b1[nod] = mn(b1[2 * nod], b1[2 * nod + 1]);
}

pi Qry1(int nod, int l, int r, int lt, int rt) {
  if (l > rt || r < lt)
    return {1e9, 1e9};
  if (l >= lt && r <= rt)
    return b1[nod];
  int mid = (l + r) / 2;
  return mn(Qry1(2 * nod, l, mid, lt, rt),
            Qry1(2 * nod + 1, mid + 1, r, lt, rt));
}

void Upd2(int nod, int l, int r, int pos, int val) {
  if (l > pos || r < pos)
    return;
  if (l == r) {
    b2[nod] = {val, pos};
    return;
  }
  int mid = (l + r) / 2;
  Upd2(2 * nod, l, mid, pos, val);
  Upd2(2 * nod + 1, mid + 1, r, pos, val);
  b2[nod] = mn(b2[2 * nod], b2[2 * nod + 1]);
}

pi Qry2(int nod, int l, int r, int lt, int rt) {
  if (l > rt || r < lt)
    return {1e9, 1e9};
  if (l >= lt && r <= rt)
    return b2[nod];
  int mid = (l + r) / 2;
  return mn(Qry2(2 * nod, l, mid, lt, rt),
            Qry2(2 * nod + 1, mid + 1, r, lt, rt));
}

pi Find(int l, int r, int p) {
  if (p) {
    pi x = Qry1(1, 1, n, l, r);
    pi y = Qry2(1, 1, n, x.second + 1, r);
    pi t, g = {x.first, y.first};
    if (x.second - 1 >= l)
      t = Find(l, x.second - 1, p), m[g].push_back(t);
    if (y.second - 1 >= x.second + 1)
      t = Find(x.second + 1, y.second - 1, !p), m[g].push_back(t);
    if (r >= y.second + 1)
      t = Find(y.second + 1, r, p), m[g].push_back(t);
    return g;
  } else {
    pi x = Qry2(1, 1, n, l, r);
    pi y = Qry1(1, 1, n, x.second + 1, r);
    pi t, g = {x.first, y.first};
    if (x.second - 1 >= l)
      t = Find(l, x.second - 1, p), m[g].push_back(t);
    if (y.second - 1 >= x.second + 1)
      t = Find(x.second + 1, y.second - 1, !p), m[g].push_back(t);
    if (r >= y.second + 1)
      t = Find(y.second + 1, r, p), m[g].push_back(t);
    return g;
  }
}

int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    if (i % 2)
      Upd1(1, 1, n, i, x), Upd2(1, 1, n, i, 1e9);
    else
      Upd1(1, 1, n, i, 1e9), Upd2(1, 1, n, i, x);
  }
  pi t = Find(1, n, 1);
  q.insert(t);
  while (q.size()) {
    t = *(q.begin());
    q.erase(q.begin());
    for (pi i : m[t]) {
      q.insert(i);
    }
    cout << t.first << ' ' << t.second << ' ';
  }
}