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

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define endl "\\n"
#define JAM(x) cout << #x << " = " << x << "\\n"
#define II()                                                                   \\
  ({                                                                           \\
    int a;                                                                     \\
    read(a);                                                                   \\
    a;                                                                         \\
  })
#define LL()                                                                   \\
  ({                                                                           \\
    ll a;                                                                      \\
    read(a);                                                                   \\
    a;                                                                         \\
  })
#define DD()                                                                   \\
  ({                                                                           \\
    double a;                                                                  \\
    scanf("%lf", &a);                                                          \\
    a;                                                                         \\
  })

// Fast Reader
template <class T> inline bool read(T &x) {
  int c = getchar();
  int sgn = 1;
  while (~c && c < '0' || c > '9') {
    if (c == '-')
      sgn = -1;
    c = getchar();
  }
  for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar())
    x = x * 10 + c - '0';
  x *= sgn;
  return ~c;
}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> PII;
typedef vector<PII> VPI;

const int INF = 2e9;
const int MX = 100005;
const int MOD = 1000000007;
const double PI = acos(-1.0);

vector<int> sieve(100001);

long long int power(long long int a, long long int b) {
  a %= MOD;
  long long int res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
bool isprime(int n) {
  if (n == 1)
    return 0;
  int i;
  for (i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

void SIEVE() {
  int i, j;
  for (i = 0; i <= 100000; i++)
    sieve[i] = i;
  for (i = 2; i * i <= 100000; i++) {
    if (sieve[i] != i)
      continue;
    for (j = i * i; j <= 100000; j = j + i) {
      if (sieve[j] == j)
        sieve[j] = i;
    }
  }
}

/*_______________________________________________*/

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  int tot;
  cin >> tot;
  int ans = 0;
  vector<int> row(n);
  int mx = 0, mxr = 0;
  vector<int> col(m), mxcol(m), mxrow(n);
  int i, j;
  map<pair<int, int>, int> bomb;
  for (i = 0; i < tot; i++) {
    int x, y;
    cin >> x >> y;
    bomb[{x - 1, y - 1}] = -1;
    row[x - 1]++;
    col[y - 1]++;
    mx = max(mx, col[y - 1]);
    mxr = max(mxr, row[x - 1]);
  }
  int k = 0, d = 0;
  for (i = 0; i < m; i++) {
    if (col[i] == mx)
      mxcol[d++] = (i);
  }
  for (i = 0; i < n; i++) {
    if (row[i] == mxr)
      mxrow[k++] = (i);
  }
  if (mxr == m) {
    cout << m + mx - 1;
    return 0;
  }
  for (i = 0; i < k; i++) {
    for (j = 0; j < d; j++) {
      ans =
          max(ans, row[mxrow[i]] + col[mxcol[j]] + bomb[{mxrow[i], mxcol[j]}]);
    }
    if (ans == mxr + mx)
      break;
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define endl "\\n"
#define JAM(x) cout << #x << " = " << x << "\\n"
#define II()                                                                   \\
  ({                                                                           \\
    int a;                                                                     \\
    read(a);                                                                   \\
    a;                                                                         \\
  })
#define LL()                                                                   \\
  ({                                                                           \\
    ll a;                                                                      \\
    read(a);                                                                   \\
    a;                                                                         \\
  })
#define DD()                                                                   \\
  ({                                                                           \\
    double a;                                                                  \\
    scanf("%lf", &a);                                                          \\
    a;                                                                         \\
  })

// Fast Reader
template <class T> inline bool read(T &x) {
  int c = getchar();
  int sgn = 1;
  while (~c && c < '0' || c > '9') {
    if (c == '-')
      sgn = -1;
    c = getchar();
  }
  for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar())
    x = x * 10 + c - '0';
  x *= sgn;
  return ~c;
}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> PII;
typedef vector<PII> VPI;

const int INF = 2e9;
const int MX = 100005;
const int MOD = 1000000007;
const double PI = acos(-1.0);

vector<int> sieve(100001);

long long int power(long long int a, long long int b) {
  a %= MOD;
  long long int res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
bool isprime(int n) {
  if (n == 1)
    return 0;
  int i;
  for (i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

void SIEVE() {
  int i, j;
  for (i = 0; i <= 100000; i++)
    sieve[i] = i;
  for (i = 2; i * i <= 100000; i++) {
    if (sieve[i] != i)
      continue;
    for (j = i * i; j <= 100000; j = j + i) {
      if (sieve[j] == j)
        sieve[j] = i;
    }
  }
}

/*_______________________________________________*/

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  int tot;
  cin >> tot;
  int ans = 0;
  vector<int> row(n);
  int mx = 0, mxr = 0;
  vector<int> col(m), mxcol(m), mxrow(n);
  int i, j;
  map<pair<int, int>, int> bomb;
  for (i = 0; i < tot; i++) {
    int x, y;
    cin >> x >> y;
    bomb[{x - 1, y - 1}] = -1;
    row[x - 1]++;
    col[y - 1]++;
    mx = max(mx, col[y - 1]);
    mxr = max(mxr, row[x - 1]);
  }
  int k = 0, d = 0;
  for (i = 0; i < m; i++) {
    if (col[i] == mx)
      mxcol[d++] = (i);
  }
  for (i = 0; i < n; i++) {
    if (row[i] == mxr)
      mxrow[k++] = (i);
  }
  if (mxr == m) {
    cout << m + mx - 1;
    return 0;
  }
  for (i = 0; i < k; i++) {
    for (j = 0; j < d; j++) {
      ans =
          max(ans, row[mxrow[i]] + col[mxcol[j]] + bomb[{mxrow[i], mxcol[j]}]);
    }
    if (ans == n + m - 1)
      break;
  }
  cout << ans;
}