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

#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
constexpr int64_t MOD = 1e9 + 1;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  using p = pair<int, int>;
  int32_t H, W, M;
  cin >> H >> W >> M;
  vector<p> row_sum(H);
  for (int i = 0; i < H; i++) {
    row_sum[i] = make_pair(0, i);
  }
  vector<p> col_sum(W);
  for (int i = 0; i < W; i++) {
    col_sum[i] = make_pair(0, i);
  }
  vector<vector<int>> tgtr(H);
  vector<vector<int>> tgtc(W);
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    row_sum[h - 1].first++;
    col_sum[w - 1].first++;
    tgtr[h - 1].emplace_back(w - 1);
    tgtc[w - 1].emplace_back(h - 1);
  }
  sort(row_sum.begin(), row_sum.end(), greater<p>());
  sort(col_sum.begin(), col_sum.end(), greater<p>());

  int res = 0;
  int rmax = row_sum[0].first;
  int cmax = col_sum[0].first;
  for (int i = 0; i < (int)row_sum.size() && row_sum[i].first == rmax; i++) {
    for (int j = 0;
         j < 1000 && j < (int)col_sum.size() && col_sum[j].first == cmax; j++) {
      int cur = row_sum[i].first + col_sum[j].first;
      int r = row_sum[i].second;
      int c = col_sum[j].second;
      for (auto t : tgtr[r]) {
        if (t == c)
          cur--;
      }
      // cout << r << "," << c << "," << cur << endl;
      res = max(res, cur);
    }
  }
  for (int j = 0; j < (int)col_sum.size() && col_sum[j].first == cmax; j++) {
    for (int i = 0;
         i < 1000 && i < (int)row_sum.size() && row_sum[i].first == rmax; i++) {
      int cur = row_sum[i].first + col_sum[j].first;
      int r = row_sum[i].second;
      int c = col_sum[j].second;
      for (auto t : tgtc[c]) {
        if (t == r)
          cur--;
      }
      // cout << r << "," << c << "," << cur << endl;
      res = max(res, cur);
    }
  }

  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
constexpr int64_t MOD = 1e9 + 1;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  using p = pair<int, int>;
  int32_t H, W, M;
  cin >> H >> W >> M;
  vector<p> row_sum(H);
  for (int i = 0; i < H; i++) {
    row_sum[i] = make_pair(0, i);
  }
  vector<p> col_sum(W);
  for (int i = 0; i < W; i++) {
    col_sum[i] = make_pair(0, i);
  }
  vector<vector<int>> tgt(H);
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    row_sum[h - 1].first++;
    col_sum[w - 1].first++;
    tgt[h - 1].emplace_back(w - 1);
  }
  sort(row_sum.begin(), row_sum.end(), greater<p>());
  sort(col_sum.begin(), col_sum.end(), greater<p>());

  int res = 0;
  int rmax = row_sum[0].first;
  int cmax = col_sum[0].first;
  for (int i = 0; i < (int)row_sum.size() && row_sum[i].first == rmax; i++) {
    for (int j = 0;
         j < 1000 && j < (int)col_sum.size() && col_sum[j].first == cmax; j++) {
      int cur = row_sum[i].first + col_sum[j].first;
      int r = row_sum[i].second;
      int c = col_sum[j].second;
      for (auto t : tgt[r]) {
        if (t == c)
          cur--;
      }
      // cout << r << "," << c << "," << cur << endl;
      res = max(res, cur);
    }
  }
  for (int j = 0; j < (int)col_sum.size() && col_sum[j].first == cmax; j++) {
    for (int i = 0;
         i < 1000 && i < (int)row_sum.size() && row_sum[i].first == rmax; i++) {
      int cur = row_sum[i].first + col_sum[j].first;
      int r = row_sum[i].second;
      int c = col_sum[j].second;
      for (auto t : tgt[r]) {
        if (t == c)
          cur--;
      }
      // cout << r << "," << c << "," << cur << endl;
      res = max(res, cur);
    }
  }

  cout << res << endl;
  return 0;
}