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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

/*
 *    coder :: ATUL_PANDEY_2608
 *        >>> INDIA <<<
 */
using namespace std;
using namespace __gnu_pbds;

// #define  part ..
#define pb(a) push_back(a)
#define all(a) a.begin(), a.end()
#define mod 1000000007
// #define maxx 200006
#define ll long long
#define quick ios_base::sync_with_stdio(NULL), cin.tie(0);
#define listll vector<long long>
#define listi vector<int>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define minheap priority_queue<long long, vector<long long>, greater<long long>>
#define rep(i, a, b) for (int i = a; i < b; i++)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// end of #define

// define globals ...
const int maxx = 2e5 + 26;

// write function from here ...

int Main() {

  ll x, y, n;
  cin >> x >> y >> n;

  ll cntx[x + 20];
  ll cnty[y + 20];
  fill(cnty, cnty + y + 20, 0LL);

  fill(cntx, cntx + x + 20, 0LL);
  ll arx[n + 3], ary[n + 3];

  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    arx[i] = a;
    ary[i] = b;
    cntx[a]++;
    cnty[b]++;
  }

  ll xx = 0;
  for (int i = 0; i < x + 15; i++) {
    if (xx < cntx[i]) {
      xx = cntx[i];
    }
  }

  ll yy = 0;
  for (int i = 0; i < y + 15; i++) {
    if (yy < cnty[i]) {
      yy = cnty[i];
    }
  }

  ll gx[x + 26], gy[y + 26];
  fill(gx, gx + x + 26, 0);
  fill(gy, gy + y + 26, 0);

  int xcnt = 0, ycnt = 0;
  for (int i = 0; i < x + 15; i++) {
    if (xx == cntx[i]) {

      gx[i] = 1;
      xcnt++;
    }
  }

  for (int i = 0; i < y + 15; i++) {
    if (yy == cnty[i]) {

      gy[i] = 1;
      ycnt++;
    }
  }

  ll cnt = 0;
  int flag = 0;
  for (int i = 0; i < n; i++) {

    if (gx[arx[i]] && gy[ary[i]])
      cnt++;
  }

  if (cnt == xcnt * ycnt)
    flag = 1;

  cout << xx + yy - flag << endl;
  return 0;
}

int main() {
  quick;
  int t = 1;
  // cin>>t;
  while (t--)
    Main();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

/*
 *    coder :: ATUL_PANDEY_2608
 *        >>> INDIA <<<
 */
using namespace std;
using namespace __gnu_pbds;

// #define  part ..
#define pb(a) push_back(a)
#define all(a) a.begin(), a.end()
#define mod 1000000007
// #define maxx 200006
#define ll long long
#define quick ios_base::sync_with_stdio(NULL), cin.tie(0);
#define listll vector<long long>
#define listi vector<int>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define minheap priority_queue<long long, vector<long long>, greater<long long>>
#define rep(i, a, b) for (int i = a; i < b; i++)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// end of #define

// define globals ...
const int maxx = 2e5 + 26;

// write function from here ...

int Main() {

  ll x, y, n;
  cin >> x >> y >> n;

  vector<set<ll>> adj(y + 4);

  ll cntx[x + 20];
  fill(cntx, cntx + x + 20, 0LL);

  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;

    cntx[a]++;
    adj[b].insert(a);
  }

  ll xx = 0;
  for (int i = 0; i < x + 15; i++) {
    if (xx < cntx[i]) {
      xx = cntx[i];
    }
  }

  vector<ll> xar;

  for (int i = 0; i < x + 15; i++) {
    if (xx == cntx[i]) {
      xar.pb(i);
    }
  }

  ll cntt = 0;

  for (auto itr : xar) {
    ll mx = itr;
    for (int i = 0; i <= y; i++) {

      ll flag = 0;

      auto it = adj[i].find(mx);

      if (it != adj[i].end())
        flag += 1;

      cntt = max(cntt, (ll)adj[i].size() - flag);
    }
  }
  cout << xx + cntt << endl;
  return 0;
}

int main() {
  quick;
  int t = 1;
  // cin>>t;
  while (t--)
    Main();

  return 0;
}