Problem p03686 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define R register int
#define AC 200100
#define ac 801000

int n, m, ans = -INT_MAX, w;
int Head[AC], Next[ac], date[ac], tot;
int tree[ac], lazy[ac], l[ac], r[ac], l_[AC], r_[AC];

inline int read() {
  int x = 0;
  char c = getchar();
  while (c > '9' || c < '0')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}

inline void add(int f, int w) {
  date[++tot] = w, Next[tot] = Head[f], Head[f] = tot;
}

inline void upmax(int &a, int b) {
  if (b > a)
    a = b;
}

void pre() {
  n = read(), m = read();
  for (R i = 1; i <= n; i++)
    l_[i] = read(), r_[i] = read(), add(r_[i], i);
}

inline void pushdown(int x) {
  if (lazy[x]) {
    int ll = x * 2, rr = ll + 1;
    lazy[ll] += lazy[x], lazy[rr] += lazy[x];
    tree[ll] += lazy[x],
        tree[rr] +=
        lazy[x]; // 这里因为是+=，所以必须用lazy[x]，不然会将lazy[ll]中的一些东西重复统计
    lazy[x] = 0; // 最后才清空！！！！！！！！！！
  } // error!!!是区间加，不是赋值，不能直接覆盖，要+=
}

inline void update(int x) { tree[x] = max(tree[x * 2], tree[x * 2 + 1]); }

void build(int x, int ll, int rr) {
  l[x] = ll, r[x] = rr;
  if (ll == rr) {
    tree[x] = -ll + 1;
    return;
  }
  int mid = (ll + rr) >> 1;
  build(x * 2, ll, mid);
  build(x * 2 + 1, mid + 1, rr);
  update(x);
}

void change(int x, int ll, int rr) {
  pushdown(x);
  if (l[x] == ll && r[x] == rr) {
    lazy[x] += w, tree[x] += w;
    return;
  }
  int mid = (l[x] + r[x]) >> 1;
  if (rr <= mid)
    change(x * 2, ll, rr);
  else if (ll > mid)
    change(x * 2 + 1, ll, rr);
  else {
    change(x * 2, ll, mid);
    change(x * 2 + 1, mid + 1, rr);
  }
  update(x);
}

void find(int x, int ll, int rr) {
  pushdown(x);
  if (l[x] == ll && r[x] == rr) {
    upmax(ans, tree[x]);
    return;
  }
  int mid = (l[x] + r[x]) >> 1;
  if (rr <= mid)
    find(x * 2, ll, rr);
  else if (ll > mid)
    find(x * 2 + 1, ll, rr);
  else
    find(x * 2, ll, mid), find(x * 2 + 1, mid + 1, rr); // 这是取max啊，，，，
}

void work() {
  int now;
  ans = n - m; // r = 0的情况
  for (R i = Head[m + 1]; i; i = Next[i]) {
    now = date[i], w = 1;
    change(1, l_[now] + 1, m + 1);
    // find(1, 4, 4);
  }
  // find(1, 4, 4);
  upmax(ans, tree[1]);
  for (R i = m; i; --i) {
    w = -1;
    change(1, 1, i + 1);
    for (R j = Head[i]; j; j = Next[j]) {
      now = date[j], w = 1;
      change(1, l_[now] + 1, i + 1);
    }
    find(1, 1, i + 1); // 左端点在后面就不合法了
  }
  printf("%d\n", ans);
}

int main() {
  pre();
  build(1, 1, m + 1); // 要多出一位来代表左端点取0的情况
  work();             // ri最大居然可以到m+1...
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define R register int
#define AC 400100
#define ac 1601000 // error!!!数据范围是2 00000, 不是1开头！！！

int n, m, ans = -INT_MAX, w;
int Head[AC], Next[ac], date[ac], tot;
int tree[ac], lazy[ac], l[ac], r[ac], l_[AC], r_[AC];

inline int read() {
  int x = 0;
  char c = getchar();
  while (c > '9' || c < '0')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}

inline void add(int f, int w) {
  date[++tot] = w, Next[tot] = Head[f], Head[f] = tot;
}

inline void upmax(int &a, int b) {
  if (b > a)
    a = b;
}

void pre() {
  n = read(), m = read();
  for (R i = 1; i <= n; i++)
    l_[i] = read(), r_[i] = read(), add(r_[i], i);
}

inline void pushdown(int x) {
  if (lazy[x]) {
    int ll = x * 2, rr = ll + 1;
    lazy[ll] += lazy[x], lazy[rr] += lazy[x];
    tree[ll] += lazy[x],
        tree[rr] +=
        lazy[x]; // 这里因为是+=，所以必须用lazy[x]，不然会将lazy[ll]中的一些东西重复统计
    lazy[x] = 0; // 最后才清空！！！！！！！！！！
  } // error!!!是区间加，不是赋值，不能直接覆盖，要+=
}

inline void update(int x) { tree[x] = max(tree[x * 2], tree[x * 2 + 1]); }

void build(int x, int ll, int rr) {
  l[x] = ll, r[x] = rr;
  if (ll == rr) {
    tree[x] = -ll + 1;
    return;
  }
  int mid = (ll + rr) >> 1;
  build(x * 2, ll, mid);
  build(x * 2 + 1, mid + 1, rr);
  update(x);
}

void change(int x, int ll, int rr) {
  pushdown(x);
  if (l[x] == ll && r[x] == rr) {
    lazy[x] += w, tree[x] += w;
    return;
  }
  int mid = (l[x] + r[x]) >> 1;
  if (rr <= mid)
    change(x * 2, ll, rr);
  else if (ll > mid)
    change(x * 2 + 1, ll, rr);
  else {
    change(x * 2, ll, mid);
    change(x * 2 + 1, mid + 1, rr);
  }
  update(x);
}

void find(int x, int ll, int rr) {
  pushdown(x);
  if (l[x] == ll && r[x] == rr) {
    upmax(ans, tree[x]);
    return;
  }
  int mid = (l[x] + r[x]) >> 1;
  if (rr <= mid)
    find(x * 2, ll, rr);
  else if (ll > mid)
    find(x * 2 + 1, ll, rr);
  else
    find(x * 2, ll, mid), find(x * 2 + 1, mid + 1, rr); // 这是取max啊，，，，
}

void work() {
  int now;
  ans = n - m; // r = 0的情况
  for (R i = Head[m + 1]; i; i = Next[i]) {
    now = date[i], w = 1;
    change(1, l_[now] + 1, m + 1);
    // find(1, 4, 4);
  }
  // find(1, 4, 4);
  upmax(ans, tree[1]);
  for (R i = m; i; --i) {
    w = -1;
    change(1, 1, i + 1);
    for (R j = Head[i]; j; j = Next[j]) {
      now = date[j], w = 1;
      change(1, l_[now] + 1, i + 1);
    }
    find(1, 1, i + 1); // 左端点在后面就不合法了
  }
  printf("%d\n", ans);
}

int main() {
  pre();
  build(1, 1, m + 1); // 要多出一位来代表左端点取0的情况
  work();             // ri最大居然可以到m+1...
  return 0;
}