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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << '\\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " | ";
  __f(comma + 1, args...);
}
ll max(ll a, ll b) { return (a > b ? a : b); }
ll min(ll a, ll b) { return (a < b ? a : b); }
#define all(x) (x).begin(), (x).end()

void solve() {
  ll ans = 0;
  ll h, w, bombs;
  cin >> h >> w >> bombs;
  ll u, v;
  map<ll, ll> x_axis, y_axis;
  set<pair<ll, ll>> points;
  for (ll i = 0; i < bombs; i++) {

    cin >> u >> v;
    points.insert({u, v});
    x_axis[u]++, y_axis[v]++;
  }
  ll mx = -1, row, col;

  for (auto i : x_axis) {
    u = i.second;
    if (u > mx) {
      mx = u;
      row = i.first;
    }
  }
  vector<ll> rows, colums;
  for (auto i : x_axis) {
    u = i.second;
    if (u == mx) {
      rows.push_back(i.first);
    }
  }
  mx = -1;
  for (auto i : y_axis) {
    u = i.second;
    if (u > mx) {
      mx = u;
    }
  }
  for (auto i : y_axis) {
    u = i.second;
    if (u == mx)
      colums.push_back(i.first);
  }
  ll n = rows.size(), m = colums.size();
  ans = -1;
  ans = x_axis[rows[0]] + y_axis[colums[0]];
  for (ll i = 0; i < n; i++)
    for (ll j = 0; j < m; j++) {
      u = x_axis[rows[i]] + y_axis[colums[j]];
      if (points.find({rows[i], colums[j]}) == points.end()) {
        cout << u;
        return;
      } else
        u--;
    }

  cout << ans - 1 << "\\n";
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  ll t;
  // cin >> t;
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << '\\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " | ";
  __f(comma + 1, args...);
}
ll max(ll a, ll b) { return (a > b ? a : b); }
ll min(ll a, ll b) { return (a < b ? a : b); }
#define all(x) (x).begin(), (x).end()

void solve() {
  ll ans = 0;
  ll h, w, bombs;
  cin >> h >> w >> bombs;
  ll u, v;
  map<ll, ll> x_axis, y_axis;
  set<pair<ll, ll>> points;
  for (ll i = 0; i < bombs; i++) {

    cin >> u >> v;
    points.insert({u, v});
    x_axis[u]++, y_axis[v]++;
  }
  ll mx = -1, row, col;

  for (auto i : x_axis) {
    u = i.second;
    if (u > mx) {
      mx = u;
      row = i.first;
    }
  }
  vector<ll> rows, colums;
  for (auto i : x_axis) {
    u = i.second;
    if (u == mx) {
      rows.push_back(i.first);
    }
  }
  mx = -1;
  for (auto i : y_axis) {
    u = i.second;
    if (u > mx) {
      mx = u;
    }
  }
  for (auto i : y_axis) {
    u = i.second;
    if (u == mx)
      colums.push_back(i.first);
  }
  ll n = rows.size(), m = colums.size();
  ans = -1;

  for (ll i = 0; i < n; i++)
    for (ll j = 0; j < m; j++) {
      u = x_axis[rows[i]] + y_axis[colums[j]];
      if (points.find({rows[i], colums[j]}) != points.end())
        u--;
      ans = max(ans, u);
    }

  cout << ans << "\\n";
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  ll t;
  // cin >> t;
  solve();
  return 0;
}