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

#include <bits/stdc++.h>
using namespace std;

#define MOD 1000000007
#define ll long long int
#define pll pair<ll, ll>
#define pb push_back
#define endl "\\n"
#define f first
#define s second
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

ll power(ll x, ll y) {
  ll ans = 1;
  x %= MOD;
  while (y) {
    if (y & 1)
      ans = (x * ans) % MOD;
    x = (x * x) % MOD;
    y >>= 1;
  }
  return ans;
}

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)

bool comp(const pll &p1, const pll &p2) {
  if (p1.f == p2.f) {
    return p2.s > p1.s;
  }
  return p2.f > p1.f;
}

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  ll row[h + 1];
  ll col[w + 1];
  memset(row, 0, sizeof(row));
  memset(col, 0, sizeof(col));

  map<pll, bool> mp;
  int k = 0;

  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    row[x]++;
    col[y]++;
    mp[{x, y}] = 1;
  }

  priority_queue<pll> pq_row, pq_col;

  for (int i = 1; i <= h; i++) {
    pq_row.push({row[i], i});
  }
  for (int i = 1; i <= w; i++) {
    pq_col.push({col[i], i});
  }

  ll ans = pq_row.top().f + pq_col.top().f;
  ll rowMax = pq_row.top().f;
  ll colMax = pq_col.top().f;

  vector<ll> xes, yes;
  while (!pq_row.empty() && pq_row.top().f == rowMax) {
    xes.pb(pq_row.top().s);
    pq_row.pop();
  }

  while (!pq_col.empty() && pq_col.top().f == colMax) {
    yes.pb(pq_col.top().s);
    pq_col.pop();
  }

  // debug(xes);
  // debug(yes);

  bool found = false;

  for (int i = 0; i < xes.size(); i++) {
    for (int j = 0; j < yes.size(); j++) {
      if (!mp[{xes[i], yes[j]}]) {
        found = true;
        break;
      }
    }
  }

  if (!found) {
    cout << ans - 1 << endl;
  } else {
    cout << ans << endl;
  }

  // debug(v);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define MOD 1000000007
#define ll long long int
#define pll pair<ll, ll>
#define pb push_back
#define endl "\\n"
#define f first
#define s second
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

ll power(ll x, ll y) {
  ll ans = 1;
  x %= MOD;
  while (y) {
    if (y & 1)
      ans = (x * ans) % MOD;
    x = (x * x) % MOD;
    y >>= 1;
  }
  return ans;
}

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)

bool comp(const pll &p1, const pll &p2) {
  if (p1.f == p2.f) {
    return p2.s > p1.s;
  }
  return p2.f > p1.f;
}

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  ll row[h + 1];
  ll col[w + 1];
  memset(row, 0, sizeof(row));
  memset(col, 0, sizeof(col));

  vector<pll> v(m);
  int k = 0;

  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    row[x]++;
    col[y]++;
    v[k++] = {x, y};
  }

  sort(v.begin(), v.end(), comp);

  // debug(v);

  ll ans = 0;

  k = 0;

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      // debug(row[i], col[j], k, v[k]);
      int is = 0;

      if (k < v.size()) {
        int x = v[k].f;
        int y = v[k].s;

        if (x == i && y == j) {
          is = 1;
          k++;
        } else if (i > x || (i == x && j > y)) {
          k++;
          j--;
          continue;
        }
      }

      // debug(row[i] + col[j] - is);
      ans = max(ans, row[i] + col[j] - is);
    }
  }

  cout << ans << endl;
}