Problem p00328 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
set<int> merge(set<int> a, set<int> b) {
  if (a.size() < b.size())
    swap(a, b);
  for (int x : b)
    a.insert(x);
  return a;
}

#define MAX_N (1 << 18)
struct SegTree {
  set<int> seg[MAX_N * 2 - 1];
  set<int> query(int a, int b, int k = 0, int l = 0, int r = MAX_N) {
    if (b <= l || r <= a)
      return set<int>();
    if (a <= l && r <= b)
      return seg[k];
    return merge(merge(query(a, b, k * 2 + 1, l, (l + r) / 2),
                       query(a, b, k * 2 + 2, (l + r) / 2, r)),
                 seg[k]);
  }

  void insert(int a, int b, int x, int k = 0, int l = 0, int r = MAX_N) {
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      seg[k].insert(x);
      return;
    }
    insert(a, b, x, k * 2 + 1, l, (l + r) / 2);
    insert(a, b, x, k * 2 + 2, (l + r) / 2, r);
  }
};
SegTree seg[2];

int N;
int AX[100000], AY[100000], BX[100000], BY[100000];
set<P> V[2][200000];
bool S[100000];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  vector<int> xs, ys;
  rep(i, N) {
    cin >> AX[i] >> AY[i] >> BX[i] >> BY[i];
    xs.pb(AX[i]), xs.pb(BX[i]);
    ys.pb(AY[i]), ys.pb(BY[i]);
  }
  sort(all(xs));
  uniq(xs);
  sort(all(ys));
  uniq(ys);
  rep(i, N) {
    AX[i] = index(xs, AX[i]), AY[i] = index(ys, AY[i]);
    BX[i] = index(xs, BX[i]), BY[i] = index(ys, BY[i]);
  }

  rep(i, N) {
    int t, l, r, y;
    if (AY[i] == BY[i]) {
      t = 0;
      l = AX[i], r = BX[i], y = AY[i];
    } else if (AX[i] == BX[i]) {
      t = 1;
      l = AY[i], r = BY[i], y = AX[i];
    } else
      assert(false);
    if (l > r)
      swap(l, r);

    set<P> &vs = V[t][y];

    auto nx = vs.lower_bound(P(l, INF));
    bool ok = (nx == vs.end() || r < nx->_1);
    if (nx != vs.begin()) {
      nx--;
      if (nx->_2 < l) {
      } else
        ok = false;
    }
    if (!ok)
      continue;

    set<int> s = seg[t ^ 1].query(y, y + 1);
    if (s.lower_bound(l) != s.upper_bound(r))
      continue;

    vs.insert(P(l, r));
    seg[t].insert(l, r + 1, y);
    S[i] = true;
  }
  rep(i, N) cout << S[i] << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
set<int> merge(set<int> a, set<int> b) {
  if (a.size() < b.size())
    swap(a, b);
  for (int x : b)
    a.insert(x);
  return a;
}

#define MAX_N (1 << 17)
struct SegTree {
  set<int> seg[MAX_N * 2 - 1];
  set<int> query(int a, int b, int k = 0, int l = 0, int r = MAX_N) {
    if (b <= l || r <= a)
      return set<int>();
    if (a <= l && r <= b)
      return seg[k];
    return merge(merge(query(a, b, k * 2 + 1, l, (l + r) / 2),
                       query(a, b, k * 2 + 2, (l + r) / 2, r)),
                 seg[k]);
  }

  void insert(int a, int b, int x, int k = 0, int l = 0, int r = MAX_N) {
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      seg[k].insert(x);
      return;
    }
    insert(a, b, x, k * 2 + 1, l, (l + r) / 2);
    insert(a, b, x, k * 2 + 2, (l + r) / 2, r);
  }
};
SegTree seg[2];

int N;
int AX[100000], AY[100000], BX[100000], BY[100000];
set<P> V[2][100000];
bool S[100000];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  vector<int> xs, ys;
  rep(i, N) {
    cin >> AX[i] >> AY[i] >> BX[i] >> BY[i];
    xs.pb(AX[i]), xs.pb(BX[i]);
    ys.pb(AY[i]), ys.pb(BY[i]);
  }
  sort(all(xs));
  uniq(xs);
  sort(all(ys));
  uniq(ys);
  rep(i, N) {
    AX[i] = index(xs, AX[i]), AY[i] = index(ys, AY[i]);
    BX[i] = index(xs, BX[i]), BY[i] = index(ys, BY[i]);
  }

  rep(i, N) {
    int t, l, r, y;
    if (AY[i] == BY[i]) {
      t = 0;
      l = AX[i], r = BX[i], y = AY[i];
    } else if (AX[i] == BX[i]) {
      t = 1;
      l = AY[i], r = BY[i], y = AX[i];
    } else
      assert(false);
    if (l > r)
      swap(l, r);

    set<P> &vs = V[t][y];

    auto nx = vs.lower_bound(P(l, INF));
    bool ok = (nx == vs.end() || r < nx->_1);
    if (nx != vs.begin()) {
      nx--;
      if (nx->_2 < l) {
      } else
        ok = false;
    }
    if (!ok)
      continue;

    set<int> s = seg[t ^ 1].query(y, y + 1);
    if (s.lower_bound(l) != s.upper_bound(r))
      continue;

    vs.insert(P(l, r));
    seg[t].insert(l, r + 1, y);
    S[i] = true;
  }
  rep(i, N) cout << S[i] << "\\n";
  return 0;
}