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

#pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
template <class T>
using ordered_set =
    __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag,
                     __gnu_pbds::tree_order_statistics_node_update>;
template <class T>
using ordered_multiset =
    __gnu_pbds::tree<T, __gnu_pbds::null_type, less_equal<T>,
                     __gnu_pbds::rb_tree_tag,
                     __gnu_pbds::tree_order_statistics_node_update>;

#define PI atan2(0, -1)
#define EPS 1e-9
#define INF 5e18
#define MOD 1000000007

#define all(x) begin(x), end(x)
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

int N, M, X, numRow[300010], numCol[300010];
set<pair<int, int>> lol;

int main() {
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  cin >> N >> M >> X;
  int mr = 0, mc = 0;
  for (int i = 0; i < X; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    lol.insert({x, y});
    numRow[x]++;
    numCol[y]++;
    mr = max(mr, numRow[x]), mc = max(mc, numCol[y]);
  }
  vector<int> maxRow, maxCol;
  for (int i = 0; i < N; i++) {
    if (numRow[i] == mr)
      maxRow.push_back(i);
  }
  for (int i = 0; i < M; i++) {
    if (numCol[i] == mc)
      maxCol.push_back(i);
  }
  int ret = mr + mc - 1;
  for (int x : maxRow)
    for (int y : maxCol) {
      if (lol.find({x, y}) != lol.end())
        continue;
      ret = mr + mc;
      break;
    }
  cout << ret << '\\n';
  return 0;
}

/******************************
Kateba ii dake no hanashi darou
Success is only a victory away
- No Game No Life Opening
******************************/


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
template <class T>
using ordered_set =
    __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag,
                     __gnu_pbds::tree_order_statistics_node_update>;
template <class T>
using ordered_multiset =
    __gnu_pbds::tree<T, __gnu_pbds::null_type, less_equal<T>,
                     __gnu_pbds::rb_tree_tag,
                     __gnu_pbds::tree_order_statistics_node_update>;

#define PI atan2(0, -1)
#define EPS 1e-9
#define INF 5e18
#define MOD 1000000007

#define all(x) begin(x), end(x)
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

int N, M, X, numRow[300010], numCol[300010];
set<pair<int, int>> lol;

int main() {
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  cin >> N >> M >> X;
  for (int i = 0; i < X; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    lol.insert({x, y});
    numRow[x]++;
    numCol[y]++;
  }
  int ret = 0;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      ret = max(ret, numRow[i] + numCol[j] - (lol.find({i, j}) != lol.end()));
  cout << ret << '\\n';
  return 0;
}

/******************************
Kateba ii dake no hanashi darou
Success is only a victory away
- No Game No Life Opening
******************************/