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>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace __gnu_pbds;
using namespace std;
/*
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
*/
#define ull unsigned long long int
#define ll long long int
#define MAX 1000000007
// 1e9+7
#define pll pair<ll, ll>
#define pii pair<int, int>
#define piii pair<int, pii>
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FO                                                                     \\
  ifstream fin("a.in");                                                        \\
  ofstream fout("a.out");
#define TEST                                                                   \\
  int testcase;                                                                \\
  cin >> testcase;                                                             \\
  while (testcase--)
#define all(x) (x).begin(), (x).end()
#define mset0(x) memset((x), 0, sizeof((x)));
#define mset1(x) memset((x), -1, sizeof((x)));
#define endl "\\n"
#define plll pair<ll, pll>
#define no                                                                     \\
  {                                                                            \\
    cout << "NO" << endl;                                                      \\
    return;                                                                    \\
  }
#define yes                                                                    \\
  { cout << "YES" << endl; }
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
const int maxn = 3e5;
int arr[maxn + 2];

set<pii> st;
int x[maxn + 2] = {0}, y[maxn + 2] = {0};
int max_a = 0, max_b = 0;
void solve() {

  int h, w, m, a, b, c;
  cin >> h >> w >> m;
  while (m--) {
    cin >> a >> b;
    x[a]++;
    y[b]++;
    st.insert(pii(a, b));
    if (x[a] > max_a) {
      max_a = x[a];
    }
    if (y[b] > max_b) {
      max_b = y[b];
    }
  }
  set<int> vx, vy;
  for (int i = 1; i <= h; i++) {
    if (x[i] == max_a)
      vx.insert(i);
  }
  for (int j = 1; j <= w; j++) {
    if (y[j] == max_b)
      vy.insert(j);
  }
  ll tot = vx.size() * vy.size();
  for (auto pp : st) {
    if (vx.count(pp.first) && vy.count(pp.second))
      tot--;
  }
  if (tot == 0)
    cout << max_a + max_b - 1;
  else
    cout << max_a + max_b;
}

int main() {
  IO

  // TEST
  solve();

  return (0);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace __gnu_pbds;
using namespace std;
/*
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
*/
#define ull unsigned long long int
#define ll long long int
#define MAX 1000000007
// 1e9+7
#define pll pair<ll, ll>
#define pii pair<int, int>
#define piii pair<int, pii>
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FO                                                                     \\
  ifstream fin("a.in");                                                        \\
  ofstream fout("a.out");
#define TEST                                                                   \\
  int testcase;                                                                \\
  cin >> testcase;                                                             \\
  while (testcase--)
#define all(x) (x).begin(), (x).end()
#define mset0(x) memset((x), 0, sizeof((x)));
#define mset1(x) memset((x), -1, sizeof((x)));
#define endl "\\n"
#define plll pair<ll, pll>
#define no                                                                     \\
  {                                                                            \\
    cout << "NO" << endl;                                                      \\
    return;                                                                    \\
  }
#define yes                                                                    \\
  { cout << "YES" << endl; }
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
const int maxn = 2e5;
int arr[maxn + 2];
map<int, int> x, y;
map<pii, int> mp;
void solve() {

  int h, w, m, a, b, c;
  cin >> h >> w >> m;
  while (m--) {
    cin >> a >> b;
    x[a]++;
    y[b]++;
    mp[{a, b}]++;
  }
  int ans = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      a = x[i];
      b = y[j];
      c = mp[{i, j}];
      ans = max(ans, a + b - c);
    }
  }
  cout << ans;
}

int main() {
  IO

  // TEST
  solve();

  return (0);
}