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

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef int Int;
typedef pair<Int, Int> pii;
typedef pair<Int, double> pid;
typedef pair<double, double> pdd;
typedef pair<Int, pii> pip;
typedef pair<pii, Int> ppi;
typedef pair<double, Int> pdp;
typedef vector<Int> veci;
typedef vector<double> vecd;
typedef vector<int> veci;
typedef vector<ll> vecll;
typedef vector<double> vecd;
typedef vector<pii> vecpii;
typedef vector<ppi> vecppi;
typedef vector<veci> mati;
typedef vector<vecll> matll;
typedef vector<vecd> matd;
#define PB(x) push_back(x)
#define EB(x) emplace_back(x)
#define ALL(x) x.begin(), x.end()
#define SZ(x) (x).size()
#define CLR(x) memset(x, 0, sizeof x)
#define pdebug() printf("%d\\n", __LINE__)
#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define FORO(i, n) REP(i, 0, (int)n - 1)
#define FORI(i, n) REP(i, 1, (int)n)
#define FORIT(i, t) for (auto i = t.begin(); i != t.end(); i++)
#define eps 1e-6
#define sqr(x) ((x) * (x))
#define dist(_a, _b) sqrt(sqr(_a.A - _b.A) + sqr(_a.B - _b.B))
#define norm(_a) sqrt(sqr(_a.A) + sqr(_a.B))
#define DEBUG 1
#if DEBUG
#define DUMP(a)                                                                \\
  do {                                                                         \\
    std::cout << #a " = " << (a) << ", ";                                      \\
  } while (false)
#define DUMPLN(a)                                                              \\
  do {                                                                         \\
    std::cout << #a " = " << (a) << std::endl;                                 \\
  } while (false)
#else
#define DUMP(a)
#define DUMPLN(a)
#endif
template <typename T, typename U> inline void getMin(T &a, U b) {
  if (a > b)
    a = b;
}
template <typename T, typename U> inline void getMax(T &a, U b) {
  if (a < b)
    a = b;
}
template <typename T> vector<T> getVector(const int n) { return vector<T>(n); }
template <typename T> vector<T> getVector(const int n, const T a) {
  return vector<T>(n, a);
}
template <typename T> vector<T> getEmptyVector() { return vector<T>(); }
template <typename T> void appendAll(vector<T> &a, vector<T> b) {
  a.insert(a.end(), b.begin(), b.end());
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << ", " << a.second << ")";
};
// #define X first
// #define Y second
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const ll MOD = 1000000007;
const int MAXN = 1 << 17;
const int inf = 1 << 27;
const ll llinf = 1LL << 50;
const int NIL = -inf;
int popcount(int x) { return __builtin_popcount(x); }

struct hash_pair {
  template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};

void test_case() {
  int H, W, M;
  cin >> H >> W >> M;
  veci h(M), w(M);
  veci hidx(H), widx(W);
  mati ps(H);
  FORO(i, M) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
    ps[h[i]].emplace_back(w[i]);
    hidx[h[i]]++;
    widx[w[i]]++;
  }
  mati hfreq(M + 2), wfreq(M + 2);
  int mxhf = 0;
  FORO(i, H) {
    sort(ALL(ps[i]));
    hfreq[hidx[i]].emplace_back(i);
    getMax(mxhf, hidx[i]);
  }
  int mxwf = 0;
  FORO(i, W) {
    wfreq[widx[i]].emplace_back(i);
    getMax(mxwf, widx[i]);
  }
  int f = mxwf + mxhf;
  int ans = 0;
  FORO(i, f + 1) {
    for (int j = 0; j < hfreq[i].size(); j++) {
      for (int k = 0; k < wfreq[f - i].size(); k++) {
        int exist = binary_search(ALL(ps[hfreq[i][j]]), wfreq[f - i][k]);
        getMax(ans, f - exist);
        if (ans == f) {
          goto ANS;
        }
      }
    }
  }
