Problem p03686 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> bool chmin(T &a, const T &b) {
  return a > b ? (a = b, 1) : 0;
}
template <class T> bool chmax(T &a, const T &b) {
  return a < b ? (a = b, 1) : 0;
}
typedef pair<int, int> P;

struct BIT {
  int n;
  vector<ll> tree;
  BIT(int n) : n(n), tree(n + 1) {}
  void fill(ll val) { REP(i, n + 1) tree[i] = (i & -i) * val; }
  void add(int idx, ll val) {
    for (int x = idx + 1; x <= n; x += x & -x)
      tree[x] += val;
  }
  ll range(int idx) {
    ll sum = 0;
    for (int x = idx + 1; x > 0; x -= x & -x)
      sum += tree[x];
    return sum;
  }
  ll range(int a, int b) { return range(b) - range(a - 1); }
};

int nextInt() {
  int x;
  scanf("%d", &x);
  return x;
}

int main2() {
  int N = nextInt();
  int M = nextInt();

  int all = 0;
  vector<P> ps;

  int maxL = -1, minR = M + 2;
  REP(i, N) {
    int L = nextInt();
    int R = nextInt();
    ps.push_back(P(L, R));
    chmax(maxL, L);
    chmin(minR, R);
  }
  sort(ALL(ps), [](const P &a, const P &b) -> bool {
    if (a.first != b.first)
      return a.first < b.first;
    return a.second > b.second;
  });

  BIT bit(100000 + 10);

  int ans = max(0, N - min(M, maxL + (M + 1) - minR));
  for (P p : ps) {
    bit.add((M + 1) - p.second, 1);
    int man = bit.range((M + 1) - p.second);
    int chair = (p.first) + ((M + 1) - p.second);
    int need = man - chair;
    ans = max(ans, need);
  }
  cout << ans << endl;
  return 0;
}

int main() {
  for (; !cin.eof(); cin >> ws)
    main2();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> bool chmin(T &a, const T &b) {
  return a > b ? (a = b, 1) : 0;
}
template <class T> bool chmax(T &a, const T &b) {
  return a < b ? (a = b, 1) : 0;
}
typedef pair<int, int> P;

struct BIT {
  int n;
  vector<ll> tree;
  BIT(int n) : n(n), tree(n + 1) {}
  void fill(ll val) { REP(i, n + 1) tree[i] = (i & -i) * val; }
  void add(int idx, ll val) {
    for (int x = idx + 1; x <= n; x += x & -x)
      tree[x] += val;
  }
  ll range(int idx) {
    ll sum = 0;
    for (int x = idx + 1; x > 0; x -= x & -x)
      sum += tree[x];
    return sum;
  }
  ll range(int a, int b) { return range(b) - range(a - 1); }
};

int nextInt() {
  int x;
  scanf("%d", &x);
  return x;
}

int main2() {
  int N = nextInt();
  int M = nextInt();

  int all = 0;
  vector<P> ps;

  int maxL = -1, minR = M + 2;
  REP(i, N) {
    int L = nextInt();
    int R = nextInt();
    ps.push_back(P(L, R));
    chmax(maxL, L);
    chmin(minR, R);
  }
  sort(ALL(ps), [](const P &a, const P &b) -> bool {
    if (a.first != b.first)
      return a.first < b.first;
    return a.second > b.second;
  });

  BIT bit(200000 + 10);

  int ans = max(0, N - min(M, maxL + (M + 1) - minR));
  for (P p : ps) {
    bit.add((M + 1) - p.second, 1);
    int man = bit.range((M + 1) - p.second);
    int chair = (p.first) + ((M + 1) - p.second);
    int need = man - chair;
    ans = max(ans, need);
  }
  cout << ans << endl;
  return 0;
}

int main() {
  for (; !cin.eof(); cin >> ws)
    main2();
  return 0;
}