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

#include <bits/stdc++.h>
#define fl(i, a, n) for (int i = a; i <= n; i++)
#define pb push_back
#define int long long int
#define ump unordered_map
#define pii pair<int, int>
#define ff first
#define ss second
#define PI 3.1415926535898
#define inf 1e18
#define vi vector<int>
#define endl "\\n"
#define print(a)                                                               \\
  for (auto x : a) {                                                           \\
    cout << x << " ";                                                          \\
  }                                                                            \\
  cout << endl;
#define mod 1000000007
// #define ONLINE_JUDGE
using namespace std;
template <typename Arg, typename... Args> void db(Arg &&arg, Args &&...args) {
  cout << std::forward<Arg>(arg);
  using expander = int[];
  (void)expander{0, (void(cout << ',' << std::forward<Args>(args)), 0)...};
  cout << endl;
}
inline void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  int hs[h + 1];
  int ws[w + 1];
  fill(hs, hs + h + 1, 0);
  fill(ws, ws + w + 1, 0);
  set<pair<int, int>> ss;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    hs[x]++;
    ws[y]++;
    ss.insert({x, y});
  }
  int mx = -1, my = -1;
  for (auto x : ws) {
    mx = max(mx, x);
  }
  for (auto y : hs) {
    my = max(my, y);
  }
  vi maybex, maybey;
  for (int i = 0; i <= w; i++) {
    if (ws[i] == mx) {
      maybex.pb(i);
    }
  }
  for (int i = 0; i <= h; i++) {
    if (hs[i] == my) {
      maybey.pb(i);
    }
  }
  for (auto x : maybex) {
    for (auto y : maybey) {
      if (!ss.count({y, x})) {
        cout << mx + my;
        return;
      }
    }
  }
  cout << mx + my - 1;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin >> t;
  fl(i, 1, t) { solve(); }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fl(i, a, n) for (int i = a; i <= n; i++)
#define pb push_back
#define int long long int
#define ump unordered_map
#define pii pair<int, int>
#define ff first
#define ss second
#define PI 3.1415926535898
#define inf 1e18
#define vi vector<int>
#define endl "\\n"
#define print(a)                                                               \\
  for (auto x : a) {                                                           \\
    cout << x << " ";                                                          \\
  }                                                                            \\
  cout << endl;
#define mod 1000000007
#define ONLINE_JUDGE
using namespace std;
template <typename Arg, typename... Args> void db(Arg &&arg, Args &&...args) {
  cout << std::forward<Arg>(arg);
  using expander = int[];
  (void)expander{0, (void(cout << ',' << std::forward<Args>(args)), 0)...};
  cout << endl;
}
inline void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  map<pair<int, int>, bool> mp;
  int r[h + 1], c[w + 1];
  // db(h + 1, w + 1);
  fill(r, r + h + 1, 0);
  fill(c, c + w + 1, 0);
  fl(i, 0, m - 1) {
    int x, y;
    cin >> x >> y;
    mp[{x, y}] = 1;
    r[x]++;
    c[y]++;
  }
  // print(r);
  // print(c);
  int ans = 0;
  fl(i, 1, h) {
    fl(j, 1, w) {
      ans = max(ans, r[i] + c[j] - (mp.count({i, j}) != 0 ? 1 : 0));
    }
  }
  cout << ans << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin >> t;
  fl(i, 1, t) { solve(); }

  return 0;
}