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

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <math.h>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

const unsigned long long BASE = (unsigned long long)(1e9 + 7);

#define FROM_0(i, n) for (int i = 0; i < (n); i++)
#define FROM_S(i, s, e) for (int i = (s); i < (e); i++)
#define ITER_BEGIN_END(iter, vec)                                              \\
  for (auto iter = vec.begin(); iter < vec.end(); iter++)

struct BombRow {
  BombRow(int i) : idx(i) {}

  int idx;
  set<int> bombs;

  bool operator>(const BombRow &other) {
    return this->bombs.size() > other.bombs.size();
  }
};

struct BombCol {
  BombCol(int i) : idx(i), bombCount(0) {}

  int idx;
  int bombCount;

  bool operator>(const BombCol &other) {
    return this->bombCount > other.bombCount;
  }
};

int main() {
  int height, width, bomb;
  cin >> height >> width >> bomb;
  // init
  vector<BombRow> rows;
  vector<BombCol> cols;
  FROM_0(i, height) { rows.push_back(BombRow(i)); }
  FROM_0(i, width) { cols.push_back(BombCol(i)); }
  // input
  FROM_0(i, bomb) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    rows[h].bombs.insert(w);
    cols[w].bombCount++;
  }
  // sort
  std::sort(rows.begin(), rows.end(), std::greater<>());
  std::sort(cols.begin(), cols.end(), std::greater<>());
  // search
  int result = 0, maxCol = cols[0].bombCount;
  ITER_BEGIN_END(pRow, rows) {
    int now = 0;
    if (pRow->bombs.size() + maxCol <= result)
      break;
    ITER_BEGIN_END(pCol, cols) {
      if (pCol->bombCount + pRow->bombs.size() <= result)
        break;
      auto pFind = pRow->bombs.find(pCol->idx);
      int sum = pRow->bombs.size() + pCol->bombCount;
      if (pFind != pRow->bombs.end())
        sum--;
      result = max(sum, result);
    }
  }
  cout << result << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <math.h>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

const unsigned long long BASE = (unsigned long long)(1e9 + 7);

#define FROM_0(i, n) for (int i = 0; i < (n); i++)
#define FROM_S(i, s, e) for (int i = (s); i < (e); i++)
#define ITER_BEGIN_END(iter, vec)                                              \\
  for (auto iter = vec.begin(); iter < vec.end(); iter++)

struct BombRow {
  BombRow(int i) : idx(i) {}

  int idx;
  set<int> bombs;

  bool operator>(const BombRow &other) {
    return this->bombs.size() > other.bombs.size();
  }
};

struct BombCol {
  BombCol(int i) : idx(i), bombCount(0) {}

  int idx;
  int bombCount;

  bool operator>(const BombCol &other) {
    return this->bombCount > other.bombCount;
  }
};

int main() {
  int height, width, bomb;
  cin >> height >> width >> bomb;
  // init
  vector<BombRow> rows;
  vector<BombCol> cols;
  FROM_0(i, height) { rows.push_back(BombRow(i)); }
  FROM_0(i, width) { cols.push_back(BombCol(i)); }
  // input
  FROM_0(i, bomb) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    rows[h].bombs.insert(w);
    cols[w].bombCount++;
  }
  // sort
  std::sort(rows.begin(), rows.end(), std::greater<>());
  std::sort(cols.begin(), cols.end(), std::greater<>());
  // search
  int result = 0, maxCol = cols[0].bombCount;
  ITER_BEGIN_END(pRow, rows) {
    int now = 0;
    if (pRow->bombs.size() + maxCol < result)
      break;
    ITER_BEGIN_END(pCol, cols) {
      if (pCol->bombCount + pRow->bombs.size() < result)
        break;
      auto pFind = pRow->bombs.find(pCol->idx);
      int sum = pRow->bombs.size() + pCol->bombCount;
      if (pFind != pRow->bombs.end())
        sum--;
      result = max(sum, result);
    }
  }
  cout << result << endl;
  return 0;
}