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

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define fi first
#define se second
#define inf (INT_MAX / 2 - 1)
#define infl (1LL << 60)
#define vi vector<int>
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) begin(a), end(a)
#define y0 y5656
#define y1 y7878
#define aaa system("pause");
#define dbg(x) cerr << (#x) << ": " << (x) << '\\n', aaa
#define dbga(x, n)                                                             \\
  cerr << (#x) << "[]: ";                                                      \\
  for (int _ = 0; _ < n; _++)                                                  \\
    cerr << x[_] << ' ';                                                       \\
  cerr << '\\n', aaa
#define dbgs(x)                                                                \\
  cerr << (#x) << "[stl]: ";                                                   \\
  for (int _ : x)                                                              \\
    cerr << _ << ' ';                                                          \\
  cerr << '\\n', aaa
#define dbgp(x) cerr << (#x) << ": " << x.fi << ' ' << x.se << '\\n', aaa
#define int ll
#define maxn 300000

using namespace std;

int n, m, k;

struct AINT {
  int yy[4 * maxn + 5];
  void upd(int p, pii itv, int x, int val) {
    if (x < itv.fi || x > itv.se)
      return;
    if (itv.fi == itv.se) {
      yy[p] = val;
      return;
    }
    if (itv.fi < itv.se) {
      int mid = (itv.fi + itv.se) / 2;
      upd(p * 2, pii(itv.fi, mid), x, val);
      upd(p * 2 + 1, pii(mid + 1, itv.se), x, val);
      yy[p] = max(yy[p * 2], yy[p * 2 + 1]);
    }
  }
  void pupd(int x, int val) { upd(1, pii(1, m), x, val); }
  int q_query;
  void qry(int p, pii itv, pii ok) {
    if (itv.se < ok.fi || ok.se < itv.fi)
      return;
    if (itv.fi >= ok.fi && itv.se <= ok.se) {
      q_query = max(q_query, yy[p]);
      return;
    }
    int mid = (itv.fi + itv.se) / 2;
    qry(p * 2, pii(itv.fi, mid), ok);
    qry(p * 2 + 1, pii(mid + 1, itv.se), ok);
  }
  int pqry(pii ok) {
    q_query = 0;
    qry(1, pii(1, m), ok);
    return q_query;
  }
};

AINT aint;
pii v[maxn + 5];
vi linii[maxn + 5], coloane[maxn + 5];
int sc[maxn + 5];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  int i, j, z;
  for (i = 1; i <= k; i++) {
    cin >> v[i].fi >> v[i].se;
    linii[v[i].fi].pb(i);
    coloane[v[i].se].pb(i);
  }
  for (j = 1; j <= m; j++) {
    sc[j] = sz(coloane[j]);
    aint.pupd(j, sc[j]);
  }
  int ans = 0, now;
  for (i = 1; i <= n; i++) {
    /// vreau sa pun bomba pe linia i
    now = sz(linii[i]);
    for (int x : linii[i]) {
      sc[v[x].se]--;
      aint.pupd(v[x].se, sc[v[x].se]);
    }
    ans = max(ans, now + aint.pqry(pii(1, m)));
    for (int x : linii[i]) {
      sc[v[x].se]++;
      aint.pupd(v[x].se, sc[v[x].se]);
    }
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define fi first
#define se second
#define inf (INT_MAX / 2 - 1)
#define infl (1LL << 60)
#define vi vector<int>
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) begin(a), end(a)
#define y0 y5656
#define y1 y7878
#define aaa system("pause");
#define dbg(x) cerr << (#x) << ": " << (x) << '\\n', aaa
#define dbga(x, n)                                                             \\
  cerr << (#x) << "[]: ";                                                      \\
  for (int _ = 0; _ < n; _++)                                                  \\
    cerr << x[_] << ' ';                                                       \\
  cerr << '\\n', aaa
#define dbgs(x)                                                                \\
  cerr << (#x) << "[stl]: ";                                                   \\
  for (int _ : x)                                                              \\
    cerr << _ << ' ';                                                          \\
  cerr << '\\n', aaa
#define dbgp(x) cerr << (#x) << ": " << x.fi << ' ' << x.se << '\\n', aaa
#define int ll
#define maxn 300000

using namespace std;

int n, m, k;

struct AINT {
  int yy[4 * maxn + 5];
  void upd(int p, pii itv, int x, int val) {
    if (itv.fi == itv.se) {
      yy[p] = val;
      return;
    }
    if (x < itv.fi || x > itv.se)
      return;
    if (itv.fi < itv.se) {
      int mid = (itv.fi + itv.se) / 2;
      upd(p * 2, pii(itv.fi, mid), x, val);
      upd(p * 2 + 1, pii(mid + 1, itv.se + 1), x, val);
      yy[p] = max(yy[p * 2], yy[p * 2 + 1]);
    }
  }
  void pupd(int x, int val) { upd(1, pii(1, m), x, val); }
  int q_query;
  void qry(int p, pii itv, pii ok) {
    if (itv.se < ok.fi || ok.se < itv.fi)
      return;
    if (itv.fi >= ok.fi && itv.se <= ok.se) {
      q_query = max(q_query, yy[p]);
      return;
    }
    int mid = (itv.fi + itv.se) / 2;
    qry(p * 2, pii(itv.fi, mid), ok);
    qry(p * 2 + 1, pii(mid + 1, itv.se), ok);
  }
  int pqry(pii ok) {
    q_query = 0;
    qry(1, pii(1, m), ok);
    return q_query;
  }
};

AINT aint;
pii v[maxn + 5];
vi linii[maxn + 5], coloane[maxn + 5];
int sc[maxn + 5];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  int i, j, z;
  for (i = 1; i <= k; i++) {
    cin >> v[i].fi >> v[i].se;
    linii[v[i].fi].pb(i);
    coloane[v[i].se].pb(i);
  }
  for (j = 1; j <= m; j++) {
    sc[j] = sz(coloane[j]);
    aint.pupd(j, sc[j]);
  }
  int ans = 0, now;
  for (i = 1; i <= n; i++) {
    /// vreau sa pun bomba pe linia i
    now = sz(linii[i]);
    for (int x : linii[i]) {
      sc[v[x].se]--;
      aint.pupd(v[x].se, sc[v[x].se]);
    }
    ans = max(ans, now + aint.pqry(pii(1, m)));
    for (int x : linii[i]) {
      sc[v[x].se]++;
      aint.pupd(v[x].se, sc[v[x].se]);
    }
  }
  cout << ans;
  return 0;
}