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

#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define in insert
#define ff first
#define ss second
#define int long long
#define rep(i, x, m) for (int i = x; i < m; i++)
#define MOD 1000000007
#define endl "\\n"
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define vi vector<int>
#define pi pair<int, int>
#define sz(x) (int)x.size()
#define pq priority_queue<int>
#define fix(y) fixed << setprecision(y)
#define print(v)                                                               \\
  for (int i = 0; i < v.size(); i++)                                           \\
    cout << v[i] << " ";                                                       \\
  cout << endl
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;

int power(int n, int m) {
  int p = 1;
  if (m == 0)
    return 1;
  p = (power(n, m / 2) % MOD);
  p = (p % MOD * p % MOD) % MOD;
  return (m & 1 ? ((p % MOD * n % MOD) % MOD) : (p % MOD));
}

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  set<pi> s;
  map<int, int> m1, m2, c1, c2;
  while (m--) {
    int x, y;
    cin >> x >> y;
    m1[x]++;
    m2[y]++;
    c1[x]++;
    c2[y]++;
    s.in({x, y});
  }

  int sum = 0;
  int mi = -1;
  int ax;
  for (auto x : m1) {
    if (x.ss > mi) {
      ax = x.ff;
      mi = x.ss;
    }
  }
  sum = mi;
  for (auto x : s) {
    if (x.ff == ax) {
      m2[x.ss]--;
    }
  }
  mi = 0;
  for (auto x : m2) {
    mi = max(mi, x.ss);
  }
  sum += mi;

  int sum1 = 0;
  mi = 0;
  int ay;
  for (auto x : c2) {
    if (x.ss > mi) {
      ay = x.ff;
      mi = x.ss;
    }
  }
  // cout<<ay<<endl;
  sum1 = mi;
  for (auto x : s) {
    if (x.ss == ay) {
      c1[x.ff]--;
    }
  }
  mi = 0;
  for (auto x : c1) {
    mi = max(mi, x.ss);
  }
  sum1 += mi;
  cout << max(sum1, sum);
}

signed main() {
  FIO

      int t = 1;
  // cin>>t;
  while (t--)
    solve();
  cerr << (float)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define in insert
#define ff first
#define ss second
#define int long long
#define rep(i, x, m) for (int i = x; i < m; i++)
#define MOD 1000000007
#define endl "\\n"
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define vi vector<int>
#define pi pair<int, int>
#define sz(x) (int)x.size()
#define pq priority_queue<int>
#define fix(y) fixed << setprecision(y)
#define print(v)                                                               \\
  for (int i = 0; i < v.size(); i++)                                           \\
    cout << v[i] << " ";                                                       \\
  cout << endl
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;

int power(int n, int m) {
  int p = 1;
  if (m == 0)
    return 1;
  p = (power(n, m / 2) % MOD);
  p = (p % MOD * p % MOD) % MOD;
  return (m & 1 ? ((p % MOD * n % MOD) % MOD) : (p % MOD));
}

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  set<pi> s;
  map<int, int> m1, m2;
  while (m--) {
    int x, y;
    cin >> x >> y;
    m1[x]++;
    m2[y]++;
    s.in({x, y});
  }
  int sum = 0;
  int mi = -1;
  rep(i, 1, h + 1) {
    rep(j, 1, w + 1) {
      sum = m1[i] + m2[j];
      if (s.count({i, j}) == 1)
        sum -= 1;
      // cout<<i<<":"<<j<<"::"<<sum<<endl;
      mi = max(mi, sum);
    }
  }
  cout << mi << endl;
}

signed main() {
  FIO

      int t = 1;
  // cin>>t;
  while (t--)
    solve();
  cerr << (float)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl;

  return 0;
}