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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define For(i, a) for (int i = 0; i < (a); i++)
#define FORR(i, a, b) for (int i = (b)-1; i >= a; i--)
#define Forr(i, a) for (int i = (a)-1; i >= 0; i--)

#define deb(...) cerr << "[" << #__VA_ARGS__ << "] : [", DBG(__VA_ARGS__)
void DBG() { cerr << "]\\n"; }
template <typename T, typename... Args> void DBG(T first, Args... args) {
  cerr << first;
  if (sizeof...(args))
    cerr << ", ";
  DBG(args...);
}

#define int ll
#define pb(x) push_back(x)
#define INF (int)1e17
#define PI 3.14159265358979323846

int del_x[4] = {-1, 1, 0, 0};
int del_y[4] = {0, 0, 1, -1};
int XX[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int YY[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  int row[h + 1], col[w + 1];
  memset(row, 0, sizeof(row));
  memset(col, 0, sizeof(col));
  map<pair<int, int>, int> points;
  For(i, m) {
    int x, y;
    cin >> x >> y;
    points[{x, y}]++;
    row[x]++;
    col[y]++;
  }

  vector<pair<int, int>> ff, ss;
  FOR(i, 1, h + 1) { ff.push_back({row[i], i}); }
  FOR(i, 1, w + 1) { ss.push_back({col[i], i}); }
  // for(auto ele : ss){
  // 	deb(ele.first,ele.second);
  // }

  sort(ff.rbegin(), ff.rend());
  sort(ss.rbegin(), ss.rend());
  int ans = 0;
  FOR(i, 1, w + 1) {
    // deb(ff[0].first,ss[i-1].first);
    int cnt = ff[0].first + ss[i - 1].first;
    if (points.count({ff[0].second, ss[i - 1].second}))
      cnt--;
    ans = max(ans, cnt);
  }
  FOR(i, 1, h + 1) {
    int cnt = ff[i - 1].first + ss[0].first;
    if (points.count({ff[i - 1].second, ss[0].second}))
      cnt--;
    ans = max(ans, cnt);
  }
  cout << ans << "\\n";
}

int32_t main() {
#ifndef ONLINE_JUDGE
  clock_t tStart = clock();
  // freopen("in.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin >> t;
  for (int tt = 1; tt <= t; tt++) {
    // cout << "Case #"<<tt<<": ";
    solve();
  }
#ifndef ONLINE_JUDGE
  cerr << "\\nRun Time -> " << (double)(clock() - tStart) / CLOCKS_PER_SEC
       << " sec\\n";
#endif
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define For(i, a) for (int i = 0; i < (a); i++)
#define FORR(i, a, b) for (int i = (b)-1; i >= a; i--)
#define Forr(i, a) for (int i = (a)-1; i >= 0; i--)

#define deb(...) cerr << "[" << #__VA_ARGS__ << "] : [", DBG(__VA_ARGS__)
void DBG() { cerr << "]\\n"; }
template <typename T, typename... Args> void DBG(T first, Args... args) {
  cerr << first;
  if (sizeof...(args))
    cerr << ", ";
  DBG(args...);
}

#define int ll
#define pb(x) push_back(x)
#define INF (int)1e17
#define PI 3.14159265358979323846

int del_x[4] = {-1, 1, 0, 0};
int del_y[4] = {0, 0, 1, -1};
int XX[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int YY[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  int row[h + 1], col[w + 1];
  memset(row, 0, sizeof(row));
  memset(col, 0, sizeof(col));
  map<pair<int, int>, int> points;
  For(i, m) {
    int x, y;
    cin >> x >> y;
    points[{x, y}]++;
    row[x]++;
    col[y]++;
  }

  int ans = 0;
  FOR(i, 1, h + 1) {
    FOR(j, 1, w + 1) {
      int cnt = row[i] + col[j];
      if (points.count({i, j}))
        cnt--;
      ans = max(ans, cnt);
    }
  }
  cout << ans << "\\n";
}

int32_t main() {
#ifndef ONLINE_JUDGE
  clock_t tStart = clock();
  // freopen("in.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin >> t;
  for (int tt = 1; tt <= t; tt++) {
    // cout << "Case #"<<tt<<": ";
    solve();
  }
#ifndef ONLINE_JUDGE
  cerr << "\\nRun Time -> " << (double)(clock() - tStart) / CLOCKS_PER_SEC
       << " sec\\n";
#endif
}