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

#include <bits/stdc++.h>

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define debug cout << "this far" << endl;
#define For(i, n) for (int i = 0; i < (n); ++i)
#define lsb(x) ((x) & (-x))
#define PI acos(-1)
#define line cout << endl;
#define getbit(n, i) (((n) & (1LL << (i))) != 0)
#define resetbit(n, i) ((n) & (~(1LL << (i))))
#define setbit(n, i) ((n) | (1LL << (i)))
#define togglebit(n, i) ((n) ^ (1LL << (i)))
#define gap " "
using namespace std;
const int N = 3e5 + 10;
multiset<int> row[N], col[N];
int mnus[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  int target;
  cin >> target;
  set<int> sr, sc;
  for (int i = 0; i < target; ++i) {
    int x, y;
    cin >> x >> y;
    sr.insert(x);
    sc.insert(y);
    row[x].insert(y);
    col[y].insert(x);
  }
  int mx_row_size = 0, mx_col_size = 0;
  for (auto i : sr)
    mx_row_size = max(mx_row_size, (int)row[i].size());
  for (auto i : sc)
    mx_col_size = max(mx_col_size, (int)col[i].size());
  set<int> mxrows, mxcols;
  for (auto i : sr) {
    if (row[i].size() == mx_row_size)
      mxrows.insert(i);
  }
  for (auto i : sc) {
    if (col[i].size() == mx_col_size)
      mxcols.insert(i);
  }
  int M = mxcols.size();
  for (auto i : mxcols) {
    for (auto j : col[i])
      mnus[j]++;
  }
  int ans = 0;
  for (auto i : sr) {
    // cout << i << gap << mnus[i] << endl;
    if (mnus[i] == M)
      ans = max(ans, (int)row[i].size() + mx_col_size - 1);
    else
      ans = max(ans, mx_col_size + (int)row[i].size());
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define debug cout << "this far" << endl;
#define For(i, n) for (int i = 0; i < (n); ++i)
#define lsb(x) ((x) & (-x))
#define PI acos(-1)
#define line cout << endl;
#define getbit(n, i) (((n) & (1LL << (i))) != 0)
#define resetbit(n, i) ((n) & (~(1LL << (i))))
#define setbit(n, i) ((n) | (1LL << (i)))
#define togglebit(n, i) ((n) ^ (1LL << (i)))
#define gap " "
using namespace std;
const int N = 3e5 + 10;
multiset<int> row[N], col[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  int target;
  cin >> target;
  set<int> sr, sc;
  for (int i = 0; i < target; ++i) {
    int x, y;
    cin >> x >> y;
    sr.insert(x);
    sc.insert(y);
    row[x].insert(y);
    col[y].insert(x);
  }
  int mx_row_size = 0, mx_col_size = 0;
  for (auto i : sr)
    mx_row_size = max(mx_row_size, (int)row[i].size());
  for (auto i : sc)
    mx_col_size = max(mx_col_size, (int)col[i].size());
  set<int> mxrows, mxcols;
  for (auto i : sr) {
    if (row[i].size() == mx_row_size)
      mxrows.insert(i);
  }
  for (auto i : sc) {
    if (col[i].size() == mx_col_size)
      mxcols.insert(i);
  }
  int ans = 0;
  for (auto i : sr) {
    int cr_ans = 0;
    for (auto j : mxcols) {
      if (!col[j].count(i))
        cr_ans = max(cr_ans, mx_col_size);
      else
        cr_ans = max(cr_ans, mx_col_size - 1);
    }
    ans = max(ans, cr_ans + (int)row[i].size());
  }
  cout << ans;
}