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

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define po pop_back
#define fi first
#define se second
#define mxn 100005
#define ll long long int
#define ldb long double
#define fr(i, a, b, k) for (int i = a; i < b; i += k)
#define frr(i, a, b, k) for (int i = a; i > b; i -= k)
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define deg(x) cout << endl << #x << " = " << (x) << endl;
#define casep(x) cout << "Case #" << (x) << ": ";
#define fast                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \\
  srand(time(NULL));

typedef pair<int, int> pp;
typedef vector<int> vi;
typedef vector<pp> vpp;
typedef vector<vi> vvi;

const int inf = INT_MAX;

ll power(int a, int n, int mod) {
  ll p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}
int gcd(int a, int b) { return __gcd(a, b); }
int lcm(int a, int b) { return a * b / __gcd(a, b); }
vector<vector<int>> adj;
bool visited[mxn];

void solve(ll test) {

  ll h, w, m;
  cin >> h >> w >> m;

  map<ll, ll> row;
  map<ll, ll> col;
  map<pp, ll> target;

  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    target[{x, y}]++;
    row[x]++;
    col[y]++;
  }

  vpp r;
  vpp c;
  for (auto i : row) {
    r.pb({i.se, i.fi});
  }
  for (auto i : col) {
    c.pb({i.se, i.fi});
  }

  sort(all(r), greater<pp>());
  sort(all(c), greater<pp>());

  ll index1 = 0;
  while (r[index1].fi == r[index1 + 1].fi && index1 + 1 < r.size()) {
    index1++;
  }

  ll index2 = 0;
  while (c[index2].fi == c[index2 + 1].fi && index2 + 1 < c.size()) {
    index2++;
  }

  // deg(index1);
  // deg(index2);
  ll ans = -inf;
  for (ll i = 0; i <= index1; i++) {
    bool ok = 0;
    for (ll j = 0; j <= index2; j++) {
      if (target[{r[i].se, c[j].se}]) {
        ll temp = (r[i].fi + c[j].fi) - 1;
        ans = max(temp, ans);
      } else {
        ll temp = (r[i].fi + c[j].fi);
        ans = max(ans, temp);
        ok = 1;
        break;
      }
    }
    if (ok)
      break;
  }

  cout << ans;
}

int main() {

  fast;
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    solve(i);
    cout << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define po pop_back
#define fi first
#define se second
#define mxn 100005
#define ll long long int
#define ldb long double
#define fr(i, a, b, k) for (int i = a; i < b; i += k)
#define frr(i, a, b, k) for (int i = a; i > b; i -= k)
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define deg(x) cout << endl << #x << " = " << (x) << endl;
#define casep(x) cout << "Case #" << (x) << ": ";
#define fast                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \\
  srand(time(NULL));

typedef pair<int, int> pp;
typedef vector<int> vi;
typedef vector<pp> vpp;
typedef vector<vi> vvi;

const int inf = INT_MAX;

ll power(int a, int n, int mod) {
  ll p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}
int gcd(int a, int b) { return __gcd(a, b); }
int lcm(int a, int b) { return a * b / __gcd(a, b); }
vector<vector<int>> adj;
bool visited[mxn];

void solve(ll test) {

  ll h, w, m;
  cin >> h >> w >> m;

  map<ll, ll> row;
  map<ll, ll> col;
  map<pp, ll> target;

  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    target[{x, y}]++;
    row[x]++;
    col[y]++;
  }

  vpp r;
  vpp c;
  for (auto i : row) {
    r.pb({i.se, i.fi});
  }
  for (auto i : col) {
    c.pb({i.se, i.fi});
  }

  sort(all(r), greater<pp>());
  sort(all(c), greater<pp>());

  while (r.back().fi != r[0].fi) {
    r.po();
  }

  while (c.back().se != c[0].fi) {
    c.po();
  }

  ll ans = -inf;
  for (ll i = 0; i < r.size(); i++) {
    bool ok = 0;
    for (ll j = 0; j < c.size(); j++) {
      int count = 0;
      if (target[{r[i].se, c[j].se}])
        count++;
      ll temp = (r[i].fi + c[j].fi) - count;
      ans = max(ans, temp);
      if (count == 0) {
        ok = 1;
        break;
      }
    }
    if (ok)
      break;
  }

  cout << ans;
}

int main() {

  fast;
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    solve(i);
    cout << endl;
  }
  return 0;
}