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

#include <bits/stdc++.h>

#define double long double
#define ff first
#define ss second
#define endl '\\n'
#define ii pair<int, int>
#define mp make_pair
#define mt make_tuple
#define DESYNC                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define vi vector<int>
#define vii vector<ii>
#define all(x) x.begin(), x.end()
#define EPS 1e-9
#define INF 1e18
#define ROOT 1
#define M 1000000007
#define curtime chrono::steady_clock::now().time_since_epoch().count
#define rep(i, beg, n, s) for (int i = beg; i < n; i += s)
const double PI = acos(-1);

using namespace std;

inline int mod(int n, int m = M) {
  int ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}

int exp(int n, int k) {
  if (k == 0)
    return 1;
  if (k == 1)
    return n;
  int ax = exp(n, k / 2);
  ax = mod(ax * ax);
  if (k % 2)
    ax = mod(ax * n);
  return ax;
}

int gcd(int a, int b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}

int solve(const vector<ii> &v) {
  int max_row = 0;
  int max_col = 0;
  for (ii p : v) {
    max_row = max(max_row, p.ff);
    max_col = max(max_col, p.ss);
  }
  vector<int> c(max_col + 1, 0);
  set<ii, greater<ii>> s;
  auto update = [&](int pos, int val) {
    int cur = c[pos];
    if (s.count(ii(cur, pos)))
      s.erase(ii(cur, pos));
    cur += val;
    c[pos] = cur;
    s.insert(ii(cur, pos));
  };
  for (ii p : v) {
    update(p.ss, 1);
    max_row = max(max_row, p.ff);
  }
  int n = v.size();
  vector<ii> rows[max_row + 1];
  for (ii p : v) {
    rows[p.ff].pb(p);
  }
  int ans = 0;
  for (int i = 0; i <= max_row; i++) {
    int id = i;
    int cur_ans = 0;
    for (auto coord : rows[i]) {
      update(coord.ss, -1);
    }
    ans = max(ans, (int)(rows[i].size()) + s.begin()->ff);
    for (auto coord : rows[i]) {
      update(coord.ss, +1);
    }
  }
  return ans;
}
// #define MULTIPLE_TEST_CASE
void solution() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ii> v(k);
  for (int i = 0; i < k; i++) {
    cin >> v[i].ff >> v[i].ss;
  }
  int ans = solve(v);
  for (ii &p : v)
    swap(p.ff, p.ss);
  ans = max(ans, solve(v));
  cout << ans << endl;
}

int32_t main() {
  DESYNC;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int t = 1;
#ifdef MULTIPLE_TEST_CASE
  cin >> t;
#endif
  while (t--)
    solution();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define int long long
#define double long double
#define ff first
#define ss second
#define endl '\\n'
#define ii pair<int, int>
#define mp make_pair
#define mt make_tuple
#define DESYNC                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define vi vector<int>
#define vii vector<ii>
#define all(x) x.begin(), x.end()
#define EPS 1e-9
#define INF 1e18
#define ROOT 1
#define M 1000000007
#define curtime chrono::steady_clock::now().time_since_epoch().count
#define rep(i, beg, n, s) for (int i = beg; i < n; i += s)
const double PI = acos(-1);

using namespace std;

inline int mod(int n, int m = M) {
  int ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}

int exp(int n, int k) {
  if (k == 0)
    return 1;
  if (k == 1)
    return n;
  int ax = exp(n, k / 2);
  ax = mod(ax * ax);
  if (k % 2)
    ax = mod(ax * n);
  return ax;
}

int gcd(int a, int b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}

int solve(const vector<ii> &v) {
  map<int, int> c;
  set<ii, greater<ii>> s;
  auto update = [&](int pos, int val) {
    int cur = c[pos];
    if (s.count(ii(cur, pos)))
      s.erase(ii(cur, pos));
    cur += val;
    c[pos] = cur;
    s.insert(ii(cur, pos));
  };
  int max_row = 0;
  for (ii p : v) {
    update(p.ss, 1);
    max_row = max(max_row, p.ff);
  }
  int n = v.size();
  vector<ii> rows[max_row + 1];
  for (ii p : v) {
    rows[p.ff].pb(p);
  }
  int ans = 0;
  for (int i = 0; i <= max_row; i++) {
    int id = i;
    int cur_ans = 0;
    for (auto coord : rows[i]) {
      update(coord.ss, -1);
    }
    ans = max(ans, (int)(rows[i].size()) + s.begin()->ff);
    for (auto coord : rows[i]) {
      update(coord.ss, +1);
    }
  }
  return ans;
}
// #define MULTIPLE_TEST_CASE
void solution() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ii> v(k);
  for (int i = 0; i < k; i++) {
    cin >> v[i].ff >> v[i].ss;
  }
  int ans = solve(v);
  for (ii &p : v)
    swap(p.ff, p.ss);
  ans = max(ans, solve(v));
  cout << ans << endl;
}

int32_t main() {
  DESYNC;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int t = 1;
#ifdef MULTIPLE_TEST_CASE
  cin >> t;
#endif
  while (t--)
    solution();
}