Problem p03686 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#define pb push_back
using namespace std;

inline char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline void read(int &x) {
  char c = nc(), b = 1;
  for (; !(c >= '0' && c <= '9'); c = nc())
    if (c == '-')
      b = -1;
  for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc())
    ;
  x *= b;
}

const int N = 200005;

int T[N << 2], F[N << 2];
inline void Build(int x, int l, int r) {
  if (l == r)
    return void(T[x] = l);
  int mid = (l + r) >> 1;
  Build(x << 1, l, mid);
  Build(x << 1 | 1, mid + 1, r);
  T[x] = max(T[x << 1], T[x << 1 | 1]);
}
inline void Add(int x, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr) {
    T[x]++;
    F[x]++;
    return;
  }
  int mid = (l + r) >> 1;
  if (ql <= mid)
    Add(x << 1, l, mid, ql, qr);
  if (qr > mid)
    Add(x << 1 | 1, mid + 1, r, ql, qr);
  T[x] = F[x] + max(T[x << 1], T[x << 1 | 1]);
}
inline int Query(int x, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr)
    return T[x];
  int mid = (l + r) >> 1, ret = 0;
  if (ql <= mid)
    ret = max(ret, Query(x << 1, l, mid, ql, qr));
  if (qr > mid)
    ret = max(ret, Query(x << 1 | 1, mid + 1, r, ql, qr));
  return ret + F[x];
}

int n, m;
vector<int> R[N];

int main() {
  int x, y;
  freopen("t.in", "r", stdin);
  freopen("t.out", "w", stdout);
  read(n);
  read(m);
  for (int i = 1; i <= n; i++)
    read(x), read(y), R[x].pb(y);
  int ans = 0;
  Build(1, 0, m + 1);
  for (int i = 0; i <= m; i++) {
    for (int x : R[i])
      Add(1, 0, m + 1, 0, x);
    ans = max(ans, Query(1, 0, m + 1, i + 1, m + 1) - i - m - 1);
  }
  printf("%d\n", max(n - m, ans));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#define pb push_back
using namespace std;

inline char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline void read(int &x) {
  char c = nc(), b = 1;
  for (; !(c >= '0' && c <= '9'); c = nc())
    if (c == '-')
      b = -1;
  for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc())
    ;
  x *= b;
}

const int N = 200005;

int T[N << 2], F[N << 2];
inline void Build(int x, int l, int r) {
  if (l == r)
    return void(T[x] = l);
  int mid = (l + r) >> 1;
  Build(x << 1, l, mid);
  Build(x << 1 | 1, mid + 1, r);
  T[x] = max(T[x << 1], T[x << 1 | 1]);
}
inline void Add(int x, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr) {
    T[x]++;
    F[x]++;
    return;
  }
  int mid = (l + r) >> 1;
  if (ql <= mid)
    Add(x << 1, l, mid, ql, qr);
  if (qr > mid)
    Add(x << 1 | 1, mid + 1, r, ql, qr);
  T[x] = F[x] + max(T[x << 1], T[x << 1 | 1]);
}
inline int Query(int x, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr)
    return T[x];
  int mid = (l + r) >> 1, ret = 0;
  if (ql <= mid)
    ret = max(ret, Query(x << 1, l, mid, ql, qr));
  if (qr > mid)
    ret = max(ret, Query(x << 1 | 1, mid + 1, r, ql, qr));
  return ret + F[x];
}

int n, m;
vector<int> R[N];

int main() {
  int x, y;
  read(n);
  read(m);
  for (int i = 1; i <= n; i++)
    read(x), read(y), R[x].pb(y);
  int ans = 0;
  Build(1, 0, m + 1);
  for (int i = 0; i <= m; i++) {
    for (int x : R[i])
      Add(1, 0, m + 1, 0, x);
    ans = max(ans, Query(1, 0, m + 1, i + 1, m + 1) - i - m - 1);
  }
  printf("%d\n", max(n - m, ans));
  return 0;
}