Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ff first
#define ss second
#define PI acos(-1)
#define pb push_back
#define int long long
#define ld long double
#define sp fixed << setprecision
#define bp __builtin_popcountll
#define all(x) x.begin(), x.end()
#define pii pair<long long, long long>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

const int M = (1e9) + 7;
const int N = 300005;

int row[N], col[N];

signed main() {
  FAST int tc = 1;
  // cin>>tc;
  for (int ti = 1; ti <= tc; ti++) {
    int n, m, k;
    cin >> n >> m >> k;
    set<pii> s;
    while (k--) {
      int x, y;
      cin >> x >> y;
      s.insert({x, y});
      row[x]++;
      col[y]++;
    }
    int mr = 0, mc = 0;
    for (int i = 1; i <= n; i++)
      mr = max(mr, row[i]);
    for (int i = 1; i <= m; i++)
      mc = max(mc, col[i]);

    vector<int> r1, r2, c1, c2;
    for (int i = 1; i <= n; i++) {
      if (row[i] == mr)
        r1.pb(i);
      if (row[i] == mr - 1 && mr - 1 > 0)
        r2.pb(i);
    }
    for (int i = 1; i <= m; i++) {
      if (col[i] == mc)
        c1.pb(i);
      if (col[i] == mc - 1 && mc - 1 > 0)
        c2.pb(i);
    }
    int ans = 0, chk = mr + mc;
    for (int x : r1)
      for (int y : c1) {
        ans = max(ans, row[x] + col[y] - (s.find({x, y}) != s.end()));
        if (ans == chk)
          return cout << ans, 0;
      }
    /*for(int x:r1)for(int y:c2)ans = max(ans, row[x]+col[y]-(s.find({x,y}) !=
    s.end())); for(int x:r2)for(int y:c1)ans = max(ans,
    row[x]+col[y]-(s.find({x,y}) != s.end())); for(int x:r2)for(int y:c2)ans =
    max(ans, row[x]+col[y]-(s.find({x,y}) != s.end()));*/
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define ff first
#define ss second
#define PI acos(-1)
#define pb push_back
#define int long long
#define ld long double
#define sp fixed << setprecision
#define bp __builtin_popcountll
#define all(x) x.begin(), x.end()
#define pii pair<long long, long long>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

const int M = (1e9) + 7;
const int N = 300005;

int row[N], col[N];

signed main() {
  FAST int tc = 1;
  // cin>>tc;
  for (int ti = 1; ti <= tc; ti++) {
    int n, m, k;
    cin >> n >> m >> k;
    set<pii> s;
    while (k--) {
      int x, y;
      cin >> x >> y;
      s.insert({x, y});
      row[x]++;
      col[y]++;
    }
    int mr = 0, mc = 0;
    for (int i = 1; i <= n; i++)
      mr = max(mr, row[i]);
    for (int i = 1; i <= m; i++)
      mc = max(mc, col[i]);

    vector<int> r1, r2, c1, c2;
    for (int i = 1; i <= n; i++) {
      if (row[i] == mr)
        r1.pb(i);
      if (row[i] == mr - 1 && mr - 1 > 0)
        r2.pb(i);
    }
    for (int i = 1; i <= m; i++) {
      if (col[i] == mc)
        c1.pb(i);
      if (col[i] == mc - 1 && mc - 1 > 0)
        c2.pb(i);
    }
    int ans = 0;
    for (int x : r1)
      for (int y : c1)
        ans = max(ans, row[x] + col[y] - (s.find({x, y}) != s.end()));
    for (int x : r1)
      for (int y : c2)
        ans = max(ans, row[x] + col[y] - (s.find({x, y}) != s.end()));
    for (int x : r2)
      for (int y : c1)
        ans = max(ans, row[x] + col[y] - (s.find({x, y}) != s.end()));
    for (int x : r2)
      for (int y : c2)
        ans = max(ans, row[x] + col[y] - (s.find({x, y}) != s.end()));
    cout << ans << endl;
  }
  return 0;
}