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

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

#define OM                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0)
#define endl '\\n'
#define pi 3.14159265358979323846

typedef long long ll;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

#ifndef ONLINE_JUDGE
string __db__;
vector<string> __var_name__;
int __db_cnt__ = 0;
void __get_var_name__() {
  int sz = 1;
  __var_name__.resize(sz);
  for (char x : __db__) {
    if (x == ' ')
      continue;
    if (x ^ ',')
      __var_name__[sz - 1].push_back(x);
    else {
      ++sz;
      __var_name__.resize(sz);
    }
  }
}
template <typename T> string to_string(const T &);

string to_string(const char x) { return string("'") + x + string("'"); };

string to_string(const string &x) { return '"' + x + '"'; }

string to_string(const char *s) { return to_string(string(s)); }

string to_string(const bool x) { return (x ? "true" : "false"); };

template <typename A, typename B> string to_string(const pair<A, B> &);

template <typename A, typename B> string to_string(const tuple<A, B> &t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ')';
}

template <typename A, typename B, typename C>
string to_string(const tuple<A, B, C> &t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " +
         to_string(get<2>(t)) + ')';
}

template <typename A, typename B> string to_string(const pair<A, B> &p) {
  return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}

template <typename T> string to_string(const T &v) {
  string res;
  res += '{';
  bool first = true;
  for (const auto &x : v) {
    if (!first)
      res += ", ";
    else
      first = false;
    res += to_string(x);
  }
  res += '}';
  return res;
}

void __deb() {
  cout << endl;
  __var_name__.clear();
  __db__.clear();
  __db_cnt__ = 0;
}

template <typename Head, typename... Tail> void __deb(Head H, Tail... T) {
  cout << '[' << __var_name__[__db_cnt__] << " : ";
  cout << to_string(H);
  cout << ']' << ' ';
  ++__db_cnt__;
  __deb(T...);
}

#define debug(...)                                                             \\
  __db__ = (#__VA_ARGS__), __get_var_name__(), __deb(__VA_ARGS__)
#define debugA(a, n)                                                           \\
  cout << '[' << (#a) << " : {";                                               \\
  for (int i = 0; i < n - 1; ++i)                                              \\
    cout << a[i] << ", ";                                                      \\
  cout << a[n - 1] << "}] \\n"
#endif

#define all(x) x.rbegin(), x.rend()

/*LETS BEGIN WITH THE NAME OF MAHADEV*/

int main() {
  OM;
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> row(h + 1);
  vector<pair<int, int>> col(w + 1);
  map<pair<int, int>, bool> mp;
  for (int i = 0, x, y; i < m; ++i) {
    cin >> x >> y;
    ++row[x].first;
    row[x].second = x;
    ++col[y].first;
    col[y].second = y;
    mp[{x, y}] = true;
  }

  sort(row.rbegin(), row.rend());
  sort(col.rbegin(), col.rend());
  int pill = row[0].first;
  int till = col[0].first;
  int answer = 0;
  int brk = row[0].first + col[0].first;
  for (int i = 0; row[i].first == pill; ++i) {
    for (int j = 0; col[j].first == till; ++j) {
      answer = max(answer, row[i].first + col[j].first -
                               (int)mp[{row[i].second, col[j].second}]);
      if (answer == brk)
        break;
    }
    if (answer == brk)
      break;
  }
  cout << answer << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define OM                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0)
#define endl '\\n'
#define pi 3.14159265358979323846

typedef long long ll;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

#ifndef ONLINE_JUDGE
string __db__;
vector<string> __var_name__;
int __db_cnt__ = 0;
void __get_var_name__() {
  int sz = 1;
  __var_name__.resize(sz);
  for (char x : __db__) {
    if (x == ' ')
      continue;
    if (x ^ ',')
      __var_name__[sz - 1].push_back(x);
    else {
      ++sz;
      __var_name__.resize(sz);
    }
  }
}
template <typename T> string to_string(const T &);

string to_string(const char x) { return string("'") + x + string("'"); };

string to_string(const string &x) { return '"' + x + '"'; }

string to_string(const char *s) { return to_string(string(s)); }

string to_string(const bool x) { return (x ? "true" : "false"); };

template <typename A, typename B> string to_string(const pair<A, B> &);

template <typename A, typename B> string to_string(const tuple<A, B> &t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ')';
}

template <typename A, typename B, typename C>
string to_string(const tuple<A, B, C> &t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " +
         to_string(get<2>(t)) + ')';
}

template <typename A, typename B> string to_string(const pair<A, B> &p) {
  return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}

template <typename T> string to_string(const T &v) {
  string res;
  res += '{';
  bool first = true;
  for (const auto &x : v) {
    if (!first)
      res += ", ";
    else
      first = false;
    res += to_string(x);
  }
  res += '}';
  return res;
}

void __deb() {
  cout << endl;
  __var_name__.clear();
  __db__.clear();
  __db_cnt__ = 0;
}

template <typename Head, typename... Tail> void __deb(Head H, Tail... T) {
  cout << '[' << __var_name__[__db_cnt__] << " : ";
  cout << to_string(H);
  cout << ']' << ' ';
  ++__db_cnt__;
  __deb(T...);
}

#define debug(...)                                                             \\
  __db__ = (#__VA_ARGS__), __get_var_name__(), __deb(__VA_ARGS__)
#define debugA(a, n)                                                           \\
  cout << '[' << (#a) << " : {";                                               \\
  for (int i = 0; i < n - 1; ++i)                                              \\
    cout << a[i] << ", ";                                                      \\
  cout << a[n - 1] << "}] \\n"
#endif

#define all(x) x.rbegin(), x.rend()

/*LETS BEGIN WITH THE NAME OF MAHADEV*/

int main() {
  OM;
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> row(h + 1);
  vector<pair<int, int>> col(w + 1);
  map<pair<int, int>, bool> mp;
  for (int i = 0, x, y; i < m; ++i) {
    cin >> x >> y;
    ++row[x].first;
    row[x].second = x;
    ++col[y].first;
    col[y].second = y;
    mp[{x, y}] = true;
  }

  sort(row.rbegin(), row.rend());
  sort(col.rbegin(), col.rend());
  int pill = row[0].first;
  int till = col[0].first;
  int answer = 0;
  for (int i = 0; row[i].first == pill; ++i) {
    for (int j = 0; col[j].first == till; ++j)
      answer = max(answer, row[i].first + col[j].first -
                               (int)mp[{row[i].second, col[j].second}]);
  }
  cout << answer << endl;
  return 0;
}