Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <thread>
#include <vector>

using namespace std;

// const bool debug = false;

#ifndef M_PI
const double M_PI = acos(-1.0);
#endif // M_PI

#define y1 roman_kaban
#define rank oryshych_konb
#define ull unsigned long long
// #define ll long long
const int mod = int(1e9) + 7;
// const int inf = 1e7;
// const long long infLL = 1e16;
// const int MX2 = 10100500; //  e7
// const long long INF = 1e18 + 0.5;
const int MX = 200500; //       e6
const int SZ = 110;

namespace tree {
const int delta = 1 << 17;
int min_val[2 * delta][2];
int rev[2 * delta];
int pos[MX];
set<int> del;

void init(int n) {
  for (int i = delta; i < 2 * delta; i++) {
    min_val[i][0] = min_val[i][1] = MX;
    rev[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    min_val[delta + i][i & 1] = p;
    pos[p] = i;
  }
  for (int i = delta - 1; i > 0; i--) {
    min_val[i][0] = min(min_val[2 * i][0], min_val[2 * i + 1][0]);
    min_val[i][1] = min(min_val[2 * i][1], min_val[2 * i + 1][1]);
    rev[i] = 0;
  }
}

pair<int, int> get_val(int u) {
  int r1 = min_val[u][0];
  int r2 = min_val[u][1];
  if (rev[u])
    swap(r1, r2);
  return {r1, r2};
}

void upd(int u) {
  if (u >= delta)
    return;
  auto p1 = get_val(2 * u);
  auto p2 = get_val(2 * u + 1);
  min_val[u][0] = min(p1.first, p2.first);
  min_val[u][1] = min(p1.second, p2.second);
}

void rm_pos(int u, int l, int r, int pos) {
  if (pos < l || pos > r)
    return;
  if (l == r) {
    min_val[u][0] = min_val[u][1] = MX;
    return;
  }
  int mid = (l + r) >> 1;
  rm_pos(2 * u, l, mid, pos);
  rm_pos(2 * u + 1, mid + 1, r, pos);
  upd(u);
}

int ll, rr;

pair<int, int> get_min(int u, int l, int r) {
  if (l > rr || ll > r)
    return {MX, MX};
  if (ll <= l && r <= rr)
    return get_val(u);
  int mid = (l + r) >> 1;
  auto p1 = get_min(2 * u, l, mid);
  auto p2 = get_min(2 * u + 1, mid + 1, r);
  // cerr << u << ' ' << l <<' ' << r << ' ' << p1.first << ' ' << p1.second <<
  // ' ' << p2.first << ' ' << p2.second << endl;
  if (p1.first > p2.first)
    p1.first = p2.first;
  if (p1.second > p2.second)
    p1.second = p2.second;
  if (rev[u])
    swap(p1.first, p1.second);
  return p1;
}

int get_1_min() {

  ll++;
  rr--;

  int ans = get_min(1, 0, delta - 1).second;
  // cerr << "min[" << ll << ";" << rr << "]= " << ans << endl;
  // cerr << get_min(1, 0, delta - 1).first << endl;
  ll--;
  rr++;
  return ans;
}

void change_parity(int u, int l, int r) {
  if (l > rr || ll > r)
    return;
  if (l == r)
    return;
  if (ll <= l && r <= rr) {
    rev[u] ^= 1;
    return;
  }
  int mid = (l + r) >> 1;
  change_parity(2 * u, l, mid);
  change_parity(2 * u + 1, mid + 1, r);
  upd(u);
}
int extract_pair() {
  int first = min_val[1][0];
  ll = pos[first];
  // cerr << "first val = " << first << "\tat pos = " << ll << endl;
  auto z = del.lower_bound(ll);
  if (z == del.end())
    rr = delta;
  else
    rr = *z;
  // cerr << "search second in (" << ll << ";" << rr << ")" << endl;
  int second = get_1_min();
  rr = pos[second];
  // cerr << "second val = " << second << "\tat pos = " << rr << endl;

  cout << first << ' ' << second << ' ';
  // cerr << endl;
  rm_pos(1, 0, delta - 1, ll);
  rm_pos(1, 0, delta - 1, rr);
  change_parity(1, 0, delta - 1);
  del.insert(ll);
  del.insert(rr);
}

} // namespace tree

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // freopen("/Users/ozzy/Documents/in.txt","r", stdin);
  int n;
  cin >> n;
  // cerr << "init start" << endl;
  tree::init(n);
  // cerr << "init ok" << endl;;
  n >>= 1;
  while (n--)
    tree::extract_pair();
  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <thread>
#include <vector>

using namespace std;

// const bool debug = false;

#ifndef M_PI
const double M_PI = acos(-1.0);
#endif // M_PI

#define y1 roman_kaban
#define rank oryshych_konb
#define ull unsigned long long
// #define ll long long
const int mod = int(1e9) + 7;
// const int inf = 1e7;
// const long long infLL = 1e16;
// const int MX2 = 10100500; //  e7
// const long long INF = 1e18 + 0.5;
const int MX = 200500; //       e6
const int SZ = 110;

namespace tree {
const int delta = 1 << 18;
int min_val[2 * delta][2];
int rev[2 * delta];
int pos[MX];
set<int> del;

void init(int n) {
  for (int i = delta; i < 2 * delta; i++) {
    min_val[i][0] = min_val[i][1] = MX;
    rev[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    min_val[delta + i][i & 1] = p;
    pos[p] = i;
  }
  for (int i = delta - 1; i > 0; i--) {
    min_val[i][0] = min(min_val[2 * i][0], min_val[2 * i + 1][0]);
    min_val[i][1] = min(min_val[2 * i][1], min_val[2 * i + 1][1]);
    rev[i] = 0;
  }
}

pair<int, int> get_val(int u) {
  int r1 = min_val[u][0];
  int r2 = min_val[u][1];
  if (rev[u])
    swap(r1, r2);
  return {r1, r2};
}

void upd(int u) {
  if (u >= delta)
    return;
  auto p1 = get_val(2 * u);
  auto p2 = get_val(2 * u + 1);
  min_val[u][0] = min(p1.first, p2.first);
  min_val[u][1] = min(p1.second, p2.second);
}

void rm_pos(int u, int l, int r, int pos) {
  if (pos < l || pos > r)
    return;
  if (l == r) {
    min_val[u][0] = min_val[u][1] = MX;
    return;
  }
  int mid = (l + r) >> 1;
  rm_pos(2 * u, l, mid, pos);
  rm_pos(2 * u + 1, mid + 1, r, pos);
  upd(u);
}

int ll, rr;

pair<int, int> get_min(int u, int l, int r) {
  if (l > rr || ll > r)
    return {MX, MX};
  if (ll <= l && r <= rr)
    return get_val(u);
  int mid = (l + r) >> 1;
  auto p1 = get_min(2 * u, l, mid);
  auto p2 = get_min(2 * u + 1, mid + 1, r);
  // cerr << u << ' ' << l <<' ' << r << ' ' << p1.first << ' ' << p1.second <<
  // ' ' << p2.first << ' ' << p2.second << endl;
  if (p1.first > p2.first)
    p1.first = p2.first;
  if (p1.second > p2.second)
    p1.second = p2.second;
  if (rev[u])
    swap(p1.first, p1.second);
  return p1;
}

int get_1_min() {

  ll++;
  rr--;

  int ans = get_min(1, 0, delta - 1).second;
  // cerr << "min[" << ll << ";" << rr << "]= " << ans << endl;
  // cerr << get_min(1, 0, delta - 1).first << endl;
  ll--;
  rr++;
  return ans;
}

void change_parity(int u, int l, int r) {
  if (l > rr || ll > r)
    return;
  if (l == r)
    return;
  if (ll <= l && r <= rr) {
    rev[u] ^= 1;
    return;
  }
  int mid = (l + r) >> 1;
  change_parity(2 * u, l, mid);
  change_parity(2 * u + 1, mid + 1, r);
  upd(u);
}
int extract_pair() {
  int first = min_val[1][0];
  ll = pos[first];
  // cerr << "first val = " << first << "\tat pos = " << ll << endl;
  auto z = del.lower_bound(ll);
  if (z == del.end())
    rr = delta;
  else
    rr = *z;
  // cerr << "search second in (" << ll << ";" << rr << ")" << endl;
  int second = get_1_min();
  rr = pos[second];
  // cerr << "second val = " << second << "\tat pos = " << rr << endl;

  cout << first << ' ' << second << ' ';
  // cerr << endl;
  rm_pos(1, 0, delta - 1, ll);
  rm_pos(1, 0, delta - 1, rr);
  change_parity(1, 0, delta - 1);
  del.insert(ll);
  del.insert(rr);
}

} // namespace tree

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // freopen("/Users/ozzy/Documents/in.txt","r", stdin);
  int n;
  cin >> n;
  // cerr << "init start" << endl;
  tree::init(n);
  // cerr << "init ok" << endl;;
  n >>= 1;
  while (n--)
    tree::extract_pair();
  cout << endl;
  return 0;
}