Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ld long double

#define pb push_back
#define F first
#define S second
#define I insert
#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()

#define debug1(a) cerr << " " << #a << " : " << a << "\\n"
#define debug2(a, b)                                                           \\
  cerr << " " << #a << " : " << a << " " << #b << " : " << b << "\\n"
#define debug3(a, b, c)                                                        \\
  cerr << " " << #a << " : " << a << " " << #b << " : " << b << " " << #c      \\
       << " : " << c << "\\n"
#define debug4(a, b, c, d)                                                     \\
  cerr << " " << #a << " : " << a << " " << #b << " : " << b << " " << #c      \\
       << " : " << c << " " << #d << " : " << d << "\\n"

#define INF (int)100000000000000000
#define PI (double)3.14159265358979
#define MOD 1000000007

struct Node {
  // Lazy Propagation
  // Node N(vector , v.begin(), v.end());
  // N.query(v.begin(), v.end());
  // N.add(v.begin(), v.end());
  Node *l = 0, *r = 0;
  int lo, hi;
  int madd = 0, val = -INF;
  Node(vector<int> &v, int lo, int hi) : lo(lo), hi(hi) {
    if (lo + 1 < hi) {
      int mid = lo + (hi - lo) / 2;
      l = new Node(v, lo, mid);
      r = new Node(v, mid, hi);
      val = max(l->val, r->val);
    } else
      val = v[lo];
  }
  int query(int L, int R) {
    if (R <= lo || hi <= L)
      return -INF;
    if (L <= lo && hi <= R)
      return val;
    push();
    return max(l->query(L, R), r->query(L, R));
  }
  void add(int L, int R, int x) {
    if (R <= lo || hi <= L)
      return;
    if (L <= lo && hi <= R) {
      madd += x;
      val += x;
    } else {
      push(), l->add(L, R, x), r->add(L, R, x);
      val = max(l->val, r->val);
    }
  }
  void push() {
    if (madd)
      l->add(lo, hi, madd), r->add(lo, hi, madd), madd = 0;
  }
};

vector<vector<int32_t>> r;
vector<int> c;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int h, w, m;

  cin >> h >> w >> m;

  r.resize(h + 5);
  c.resize(w + 5, 0);

  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    r[x].pb(y);
    c[y]++;
  }
  // for (auto j:c)
  //     debug1(j);
  int res = 0;
  Node N(c, 0, w + 1);
  for (int i = 1; i <= h; ++i) {
    int cur = 0;
    cur += sz(r[i]);
    for (auto j : r[i])
      N.add(j, j + 1, -1);
    cur += N.query(1, w + 1);
    // debug3(i, sz(r[i]), N.query(1, w+1));
    res = max(res, cur);
    for (auto j : r[i])
      N.add(j, j + 1, 1);
  }
  cout << res << "\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ld long double

#define pb push_back
#define F first
#define S second
#define I insert
#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()

#define debug1(a) cerr << " " << #a << " : " << a << "\\n"
#define debug2(a, b)                                                           \\
  cerr << " " << #a << " : " << a << " " << #b << " : " << b << "\\n"
#define debug3(a, b, c)                                                        \\
  cerr << " " << #a << " : " << a << " " << #b << " : " << b << " " << #c      \\
       << " : " << c << "\\n"
#define debug4(a, b, c, d)                                                     \\
  cerr << " " << #a << " : " << a << " " << #b << " : " << b << " " << #c      \\
       << " : " << c << " " << #d << " : " << d << "\\n"

#define INF (int)100000000000000000
#define PI (double)3.14159265358979
#define MOD 1000000007

struct Node {
  // Lazy Propagation
  // Node N(vector , v.begin(), v.end());
  // N.query(v.begin(), v.end());
  // N.add(v.begin(), v.end());
  Node *l = 0, *r = 0;
  int lo, hi;
  int madd = 0, val = -INF;
  Node(vector<int> &v, int lo, int hi) : lo(lo), hi(hi) {
    if (lo + 1 < hi) {
      int mid = lo + (hi - lo) / 2;
      l = new Node(v, lo, mid);
      r = new Node(v, mid, hi);
      val = max(l->val, r->val);
    } else
      val = v[lo];
  }
  int query(int L, int R) {
    if (R <= lo || hi <= L)
      return -INF;
    if (L <= lo && hi <= R)
      return val;
    push();
    return max(l->query(L, R), r->query(L, R));
  }
  void add(int L, int R, int x) {
    if (R <= lo || hi <= L)
      return;
    if (L <= lo && hi <= R) {
      madd += x;
      val += x;
    } else {
      push(), l->add(L, R, x), r->add(L, R, x);
      val = max(l->val, r->val);
    }
  }
  void push() {
    if (madd)
      l->add(lo, hi, madd), r->add(lo, hi, madd), madd = 0;
  }
};

vector<vector<int32_t>> r;
vector<int> c;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int h, w, m;

  cin >> h >> w >> m;

  r.resize(h + 5);
  c.resize(w + 5, 0);

  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    r[x].pb(y);
    c[y]++;
  }
  int res = 0;
  Node N(c, 0, h + 1);
  for (int i = 1; i <= h; ++i) {
    int cur = 0;
    cur += sz(r[i]);
    for (auto j : r[i])
      N.add(j, j + 1, -1);
    cur += N.query(1, w + 1);
    res = max(res, cur);
    for (auto j : r[i])
      N.add(j, j + 1, 1);
  }
  cout << res << "\\n";

  return 0;
}