Problem p03686 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define For(i, j, k) for (int i = j; i <= k; ++i)
#define Forr(i, j, k) for (int i = j; i >= k; --i)
#define ll long long
using namespace std;
const int N = 200010, M = 200010, INF = 0x3f3f3f3f;
template <class T> void read(T &x) {
  x = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    x = x * 10 + c - 48, c = getchar();
}
inline void file() {
#ifndef ONLINE_JUDGE
  freopen("test.in", "r", stdin);
  freopen("test.out", "w", stdout);
#endif
}
struct Segment_tree {
#define lc h << 1
#define rc h << 1 | 1
#define Mid ((l + r) >> 1)
#define Lc lc, l, Mid
#define Rc rc, Mid + 1, r
  ll Sum[N << 2], Add[N << 2], Max[N << 2], Sum1[N << 2], Max1[N << 2];
  inline void Pushup(int h) {
    Sum[h] = Sum[lc] + Sum[rc];
    Max[h] = max(Max[lc], Max[rc]);
    Sum1[h] = Sum1[lc] + Sum1[rc];
    Max1[h] = max(Max1[lc], Max1[rc]);
  }
  inline void Pushdown(int h, int l, int r) {
    if (Add[h]) {
      Sum[lc] += Add[h] * (Mid - l + 1), Sum[rc] += Add[h] * (r - Mid);
      Sum1[lc] += Add[h] * (Mid - l + 1), Sum1[rc] += Add[h] * (r - Mid);

      Add[lc] += Add[h], Add[rc] += Add[h];
      Max[lc] += Add[h], Max[rc] += Add[h];
      Max1[lc] += Add[h], Max1[rc] += Add[h];
      Add[h] = 0;
    }
  }
  void Build(int h, int l, int r, int n) {
    if (l == r)
      return void(Max[h] = Sum[h] = -(n - l + 1));
    Build(Lc, n), Build(Rc, n);
    Pushup(h);
  }
  void Modify(int h, int l, int r, int L, int R) {
    if (L <= l && r <= R) {
      Add[h] += 1, Sum[h] += r - l + 1, Max[h] += 1;
      Max1[h] += 1, Sum1[h] += r - l + 1;
      return;
    }
    Pushdown(h, l, r);
    if (L <= Mid)
      Modify(Lc, L, R);
    if (R > Mid)
      Modify(Rc, L, R);
    Pushup(h);
  }
  ll Query(int h, int l, int r, int L, int R) {
    if (L <= l && r <= R)
      return Max[h];
    Pushdown(h, l, r);
    ll ans = 0;
    if (L <= Mid)
      ans = max(ans, Query(Lc, L, R));
    if (R > Mid)
      ans = max(ans, Query(Rc, L, R));
    return ans;
  }
} t;
struct node {
  int x, y;
  bool operator<(const node &r) const { return x < r.x; }
} P[N];
int n, m;
void init() {
  read(n), read(m);
  For(i, 1, n) read(P[i].x), read(P[i].y);
  sort(P + 1, P + n + 1);
}
void solve() {
  ll ans = 0;
  t.Build(1, 1, m + 1, m);
  For(i, 1, n) {
    t.Modify(1, 1, m + 1, 1, P[i].y);
    ans = max(ans, t.Query(1, 1, m + 1, P[i].x + 1, m + 1) - P[i].x);
    ans = max(ans, t.Max1[1] - m);
    // printf("%d\n",t.Max1[1]);
  }
  printf("%lld\n", ans);
}
int main() {
  file();
  init();
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define For(i, j, k) for (int i = j; i <= k; ++i)
#define Forr(i, j, k) for (int i = j; i >= k; --i)
#define ll long long
using namespace std;
const int N = 200010, M = 200010, INF = 0x3f3f3f3f;
template <class T> void read(T &x) {
  x = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    x = x * 10 + c - 48, c = getchar();
}
inline void file() {
#ifndef ONLINE_JUDGE
  freopen("test.in", "r", stdin);
  freopen("test.out", "w", stdout);
#endif
}
struct Segment_tree {
#define lc h << 1
#define rc h << 1 | 1
#define Mid ((l + r) >> 1)
#define Lc lc, l, Mid
#define Rc rc, Mid + 1, r
  ll Sum[N << 2], Add[N << 2], Max[N << 2], Sum1[N << 2], Max1[N << 2];
  inline void Pushup(int h) {
    Sum[h] = Sum[lc] + Sum[rc];
    Max[h] = max(Max[lc], Max[rc]);
    Sum1[h] = Sum1[lc] + Sum1[rc];
    Max1[h] = max(Max1[lc], Max1[rc]);
  }
  inline void Pushdown(int h, int l, int r) {
    if (Add[h]) {
      Sum[lc] += Add[h] * (Mid - l + 1), Sum[rc] += Add[h] * (r - Mid);
      Sum1[lc] += Add[h] * (Mid - l + 1), Sum1[rc] += Add[h] * (r - Mid);

      Add[lc] += Add[h], Add[rc] += Add[h];
      Max[lc] += Add[h], Max[rc] += Add[h];
      Max1[lc] += Add[h], Max1[rc] += Add[h];
      Add[h] = 0;
    }
  }
  void Build(int h, int l, int r, int n) {
    if (l == r)
      return void(Max[h] = Sum[h] = -(n - l + 1));
    Build(Lc, n), Build(Rc, n);
    Pushup(h);
  }
  void Modify(int h, int l, int r, int L, int R) {
    if (L <= l && r <= R) {
      Add[h] += 1, Sum[h] += r - l + 1, Max[h] += 1;
      Max1[h] += 1, Sum1[h] += r - l + 1;
      return;
    }
    Pushdown(h, l, r);
    if (L <= Mid)
      Modify(Lc, L, R);
    if (R > Mid)
      Modify(Rc, L, R);
    Pushup(h);
  }
  ll Query(int h, int l, int r, int L, int R) {
    if (L <= l && r <= R)
      return Max[h];
    Pushdown(h, l, r);
    ll ans = 0;
    if (L <= Mid)
      ans = max(ans, Query(Lc, L, R));
    if (R > Mid)
      ans = max(ans, Query(Rc, L, R));
    return ans;
  }
} t;
struct node {
  int x, y;
  bool operator<(const node &r) const { return x < r.x; }
} P[N];
int n, m;
void init() {
  read(n), read(m);
  For(i, 1, n) read(P[i].x), read(P[i].y);
  sort(P + 1, P + n + 1);
}
void solve() {
  ll ans = 0;
  t.Build(1, 1, m + 1, m);
  For(i, 1, n) {
    t.Modify(1, 1, m + 1, 1, P[i].y);
    ans = max(ans, t.Query(1, 1, m + 1, P[i].x + 1, m + 1) - P[i].x);
    ans = max(ans, t.Max1[1] - m);
    // printf("%d\n",t.Max1[1]);
  }
  printf("%lld\n", ans);
}
int main() {
  init();
  solve();
  return 0;
}