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

#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mset(x, y) memset((x), (y), sizeof(x))
#define sz(x) ((int)(x).size())
#define rep(i, a, b) for (i = a; i < b; i++)
#define per(i, a, b) for (i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define cerr cout
#define endl '\\n'
using namespace std;

string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A> string to_string(A);
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool f = 1;
  string r = "{";
  for (const auto &x : v) {
    if (!f)
      r += ", ";
    f = 0;
    r += to_string(x);
  }
  return r + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "] :", debug_out(__VA_ARGS__)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
const ld eps = 1e-7;
const ll mod = 1000000007;
const ll N = 998244353;

ll binpow(ll b, ll p) {
  ll ans = 1;
  while (p > 0) {
    if (p & 1)
      ans = (ans * b) % mod;
    b = (b * b) % mod;
    p >>= 1;
  }
  return ans;
}
ll modinv(ll x) { return binpow(x, mod - 2); }

struct PairCmp {
  bool operator()(const pii &lhs, const pii &rhs) const {
    return lhs.second < rhs.second;
  }
};

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int h, w, i, j, m, mx = 0, my = 0, curr = 0;
  cin >> h >> w >> m;
  int x[m], y[m];
  map<int, int> xc, yc;
  map<int, set<int>> xhas;
  rep(i, 0, m) {
    cin >> x[i] >> y[i];
    xc[x[i]]++;
    yc[y[i]]++;
    xhas[x[i]].insert(y[i]);
  }

  for (auto xi : xc)
    mx = max(mx, xi.ss);
  for (auto yi : yc)
    my = max(my, yi.ss);
  set<int> sx, sy;
  for (auto xi : xc) {
    if (mx == xi.ss)
      sx.insert(xi.ff);
  }
  for (auto yi : yc) {
    if (my == yi.ss)
      sy.insert(yi.ff);
  }
  // pr(sx, sy, mx, my);

  for (auto xi : sx) {
    for (auto yi : sy) {
      if (xhas[xi].find(yi) == xhas[xi].end()) {
        cout << mx + my;
        return 0;
      }
    }
  }
  cout << mx + my - 1;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mset(x, y) memset((x), (y), sizeof(x))
#define sz(x) ((int)(x).size())
#define rep(i, a, b) for (i = a; i < b; i++)
#define per(i, a, b) for (i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define cerr cout
#define endl '\\n'
using namespace std;

string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A> string to_string(A);
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool f = 1;
  string r = "{";
  for (const auto &x : v) {
    if (!f)
      r += ", ";
    f = 0;
    r += to_string(x);
  }
  return r + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "] :", debug_out(__VA_ARGS__)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
const ld eps = 1e-7;
const ll mod = 1000000007;
const ll N = 998244353;

ll binpow(ll b, ll p) {
  ll ans = 1;
  while (p > 0) {
    if (p & 1)
      ans = (ans * b) % mod;
    b = (b * b) % mod;
    p >>= 1;
  }
  return ans;
}
ll modinv(ll x) { return binpow(x, mod - 2); }

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int h, w, i, j, m, mx = 0, curr = 0;
  cin >> h >> w >> m;
  int x[m], y[m];
  map<int, int> xc, yc;
  rep(i, 0, m) {
    cin >> x[i] >> y[i];
    xc[x[i]]++;
    yc[y[i]]++;
  }
  map<int, map<int, int>> myx;
  for (auto xi : xc) {
    myx[xi.ff] = yc;
  }

  rep(i, 0, m) { myx[x[i]][y[i]]--; }

  for (auto xi : xc)
    mx = max(mx, xi.ss);
  set<int> s;
  for (auto xi : xc) {
    if (mx == xi.ss)
      s.insert(xi.ff);
  }
  for (auto t : s) {
    mx = 0;
    for (auto p : myx[t]) {
      mx = max(mx, p.ss);
    }

    curr = max(curr, xc[t] + mx);
  }
  cout << curr;
}