Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define endl '\n'

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

const ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// head

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  // find most common row, most common column, then

  set<pair<int, int>> ps;
  int r, c, m;
  cin >> r >> c >> m;
  vector<int> rows(r + 1);
  vector<int> cols(c + 1);

  for (int i = 0; i < m; i++) {
    int y, x;
    cin >> y >> x;
    rows[y]++;
    cols[x]++;
    ps.insert(mp(y, x));
  }

  vector<int> indexRows;
  vector<int> indexCols;
  int bestRow = 0;
  int bestCol = 0;
  for (int i = 1; i <= r; i++) {
    if (rows[i] == bestRow) {
      indexRows.pb(i);
    } else if (rows[i] > bestRow) {
      indexRows.clear();
      indexRows.pb(i);
      bestRow = rows[i];
    }
  }
  for (int i = 1; i <= c; i++) {
    if (cols[i] == bestCol) {
      indexCols.pb(i);
    } else if (cols[i] > bestCol) {
      indexCols.clear();
      indexCols.pb(i);
      bestCol = cols[i];
    }
  }
  int ans = 0;
  trav(x, indexRows) {
    trav(y, indexCols) {
      if (ps.count(mp(x, y))) {
        ans = max(ans, bestRow + bestCol - 1);
      } else {
        ans = max(ans, bestRow + bestCol);
      }
    }
  }
  cout << ans << endl;

  return 0;
}


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

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define endl '\n'

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

const ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// head

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  // find most common row, most common column, then

  set<pair<int, int>> ps;
  int r, c, m;
  cin >> r >> c >> m;
  vector<int> rows(r + 1);
  vector<int> cols(c + 1);

  for (int i = 0; i < m; i++) {
    int y, x;
    cin >> y >> x;
    rows[y]++;
    cols[x]++;
    ps.insert(mp(y, x));
  }

  vector<int> indexRows;
  vector<int> indexCols;
  int bestRow = 0;
  int bestCol = 0;
  for (int i = 1; i <= r; i++) {
    if (rows[i] == bestRow) {
      indexRows.pb(i);
    } else if (rows[i] > bestRow) {
      indexRows.clear();
      indexRows.pb(i);
      bestRow = rows[i];
    }
  }
  for (int i = 1; i <= c; i++) {
    if (cols[i] == bestCol) {
      indexCols.pb(i);
    } else if (cols[i] > bestCol) {
      indexCols.clear();
      indexCols.pb(i);
      bestCol = cols[i];
    }
  }
  int ans = 0;
  trav(x, indexRows) {
    trav(y, indexCols) {
      if (ps.count(mp(x, y))) {
        ans = max(ans, bestRow + bestCol - 1);
      } else {
        ans = max(ans, bestRow + bestCol);
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;

  return 0;
}