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

#include <bits/stdc++.h>

using namespace std;

#define debug(x) cout << (#x) << " is " << x << endl;
#define Q                                                                      \\
  int t;                                                                       \\
  scanf("%d", &t);                                                             \\
  for (int q = 1; q <= t; q++)
typedef long long int lli;
typedef pair<int, int> pi;

#define N 300005
int H[N], W[N];

int main() {
  int h, w, m, hmx = 0, wmx = 0;
  cin >> h >> w >> m;
  vector<pi> p;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    H[x]++;
    W[y]++;
    p.push_back({x, y});
    hmx = max(hmx, H[x]);
    wmx = max(wmx, W[y]);
  }
  lli hc = 0, wc = 0;
  for (int i = 1; i <= h; i++)
    hc += (H[i] == hmx);
  for (int i = 1; i <= w; i++)
    wc += (W[i] == wmx);

  lli cc = 0;
  for (auto el : p) {
    if (H[el.first] == hmx && W[el.second] == wmx)
      cc++;
  }
  if (cc == hc * wc) {
    cout << hmx + wmx - 1 << endl;
  } else
    cout << hmx + wmx << endl;

  return 0;
}

Error: TLE

Fixed submission:


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define speed                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define ull unsigned long long
#define db double
#define vi vector<ll>
#define vvi vector<vector<int>>
#define vvii vector<vector<pii>>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define endl "\\n"
#define pii pair<ll, ll>
#define piii pair<ll, pair<ll, ll>>
#define mem(a, b) memset(a, b, sizeof a);
#define pb push_back
#define pf push_front
#define F first
#define S second
#define mp make_pair
#define all(v) v.begin(), v.end()

#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

#define deb1(a) cout << #a << "=*" << a << endl;
#define deb2(a) cout << #a << "=@" << a << endl;
#define deb3(a) cout << #a << "=#" << a << endl;
#define deb4(a, b) cout << #a << "=" << a << " and " << #b << "=" << b << endl;
#define deb5(a, b) cout << #a << "=" << a << " and " << #b << "=" << b << endl;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

ll mod = 1000000007;
ll vis[212345] = {0};
vvi v(212345);
ll d[212345] = {0};

ll pow(ll a, ll b, ll m) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

// extended euclidean algorithm( x is inverse of a wrt b onlt if gcd!=1)
ll gcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll x1, y1;
  ll d = gcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return d;
}

ll r[312345];
ll c[312345];

int main() {
  speed ll h, w, m;
  cin >> h >> w >> m;
  rep(i, 1, h + 1) r[i] = 0;
  rep(i, 1, w + 1) c[i] = 0;

  ll mc = 0;
  ll mr = 0;
  ll p1, p2;
  map<pair<ll, ll>, ll> m1;
  // pii mm[m];
  ll mz = 0;
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    // m[i]={a,b};
    m1[{a, b}]++;
    r[a]++;
    c[b]++;
    //	mz=max(mr+c[b]-m1[{p1,b}],mz);
    //	mz=max(r[a]+mc-m1[{a,p2}],mz);
    //	mz=max(r[a]+c[b]-1,mz);

    if (r[a] > mr) {
      p1 = a;
    }
    if (c[b] > mc)
      p2 = b;
    // mz=max(r[a]+c[b]-1,mz);
    mr = max(mr, r[a]);
    mc = max(mc, c[b]);
    // deb1(mr);
  }
  // mz=max(mz,mr+mc-m1[{p1,p2}]);
  mz = 0;
  priority_queue<pii> pq;
  rep(i, 1, w + 1) {
    if (c[i]) {
      pq.push({c[i], i});
    }
  }
  priority_queue<pii> pq1;

  rep(i, 1, h + 1) { pq1.push({r[i], i}); }
  pii r1 = pq1.top();
  pii c1 = pq.top();
  // pq.pop();
  // pq.pop();
  vector<pii> v1;
  while (!pq.empty() && pq.top().F == c1.F) {
    v1.pb(pq.top());
    pq.pop();
    //	deb1(r1.F);
  }
  vector<pii> v2;
  while (!pq1.empty() && pq1.top().F == r1.F) {
    pii pp = pq1.top();
    v2.pb(pp);
    pq1.pop();
    // deb1(c1.F);
  }
  rep(i, 0, v1.size()) {
    rep(j, 0, v2.size()) {
      pii a = v1[i];
      pii b = v2[j];
      mz = max(mz, a.F + b.F - m1[{b.S, a.S}]);
    }
  }
  cout << mz << endl;
}