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

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <math.h>
#include <numeric>
#include <utility>
#include <vector>

#define int long long

struct edge {
  int to;
  int cost;
};

using namespace std;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vb = vector<bool>;
using qi = queue<int>;
using P = pair<int, int>;
using graph = vector<vector<edge>>;
// using graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i, n) for (int i = 0; i < n; i++)
#define delim(i, n) cout << (i == n - 1 ? "\\n" : " ");

const int inf = 1LL << 60;
// const int inf = 100000000;
const int mod = 1000000007;

const int maxn = 300000;
int h, w, m;
vi hh(maxn), ww(maxn);
set<P> bomb, nobomb;
vii table(maxn);

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

  cin >> h >> w >> m;
  hh.assign(h, 0);
  ww.assign(w, 0);
  REP(i, m) {
    P p;
    cin >> p.first >> p.second;
    p.first--;
    p.second--;
    hh[p.first]++;
    ww[p.second]++;
    bomb.insert(p);
  }
  int hmax = 0;
  int hcnt = 0;
  vi hmaxs(h);
  REP(i, h) {
    if (hh[i] > hmax) {
      hmaxs[0] = i;
      hcnt = 1;
      hmax = hh[i];
    } else if (hh[i] == hmax) {
      hmaxs[hcnt] = i;
      hcnt++;
    }
  }

  int wmax = 0;
  int wcnt = 0;
  vi wmaxs(w);
  REP(i, w) {
    if (ww[i] > wmax) {
      wmaxs[0] = i;
      wcnt = 1;
      wmax = ww[i];
    } else if (ww[i] == wmax) {
      wmaxs[wcnt] = i;
      wcnt++;
    }
  }

  int cnt = 0;
  if (hcnt * wcnt <= m) {
    for (auto b : bomb) {
      if (binary_search(hmaxs.begin(), hmaxs.begin() + hcnt, b.first) &&
          binary_search(wmaxs.begin(), wmaxs.begin() + wcnt, b.second))
        cnt++;
    }
  }
  if (cnt == hcnt * wcnt)
    wmax--;

  cout << hmax + wmax << "\\n";
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <math.h>
#include <numeric>
#include <utility>
#include <vector>

#define int long long

struct edge {
  int to;
  int cost;
};

using namespace std;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vb = vector<bool>;
using qi = queue<int>;
using P = pair<int, int>;
using graph = vector<vector<edge>>;
// using graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i, n) for (int i = 0; i < n; i++)
#define delim(i, n) cout << (i == n - 1 ? "\\n" : " ");

const int inf = 1LL << 60;
// const int inf = 100000000;
const int mod = 1000000007;

const int maxn = 300000;
int h, w, m;
vi hh(maxn), ww(maxn);
set<P> bomb, nobomb;
vii table(maxn);

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

  cin >> h >> w >> m;
  hh.assign(h, 0);
  ww.assign(w, 0);
  REP(i, m) {
    P p;
    cin >> p.first >> p.second;
    p.first--;
    p.second--;
    hh[p.first]++;
    ww[p.second]++;
    bomb.insert(p);
  }
  REP(i, h) {
    REP(j, w) { table[i].push_back(hh[i] + ww[j]); }
  }
  for (auto b : bomb) {
    table[b.first][b.second]--;
  }

  int mm = 0;
  REP(i, h) {
    REP(j, w) { mm = max(mm, table[i][j]); }
  }
  cout << mm << "\\n";
}