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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define fl long double
#define pll pair<ll, ll>
#define onebits(x) __builtin_popcountll(x)
#define fbo(x) find_by_order(x)
#define ook(x) order_of_key(x)
#define pb push_back
#define mk make_pair
#define endl "\\n"
#define ff first
#define ss second
#define mt make_tuple
#define np next_permutation
#define pi 3.141592653589793238
#define deb(x) cerr << #x << ' ' << '=' << ' ' << x << '\\n'
#define vll vector<ll>
#define vp vector<pair<ll, ll>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define revpq priority_queue<int, vector<int>, greater<int>>
#define pq priority_queue<ll>
#define ps(x, y) fixed << setprecision(y) << x
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const ll MAXN = 200005;
ll power(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod % mod;
    a = a * a % mod;
  }
  return res;
}

typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int main() {
  fastio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll i, j, t, k, l, r, tp, n, m;
  ll h, w;
  cin >> h >> w;
  cin >> m;
  ll ch[h], cw[w];
  memset(ch, 0, sizeof(ch));
  memset(cw, 0, sizeof(cw));
  ll x[m], y[m];
  map<pair<ll, ll>, ll> mp;
  for (i = 0; i < m; i++) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
    mp[{x[i], y[i]}]++;
    ch[x[i]]++;
    cw[y[i]]++;
  }
  ll ans = 0;
  ll mx = 0;
  for (i = 0; i < h; i++) {
    mx = max(ch[i], mx);
  }
  vll mh;
  for (i = 0; i < h; i++) {
    if (ch[i] == mx) {
      mh.pb(i);
    }
  }
  mx = 0;
  vll mw;
  for (i = 0; i < w; i++) {
    mx = max(mx, cw[i]);
  }
  for (i = 0; i < w; i++) {
    if (cw[i] == mx) {
      mw.pb(i);
    }
  }
  ll flg = 0;
  for (auto x : mh) {
    for (auto y : mw) {
      flg = 0;
      tp = 0;
      tp += ch[x];
      tp += cw[y];
      if (mp[{x, y}] > 0) {
        tp--;
        flg = 1;
      }
      ans = max(ans, tp);
      if (flg == 0) {
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define fl long double
#define pll pair<ll, ll>
#define onebits(x) __builtin_popcountll(x)
#define fbo(x) find_by_order(x)
#define ook(x) order_of_key(x)
#define pb push_back
#define mk make_pair
#define endl "\\n"
#define ff first
#define ss second
#define mt make_tuple
#define np next_permutation
#define pi 3.141592653589793238
#define deb(x) cerr << #x << ' ' << '=' << ' ' << x << '\\n'
#define vll vector<ll>
#define vp vector<pair<ll, ll>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define revpq priority_queue<int, vector<int>, greater<int>>
#define pq priority_queue<ll>
#define ps(x, y) fixed << setprecision(y) << x
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const ll MAXN = 200005;
ll power(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod % mod;
    a = a * a % mod;
  }
  return res;
}

typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int main() {
  fastio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll i, j, t, k, l, r, tp, n, m;
  ll h, w;
  cin >> h >> w;
  cin >> m;
  ll ch[h], cw[w];
  memset(ch, 0, sizeof(ch));
  memset(cw, 0, sizeof(cw));
  ll x[m], y[m];
  map<pair<ll, ll>, ll> mp;
  for (i = 0; i < m; i++) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
    mp[{x[i], y[i]}]++;
    ch[x[i]]++;
    cw[y[i]]++;
  }
  ll ans = 0;
  ll mx = 0;
  for (i = 0; i < h; i++) {
    mx = max(ch[i], mx);
  }
  vll mh;
  for (i = 0; i < h; i++) {
    if (ch[i] == mx) {
      mh.pb(i);
    }
  }
  mx = 0;
  vll mw;
  for (i = 0; i < w; i++) {
    mx = max(mx, cw[i]);
  }
  for (i = 0; i < w; i++) {
    if (cw[i] == mx) {
      mw.pb(i);
    }
  }
  for (auto x : mh) {
    for (auto y : mw) {
      tp = 0;
      tp += ch[x];
      tp += cw[y];
      if (mp[{x, y}] > 0) {
        tp--;
      }
      ans = max(ans, tp);
    }
  }
  cout << ans << endl;
  return 0;
}