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

/*

*/
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define LSOne(S) (S & (-S))
#define EPS 1e-9

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pd;
typedef tree<pi, null_type, less<pi>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0); // FOR QUERY PROBLEM DON'T FORGET TO UNCOMMENT THIS
  int h, w, m;
  cin >> h >> w >> m;
  // set<pi> is;
  vector<int> vrow(3e5 + 5);
  vector<int> vcols(3e5 + 5);
  vector<vector<int>> vvcols(3e5 + 5);

  for (int i = 0; i < m; ++i) {
    int hh, ww;
    cin >> hh >> ww;
    // is.insert({hh, ww});
    ++vrow[hh];
    ++vcols[ww];
    vvcols[ww].push_back(hh);
  }

  vector<int> mxrow;
  mxrow.reserve(m + 5);
  vector<int> mxcols;
  mxcols.reserve(m + 5);

  set<int> sr;
  int mxr = *max_element(vrow.begin(), vrow.end());
  for (int i = 0; i < (int)vrow.size(); ++i) {
    if (vrow[i] == mxr) {
      mxrow.push_back(i);
      sr.insert(i);
    }
  }

  int mxc = *max_element(vcols.begin(), vcols.end());
  for (int i = 0; i < (int)vcols.size(); ++i) {
    if (vcols[i] == mxc) {
      mxcols.push_back(i);
    }
  }

  int ans = 0;
  for (auto &mc : mxcols) {
    int cnt = 0;
    for (auto &r : vvcols[mc]) {
      if (sr.count(r)) {
        ++cnt;
      }
    }
    int red = (cnt == (int)sr.size());
    ans = max(ans, mxr + mxc - red);
  }
  cout << ans << "\\n";
  return 0;
}

/*

*/

Error: TLE

Fixed submission:

/*

*/
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define LSOne(S) (S & (-S))
#define EPS 1e-9

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pd;
typedef tree<pi, null_type, less<pi>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0); // FOR QUERY PROBLEM DON'T FORGET TO UNCOMMENT THIS
  int h, w, m;
  cin >> h >> w >> m;
  set<pi> is;
  vector<int> vrow(3e5 + 5);
  vector<int> vcols(3e5 + 5);

  for (int i = 0; i < m; ++i) {
    int hh, ww;
    cin >> hh >> ww;
    is.insert({hh, ww});
    ++vrow[hh];
    ++vcols[ww];
  }

  vector<int> mxrow;
  mxrow.reserve(m + 5);
  vector<int> mxcols;
  mxcols.reserve(m + 5);

  int mxr = *max_element(vrow.begin(), vrow.end());
  for (int i = 0; i < (int)vrow.size(); ++i) {
    if (vrow[i] == mxr) {
      mxrow.push_back(i);
    }
  }

  int mxc = *max_element(vcols.begin(), vcols.end());
  for (int i = 0; i < (int)vcols.size(); ++i) {
    if (vcols[i] == mxc) {
      mxcols.push_back(i);
    }
  }

  int ans = 0;
  for (auto &mr : mxrow) {
    for (auto &mc : mxcols) {
      int temp = vrow[mr] + vcols[mc] - (is.count({mr, mc}));
      ans = max(ans, temp);
    }
  }
  cout << ans << "\\n";
  return 0;
}

/*

*/