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

#include <bits/stdc++.h>
#include <ctime>
using namespace std;
#define bit(x, i) (x & (1 << i))          // select the bit of position i of x
#define lowbit(x) ((x) & ((x) ^ ((x)-1))) // get the lowest bit of x
#define hBit(msb, n)                                                           \\
  asm("bsrl %1,%0"                                                             \\
      : "=r"(msb)                                                              \\
      : "r"(n)) // get the highest bit of x, maybe the fastest
#define max(a, b) (a < b ? b : a)
#define min(a, b) (a > b ? b : a)
#define abs(x)                                                                 \\
  (x < 0 ? (-x) : x) // big bug here if "-x" is not surrounded by "()"
#define IN(i, l, r) (l < i && i < r) // the next for are for checking bound
#define LINR(i, l, r) (l <= i && i <= r)
#define LIN(i, l, r) (l <= i && i < r)
#define INR(i, l, r) (l < i && i <= r)
#define rep(i, n) for (i = 0; i < n; ++i)
#define REP(i, k, n) for (i = k; i <= n; ++i)
#define REPR(i, k, n) for (i = k; i >= n; --i)
#define getI(a)                                                                \\
  scanf("%d", &a) // next three are handy ways to get ints, it's also force you
                  // to use '&' sign
#define getII(a, b) scanf("%d%d", &a, &b)
#define getIII(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define wez(n)                                                                 \\
  int(n);                                                                      \\
  scanf(                                                                       \\
      "%d",                                                                    \\
      &(n)) // handy if the input is right after the definition of a variable
#define wez2(n, m)                                                             \\
  int(n), (m);                                                                 \\
  scanf("%d %d", &(n), &(m))
#define wez3(n, m, k)                                                          \\
  int(n), (m), (k);                                                            \\
  scanf("%d %d %d", &(n), &(m), &(k))
#define TESTS wez(testow) while (testow--) // for multilple cases problems
#define whileZ                                                                 \\
  int T;                                                                       \\
  getI(T);                                                                     \\
  while (T--)                             // the same as above
#define getS(x) scanf("%s", x)            // get a char* string
#define clr(a, x) memset(a, x, sizeof(a)) // set elements of array to some value
#define char2Int(c) (c - '0')
#define lastEle(vec) vec[vec.size() - 1]
#define SZ(x) ((int)((x).size()))
#define REMAX(a, b) (a) = max((a), (b)) // set a to the maximum of a and b
#define REMIN(a, b) (a) = min((a), (b));
#define FOREACH(i, t)                                                          \\
  for (typeof(t.begin()) i = t.begin(); i != t.end();                          \\
       i++)                           // traverse an STL data structure
