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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define Rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
// const ll INF = 1LL << 60;
const ll INF = 1000000000;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

// rep(i, 0, sz(v)) cout << v[i] << " \\n"[i==sz(v)-1];

////////////////////////////////////////////////////////

ll y[300010], x[300010], vy[300010], vx[300010];

int main() {
  ll H, W, m;
  cin >> H >> W >> m;
  Vl h(m), w(m);
  rep(i, 0, m) {
    cin >> h[i] >> w[i];
    y[h[i]]++, x[w[i]]++;
  }

  ll yi, xi;
  ll mx_y = 0, mx_x = 0;
  rep(i, 0, 300001) chmax(mx_y, y[i]);
  rep(i, 0, 300001) chmax(mx_x, x[i]);

  ll a = 0, b = 0;
  rep(i, 0, 300001) if (y[i] == mx_y) vy[i]++, a++;
  rep(i, 0, 300001) if (x[i] == mx_x) vx[i]++, b++;

  ll t = a * b;
  ll ans = mx_y + mx_x - 1;

  rep(i, 0, m) {
    if (vy[h[i]] && vx[w[i]])
      t--;
  }
  if (t >= 1)
    co(++ans);
  else
    co(ans);

  // co(t);
  // for(auto i : vy) cout << i << " ";
  // coel;
  // for(auto i : vx) cout << i << " ";
  // coel;

  // rep(i, 0, sz(vy)) rep(j, 0, sz(vx)){
  //     bool f = true;
  //     rep(k, 0, m){
  //         if(vy[i] == h[k] && vx[j] == w[k]) f = false;
  //     }
  //     if(f){
  //         co(++ans);
  //         return 0;
  //     }
  // }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define Rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
// const ll INF = 1LL << 60;
const ll INF = 1000000000;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

// rep(i, 0, sz(v)) cout << v[i] << " \\n"[i==sz(v)-1];

////////////////////////////////////////////////////////

ll y[300010], x[300010];

int main() {
  ll H, W, m;
  cin >> H >> W >> m;
  Vl h(m), w(m);
  rep(i, 0, m) {
    cin >> h[i] >> w[i];
    y[h[i]]++, x[w[i]]++;
  }

  ll yi, xi;
  ll mx_y = 0, mx_x = 0;
  rep(i, 0, 300001) chmax(mx_y, y[i]);
  rep(i, 0, 300001) chmax(mx_x, x[i]);

  set<ll> vy, vx;
  rep(i, 0, 300001) if (y[i] == mx_y) vy.insert(i);
  rep(i, 0, 300001) if (x[i] == mx_x) vx.insert(i);

  ll ans = mx_y + mx_x - 1;
  ll t = sz(vy) * sz(vx);
  rep(i, 0, m) {
    set<ll> sy = vy, sx = vx;
    sy.insert(h[i]);
    sx.insert(w[i]);
    if (sz(sy) == sz(vy) && sz(sx) == sz(vx))
      t--;
  }
  if (t >= 1)
    co(++ans);
  else
    co(ans);

  // co(t);
  // for(auto i : vy) cout << i << " ";
  // coel;
  // for(auto i : vx) cout << i << " ";
  // coel;

  // rep(i, 0, sz(vy)) rep(j, 0, sz(vx)){
  //     bool f = true;
  //     rep(k, 0, m){
  //         if(vy[i] == h[k] && vx[j] == w[k]) f = false;
  //     }
  //     if(f){
  //         co(++ans);
  //         return 0;
  //     }
  // }

  return 0;
}