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

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

using namespace std;
using namespace __gnu_pbds;

#define int long long
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define yn(x) ((x) ? "Yes" : "No")

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

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

const int siz = 3e5 + 10;
const int SIZ = 1e6 + 10;
const int mod = 1e9 + 7;
const int maxx = 2e9;
const int MAXX = 1e18;
const string file = "1";

int sumc[siz], sumr[siz];

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  //    freopen ((file + ".inp").c_str(), "r", stdin);
  //    freopen ((file + ".out").c_str(), "w", stdout);

  int m, n, k;
  cin >> m >> n >> k;
  set<ii> se;
  while (k--) {
    int x, y;
    cin >> x >> y;

    sumr[x]++;
    sumc[y]++;
    se.insert(ii(x, y));
  }

  int mxr = *max_element(sumr + 1, sumr + 1 + m);
  int mxc = *max_element(sumc + 1, sumc + 1 + n);

  vector<int> mxrs, mxcs;
  for (int i = 1; i <= m; i++) {
    if (sumr[i] == mxr) {
      mxrs.push_back(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (sumc[i] == mxc) {
      mxcs.push_back(i);
    }
  }

  for (auto &x : mxrs) {
    for (auto &y : mxcs) {
      if (se.find(ii(x, y)) == se.end()) {
        return cout << mxr + mxc << "\\n", 0;
      }
    }
  }

  cout << mxr + mxc - 1 << "\\n";

  //    cerr << "Time: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";

  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;
using namespace __gnu_pbds;

#define int long long
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define yn(x) ((x) ? "Yes" : "No")

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

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

const int siz = 3e5 + 10;
const int SIZ = 1e6 + 10;
const int mod = 1e9 + 7;
const int maxx = 2e9;
const int MAXX = 1e18;
const string file = "1";

map<ii, int> a;
int sumc[siz], sumr[siz];

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  //    freopen ((file + ".inp").c_str(), "r", stdin);
  //    freopen ((file + ".out").c_str(), "w", stdout);

  int m, n, k;
  cin >> m >> n >> k;
  while (k--) {
    int x, y;
    cin >> x >> y;
    a[ii(x, y)] = 1;
    sumr[x]++;
    sumc[y]++;
  }

  int ans = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      ans = max(ans, sumr[i] + sumc[j] - (a[ii(i, j)] == 1));
    }
  }

  cout << ans << "\\n";

  //    cerr << "Time: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";

  return 0;
}