#define ALL(c) (c).begin(), (c).end() // handy for function like "sort()"
#define PRESENT(c, x) ((c).find(x) != (c).end())
#define CPRESENT(c, x) (find(ALL(c), x) != (c).end())
#define ll                                                                     \\
  long long // data types used often, but you don't want to type them time by
            // time
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0); // to synchronize the input of cin and scanf
#define INF 1001001001
#define PI 3.1415926535897932384626
// for map, pair
#define mp make_pair
#define fi first
#define se second
// for debug
inline void pisz(int n) { printf("%d\\n", n); }
#define DBG(vari) cerr << #vari << " = " << (vari) << endl;
#define printA(a, L, R) FE(i, L, R) cout << a[i] << (i == R ? '\\n' : ' ')
#define printV(a) printA(a, 0, a.size() - 1)
#define MAXN 10000
// for vectors
#define pb push_back
typedef int elem_t;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
// directions
const int fx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
const int fxx[9][2] = {{0, 0}, {0, 1},  {0, -1}, {1, 0},  {-1, 0},
                       {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
const int N = 500;
const ll mod = 1000000007;

int main() {
  IOS

      ll h,
      w, m;
  cin >> h >> w >> m;
  vector<pair<ll, ll>> col(w), row(h);
  for (ll i = 0; i < w; i++) {
    col[i] = {0, i};
  }
  for (ll i = 0; i < h; i++) {
    row[i] = {0, i};
  }
  set<ii> targets;
  for (ll i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    targets.insert({u, v});
    col[v].fi++;
    row[u].fi++;
  }

  sort(col.begin(), col.end());
  sort(row.begin(), row.end());
  ll ans = col[col.size() - 1].fi + row[row.size() - 1].fi;

  unordered_set<int> coll, roww;
  for (ll i = col.size() - 1; i >= 0; i--) {
    if (col[i].fi == col[col.size() - 1].fi) {
      coll.insert(col[i].se);
    }
  }
  for (ll i = row.size() - 1; i >= 0; i--) {
    if (row[i].fi == row[row.size() - 1].fi) {
      roww.insert(row[i].se);
    }
  }
  ll err = 1;
  ll cnt = 0;
  for (auto x : roww) {
    for (auto y : coll) {
      cnt++;
      if (cnt > 10000000)
        break;
      pair<ll, ll> temp = {x, y};
      if (targets.find(temp) == targets.end())
        err = 0;
    }
  }
  ans -= err;
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ctime>
using namespace std;
#define bit(x, i) (x & (1 << i))          // select the bit of position i of x
#define lowbit(x) ((x) & ((x) ^ ((x)-1))) // get the lowest bit of x
#define hBit(msb, n)                                                           \\
  asm("bsrl %1,%0"                                                             \\
      : "=r"(msb)                                                              \\
      : "r"(n)) // get the highest bit of x, maybe the fastest
#define max(a, b) (a < b ? b : a)
#define min(a, b) (a > b ? b : a)
#define abs(x)                                                                 \\
  (x < 0 ? (-x) : x) // big bug here if "-x" is not surrounded by "()"
#define IN(i, l, r) (l < i && i < r) // the next for are for checking bound
#define LINR(i, l, r) (l <= i && i <= r)
#define LIN(i, l, r) (l <= i && i < r)
#define INR(i, l, r) (l < i && i <= r)
#define rep(i, n) for (i = 0; i < n; ++i)
#define REP(i, k, n) for (i = k; i <= n; ++i)
#define REPR(i, k, n) for (i = k; i >= n; --i)
#define getI(a)                                                                \\
  scanf("%d", &a) // next three are handy ways to get ints, it's also force you
                  // to use '&' sign
#define getII(a, b) scanf("%d%d", &a, &b)
#define getIII(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define wez(n)                                                                 \\
  int(n);                                                                      \\
  scanf(                                                                       \\
      "%d",                                                                    \\
      &(n)) // handy if the input is right after the definition of a variable
#define wez2(n, m)                                                             \\
  int(n), (m);                                                                 \\
  scanf("%d %d", &(n), &(m))
#define wez3(n, m, k)                                                          \\
  int(n), (m), (k);                                                            \\
  scanf("%d %d %d", &(n), &(m), &(k))
#define TESTS wez(testow) while (testow--) // for multilple cases problems
#define whileZ                                                                 \\
  int T;                                                                       \\
  getI(T);                                                                     \\
  while (T--)                             // the same as above
#define getS(x) scanf("%s", x)            // get a char* string
#define clr(a, x) memset(a, x, sizeof(a)) // set elements of array to some value
#define char2Int(c) (c - '0')
#define lastEle(vec) vec[vec.size() - 1]
#define SZ(x) ((int)((x).size()))
#define REMAX(a, b) (a) = max((a), (b)) // set a to the maximum of a and b
#define REMIN(a, b) (a) = min((a), (b));
#define FOREACH(i, t)                                                          \\
  for (typeof(t.begin()) i = t.begin(); i != t.end();                          \\
       i++)                           // traverse an STL data structure
#define ALL(c) (c).begin(), (c).end() // handy for function like "sort()"
#define PRESENT(c, x) ((c).find(x) != (c).end())
#define CPRESENT(c, x) (find(ALL(c), x) != (c).end())
#define ll                                                                     \\
  long long // data types used often, but you don't want to type them time by
            // time
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0); // to synchronize the input of cin and scanf
#define INF 1001001001
#define PI 3.1415926535897932384626
// for map, pair
#define mp make_pair
#define fi first
#define se second
// for debug
inline void pisz(int n) { printf("%d\\n", n); }
#define DBG(vari) cerr << #vari << " = " << (vari) << endl;
#define printA(a, L, R) FE(i, L, R) cout << a[i] << (i == R ? '\\n' : ' ')
#define printV(a) printA(a, 0, a.size() - 1)
#define MAXN 10000
// for vectors
#define pb push_back
typedef int elem_t;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
// directions
const int fx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
const int fxx[9][2] = {{0, 0}, {0, 1},  {0, -1}, {1, 0},  {-1, 0},
                       {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
const int N = 500;
const ll mod = 1000000007;

int main() {
  IOS

      ll h,
      w, m;
  cin >> h >> w >> m;
  vector<pair<ll, ll>> col(w), row(h);
  for (ll i = 0; i < w; i++) {
    col[i] = {0, i};
  }
  for (ll i = 0; i < h; i++) {
    row[i] = {0, i};
  }
  set<ii> targets;
  for (ll i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    targets.insert({u, v});
    col[v].fi++;
    row[u].fi++;
  }

  sort(col.begin(), col.end());
  sort(row.begin(), row.end());
  ll ans = col[col.size() - 1].fi + row[row.size() - 1].fi;

  unordered_set<int> coll, roww;
  for (ll i = col.size() - 1; i >= 0; i--) {
    if (col[i].fi == col[col.size() - 1].fi) {
      coll.insert(col[i].se);
    }
  }
  for (ll i = row.size() - 1; i >= 0; i--) {
    if (row[i].fi == row[row.size() - 1].fi) {
      roww.insert(row[i].se);
    }
  }
  ll err = 1;

  for (auto x : roww) {
    for (auto y : coll) {
      pair<ll, ll> temp = {x, y};
      if (targets.find(temp) == targets.end())
        err = 0;
    }
  }
  ans -= err;
  cout << ans;
}