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

#include <bits/stdc++.h>
#define ll long long
#define pii pair<ll int, ll int>
#define PI 2 * acos(0)
#define INF 1e18
#define mx 200005
using namespace std;
// const ll int p=1000000007;
// vector<ll > prime;
// bool mark[mx];
ll int mod(ll int a, ll int b) {
  ll int m = a % b;
  if (m < 0)
    m = (b < 0) ? (m - b) : (m + b);
  return m;
}
ll maxpow(ll n) { return (n & (~(n - 1))); }
bool isPrime(ll n) {
  if (n < 2)
    return false;
  if (n == 2)
    return true;
  if (n % 2 == 0)
    return false;
  for (ll i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}
/*void init()
{
    mark[1]=1;
    int i,j,limit=sqrt(mx*1.0)+2;
    prime.push_back(2);
    for(i=4;i<mx;i+=2) mark[i]=1;
    mark[2]=0;
    for(i=3;i<mx;i+=2){
        if(!mark[i]){
            prime.push_back(i);
            if(i<=limit){
                for(j=i*i;j<mx;j+=2*i){
                    mark[j]=1;
                }
            }
        }
    }
}
/*
ll maxdiv(ll n)
{
    ll i,ans=1;
    for(i=0;i<prime.size();i++){
        if(n%prime[i]==0){
            ans=n/prime[i];break;
            //return ans;
        }
    }
    return ans;
}*/
ll lcm(ll a, ll b) {
  ll ans = a * b;
  return (ans / __gcd(a, b));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n, i, j, k, z, x, sum, cnt, ans, H, W, m;
  cin >> H >> W >> m;
  ll h[m], w[m];
  map<ll, ll> row, col;
  map<pii, ll> mp;
  for (i = 0; i < m; i++) {
    cin >> h[i] >> w[i];
    row[h[i]]++;
    col[w[i]]++;
    mp[{h[i], w[i]}]++;
  }
  ll mr = -1, mc = -1;
  for (i = 1; i <= H; i++)
    mr = max(mr, row[i]);
  for (i = 1; i <= W; i++)
    mc = max(mc, col[i]);
  vector<ll> vr, vc;
  for (i = 1; i <= H; i++)
    if (row[i] == mr)
      vr.push_back(i);
  for (i = 1; i <= W; i++)
    if (col[i] == mc)
      vc.push_back(i);
  ans = mr + mc - 1;
  for (i = 0; i < vr.size(); i++) {
    for (j = 0; j < vc.size(); j++) {
      if (mp[{vr[i], vc[j]}] == 0) {
        cout << ans + 1;
        return 0;
      }
    }
  }
  cout << ans;
  return 0;
}
// 1028C


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pii pair<ll int, ll int>
#define PI 2 * acos(0)
#define INF 1e18
#define mx 200005
using namespace std;
// const ll int p=1000000007;
// vector<ll > prime;
// bool mark[mx];
ll int mod(ll int a, ll int b) {
  ll int m = a % b;
  if (m < 0)
    m = (b < 0) ? (m - b) : (m + b);
  return m;
}
ll maxpow(ll n) { return (n & (~(n - 1))); }
bool isPrime(ll n) {
  if (n < 2)
    return false;
  if (n == 2)
    return true;
  if (n % 2 == 0)
    return false;
  for (ll i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}
/*void init()
{
    mark[1]=1;
    int i,j,limit=sqrt(mx*1.0)+2;
    prime.push_back(2);
    for(i=4;i<mx;i+=2) mark[i]=1;
    mark[2]=0;
    for(i=3;i<mx;i+=2){
        if(!mark[i]){
            prime.push_back(i);
            if(i<=limit){
                for(j=i*i;j<mx;j+=2*i){
                    mark[j]=1;
                }
            }
        }
    }
}
/*
ll maxdiv(ll n)
{
    ll i,ans=1;
    for(i=0;i<prime.size();i++){
        if(n%prime[i]==0){
            ans=n/prime[i];break;
            //return ans;
        }
    }
    return ans;
}*/
ll lcm(ll a, ll b) {
  ll ans = a * b;
  return (ans / __gcd(a, b));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n, i, j, k, z, x, sum, cnt, ans, H, W, m;
  cin >> H >> W >> m;
  ll h[m], w[m];
  map<ll, ll> row, col;
  map<pii, ll> mp;
  for (i = 0; i < m; i++) {
    cin >> h[i] >> w[i];
    row[h[i]]++;
    col[w[i]]++;
    mp[{h[i], w[i]}]++;
  }
  ll mr = -1, mc = -1;
  for (i = 1; i <= H; i++)
    mr = max(mr, row[i]);
  for (i = 1; i <= W; i++)
    mc = max(mc, col[i]);
  vector<ll> vr, vc;
  for (i = 1; i <= H; i++)
    if (row[i] == mr)
      vr.push_back(i);
  for (i = 1; i <= W; i++)
    if (col[i] == mc)
      vc.push_back(i);
  cnt = INF;
  for (i = 0; i < vr.size(); i++) {
    for (j = 0; j < vc.size(); j++) {
      cnt = min(cnt, mp[{vr[i], vc[j]}]);
    }
  }
  cout << mr + mc - cnt;
  return 0;
}
// 1028C