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

#include "bits/stdc++.h"
using namespace std;
using ll = long long;

template <typename T>
void print_vec(const vector<T> &v, bool is_reverse = false, ll num = 0) {
  if (num == 0)
    num = (ll)v.size();
  if (is_reverse)
    for (ll i = num - 1; i >= 0; i--) {
      cout << v[i];
      if (i != 0)
        cout << " ";
    }
  else
    for (ll i = 0; i < num; i++) {
      cout << v[i];
      if (i != num - 1)
        cout << " ";
    }
  cout << endl;
}

template <typename T> void print_pairvec(const vector<T> &v, ll num = 0) {
  if (num == 0)
    num = (ll)v.size();
  cout << endl;
  for (ll i = 0; i < num; i++) {
    cout << v[i].first << " " << v[i].second << endl;
  }
}

template <typename T> void print_vec2(const vector<vector<T>> &v) {
  cout << endl;
  cout << "       ";
  for (ll i = 0; i < v[0].size(); i++)
    cout << i << "   ";
  cout << endl;
  for (ll i = 0; i < v.size(); i++) {
    cout << "i=" << i << ":   ";
    for (ll j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 0)
        cout << "\\x1B[0m" << v[i][j] << "   ";
      else
        cout << "\\x1B[31m" << v[i][j]
             << "   "; // https://stackoverrun.com/ja/q/12618775
    }
    cout << "\\x1B[0m" << endl;
  }
}

// const int SIZE = 3e5+1;
// vector<vector<bool>> is_bomb(SIZE, vector<bool>(SIZE,
// false));//グローバルにしてもREした bool is_bomb[300000][300000];
int main() {
  int H, W;
  cin >> H >> W;
  int M;
  cin >> M;

  // vector<vector<bool>> is_bomb(H, vector<bool>(W, false));//
  // 配列がでかすぎるとREになるらしい  かわりにmapでやってみる
  using P = pair<int, int>;
  map<P, int> mp;
  vector<int> num_h(H, 0), num_w(W, 0);
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    // is_bomb[h][w] = true;
    mp[P(h, w)]++;
    num_h[h]++;
    num_w[w]++;
  }

  int h_num_max = 0, w_num_max = 0;
  for (int i = 0; i < H; i++)
    h_num_max = max(h_num_max, num_h[i]);
  for (int i = 0; i < W; i++)
    w_num_max = max(w_num_max, num_w[i]);

  vector<int> maxs_h, maxs_w;
  for (int i = 0; i < H; i++)
    if (num_h[i] == h_num_max)
      maxs_h.emplace_back(i);
  for (int i = 0; i < W; i++)
    if (num_w[i] == w_num_max)
      maxs_w.emplace_back(i);

  int ans = 0;
  for (int i = 0; i < (int)maxs_h.size(); i++) {
    for (int j = 0; j < (int)maxs_w.size(); j++) {
      int tmp = num_h[maxs_h[i]] + num_w[maxs_w[j]];
      // if(is_bomb[maxs_h[i]][maxs_w[j]] == true) tmp--;
      if (mp[P(maxs_h[i], maxs_w[j])])
        tmp--;
      else {
        cout << tmp << endl;
        return 0; // これいれんとTLEになった
      }
      ans = max(ans, tmp);
    }
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using ll = long long;

template <typename T>
void print_vec(const vector<T> &v, bool is_reverse = false, ll num = 0) {
  if (num == 0)
    num = (ll)v.size();
  if (is_reverse)
    for (ll i = num - 1; i >= 0; i--) {
      cout << v[i];
      if (i != 0)
        cout << " ";
    }
  else
    for (ll i = 0; i < num; i++) {
      cout << v[i];
      if (i != num - 1)
        cout << " ";
    }
  cout << endl;
}

template <typename T> void print_pairvec(const vector<T> &v, ll num = 0) {
  if (num == 0)
    num = (ll)v.size();
  cout << endl;
  for (ll i = 0; i < num; i++) {
    cout << v[i].first << " " << v[i].second << endl;
  }
}

template <typename T> void print_vec2(const vector<vector<T>> &v) {
  cout << endl;
  cout << "       ";
  for (ll i = 0; i < v[0].size(); i++)
    cout << i << "   ";
  cout << endl;
  for (ll i = 0; i < v.size(); i++) {
    cout << "i=" << i << ":   ";
    for (ll j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 0)
        cout << "\\x1B[0m" << v[i][j] << "   ";
      else
        cout << "\\x1B[31m" << v[i][j]
             << "   "; // https://stackoverrun.com/ja/q/12618775
    }
    cout << "\\x1B[0m" << endl;
  }
}

// const int SIZE = 3e5+1;
// vector<vector<bool>> is_bomb(SIZE, vector<bool>(SIZE, false));
bool is_bomb[1][1];
int main() {
  ll H, W;
  cin >> H >> W;
  ll M;
  cin >> M;

  // vector<vector<bool>> is_bomb(H, vector<bool>(W, false));//
  // 配列がでかすぎるとREになるらしい  かわりにmapでやってみる
  using P = pair<int, int>;
  map<P, int> mp;
  vector<ll> num_h(H, 0), num_w(W, 0);
  for (ll i = 0; i < M; i++) {
    ll h, w;
    cin >> h >> w;
    h--;
    w--;
    // is_bomb[h][w] = true;
    mp[P(h, w)]++;
    num_h[h]++;
    num_w[w]++;
  }

  ll h_num_max = 0, w_num_max = 0;
  for (ll i = 0; i < H; i++)
    h_num_max = max(h_num_max, num_h[i]);
  for (ll i = 0; i < W; i++)
    w_num_max = max(w_num_max, num_w[i]);

  vector<ll> maxs_h, maxs_w;
  for (ll i = 0; i < H; i++)
    if (num_h[i] == h_num_max)
      maxs_h.emplace_back(i);
  for (ll i = 0; i < W; i++)
    if (num_w[i] == w_num_max)
      maxs_w.emplace_back(i);

  ll ans = 0;
  for (ll i = 0; i < (ll)maxs_h.size(); i++) {
    for (ll j = 0; j < (ll)maxs_w.size(); j++) {
      ll tmp = num_h[maxs_h[i]] + num_w[maxs_w[j]];
      // if(is_bomb[maxs_h[i]][maxs_w[j]] == true) tmp--;
      if (mp[P(maxs_h[i], maxs_w[j])])
        tmp--;
      else {
        // cout << tmp << endl;
        // return 0;
      }
      ans = max(ans, tmp);
    }
  }

  cout << ans << endl;
  return 0;
}