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

#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define mem0(x) memset(x, 0, sizeof(x))
#define meminf(x) memset(x, 0x3f, sizeof(x))
#define VI vector<int>
#define VL vector<ll>
using namespace std;

const int N = 3e5 + 5;
int hb[N], wb[N];
map<pii, int> mp;
vector<int> v1, v2;

int main() {
  ios::sync_with_stdio(0);
  int h, w;
  cin >> h >> w;
  int m;
  cin >> m;
  int maxhidx = 0, maxwidx = 0, maxhb = 0, maxwb = 0;
  int ans = 0;
  rep(i, 1, m) {
    int hi, wi;
    cin >> hi >> wi;
    mp[{hi, wi}] = 1;
    ++hb[hi];
    ++wb[wi];
    if (maxhb < hb[hi]) {
      maxhb = hb[hi];
      maxhidx = hi;
    }
    if (maxwb < wb[wi]) {
      maxwb = wb[wi];
      maxwidx = wi;
    }
  }

  // v1.push_back(maxhidx);v2.push_back(maxwidx);
  //  for(auto v:mp){
  //      pii tmp = v.first; int hi = tmp.first,wi = tmp.second;
  //      if(maxhb==hb[hi]){
  //          v1.push_back(hi);
  //      }
  //      if(maxwb==wb[wi]){
  //          v2.push_back(wi);
  //      }
  //  }
  rep(i, 1, h) {
    if (hb[i] == maxhb)
      v1.push_back(i);
  }
  rep(j, 1, w) {
    if (wb[j] == maxwb)
      v2.push_back(j);
  }
  // if(!mp[{maxhidx,maxwidx}]){
  //     ans = maxhb+maxwb; cout<<ans;return 0;
  // }
  int siz1 = v1.size(), siz2 = v2.size(), fg = 0;
  rep(i, 0, siz1 - 1) {
    rep(j, 0, siz2 - 1) {
      if (!mp[{v1[i], v2[j]}]) {
        fg = 1;
        break;
      }
    }
    if (fg)
      break;
  }
  if (fg) {
    ans = maxhb + maxwb;
  } else {
    ans = maxhb + maxwb - 1;
  }

  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define mem0(x) memset(x, 0, sizeof(x))
#define meminf(x) memset(x, 0x3f, sizeof(x))
#define VI vector<int>
#define VL vector<ll>
using namespace std;

const int N = 3e5 + 5;
int hb[N], wb[N];
map<pii, int> mp;
vector<int> v1, v2;

int main() {
  ios::sync_with_stdio(0);
  int h, w;
  cin >> h >> w;
  int m;
  cin >> m;
  int maxhidx = 0, maxwidx = 0, maxhb = 0, maxwb = 0;
  rep(i, 1, m) {
    int hi, wi;
    cin >> hi >> wi;
    mp[{hi, wi}] = 1;
    ++hb[hi];
    ++wb[wi];
    if (maxhb < hb[hi]) {
      maxhb = hb[hi];
      maxhidx = hi;
    }
    if (maxwb < wb[wi]) {
      maxwb = wb[wi];
      maxwidx = wi;
    }
  }
  int ans = 0;
  v1.push_back(maxhidx);
  v2.push_back(maxwidx);
  for (auto v : mp) {
    pii tmp = v.first;
    int hi = tmp.first, wi = tmp.second;
    if (maxhb == hb[hi]) {
      v1.push_back(hi);
    }
    if (maxwb == wb[wi]) {
      v2.push_back(wi);
    }
  }
  if (!mp[{maxhidx, maxwidx}]) {
    ans = maxhb + maxwb;
    cout << ans;
    return 0;
  }
  int siz1 = v1.size(), siz2 = v2.size(), fg = 0;
  rep(i, 0, siz1 - 1) {
    rep(j, 0, siz2 - 1) {
      if (!mp[{v1[i], v2[j]}]) {
        fg = 1;
        break;
      }
    }
    if (fg)
      break;
  }
  if (fg) {
    ans = maxhb + maxwb;
  } else {
    ans = maxhb + maxwb - 1;
  }
  // if(mp[{maxhidx,maxwidx}]==1){
  //     ans = maxhb+maxwb-1;
  // }else{
  //     ans = maxhb+maxwb;
  // }
  cout << ans;
}