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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ACC                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mod 1000000007
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int dx8[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy8[] = {0, 0, 1, -1, 1, -1, 1, -1};
bool isvowel(char c) {
  switch (c) {
  case 'a':
  case 'e':
  case 'i':
  case 'o':
  case 'u':
    return true;
  default:
    return false;
  }
}
bool isValid(ll x, ll y) {
  // if(x<0||x>h-1||y<0||y>w-1)return false;
  // if(s[x][y]=='#')return false;
  // if(vis[x][y])return false;
  return true;
}
bool comp(pair<ll, ll> x, pair<ll, ll> y) {
  if (x.first == y.first)
    return x.second < y.second;
  return x.first < y.first;
}

int main() {
  ACC int T;
  T = 1;
  // cin>>T;
  while (T--) {
    ll i, h, w, b;
    cin >> h >> w >> b;
    vector<pair<ll, ll>> v;
    map<ll, ll> r, c;
    map<pair<ll, ll>, ll> m;
    for (i = 0; i < b; i++) {
      ll x, y;
      cin >> x >> y;
      v.pb({x, y});
      r[x]++;
      c[y]++;
      m[{x, y}]++;
    }
    vector<pair<ll, ll>> vr, vc;
    for (auto j : r) {
      vr.pb({j.second, j.first});
    }
    for (auto j : c) {
      vc.pb({j.second, j.first});
    }
    ll ans = 0;
    sort(vr.begin(), vr.end());
    sort(vc.begin(), vc.end());
    // cout<<vr.size()<<","<<vc.size()<<"\\n";
    ll j, x;
    bool f = false;
    for (i = vr.size() - 1; i >= 0; i--) {
      if (vr[vr.size() - 1].first - vr[i].first > 1)
        break;
      for (j = vc.size() - 1; j >= 0; j--) {
        if (vc[vc.size() - 1].first - vc[j].first > 1)
          break;
        x = vr[i].first + vc[j].first;
        if (m[{vr[i].second, vc[j].second}]) {
          x--;
        } else {
          f = true;
          ans = max(ans, x);
          break;
        }
        ans = max(ans, x);
      }
      if (f)
        break;
    }
    cout << ans << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ACC                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mod 1000000007
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int dx8[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy8[] = {0, 0, 1, -1, 1, -1, 1, -1};
bool isvowel(char c) {
  switch (c) {
  case 'a':
  case 'e':
  case 'i':
  case 'o':
  case 'u':
    return true;
  default:
    return false;
  }
}
bool isValid(ll x, ll y) {
  // if(x<0||x>h-1||y<0||y>w-1)return false;
  // if(s[x][y]=='#')return false;
  // if(vis[x][y])return false;
  return true;
}
bool comp(pair<ll, ll> x, pair<ll, ll> y) {
  if (x.first == y.first)
    return x.second < y.second;
  return x.first < y.first;
}

int main() {
  ACC int T;
  T = 1;
  // cin>>T;
  while (T--) {
    ll i, h, w, b;
    cin >> h >> w >> b;
    vector<pair<ll, ll>> v;
    map<ll, ll> r, c;
    map<pair<ll, ll>, ll> m;
    for (i = 0; i < b; i++) {
      ll x, y;
      cin >> x >> y;
      v.pb({x, y});
      r[x]++;
      c[y]++;
      m[{x, y}]++;
    }
    vector<pair<ll, ll>> vr, vc;
    for (auto j : r) {
      vr.pb({j.second, j.first});
    }
    for (auto j : c) {
      vc.pb({j.second, j.first});
    }
    ll ans = 0;
    sort(vr.begin(), vr.end());
    sort(vc.begin(), vc.end());
    // cout<<vr.size()<<","<<vc.size()<<"\\n";
    ll j, x;
    for (i = vr.size() - 1; i >= 0; i--) {
      if (vr[vr.size() - 1].first - vr[i].first > 1)
        break;
      for (j = vc.size() - 1; j >= 0; j--) {
        if (vc[vc.size() - 1].first - vc[j].first > 1)
          break;
        x = vr[i].first + vc[j].first;
        if (m[{vr[i].second, vc[j].second}]) {
          x--;
        }
        ans = max(ans, x);
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}