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

#include <bits/stdc++.h>
using namespace std;
#pragma region
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...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << (#x) << "] = [", _print(x)
#else
#define debug(x...)
#endif
#pragma endregion
typedef long long ll;
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define sz(v) int((v).size())
// #define int ll
#define f first
#define s second
#define mp make_pair
#define mt make_tuple
#define pb push_back

int r, c, m;

int r_cnt[333'333];
int c_cnt[333'333];
set<pair<int, int>> st;
void solve() {
  cin >> r >> c >> m;

  forn(i, m) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    r_cnt[x]++;
    c_cnt[y]++;
    st.insert(mp(x, y));
  }
  int rmx = *max_element(r_cnt, r_cnt + r);
  int cmx = *max_element(c_cnt, c_cnt + c);
  vector<int> rdx, cdx;
  forn(i, r) if (r_cnt[i] == rmx) rdx.pb(i);
  forn(i, c) if (c_cnt[i] == cmx) cdx.pb(i);
  forn(i, sz(rdx)) forn(j, sz(cdx)) {
    if (st.count(mp(rdx[i], cdx[j])) == 0) {
      cout << (rmx + cmx) << '\\n';
      return;
    }
  }
  cout << rmx + cmx - 1 << '\\n';
}

signed main(void) {
  int T = 1;
  // cout << fixed;
  // cout.precision(44);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cin >> T;
  while (T--)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma region
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...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << (#x) << "] = [", _print(x)
#else
#define debug(x...)
#endif
#pragma endregion
typedef long long ll;
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define sz(v) int((v).size())
// #define int ll
#define f first
#define s second
#define mp make_pair
#define mt make_tuple
#define pb push_back

int r, c, m;

vector<pair<int, int>> v;
int r_cnt[333'333];
int c_cnt[333'333];
void solve() {
  cin >> r >> c >> m;

  v.resize(m);
  forn(i, m) {
    cin >> v[i].f >> v[i].s;
    --v[i].f;
    --v[i].s;
    r_cnt[v[i].f]++;
    c_cnt[v[i].s]++;
  }
  sort(v.begin(), v.end());
  int ans = 0;
  forn(i, r) forn(j, c) {
    int die = r_cnt[i] + c_cnt[j];

    int lf = 0, rg = m - 1;
    bool fd = false;
    pair<int, int> tar = mp(i, j);
    while (lf <= rg) {
      int mid = (lf + rg) / 2;
      if (v[mid] == tar) {
        fd = true;
        break;
      } else if (v[mid] > tar) {
        rg = mid - 1;
      } else
        lf = mid + 1;
    }
    die -= fd;
    ans = max(ans, die);
  }
  cout << ans << '\\n';
}

signed main(void) {
  int T = 1;
  // cout << fixed;
  // cout.precision(44);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cin >> T;
  while (T--)
    solve();
  return 0;
}