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

#include <bits/stdc++.h>
using namespace std;
#define pr(args...)                                                            \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
#define all(v) (v).begin(), (v).end()
#define int long long
#define sz(v) (int)(v).size()
#define mod 1000000007 // ((a- b)% mod+ mod)% mod

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

map<int, int> mp[300001];

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> r(n + 1), c(m + 1);

  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    mp[x][y] = 1;
    r[x]++;
    c[y]++;
  }

  int mxr = *max_element(all(r));
  int mxc = *max_element(all(c));

  vector<int> anr, anc;

  for (int i = 0; i < n; i++)
    if (r[i] == mxr)
      anr.push_back(i);

  for (int i = 0; i < m; i++)
    if (c[i] == mxc)
      anc.push_back(i);

  n = anr.size(), m = anc.size();
  int ans = 0;
  bool ok = 0;

  for (int i = 0; i < anr.size(); i++) {
    for (int j = 0; j < anc.size(); j++) {
      ans = max(ans, mxr + mxc - mp[anr[i]][anc[j]]);
      if (ans == mxr + mxc) {
        cout << ans << "\\n";
        ok = 1;
        break;
      }
    }
    if (ok)
      break;
  }
  if (!ok)
    cout << ans;
}

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}


Error: 1

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pr(args...)                                                            \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
#define all(v) (v).begin(), (v).end()
#define int long long
#define sz(v) (int)(v).size()
#define mod 1000000007 // ((a- b)% mod+ mod)% mod

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

map<int, int> mp[300001];

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> r(n + 1), c(m + 1);

  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    mp[x][y] = 1;
    r[x]++;
    c[y]++;
  }

  int mxr = *max_element(all(r));
  int mxc = *max_element(all(c));

  vector<int> anr, anc;

  for (int i = 0; i < n; i++)
    if (r[i] == mxr)
      anr.push_back(i);

  for (int i = 0; i < m; i++)
    if (c[i] == mxc)
      anc.push_back(i);

  n = anr.size(), m = anc.size();
  int ans = 0;

  for (int i = 0; i < anr.size(); i++) {
    for (int j = 0; j < anc.size(); j++) {
      ans = max(ans, mxr + mxc - mp[anr[i]][anc[j]]);
      if (ans == mxr + mxc) {
        cout << ans << "\\n";
        exit(1);
      }
    }
  }
  cout << ans;
}

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}