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

/// Code by: Luis Alejandro
/// Handle: Sekai
/// Country: Cuba

#include <bits/stdc++.h>
#define endl '\\n'
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC option("arch=native", "tune=native", "no-zero-upper")
#pragma GCC target("avx2")

using namespace std;

typedef unsigned long long ull;
typedef long long lld;
typedef long double ld;
typedef pair<int, int> pii;

const int MAXN = 3 * 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ld PI = acos(-1);
const ld EPS = 1e-9;

#define lg2(x) 31 - __builtin_clz(x)
#define logx(a, b) (log(a) / log(b))
#define pow2(a) a *a
#define pow3(a) a *a *a
#define lcm(a, b) (a * b) / __gcd(a, b)

pii mkp(int a, int b) {
  if (a > b)
    swap(a, b);
  return make_pair(a, b);
}

int mod_pow(int a, int b) {
  if (!b)
    return 1;
  if (b & 1)
    return mod_pow(a, b - 1) * a % MOD;
  int power = mod_pow(a, b >> 1);
  return power * power % MOD;
}

int modular_inverse(int a) { return mod_pow(a, MOD - 2); }

vector<int> movy = {0, 0, 1, -1};
vector<int> movx = {1, -1, 0, 0};

void solve() {
  int h, w, m, y, x;
  cin >> h >> w >> m;
  vector<int> row(h, 0), col(w, 0);
  map<pii, int> a;
  for (int i = 0; i < m; i++) {
    cin >> y >> x;
    row[y - 1]++;
    col[x - 1]++;
    a[{y - 1, x - 1}] = 1;
  }
  int mxr = *max_element(row.begin(), row.end());
  int mxc = *max_element(col.begin(), col.end());
  vector<int> v1, v2;
  for (int i = 0; i < h; i++) {
    if (row[i] == mxr) {
      v1.push_back(i);
    }
  }
  for (int i = 0; i < w; i++) {
    if (col[i] == mxc) {
      v2.push_back(i);
    }
  }
  for (int y : v1) {
    for (int x : v2) {
      if (a[{y, x}] == 0) {
        cout << mxr + mxc << endl;
        return;
      }
    }
  }
  cout << mxr + mxc - 1 << endl;
  return;
}

int main() {
#ifndef ONLINE_JUDGE
  // freopen("IN.txt","r",stdin);
  // freopen("OUT.txt","w",stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

/// Code by: Luis Alejandro
/// Handle: Sekai
/// Country: Cuba

#include <bits/stdc++.h>
#define endl '\\n'
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC option("arch=native", "tune=native", "no-zero-upper")
#pragma GCC target("avx2")

using namespace std;

typedef unsigned long long ull;
typedef long long lld;
typedef long double ld;
typedef pair<int, int> pii;

const int MAXN = 3 * 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ld PI = acos(-1);
const ld EPS = 1e-9;

#define lg2(x) 31 - __builtin_clz(x)
#define logx(a, b) (log(a) / log(b))
#define pow2(a) a *a
#define pow3(a) a *a *a
#define lcm(a, b) (a * b) / __gcd(a, b)

pii mkp(int a, int b) {
  if (a > b)
    swap(a, b);
  return make_pair(a, b);
}

int mod_pow(int a, int b) {
  if (!b)
    return 1;
  if (b & 1)
    return mod_pow(a, b - 1) * a % MOD;
  int power = mod_pow(a, b >> 1);
  return power * power % MOD;
}

int modular_inverse(int a) { return mod_pow(a, MOD - 2); }

vector<int> movy = {0, 0, 1, -1};
vector<int> movx = {1, -1, 0, 0};

int fqr[MAXN], fqc[MAXN], y, x, h, w, m, mx = 0, idx = -1, ans = 0;
int fq[MAXN];

void solve() {
  cin >> h >> w >> m;
  vector<pii> p;
  for (int i = 0; i < m; i++) {
    cin >> y >> x;
    fqr[y]++;
    if (fqr[y] > mx) {
      mx = fqr[y];
      idx = y;
    }
    p.push_back({y, x});
  }
  set<int> best_ys;
  for (auto it : p) {
    y = it.first, x = it.second;
    if (fqr[y] == mx) {
      best_ys.insert(y);
    }
  }
  map<pii, int> fqmap;
  for (int i = 0; i < m; i++) {
    y = p[i].first, x = p[i].second;
    for (int by : best_ys) {
      if (by != y) {
        fqmap[{by, x}]++;
        fq[by] = max(fq[by], fqmap[{by, x}]);
      }
    }
  }
  for (int by : best_ys) {
    ans = max(ans, fqr[by] + fq[by]);
  }
  cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  // freopen("IN.txt","r",stdin);
  // freopen("OUT.txt","w",stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}