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

/* بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ */
// codeforces
#include <bits/stdc++.h>
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define mp make_pair
#define pb push_back
#define sz(v) ((int)v.size())
#define all(v) v.begin(), v.end()
void parseArray(ll *A, ll n) {
  for (ll K = 0; K < n; K++) {
    cin >> A[K];
  }
}
ll modInverse(ll a, ll b) {
  return 1 < a ? b - modInverse(b % a, a) * b / a : 1;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ld dist(ld x, ld y, ld a, ld b) {
  return sqrt((x - a) * (x - a) + (y - b) * (y - b));
}
void debug(ll *a, ll n) {
  for (ll k = 0; k < n; k++) {
    cerr << a[k] << " ";
  }
  cout << "\\n";
}
#define PI 3.14159265358979323846
#define FF first
#define SS second

int main() {
  FASTIO;
  ll h, w, n;
  cin >> h >> w >> n;
  ll x[333333], y[333333];
  memset(x, 0, sizeof x);
  memset(y, 0, sizeof y);
  vector<ll> M[333333];
  set<pair<ll, ll>> sorted;
  for (int k = 0; k < n; k++) {
    ll a, b;
    cin >> a >> b;
    x[a]++, y[b]++;
    M[a].pb(b);
  }
  for (int k = 0; k < 333333; k++) {
    if (y[k])
      sorted.insert(mp(y[k], k));
  }
  ll ans = 0;
  ll cur = 3333333;
  for (int k = 0; k < 333333; k++) {
    if (M[k].empty()) {
      ans = max(ans, x[k]);
      continue;
    }
    map<ll, ll> cnt;
    for (ll e : M[k]) {
      cnt[e]++;
    }
    for (const auto &it : cnt) {
      sorted.erase(sorted.lower_bound(mp(y[it.first], 0)));
      sorted.insert(mp(y[it.first] - it.second, cur++));
    }
    ans = max(ans, x[k] + sorted.rbegin()->first);
    for (const auto &it : cnt) {
      sorted.erase(sorted.lower_bound(mp(y[it.first] - it.second, 0)));
      sorted.insert(mp(y[it.first], cur++));
    }
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

/* بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ */
// codeforces
#include <bits/stdc++.h>
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define mp make_pair
#define pb push_back
#define sz(v) ((int)v.size())
#define all(v) v.begin(), v.end()
void parseArray(ll *A, ll n) {
  for (ll K = 0; K < n; K++) {
    cin >> A[K];
  }
}
ll modInverse(ll a, ll b) {
  return 1 < a ? b - modInverse(b % a, a) * b / a : 1;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ld dist(ld x, ld y, ld a, ld b) {
  return sqrt((x - a) * (x - a) + (y - b) * (y - b));
}
void debug(ll *a, ll n) {
  for (ll k = 0; k < n; k++) {
    cerr << a[k] << " ";
  }
  cout << "\\n";
}
#define PI 3.14159265358979323846
#define FF first
#define SS second

int main() {
  FASTIO;
  ll h, w, n;
  cin >> h >> w >> n;
  ll x[333333], y[333333];
  memset(x, 0, sizeof x);
  memset(y, 0, sizeof y);
  vector<ll> M[333333];
  set<pair<ll, ll>> sorted;
  for (int k = 0; k < n; k++) {
    ll a, b;
    cin >> a >> b;
    x[a]++, y[b]++;
    M[a].pb(b);
  }
  for (int k = 0; k < 333333; k++) {
    if (y[k])
      sorted.insert(mp(y[k], k));
  }
  ll ans = 0;
  ll cur = n;
  for (int k = 0; k < 333333; k++) {
    if (M[k].empty()) {
      ans = max(ans, x[k]);
      continue;
    }
    map<ll, ll> cnt;
    for (ll e : M[k]) {
      cnt[e]++;
    }
    for (const auto &it : cnt) {
      sorted.erase(sorted.lower_bound(mp(y[it.first], 0)));
      sorted.insert(mp(y[it.first] - it.second, cur++));
    }
    ans = max(ans, x[k] + sorted.rbegin()->first);
    for (const auto &it : cnt) {
      sorted.erase(sorted.lower_bound(mp(y[it.first] - it.second, 0)));
      sorted.insert(mp(y[it.first], cur++));
    }
  }
  cout << ans << endl;
  return 0;
}