Problem p03411 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
void debug_out() { cout << "\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    assert(i < n);
    for (i++; i <= n; i += i & -i) { // LSB:i&(-i)
      d[i] += x;
    }
  }
  T sum(int i) {
    assert(i < n);
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { //[l,r)
    assert(l < n);
    assert(r <= n);
    return sum(r - 1) - sum(l - 1);
  }
  T operator[](const int &i) {
    assert(i < n);
    return sum(i, i + 1);
  }
};

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  int n;
  cin >> n;
  vector<vector<int>> a(2 * n), b(2 * n);
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    a[x].emplace_back(y);
  }
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    b[x].emplace_back(y);
  }
  BIT<int> bit(2 * n);
  int ans = 0;
  for (int bx = 2 * n - 1; bx > 0; --bx) {
    for (auto by : b[bx]) {
      bit.add(by);
    }
    if (a[bx - 1].size() == 0)
      continue;
    sort(all(a[bx - 1]), greater<int>());
    for (auto y : a[bx - 1]) {
      if (bit.sum(y + 1, 2 * n) > 0) {
        for (int i = y + 1; i < 2 * n; ++i) {
          if (bit[i] > 0) {
            bit.add(i, -1);
            ans++;
            break;
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
void debug_out() { cout << "\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    assert(i < n);
    for (i++; i <= n; i += i & -i) { // LSB:i&(-i)
      d[i] += x;
    }
  }
  T sum(int i) {
    assert(i < n);
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { //[l,r)
    assert(l < n);
    assert(r <= n);
    return sum(r - 1) - sum(l - 1);
  }
  T operator[](const int &i) {
    assert(i < n);
    return sum(i, i + 1);
  }
};

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  int n;
  cin >> n;
  vector<vector<int>> a(2 * n), b(2 * n);
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    a[x].emplace_back(y);
  }
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    b[x].emplace_back(y);
  }
  BIT<int> bit(2 * n);
  int ans = 0;
  for (int bx = 2 * n - 1; bx > 0; --bx) {
    for (auto by : b[bx]) {
      bit.add(by);
    }
    if (a[bx - 1].size() == 0)
      continue;
    sort(all(a[bx - 1]), greater<int>());
    for (auto y : a[bx - 1]) {
      if (y == 2 * n - 1)
        continue; // どうやっても取れない
      if (bit.sum(y + 1, 2 * n) > 0) {
        for (int i = y + 1; i < 2 * n; ++i) {
          if (bit[i] > 0) {
            bit.add(i, -1);
            ans++;
            break;
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}