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

#pragma GCC optimize(2)
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 300005;
const int MAXM = 50005;
const int MOD = 1e9 + 7;

#define MP make_pair
#define INF 0x3f3f3f3f
#define closeSync                                                              \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mst(X, Y) memset(X, Y, sizeof(X))
#define rep(i, a, n) for (int i = (a); i <= (n); i++)
#define per(i, a, n) for (int i = (a); i >= (n); i--)
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

struct Node {
  int cr;
  int val;
  bool operator<(const Node &x) const { return val > x.val; }
};
int r[MAXN];
int c[MAXN];
map<pii, int> mp;
int main() {
  closeSync;
#ifdef DEBUG
  // freopen("C:/Users/86159/Desktop/a2.in.txt", "r", stdin);
  // freopen("C:/Users/86159/Desktop/output.txt", "w", stdout);
#endif
  // clock_t c1 = clock();
  int h, w, k;
  cin >> h >> w >> k;
  rep(i, 1, k) {
    int hh, ww;
    cin >> hh >> ww;
    mp[MP(hh, ww)]++;
    r[hh]++, c[ww]++;
  }
  vector<Node> vr;
  vector<Node> vc;
  rep(i, 1, h) if (r[i]) vr.push_back({i, r[i]});
  rep(i, 1, w) if (c[i]) vc.push_back({i, c[i]});
  sort(vr.begin(), vr.end());
  sort(vc.begin(), vc.end());
  int endr = vr.size() - 1, endc = vc.size() - 1;
  int tick1 = 1, tick2 = 1;
  while (tick1 <= endr && vr[tick1].val == vr[tick1 - 1].val) {
    tick1++;
  }
  while (tick2 <= endc && vc[tick2].val == vc[tick2 - 1].val) {
    tick2++;
  }
  int ans = -1;
  // bool flag = false;
  rep(i, 0, tick1 - 1) {
    Node cur = vr[i];
    rep(j, 0, tick2 - 1) {
      ans = max(ans, cur.val + vc[j].val - mp[MP(cur.cr, vc[j].cr)]);
      if (!mp[MP(cur.cr, vc[j].cr)])
        break;
    }
  }
  cout << ans << endl;

  // cerr << "Time:" << clock() - c1 << "ms" <<endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 300005;
const int MAXM = 50005;
const int MOD = 1e9 + 7;

#define MP make_pair
#define INF 0x3f3f3f3f
#define closeSync                                                              \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mst(X, Y) memset(X, Y, sizeof(X))
#define rep(i, a, n) for (int i = (a); i <= (n); i++)
#define per(i, a, n) for (int i = (a); i >= (n); i--)
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

struct Node {
  ll cr;
  ll val;
  bool operator<(const Node &x) const { return val > x.val; }
};
ll r[MAXN];
ll c[MAXN];
map<pll, int> mp;
int main() {
  closeSync;
#ifdef DEBUG
  // freopen("C:/Users/86159/Desktop/a2.in.txt", "r", stdin);
  // freopen("C:/Users/86159/Desktop/output.txt", "w", stdout);
#endif
  // clock_t c1 = clock();
  ll h, w, k;
  cin >> h >> w >> k;
  rep(i, 1, k) {
    ll hh, ww;
    cin >> hh >> ww;
    mp[MP(hh, ww)] = 1;
    r[hh]++, c[ww]++;
  }
  vector<Node> vr;
  vector<Node> vc;
  rep(i, 1, h) if (r[i]) vr.push_back({i, r[i]});
  rep(i, 1, w) if (c[i]) vc.push_back({i, c[i]});
  sort(vr.begin(), vr.end());
  sort(vc.begin(), vc.end());
  int endr = vr.size() - 1, endc = vc.size() - 1;
  int tick1 = 1, tick2 = 1;
  while (tick1 <= endr && vr[tick1].val == vr[tick1 - 1].val) {
    tick1++;
  }
  while (tick2 <= endc && vc[tick2].val == vc[tick2 - 1].val) {
    tick2++;
  }
  ll ans = -1;
  rep(i, 0, tick1 - 1) {
    Node cur = vr[i];
    rep(j, 0, tick2 - 1) {
      ans = max(ans, cur.val + vc[j].val - mp[MP(cur.cr, vc[j].cr)]);
    }
  }

  cout << ans << endl;

  // cerr << "Time:" << clock() - c1 << "ms" <<endl;
  return 0;
}