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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
const ll INF = ll(1e18) + 1;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll h, w, m;
  cin >> h >> w >> m;
  vector<P> hmwm;

  vector<ll> row(w, 0);
  vector<ll> column(h, 0);

  ll hm, wm;

  rep(i, m) {
    cin >> hm >> wm;
    hmwm.push_back(make_pair(hm, wm));
    column[hmwm[i].first - 1]++;
    row[hmwm[i].second - 1]++;
  }

  ll result = 0;
  ll max_column = 0;
  vector<ll> max_column_list;
  ll max_row = 0;
  vector<ll> max_row_list;
  rep(i, h) {
    if (max_column < column[i]) {
      max_column = column[i];
      max_column_list = {i};
    } else if (max_column == column[i]) {
      max_column_list.push_back(i);
    }
  }
  rep(i, w) {
    if (max_row < row[i]) {
      max_row = row[i];
      max_row_list = {i};
    } else if (max_row == row[i]) {
      max_row_list.push_back(i);
    }
  }
  ll temp = 0;
  // rep(i,m){
  //     if(find(all(max_column_list),hmwm[i].first-1)!=max_column_list.end()
  //     and find(all(max_row_list),hmwm[i].second-1)!=max_row_list.end()){
  //         temp++;
  //     }
  // }

  // rep(i,max_column_list.size())rep(j,max_row_list.size()){
  //     if(find(all(hmwm),make_pair(max_column_list[i]+1,max_row_list[j]+1)) ==
  //     hmwm.end()) {
  //         cout << max_column + max_row;
  //         return 0;
  //     }
  // }

  // cout << max_column + max_row - 1;

  // cout << "max_col: " << max_column << "\\n";
  // cout << "max_row: " << max_row << "\\n";
  // cout << "temp: " << temp << "\\n";

  // if(temp == max_column_list.size() * max_row_list.size()) cout << max_column
  // + max_row -1; else cout << max_column + max_row;

  result = max_column + max_row;

  rep(i, m) {
    if (column[hmwm[i].first - 1] == max_column &&
        row[hmwm[i].second - 1] == max_row) {
      temp++;
    }
  }

  if (max_column_list.size() * max_row_list.size() == temp) {
    result--;
  }

  cout << result;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
const ll INF = ll(1e18) + 1;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll h, w, m;
  cin >> h >> w >> m;
  vector<P> hmwm;

  vector<ll> row(w, 0);
  vector<ll> column(h, 0);

  ll hm, wm;

  rep(i, m) {
    cin >> hm >> wm;
    hmwm.push_back(make_pair(hm, wm));
    column[hmwm[i].first - 1]++;
    row[hmwm[i].second - 1]++;
  }

  ll result = 0;
  ll max_column = 0;
  vector<ll> max_column_list;
  ll max_row = 0;
  vector<ll> max_row_list;
  rep(i, h) {
    if (max_column < column[i]) {
      max_column = column[i];
      max_column_list = {i};
    } else if (max_column == column[i]) {
      max_column_list.push_back(i);
    }
  }
  rep(i, w) {
    if (max_row < row[i]) {
      max_row = row[i];
      max_row_list = {i};
    } else if (max_row == row[i]) {
      max_row_list.push_back(i);
    }
  }
  ll temp = 0;
  // rep(i,m){
  //     if(find(all(max_column_list),hmwm[i].first-1)!=max_column_list.end()
  //     and find(all(max_row_list),hmwm[i].second-1)!=max_row_list.end()){
  //         temp++;
  //     }
  // }

  rep(i, max_column_list.size()) rep(j, max_row_list.size()) {
    if (find(all(hmwm), make_pair(max_column_list[i] + 1,
                                  max_row_list[j] + 1)) == hmwm.end()) {
      cout << max_column + max_row;
      return 0;
    }
  }

  cout << max_column + max_row - 1;

  // cout << "max_col: " << max_column << "\\n";
  // cout << "max_row: " << max_row << "\\n";
  // cout << "temp: " << temp << "\\n";

  // if(temp == max_column_list.size() * max_row_list.size()) cout << max_column
  // + max_row -1; else cout << max_column + max_row;

  return 0;
}