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

// #define _GLIBCXX_DEBUG
// #pragma GCC optimize ("trapv") // kills the program on integer overﬂows (but
// is really slow).
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;

#define ll long long
#define ull unsigned long long
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define vt vector
#define vll vt<ll>
#define pll pair<ll, ll>
#define vpll vt<pll>
#define vvll vt<vll>
#define all(v) v.begin(), v.end()
#define FOR(i, n) for (ll i = 0; i < n; i++)
#define ffo(i, a, b) for (ll i = a; i <= b; i++)
#define rfo(i, a, b) for (ll i = a; i >= b; i--)
#define space cout << "\\n\\n";
#define endl "\\n"
#define pqmx priority_queue<ll>
#define pqmn priority_queue<ll, vll, greater<ll>>
#define fps(x, y) fixed << setprecision(y) << x
#define merg(a, b, c)                                                          \\
  set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()))
#define set_ar(arr, v) memset(arr, v, sizeof(arr))
#define go_t                                                                   \\
  int testcases;                                                               \\
  cin >> testcases;                                                            \\
  ffo(caseno, 1, testcases)

#define ctime auto start = high_resolution_clock::now()
#define etime auto stop = high_resolution_clock::now()
#define ptime                                                                  \\
  auto z1z = duration_cast<microseconds>(stop - start);                        \\
  cout << "Time elapsed : " << z1z.count() << " microseconds\\n"

#define dbg1(x) cout << #x << ": " << x << '\\n';
#define dbg2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << '\\n';
#define dbg3(x, y, z)                                                          \\
  cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \\
       << z << '\\n';
#define dbg4(a, b, c, d)                                                       \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << '\\n';
#define dbg5(a, b, c, d, e)                                                    \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << '\\n';

const ll mod = 1e9 + 7;
const ll N = 3e3 + 6;
const ll maxN = 1e4 + 5;
const ll MAX_SIZE = 2e6 + 6;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const double PI = 3.14159265359;

ll powerM(ll x, ll y, ll M = mod) { // default argument
  ll v = 1;
  x = x % M;
  while (y > 0) {
    if (y & 1)
      v = (v * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return v;
}

ll power(ll x, ll y) {
  ll v = 1;
  while (y > 0) {
    if (y & 1)
      v = v * x;
    y = y >> 1;
    x = x * x;
  }
  return v;
}

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
// up, right, down, left
const int mx = 3e5 + 10;
ll row[mx];
ll col[mx];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in3.txt", "r", stdin);
  freopen("out2.txt", "w", stdout);
#endif
  FIO ll h, w, m;
  cin >> h >> w >> m;
  ll hi, wi;
  vt<pll> mp1;
  FOR(i, m) {
    cin >> hi >> wi;
    pll t = {hi, wi};
    mp1.pb(t);
    row[hi]++;
    col[wi]++;
  }
  ll ans = 0;
  ll v1 = 0, v2 = 0;
  for (int i = 1; i <= 3e5; ++i) {
    v1 = max(v1, row[i]);
    v2 = max(v2, col[i]);
  }
  ll rr = 0, cc = 0;
  for (int i = 1; i < 3e5 + 1; ++i) {
    if (v1 == row[i])
      ++rr;
    if (v2 == col[i])
      ++cc;
  }
  ans = v1 + v2;
  // now if there exists a pair i,j from rr,cc such (i,j) is not a target then
  // ans remains same otherwise it gets decremented by 1
  // So we will be approaching this in reverse order i.e.,
  // we will count the number of targets whose rows have v1 targets and columns
  // have v2 targets if it's equal to all the possible cells (i.e. size(rr) *
  // size(cc)) which common for any pair of row and column from rr and cc, then
  // we would have to decrement the ans
  bool flg = 0;
  ll cnt = 0;
  for (int i = 0; i < mp1.size(); ++i) {
    if (row[mp1[i].ff] == v1 && col[mp1[i].ss] == v2)
      ++cnt;
  }
  if (cnt == rr * cc)
    --ans;
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
// #pragma GCC optimize ("trapv") // kills the program on integer overﬂows (but
// is really slow).
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;

#define ll long long
#define ull unsigned long long
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define vt vector
#define vll vt<ll>
#define pll pair<ll, ll>
#define vpll vt<pll>
#define vvll vt<vll>
#define all(v) v.begin(), v.end()
#define FOR(i, n) for (ll i = 0; i < n; i++)
#define ffo(i, a, b) for (ll i = a; i <= b; i++)
#define rfo(i, a, b) for (ll i = a; i >= b; i--)
#define space cout << "\\n\\n";
#define endl "\\n"
#define pqmx priority_queue<ll>
#define pqmn priority_queue<ll, vll, greater<ll>>
#define fps(x, y) fixed << setprecision(y) << x
#define merg(a, b, c)                                                          \\
  set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()))
#define set_ar(arr, v) memset(arr, v, sizeof(arr))
#define go_t                                                                   \\
  int testcases;                                                               \\
  cin >> testcases;                                                            \\
  ffo(caseno, 1, testcases)

#define ctime auto start = high_resolution_clock::now()
#define etime auto stop = high_resolution_clock::now()
#define ptime                                                                  \\
  auto z1z = duration_cast<microseconds>(stop - start);                        \\
  cout << "Time elapsed : " << z1z.count() << " microseconds\\n"

#define dbg1(x) cout << #x << ": " << x << '\\n';
#define dbg2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << '\\n';
#define dbg3(x, y, z)                                                          \\
  cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \\
       << z << '\\n';
#define dbg4(a, b, c, d)                                                       \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << '\\n';
#define dbg5(a, b, c, d, e)                                                    \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << '\\n';

const ll mod = 1e9 + 7;
const ll N = 3e3 + 6;
const ll maxN = 1e4 + 5;
const ll MAX_SIZE = 2e6 + 6;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const double PI = 3.14159265359;

ll powerM(ll x, ll y, ll M = mod) { // default argument
  ll v = 1;
  x = x % M;
  while (y > 0) {
    if (y & 1)
      v = (v * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return v;
}

ll power(ll x, ll y) {
  ll v = 1;
  while (y > 0) {
    if (y & 1)
      v = v * x;
    y = y >> 1;
    x = x * x;
  }
  return v;
}

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
// up, right, down, left
const int mx = 3e5 + 10;
ll mid[mx];
ll row[mx];
ll col[mx];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in3.txt", "r", stdin);
  freopen("out2.txt", "w", stdout);
#endif
  FIO ll h, w, m;
  cin >> h >> w >> m;
  ll hi, wi;
  map<pll, ll> mp1;
  FOR(i, m) {
    cin >> hi >> wi;
    pll t = {hi, wi};
    mp1[t]++;
    row[hi]++;
    col[wi]++;
  }
  ll ans = 0;
  ll v1 = 0, v2 = 0;
  ll pr = 0, pc = 0;
  vll pps;
  for (int i = 1; i <= 3e5; ++i) {
    if (row[i] > v1) {
      pps.clear();
      pps.pb(i);
      v1 = row[i];
    } else if (row[i] == v1) {
      pps.pb(i);
    }
  }
  vll ppr;
  for (int i = 1; i <= 3e5; ++i) {
    if (col[i] > v2) {
      ppr.clear();
      ppr.pb(i);
      v2 = col[i];
    } else if (col[i] == v2) {
      ppr.pb(i);
    }
  }
  for (auto j : ppr) {
    for (auto it : pps) {
      pll t = {it, j};
      ans = max(ans, v1 + v2 - (mp1.find(t) == mp1.end() ? 0 : mp1[t]));
    }
  }
  cout << ans;
  return 0;
}