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

// Littleboy123 Template 1.7
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

// pragmas

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")t
// #pragma GCC optimization ("unroll-loops")

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef unsigned long long int ull;
typedef long double ld;
typedef unsigned long long ull;

const long double pi = acos(-1.0);

#define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
#define FORE(a, b, c) for (ll(a) = (b); (a) <= (c); ++(a))
#define FORN(a, b, c) for (ll(a) = (b); (a) > (c); --(a))
#define FORNE(a, b, c) for (ll(a) = (b); (a) >= (c); --(a))
#define TC(n) cout << "Case #" << (n) << ": "
#define DEBUG(x) cout << "DEBUG " << #x << ' ' << x << '\\n';
#define MEM(a, b) memset((a), (b), sizeof((a)))
#define MAX(a, b) (a) = max((a), (b))
#define MIN(a, b) (a) = min((a), (b))
#define nl '\\n'
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ordered_set                                                            \\
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \\
       tree_order_statistics_node_update>

void init() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

void infile() { freopen("fenwick.out", "r", stdin); }

void outfile() { freopen("fenwick.out", "w", stdout); }

ll modex(ll a, ll b, ll mod) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll tmp = modex(a, b / 2LL, mod);
  tmp = (tmp * tmp) % mod;
  if (b & 1LL)
    tmp = (tmp * a) % mod;
  return tmp;
}

// End of template

ll col[300005];
ll row[300005];
ii arr[300005];
ll samerow[300005], samecol[300005];
vi c, r;

int main() {
  init();

  FOR(i, 0, 300005) {
    row[i] = col[i] = 0;
    samerow[i] = samecol[i] = 0;
  }
  ll h, w, m, u, v;
  cin >> h >> w >> m;
  FOR(i, 0, m) {
    cin >> u >> v;
    col[u - 1]++;
    row[v - 1]++;
    arr[i] = mp(u - 1, v - 1);
  }
  ll maksc = -1;
  FOR(i, 0, 300005) { maksc = max(maksc, col[i]); }
  FOR(i, 0, 300005) {
    if (col[i] == maksc) {
      c.pb(i);
    }
  }
  ll maksr = -1;
  FOR(i, 0, 300005) { maksr = max(maksr, row[i]); }
  FOR(i, 0, 300005) {
    if (maksr == row[i]) {
      r.pb(i);
    }
  }
  FOR(i, 0, m) {
    if (row[arr[i].se] == maksr) {
      samecol[arr[i].fi]++;
    }
    if (col[arr[i].fi] == maksc) {
      samerow[arr[i].se]++;
    }
  }
  int sz = r.size();
  int szc = c.size();
  ll maks, tmp;
  maks = 0;
  FOR(i, 0, sz) {
    if (samerow[r[i]] == szc) {
      maks = max(maks, maksr + maksc - 1);
    } else {
      maks = max(maks, maksr + maksc);
      break;
    }
  }
  FOR(j, 0, szc) {
    if (samecol[c[j]] == sz) {
      maks = max(maks, maksr + maksc - 1);
    } else {
      maks = max(maks, maksr + maksc);
      break;
    }
  }
  cout << maks << endl;
  return 0;
}


Error: TLE

Fixed submission:

// Littleboy123 Template 1.7
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

// pragmas

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")t
// #pragma GCC optimization ("unroll-loops")

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef unsigned long long int ull;
typedef long double ld;
typedef unsigned long long ull;

const long double pi = acos(-1.0);

#define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
#define FORE(a, b, c) for (ll(a) = (b); (a) <= (c); ++(a))
#define FORN(a, b, c) for (ll(a) = (b); (a) > (c); --(a))
#define FORNE(a, b, c) for (ll(a) = (b); (a) >= (c); --(a))
#define TC(n) cout << "Case #" << (n) << ": "
#define DEBUG(x) cout << "DEBUG " << #x << ' ' << x << '\\n';
#define MEM(a, b) memset((a), (b), sizeof((a)))
#define MAX(a, b) (a) = max((a), (b))
#define MIN(a, b) (a) = min((a), (b))
#define nl '\\n'
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ordered_set                                                            \\
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \\
       tree_order_statistics_node_update>

void init() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

void infile() { freopen("fenwick.out", "r", stdin); }

void outfile() { freopen("fenwick.out", "w", stdout); }

ll modex(ll a, ll b, ll mod) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll tmp = modex(a, b / 2LL, mod);
  tmp = (tmp * tmp) % mod;
  if (b & 1LL)
    tmp = (tmp * a) % mod;
  return tmp;
}

// End of template

ll col[300005];
ll row[300005];
map<ii, int> ada;
vi c, r;

int main() {
  init();

  FOR(i, 0, 300005) { row[i] = col[i] = 0; }
  ll h, w, m, u, v;
  cin >> h >> w >> m;
  FOR(i, 0, m) {
    cin >> u >> v;
    col[u - 1]++;
    row[v - 1]++;
    ada[mp(u - 1, v - 1)] = 1;
  }
  ll maks = -1;
  FOR(i, 0, 300005) { maks = max(maks, col[i]); }
  FOR(i, 0, 300005) {
    if (col[i] == maks) {
      c.pb(i);
    }
  }
  maks = -1;
  FOR(i, 0, 300005) { maks = max(maks, row[i]); }
  FOR(i, 0, 300005) {
    if (maks == row[i]) {
      r.pb(i);
    }
  }
  int szr, szc;
  szr = r.size();
  szc = c.size();
  maks = 0;
  ll tmp;
  FOR(i, 0, szr) {
    FOR(j, 0, szc) {
      tmp = row[r[i]] + col[c[j]];
      if (ada[mp(c[j], r[i])] == 1) {
        tmp--;
      }
      maks = max(maks, tmp);
    }
  }
  cout << maks << endl;
  return 0;
}