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

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define INF 0x3f3f3f3f
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
const ll mod = 1000000007;
const double PI = acos(-1.0);
const double epsilon = PI / 180.0; // 角度转弧度
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const int N = 3e5 + 10, M = 1e6 + 10;
struct P {
  int h, w;
} a[N];
int h, w, m;
int fx[N], fy[N];
int main() {
  cin >> h >> w >> m;
  int mx1 = 0, mx2 = 0;
  for (int i = 1; i <= m; i++) {
    cin >> a[i].h >> a[i].w;
    fx[a[i].h]++;
    fy[a[i].w]++;
    mx1 = max(mx1, fx[a[i].h]);
    mx2 = max(mx2, fy[a[i].w]);
  }
  int cnt1 = 0, cnt2 = 0, cnt = 0;
  for (int i = 0; i < N; i++) {
    if (fx[i] == mx1)
      cnt1++;
    if (fy[i] == mx2)
      cnt2++;
  }
  for (int i = 1; i <= m; i++) {
    if (fx[a[i].h] == mx1 && fy[a[i].w] == mx2)
      cnt++;
  }
  cout << mx1 + mx2 - (cnt == ((ll)cnt1 * cnt2));
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define INF 0x3f3f3f3f
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
const ll mod = 1000000007;
const double PI = acos(-1.0);
const double epsilon = PI / 180.0; // 角度转弧度
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const int N = 3e5 + 10, M = 1e6 + 10;
struct P {
  int h, w;
} a[N];
int h, w, m;
int fx[N], fy[N];
int f1[N], f2[N];
map<PII, int> _hash;
int main() {
  cin >> h >> w >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i].h >> a[i].w;
    fx[a[i].h]++;
    fy[a[i].w]++;
    _hash[{a[i].h, a[i].w}] = 1;
  }
  int mx1 = 0, mx2 = 0;
  for (int i = 1; i <= m; i++) {
    mx1 = max(mx1, fx[a[i].h]);
    mx2 = max(mx2, fy[a[i].w]);
  }
  VI v1, v2;
  for (int i = 1; i <= m; i++) {
    if (fx[a[i].h] == mx1 && f1[a[i].h] == 0) {
      v1.pb(a[i].h);
      f1[a[i].h] = 1;
    }
    if (fy[a[i].w] == mx2 && f2[a[i].w] == 0) {
      v2.pb(a[i].w);
      f2[a[i].w] = 1;
    }
  }
  int ans = 0;
  for (int i = 0; i < v1.size(); i++) {
    for (int j = 0; j < v2.size(); j++) {
      int hh = v1[i], ww = v2[j];
      ans = max(ans, fx[hh] + fy[ww] - _hash[{hh, ww}]);
    }
  }
  cout << ans;
  return 0;
}