ANS:
  cout << ans << endl;
}

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

  int T = 1;
  while (T--) {
    test_case();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef int Int;
typedef pair<Int, Int> pii;
typedef pair<Int, double> pid;
typedef pair<double, double> pdd;
typedef pair<Int, pii> pip;
typedef pair<pii, Int> ppi;
typedef pair<double, Int> pdp;
typedef vector<Int> veci;
typedef vector<double> vecd;
typedef vector<int> veci;
typedef vector<ll> vecll;
typedef vector<double> vecd;
typedef vector<pii> vecpii;
typedef vector<ppi> vecppi;
typedef vector<veci> mati;
typedef vector<vecll> matll;
typedef vector<vecd> matd;
#define PB(x) push_back(x)
#define EB(x) emplace_back(x)
#define ALL(x) x.begin(), x.end()
#define SZ(x) (x).size()
#define CLR(x) memset(x, 0, sizeof x)
#define pdebug() printf("%d\\n", __LINE__)
#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define FORO(i, n) REP(i, 0, (int)n - 1)
#define FORI(i, n) REP(i, 1, (int)n)
#define FORIT(i, t) for (auto i = t.begin(); i != t.end(); i++)
#define eps 1e-6
#define sqr(x) ((x) * (x))
#define dist(_a, _b) sqrt(sqr(_a.A - _b.A) + sqr(_a.B - _b.B))
#define norm(_a) sqrt(sqr(_a.A) + sqr(_a.B))
#define DEBUG 1
#if DEBUG
#define DUMP(a)                                                                \\
  do {                                                                         \\
    std::cout << #a " = " << (a) << ", ";                                      \\
  } while (false)
#define DUMPLN(a)                                                              \\
  do {                                                                         \\
    std::cout << #a " = " << (a) << std::endl;                                 \\
  } while (false)
#else
#define DUMP(a)
#define DUMPLN(a)
#endif
template <typename T, typename U> inline void getMin(T &a, U b) {
  if (a > b)
    a = b;
}
template <typename T, typename U> inline void getMax(T &a, U b) {
  if (a < b)
    a = b;
}
template <typename T> vector<T> getVector(const int n) { return vector<T>(n); }
template <typename T> vector<T> getVector(const int n, const T a) {
  return vector<T>(n, a);
}
template <typename T> vector<T> getEmptyVector() { return vector<T>(); }
template <typename T> void appendAll(vector<T> &a, vector<T> b) {
  a.insert(a.end(), b.begin(), b.end());
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << ", " << a.second << ")";
};
// #define X first
// #define Y second
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const ll MOD = 1000000007;
const int MAXN = 1 << 17;
const int inf = 1 << 27;
const ll llinf = 1LL << 50;
const int NIL = -inf;
int popcount(int x) { return __builtin_popcount(x); }

struct hash_pair {
  template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};

void test_case() {
  int H, W, M;
  cin >> H >> W >> M;
  veci h(M), w(M);
  veci hidx(H), widx(W);
  mati ps(H);
  FORO(i, M) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
    ps[h[i]].emplace_back(w[i]);
    hidx[h[i]]++;
    widx[w[i]]++;
  }
  mati hfreq(M + 2), wfreq(M + 2);
  int mxhf = 0;
  FORO(i, H) {
    sort(ALL(ps[i]));
    hfreq[hidx[i]].emplace_back(i);
    getMax(mxhf, hidx[i]);
  }
  int mxwf = 0;
  FORO(i, W) {
    wfreq[widx[i]].emplace_back(i);
    getMax(mxwf, widx[i]);
  }
  int f = mxwf + mxhf;
  int ans = 0;
  FORO(i, f + 1) {
    for (int j = 0; j < hfreq[i].size(); j++) {
      for (int k = 0; k < wfreq[f - i].size(); k++) {
        int exist = binary_search(ALL(ps[hfreq[i][j]]), wfreq[f - i][k]);
        getMax(ans, f - exist);
      }
    }
  }
  cout << ans << endl;
}

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

  int T = 1;
  while (T--) {
    test_case();
  }

  return 0;
}