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

#include <bits/stdc++.h>
using namespace std;
#define task "atcoder"
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define BIT(x, i) (((x) >> (i)) & 1)
#define lb lower_bound
#define ub upper_bound
#define lla(v) (v).begin(), (v).end()
#define zs(v) int((v).size())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ar array
#define endl '\\n'
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007; /// 998244353
const int base = 311;
const int N = 3e5 + 5;

int cnth[N], cntc[N];
map<pii, int> M;
void solved() {
  int r, c, m;
  cin >> r >> c >> m;
  int mxc = 0, mxh = 0;
  for (int i = 1; i <= m; ++i) {
    int x, y;
    cin >> x >> y;
    cnth[x]++;
    cntc[y]++;
    mxc = max(mxc, cntc[y]);
    mxh = max(mxh, cnth[x]);
    M[mp(x, y)] = 1;
  }
  vector<int> s;
  for (int i = 1; i <= c; ++i) {
    if (cntc[i] == mxc)
      s.pb(i);
  }
  sort(lla(s));
  s.resize(unique(lla(s)) - s.begin());
  vector<int> q;
  for (int i = 1; i <= r; ++i) {
    if (cnth[i] == mxh)
      q.pb(i);
  }
  sort(lla(q));
  q.resize(unique(lla(q)) - q.begin());
  int ans = 0;
  for (int i : q)
    for (int j : s) {
      if (M[{i, j}] == 0) {
        cout << cnth[i] + cntc[j];
        return;
      }
      ans = max(ans, cnth[i] + cntc[j] - M[{i, j}]);
    }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen(task ".inp", "r")) {
    freopen(task ".inp", "r", stdin);
    freopen(task ".out", "w", stdout);
  }
  solved();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define task "atcoder"
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define BIT(x, i) (((x) >> (i)) & 1)
#define lb lower_bound
#define ub upper_bound
#define lla(v) (v).begin(), (v).end()
#define zs(v) int((v).size())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ar array
#define endl '\\n'
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007; /// 998244353
const int base = 311;
const int N = 3e5 + 5;

int cnth[N], cntc[N];
map<pii, int> M;
void solved() {
  int r, c, m;
  cin >> r >> c >> m;
  int mxc = 0, mxh = 0;
  for (int i = 1; i <= m; ++i) {
    int x, y;
    cin >> x >> y;
    cnth[x]++;
    cntc[y]++;
    mxc = max(mxc, cntc[y]);
    mxh = max(mxh, cnth[x]);
    M[mp(x, y)] = 1;
  }
  vector<int> s;
  for (int i = 1; i <= c; ++i) {
    if (cntc[i] == mxc)
      s.pb(i);
  }
  sort(lla(s));
  s.resize(unique(lla(s)) - s.begin());
  vector<int> q;
  for (int i = 1; i <= r; ++i) {
    if (cnth[i] == mxh)
      q.pb(i);
  }
  sort(lla(q));
  q.resize(unique(lla(q)) - q.begin());
  int ans = 0;
  for (int i : q)
    for (int j : s)
      ans = max(ans, cnth[i] + cntc[j] - M[{i, j}]);
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen(task ".inp", "r")) {
    freopen(task ".inp", "r", stdin);
    freopen(task ".out", "w", stdout);
  }
  solved();
}