Problem p03686 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define NFOR(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define endl "\n"
#define mp make_pair
#define X first
#define Y second
#define inf 1e9
#define mod 1000000007
#define pb push_back
#define Case cout << "Case #" << ++cas << ": ";
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(v) v.begin(), v.end()
#define sz(x) int(x.size())
// #define lli int
typedef long long int lli;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef pair<lli, lli> pll;
typedef vector<lli> vl;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef complex<long double> complex_t;
const long double PI = acos((long double)-1.0);

#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {
  cerr << str << " : " << t << "\n";
}
template <class T, class... S> void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ",";
  dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const set<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a)
      cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}

typedef struct node2 {
  int val;
  int lazyp;
} node1;

node1 tree[800010];
vi Adj[100010];
int n, m;

void build(int node, int b, int e) {
  if (b > e)
    return;
  if (b == e) {
    tree[node].val = b - (m + 1);
    tree[node].lazyp = 0;
    return;
  }
  build(2 * node, b, (b + e) / 2);
  build(2 * node + 1, (b + e) / 2 + 1, e);
  tree[node].val = max(tree[2 * node].val, tree[2 * node + 1].val);
  tree[node].lazyp = 0;
  return;
}

void push(int node, int b, int e) {
  if (tree[node].lazyp == 0)
    return;
  if (b != e) {
    tree[2 * node].lazyp += tree[node].lazyp;
    tree[2 * node + 1].lazyp += tree[node].lazyp;
  }
  tree[node].val += tree[node].lazyp;
  tree[node].lazyp = 0;
}

void update(int node, int b, int e, int i, int j, int val) {
  push(node, b, e);
  if (b > e || i > e || j < b)
    return;
  if (i <= b && e <= j) {
    tree[node].val += val;
    if (b != e) {
      tree[2 * node].lazyp += val, tree[2 * node + 1].lazyp += val;
    }
    return;
  }
  update(2 * node, b, (b + e) / 2, i, j, val);
  update(2 * node + 1, (b + e) / 2 + 1, e, i, j, val);
  tree[node].val =
      max(tree[node].val, max(tree[2 * node].val, tree[2 * node + 1].val));
}

void query(node1 &res, int node, int b, int e, int i, int j) {
  push(node, b, e);
  if (b > e || i > e || j < b)
    return;
  if (i <= b && e <= j) {
    res = tree[node];
    return;
  }
  node1 l, r;
  l.val = r.val = -inf;
  query(l, 2 * node, b, (b + e) / 2, i, j);
  query(r, 2 * node + 1, (b + e) / 2 + 1, e, i, j);
  res.val = max(l.val, r.val);
}

int main() {
  cin >> n >> m;
  FOR(i, 1, n) {
    int l1, r1;
    cin >> l1 >> r1;
    Adj[l1].pb(r1);
  }
  build(1, 0, m + 1);
  int answer1 = n - m;
  FOR(i, 0, m + 1) {
    // pr(i);
    for (auto v : Adj[i]) {
      update(1, 0, m + 1, 0, v, 1);
    }
    node1 res;
    res.val = 0;
    query(res, 1, 0, m + 1, 0, m + 1);
    answer1 = max(answer1, res.val - i);
  }
  cout << answer1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define NFOR(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define endl "\n"
#define mp make_pair
#define X first
#define Y second
#define inf 1e9
#define mod 1000000007
#define pb push_back
#define Case cout << "Case #" << ++cas << ": ";
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(v) v.begin(), v.end()
#define sz(x) int(x.size())
// #define lli int
typedef long long int lli;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef pair<lli, lli> pll;
typedef vector<lli> vl;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef complex<long double> complex_t;
const long double PI = acos((long double)-1.0);

#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {
  cerr << str << " : " << t << "\n";
}
template <class T, class... S> void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ",";
  dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const set<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a)
      cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}

typedef struct node2 {
  int val;
  int lazyp;
} node1;

node1 tree[800010];
vi Adj[200010];
int n, m;

void build(int node, int b, int e) {
  if (b > e)
    return;
  if (b == e) {
    tree[node].val = b - (m + 1);
    tree[node].lazyp = 0;
    return;
  }
  build(2 * node, b, (b + e) / 2);
  build(2 * node + 1, (b + e) / 2 + 1, e);
  tree[node].val = max(tree[2 * node].val, tree[2 * node + 1].val);
  tree[node].lazyp = 0;
  return;
}

void push(int node, int b, int e) {
  if (tree[node].lazyp == 0)
    return;
  if (b != e) {
    tree[2 * node].lazyp += tree[node].lazyp;
    tree[2 * node + 1].lazyp += tree[node].lazyp;
  }
  tree[node].val += tree[node].lazyp;
  tree[node].lazyp = 0;
}

void update(int node, int b, int e, int i, int j, int val) {
  push(node, b, e);
  if (b > e || i > e || j < b)
    return;
  if (i <= b && e <= j) {
    tree[node].val += val;
    if (b != e) {
      tree[2 * node].lazyp += val, tree[2 * node + 1].lazyp += val;
    }
    return;
  }
  update(2 * node, b, (b + e) / 2, i, j, val);
  update(2 * node + 1, (b + e) / 2 + 1, e, i, j, val);
  tree[node].val =
      max(tree[node].val, max(tree[2 * node].val, tree[2 * node + 1].val));
}

void query(node1 &res, int node, int b, int e, int i, int j) {
  push(node, b, e);
  if (b > e || i > e || j < b)
    return;
  if (i <= b && e <= j) {
    res = tree[node];
    return;
  }
  node1 l, r;
  l.val = r.val = -inf;
  query(l, 2 * node, b, (b + e) / 2, i, j);
  query(r, 2 * node + 1, (b + e) / 2 + 1, e, i, j);
  res.val = max(l.val, r.val);
}

int main() {
  cin >> n >> m;
  FOR(i, 1, n) {
    int l1, r1;
    cin >> l1 >> r1;
    Adj[l1].pb(r1);
  }
  build(1, 0, m + 1);
  int answer1 = n - m;
  FOR(i, 0, m + 1) {
    // pr(i);
    for (auto v : Adj[i]) {
      update(1, 0, m + 1, 0, v, 1);
    }
    node1 res;
    res.val = 0;
    query(res, 1, 0, m + 1, 0, m + 1);
    answer1 = max(answer1, res.val - i);
  }
  cout << answer1 << endl;
  return 0;
}