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

// Vivek Rai
// Blazer_007

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#include <bits/stdc++.h>

#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define int long long
#define pb push_back
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll, ll>
#define mii map<pii, ll>
#define all(a) (a).begin(), (a).end()
#define FF first
#define SS second
#define sz(x) (int)x.size()
#define endl '\\n'
#define hell 1000000007
#define loop(i, a, b) for (int i = a; i < b; i++)

using namespace std;

void solution() {
  int h, w, m;
  cin >> h >> w >> m;
  mi row, col;
  mii isPre;
  loop(i, 0, m) {
    int r, c;
    cin >> r >> c;
    isPre[{r, c}] = 1;
    row[r]++;
    col[c]++;
  }
  vii coll, roww;
  int rowsum = 0, colsum = 0;
  for (auto ele : row) {
    // cout << ele.SS << " " << ele.FF << endl;
    roww.pb({ele.SS, ele.FF});
    rowsum += ele.SS;
  }
  for (auto ele : col) {
    // cout << ele.SS << " " << ele.FF << endl;
    coll.pb({ele.SS, ele.FF});
    colsum += ele.SS;
  }
  if (rowsum == colsum and rowsum == (h * w)) {
    cout << h + w - 1 << endl;
    return;
  }
  sort(all(roww), greater<pii>());
  sort(all(coll), greater<pii>());

  int ans = roww[0].FF + coll[0].FF;

  if (roww[0].FF == w and coll[0].FF == h) {
    cout << ans - 1 << endl;
    return;
  }

  if (isPre.find({roww[0].SS, coll[0].SS}) != isPre.end())
    ans -= 1;
  else {
    cout << ans << endl;
    return;
  }
  loop(i, 0, sz(roww)) {
    int curans = roww[i].FF + coll[0].FF;
    if (isPre.find({roww[i].SS, coll[0].SS}) != isPre.end())
      curans -= 1;
    loop(j, 1, sz(coll)) {
      if (coll[j].FF == coll[j - 1].FF) {
        int curr = roww[i].FF + coll[j].FF;
        if (isPre.find({roww[i].SS, coll[j].SS}) != isPre.end())
          curr -= 1;
        curans = max(curans, curr);
      } else
        break;
    }
    ans = max(ans, curans);
    if (i != sz(roww) - 1) {
      if (roww[i].FF == roww[i + 1].FF)
        continue;
      else
        break;
    }
  }
  // cout << roww[0].SS << " " << coll[0].SS << endl;
  // if (isPre.find({roww[0].SS, coll[0].SS}) != isPre.end())
  //     ans -= 1;
  cout << ans << endl;
}

signed main() {
  fastio ll t = 1;
  // cin >> t;
  loop(i, 1, t + 1) {
    // cout<<"Case #"<<i<<": ";
    solution();
  }
}


Error: TLE

Fixed submission:

// Vivek Rai
// Blazer_007

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

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

#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define int long long
#define pb push_back
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll, ll>
#define mii map<pii, ll>
#define ppi pair<ll, pii>
#define all(a) (a).begin(), (a).end()
#define FF first
#define SS second
#define sz(x) (int)x.size()
#define endl '\\n'
#define hell 1000000007
#define bit(x, y) (((x) >> (y)) & 1)
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rloop(i, a, b) for (int i = a; i >= b; i--)

using namespace std;
using namespace __gnu_pbds;

typedef priority_queue<int> maxHeap;
typedef priority_queue<int, vector<int>, greater<int>> minHeap;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// find_by_order(index) -> value at given index or Kth smallest element in set
// order_of_key(X) -> number of elements strictly less than X
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    OST;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
  // use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

istream &operator>>(istream &in, vector<int> &arr) {
  for (int i = 0; i < arr.size(); i++)
    cin >> arr[i];
  return in;
}

ostream &operator<<(ostream &os, vector<int> arr) {
  for (int i = 0; i < arr.size(); i++)
    cout << arr[i] << " ";
  cout << endl;
  return os;
}

void solution() {
  int h, w, m;
  cin >> h >> w >> m;
  mi row, col;
  mii isPre;
  loop(i, 0, m) {
    int r, c;
    cin >> r >> c;
    isPre[{r, c}] = 1;
    row[r]++;
    col[c]++;
  }
  vii coll, roww;
  int rowsum = 0, colsum = 0;
  for (auto ele : row) {
    // cout << ele.SS << " " << ele.FF << endl;
    roww.pb({ele.SS, ele.FF});
    rowsum += ele.SS;
  }
  for (auto ele : col) {
    // cout << ele.SS << " " << ele.FF << endl;
    coll.pb({ele.SS, ele.FF});
    colsum += ele.SS;
  }
  if (rowsum == colsum and rowsum == (h * w)) {
    cout << h + w - 1 << endl;
    return;
  }
  sort(all(roww), greater<pii>());
  sort(all(coll), greater<pii>());
  if (roww[0].FF == roww[sz(roww) - 1].FF and
      coll[0].FF == coll[sz(coll) - 1].FF) {
  }
  int ans = roww[0].FF + coll[0].FF;
  if (isPre.find({roww[0].SS, coll[0].SS}) != isPre.end())
    ans -= 1;
  loop(i, 0, sz(roww)) {
    int curans = roww[i].FF + coll[0].FF;
    if (isPre.find({roww[i].SS, coll[0].SS}) != isPre.end())
      curans -= 1;
    loop(j, 1, sz(coll)) {
      if (coll[j].FF == coll[j - 1].FF) {
        int curr = roww[i].FF + coll[j].FF;
        if (isPre.find({roww[i].SS, coll[j].SS}) != isPre.end())
          curr -= 1;
        curans = max(curans, curr);
      } else
        break;
    }
    ans = max(ans, curans);
    if (i != sz(roww) - 1) {
      if (roww[i].FF == roww[i + 1].FF)
        continue;
      else
        break;
    }
  }
  // cout << roww[0].SS << " " << coll[0].SS << endl;
  // if (isPre.find({roww[0].SS, coll[0].SS}) != isPre.end())
  //     ans -= 1;
  cout << ans << endl;
}

signed main() {
  fastio

      ll t = 1;
  // cin >> t;
  loop(i, 1, t + 1) {
    // cout<<"Case #"<<i<<": ";
    solution();
  }
}