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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>

using namespace std;
#include <vector>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define INF 1e9
#define llINF 1e18
#define base10_4 10000      // 1e4
#define base10_5 100000     // 1e5
#define base10_6 1000000    // 1e6
#define base10_7 10000000   // 1e7
#define base10_8 100000000  // 1e8
#define base10_9 1000000000 // 1e9

#define MOD 1000000007
#define pb push_back
#define ll long long
#define ull unsigned long long
#define vint vector<int>
#define vll vector<ll>

// #include <stack>
// #include <queue>

// #include <iomanip>
//  cout << fixed << setprecision(15) << y << endl;

string ans_Yes = "Yes";
string ans_No = "No";
string ans_yes = "yes";
string ans_no = "no";

ll A;
ll B;
ll C;
ll M;
ll H;
ll W;
vll h;
vll w;

ll ltmp;
string stmp;
double dtmp;
ll Hcnt[base10_6] = {};
ll Wcnt[base10_6] = {};

/*
max 448,000,000
map<string,ll> count_map;
count_map['0']=0;
for(auto x : count_map) {
    string key = x.first;
    ll value = x.second;
}
*/
map<string, ll> count_map;

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> H;
  cin >> W;
  cin >> M;

  rep(mi, M) {
    ll hi, wi;
    cin >> hi;
    hi--;
    h.push_back(hi);
    Hcnt[hi]++;
    cin >> wi;
    wi--;
    w.push_back(wi);
    Wcnt[wi]++;
    count_map[to_string(hi) + ":" + to_string(wi)] = 1;
  }
  ll maxHi = 0;
  ll maxWi = 0;
  rep(hi, H) {
    if (Hcnt[hi] > Hcnt[maxHi])
      maxHi = hi;
  }
  rep(wi, W) {
    if (Wcnt[wi] > Wcnt[maxWi])
      maxWi = wi;
  }

  vll mhi, mwi;
  rep(hi, H) {
    if (Hcnt[hi] == Hcnt[maxHi])
      mhi.push_back(hi);
  }
  rep(wi, W) {
    if (Wcnt[wi] == Wcnt[maxWi])
      mwi.push_back(wi);
  }

  ll hosei = 1;
  rep(h1, mhi.size()) {
    rep(w1, mwi.size()) {
      ll hi = mhi[h1];
      ll wi = mwi[w1];
      string key = to_string(hi) + ":" + to_string(wi);
      ltmp = count_map[key];
      if (ltmp == 0) {
        hosei = 0;
        break;
      }
    }
  }

  cout << Hcnt[maxHi] + Wcnt[maxWi] - hosei << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>

using namespace std;
#include <vector>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define INF 1e9
#define llINF 1e18
#define base10_4 10000      // 1e4
#define base10_5 100000     // 1e5
#define base10_6 1000000    // 1e6
#define base10_7 10000000   // 1e7
#define base10_8 100000000  // 1e8
#define base10_9 1000000000 // 1e9

#define MOD 1000000007
#define pb push_back
#define ll long long
#define ull unsigned long long
#define vint vector<int>
#define vll vector<ll>

// #include <stack>
// #include <queue>

// #include <iomanip>
//  cout << fixed << setprecision(15) << y << endl;

string ans_Yes = "Yes";
string ans_No = "No";
string ans_yes = "yes";
string ans_no = "no";

ll A;
ll B;
ll C;
ll M;
ll H;
ll W;
vll h;
vll w;

ll ltmp;
string stmp;
double dtmp;
ll Hcnt[base10_6] = {};
ll Wcnt[base10_6] = {};

/*
max 448,000,000
map<string,ll> count_map;
count_map['0']=0;
for(auto x : count_map) {
    string key = x.first;
    ll value = x.second;
}
*/
map<string, ll> count_map;

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> H;
  cin >> W;
  cin >> M;

  rep(mi, M) {
    ll hi, wi;
    cin >> hi;
    hi--;
    h.push_back(hi);
    Hcnt[hi]++;
    cin >> wi;
    wi--;
    w.push_back(wi);
    Wcnt[wi]++;
    count_map[to_string(hi) + ":" + to_string(wi)] = 1;
  }
  ll maxHi = 0;
  ll maxWi = 0;
  rep(hi, H) {
    if (Hcnt[hi] > Hcnt[maxHi])
      maxHi = hi;
  }
  rep(wi, W) {
    if (Wcnt[wi] > Wcnt[maxWi])
      maxWi = wi;
  }

  vll mhi, mwi;
  rep(hi, H) {
    if (Hcnt[hi] == Hcnt[maxHi])
      mhi.push_back(hi);
  }
  rep(wi, W) {
    if (Wcnt[wi] == Wcnt[maxWi])
      mwi.push_back(wi);
  }

  ll hosei = 1;
  rep(h1, mhi.size()) {
    rep(w1, mwi.size()) {
      ll hi = mhi[h1];
      ll wi = mwi[w1];
      string key = to_string(hi) + ":" + to_string(wi);
      ltmp = count_map[key];
      if (ltmp == 0)
        hosei = 0;
    }
  }

  cout << Hcnt[maxHi] + Wcnt[maxWi] - hosei << endl;
}