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

#include <bits/stdc++.h>
#define ll long long
#define endl '\\n'
#define pii pair<int, int>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define fio                                                                    \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define vll vector<long long int>
#define vpii vector<pair<int, int>>
#define dsc [](int a, int b) -> bool { return a > b; }
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define MAX 1000000000000000000
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;
#define MOD 1000000007LL
#define all(x) (x).begin(), (x).end()

using namespace std;

ll nxt() {
  ll x;
  cin >> x;
  return x;
}

// ll n,m,currX,currY,destX,destY;
// vector<string> a;
// vector<vector<bool>>visited;

// ll magic;

// void dfs(){

// }

int main() {
#ifndef ONLINE_JUDGE
  fio
#endif
      IOS

          // n = nxt(),m = nxt();
          // currX = nxt(), currY = nxt();
          // destX = nxt(), destY = nxt();
          // a.resize(n+1);
          // visited.resize(n+1, vector<bool>(m+1,0));
          // for(int i = 1; i<=n; i++){
          //     string tmp;
          //     cin>>tmp;
          //     a[i] = " "+tmp;
          // }
          // magic = LLONG_MAX;
          // dfs(currX, currY);

          ll h,
      w, m;
  cin >> h >> w >> m;
  vector<pair<ll, int>> H;
  vector<pair<ll, int>> W;
  for (int i = 0; i <= h; i++) {
    H.pb({0, i});
  }

  for (int i = 0; i <= w; i++) {
    W.pb({0, i});
  }

  map<pair<ll, ll>, bool> cell;
  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    H[x].first++;
    W[y].first++;
    cell[{x, y}] = 1;
  }
  sort(all(H));
  sort(all(W));
  ll tmp1 = 0, tmp2 = 0;
  int i = h;
  int j = w;

  while (j > 0) {
    ll tmp;
    if (cell[{H[i].second, W[j].second}])
      tmp = H[i].first + W[j].first - 1;
    else
      tmp = H[i].first + W[j].first;
    if (tmp > tmp1)
      tmp1 = tmp;
    j--;
  }

  i = h, j = w;
  while (i > 0) {
    ll tmp;
    if (cell[{H[i].second, W[j].second}])
      tmp = H[i].first + W[j].first - 1;
    else
      tmp = H[i].first + W[j].first;
    if (tmp > tmp2)
      tmp2 = tmp;
    i--;
  }

  cout << max(tmp1, tmp2) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define endl '\\n'
#define pii pair<int, int>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define fio                                                                    \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define vll vector<long long int>
#define vpii vector<pair<int, int>>
#define dsc [](int a, int b) -> bool { return a > b; }
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define MAX 1000000000000000000
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;
#define MOD 1000000007LL
#define all(x) (x).begin(), (x).end()

using namespace std;

ll nxt() {
  ll x;
  cin >> x;
  return x;
}

// ll n,m,currX,currY,destX,destY;
// vector<string> a;
// vector<vector<bool>>visited;

// ll magic;

// void dfs(){

// }

int main() {
#ifndef ONLINE_JUDGE
  fio
#endif
      IOS

          // n = nxt(),m = nxt();
          // currX = nxt(), currY = nxt();
          // destX = nxt(), destY = nxt();
          // a.resize(n+1);
          // visited.resize(n+1, vector<bool>(m+1,0));
          // for(int i = 1; i<=n; i++){
          //     string tmp;
          //     cin>>tmp;
          //     a[i] = " "+tmp;
          // }
          // magic = LLONG_MAX;
          // dfs(currX, currY);

          ll h,
      w, m;
  cin >> h >> w >> m;
  vector<ll> H(h + 1);
  vector<ll> W(w + 1);
  map<pair<ll, ll>, bool> cell;
  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    H[x]++;
    W[y]++;
    cell[{x, y}] = 1;
  }
  ll ans = 0;
  for (ll i = 1; i <= h; i++) {
    for (ll j = 1; j <= w; j++) {
      if (cell[{i, j}]) {
        ans = max(ans, H[i] + W[j] - 1);
      } else
        ans = max(ans, H[i] + W[j]);
    }
  }

  cout << ans << endl;

  return 0;
}