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

#include "bits/stdc++.h"

using namespace std;

#define endl "\\n"
typedef long long int ll;
#define f first
#define s second

inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
inline void setPrecision(int n) { cout.precision(n); }

// DEBUG
#define dbg(x) cerr << (#x) << ": " << x << endl
#define dbgV(x)                                                                \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << it << " ";                                                         \\
  cerr << endl;
#define dbgS(x)                                                                \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << it << " ";                                                         \\
  cerr << endl;
#define dbgM(x)                                                                \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << "[" << it.f << ", " << it.s << "] ";                               \\
  cerr << endl;
#define dbg2D(x)                                                               \\
  cerr << (#x) << ": \\n";                                                      \\
  for (auto y : x) {                                                           \\
    for (auto it : y)                                                          \\
      cerr << it << " ";                                                       \\
    cerr << endl;                                                              \\
  }                                                                            \\
  cerr << endl;
#define dbgA(x, n)                                                             \\
  cerr << (#x) << ": ";                                                        \\
  for (int i = 0; i < n; ++i)                                                  \\
    cerr << x[i] << " ";                                                       \\
  cerr << endl;
#define dbgVP(x)                                                               \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << "[" << it.f << ", " << it.s << "] ";                               \\
  cerr << endl;

ll INF = 1e10;
int MOD = 1e9 + 7;

int main() {
  fastio();

  int h, w, m;
  cin >> h >> w >> m;
  map<int, int> x, y;
  set<pair<int, int>> t;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    x[a]++;
    y[b]++;
    t.insert({a, b});
  }
  int mx = 0;
  for (auto it : x) {
    mx = max(mx, it.s);
  }
  int my = 0;
  for (auto it : y) {
    my = max(my, it.s);
  }

  set<pair<int, int>> px, py;
  for (auto it : x)
    if (it.s == mx)
      px.insert({it.f, it.s});

  for (auto it : y) {
    if (it.s == my)
      py.insert({it.f, it.s});
  }

  int ans = 0;
  for (auto it1 : px) {
    for (auto it2 : py) {
      ans = max(ans, it1.s + it2.s - (int)t.count({it1.f, it2.f}));
      if (ans == it1.s + it2.s)
        break;
    }
  }
  cout << ans;
  return 0;
}

/*

*/

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define endl "\\n"
typedef long long int ll;
#define f first
#define s second

inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
inline void setPrecision(int n) { cout.precision(n); }

// DEBUG
#define dbg(x) cerr << (#x) << ": " << x << endl
#define dbgV(x)                                                                \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << it << " ";                                                         \\
  cerr << endl;
#define dbgS(x)                                                                \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << it << " ";                                                         \\
  cerr << endl;
#define dbgM(x)                                                                \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << "[" << it.f << ", " << it.s << "] ";                               \\
  cerr << endl;
#define dbg2D(x)                                                               \\
  cerr << (#x) << ": \\n";                                                      \\
  for (auto y : x) {                                                           \\
    for (auto it : y)                                                          \\
      cerr << it << " ";                                                       \\
    cerr << endl;                                                              \\
  }                                                                            \\
  cerr << endl;
#define dbgA(x, n)                                                             \\
  cerr << (#x) << ": ";                                                        \\
  for (int i = 0; i < n; ++i)                                                  \\
    cerr << x[i] << " ";                                                       \\
  cerr << endl;
#define dbgVP(x)                                                               \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << "[" << it.f << ", " << it.s << "] ";                               \\
  cerr << endl;

ll INF = 1e10;
int MOD = 1e9 + 7;

int main() {
  fastio();

  int h, w, m;
  cin >> h >> w >> m;
  map<int, int> x, y;
  set<pair<int, int>> t;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    x[a]++;
    y[b]++;
    t.insert({a, b});
  }
  int mx = 0;
  for (auto it : x) {
    mx = max(mx, it.s);
  }
  int my = 0;
  for (auto it : y) {
    my = max(my, it.s);
  }

  set<pair<int, int>> px, py;
  for (auto it : x)
    if (it.s == mx)
      px.insert({it.f, it.s});

  for (auto it : y) {
    if (it.s == my)
      py.insert({it.f, it.s});
  }

  int ans = 0;
  for (auto it1 : px) {
    for (auto it2 : py) {
      ans = max(ans, it1.s + it2.s - (int)t.count({it1.f, it2.f}));
    }
  }
  cout << ans;
  return 0;
}

/*

*/