Problem p03686 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define debug(...) fprintf(stderr, __VA_ARGS__)

using namespace std;

const int maxn = 200005;
const int inf = 1e9;

struct data {
  int x, y;
  bool operator<(const data &other) const { return x < other.x; }
} a[maxn];
int n, m, mx[maxn], tag[maxn];

#define mid (l + r >> 1)
#define lson (o << 1)
#define rson (o << 1 | 1)
void pushup(int o) { mx[o] = max(mx[lson], mx[rson]); }

void pushdown(int o) {
  if (tag[o]) {
    mx[lson] += tag[o], tag[lson] += tag[o];
    mx[rson] += tag[o], tag[rson] += tag[o];
    tag[o] = 0;
  }
}

void build(int l, int r, int o) {
  if (l == r) {
    mx[o] = -(m + 1 - l);
    return;
  }
  build(l, mid, lson);
  build(mid + 1, r, rson);
  pushup(o);
}

int query(int l, int r, int o, int a, int b) {
  if (a <= l && r <= b)
    return mx[o];
  pushdown(o);
  int ans = 0;
  if (a <= mid)
    ans = max(ans, query(l, mid, lson, a, b));
  if (b > mid)
    ans = max(ans, query(mid + 1, r, rson, a, b));
  return ans;
}

void add(int l, int r, int o, int a, int b, int c) {
  if (a <= l && r <= b) {
    mx[o] += c, tag[o] += c;
    return;
  }
  pushdown(o);
  if (a <= mid)
    add(l, mid, lson, a, b, c);
  if (b > mid)
    add(mid + 1, r, rson, a, b, c);
  pushup(o);
}

int main() {
  scanf("%d%d", &n, &m);
  rep(i, 1, n) scanf("%d%d", &a[i].x, &a[i].y);
  sort(a + 1, a + n + 1);
  build(1, m + 1, 1);
  int ans = max(0, n - m);
  rep(i, 1, n) {
    add(1, m + 1, 1, 1, a[i].y, 1);
    ans = max(ans, query(1, m + 1, 1, a[i].x + 1, m + 1) - a[i].x);
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define debug(...) fprintf(stderr, __VA_ARGS__)

using namespace std;

const int maxn = 200005;
const int inf = 1e9;

struct data {
  int x, y;
  bool operator<(const data &other) const { return x < other.x; }
} a[maxn];
int n, m, mx[maxn << 2], tag[maxn << 2];

#define mid (l + r >> 1)
#define lson (o << 1)
#define rson (o << 1 | 1)
void pushup(int o) { mx[o] = max(mx[lson], mx[rson]); }

void pushdown(int o) {
  if (tag[o]) {
    mx[lson] += tag[o], tag[lson] += tag[o];
    mx[rson] += tag[o], tag[rson] += tag[o];
    tag[o] = 0;
  }
}

void build(int l, int r, int o) {
  if (l == r) {
    mx[o] = -(m + 1 - l);
    return;
  }
  build(l, mid, lson);
  build(mid + 1, r, rson);
  pushup(o);
}

int query(int l, int r, int o, int a, int b) {
  if (a <= l && r <= b)
    return mx[o];
  pushdown(o);
  int ans = 0;
  if (a <= mid)
    ans = max(ans, query(l, mid, lson, a, b));
  if (b > mid)
    ans = max(ans, query(mid + 1, r, rson, a, b));
  return ans;
}

void add(int l, int r, int o, int a, int b, int c) {
  if (a <= l && r <= b) {
    mx[o] += c, tag[o] += c;
    return;
  }
  pushdown(o);
  if (a <= mid)
    add(l, mid, lson, a, b, c);
  if (b > mid)
    add(mid + 1, r, rson, a, b, c);
  pushup(o);
}

int main() {
  scanf("%d%d", &n, &m);
  rep(i, 1, n) scanf("%d%d", &a[i].x, &a[i].y);
  sort(a + 1, a + n + 1);
  build(1, m + 1, 1);
  int ans = max(0, n - m);
  rep(i, 1, n) {
    add(1, m + 1, 1, 1, a[i].y, 1);
    ans = max(ans, query(1, m + 1, 1, a[i].x + 1, m + 1) - a[i].x);
  }
  printf("%d\n", ans);
  return 0;
}