alexjercan/bugnet huggingface dataset:
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define chmax(p, q) (p = max(p, q))
int H, W, M;
int main() {
  cin >> H >> W >> M;
  int mh = 0, mw = 0;
  map<int, int> cnt_H, cnt_W;

  set<pair<int, int>> P;
  vector<int> max_h, max_w;
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    chmax(mh, ++cnt_H[--h]);
    chmax(mw, ++cnt_W[--w]);
    P.emplace(make_pair(h, w));
  }
  for (auto x : P) {
    int h = x.first, w = x.second;
    // if(cnt_H[h]==mh)max_h.push_back(h);
    // if(cnt_W[w]==mw)max_w.push_back(w);
  }

  for (auto tmp : cnt_H) {
    if (mh == tmp.second)
      max_h.push_back(tmp.first);
  }
  for (auto tmp : cnt_W) {
    if (mw == tmp.second)
      max_w.push_back(tmp.first);
  }

  for (auto h : max_h) {
    for (auto w : max_w) {
      // cout<<"max:"<<h<<" "<<w<<endl;
      if (P.find(make_pair(h, w)) == P.end()) {
        cout << mh + mw << endl;
        return 0;
      }
    }
  }
  cout << mh + mw - 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define chmax(p, q) (p = max(p, q))
int H, W, M;
int main() {
  cin >> H >> W >> M;
  int mh = 0, mw = 0;
  map<int, int> cnt_H, cnt_W;

  set<pair<int, int>> P;
  vector<int> max_h, max_w;
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    chmax(mh, ++cnt_H[--h]);
    chmax(mw, ++cnt_W[--w]);
    P.emplace(make_pair(h, w));
  }
  for (auto x : P) {
    int h = x.first, w = x.second;
    if (cnt_H[h] == mh)
      max_h.push_back(h);
    if (cnt_W[w] == mw)
      max_w.push_back(w);
  }
  for (auto h : max_h) {
    for (auto w : max_w) {
      // cout<<"max:"<<h<<" "<<w<<endl;
      if (P.find(make_pair(h, w)) == P.end()) {
        cout << mh + mw << endl;
        return 0;
      }
    }
  }
  cout << mh + mw - 1 << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

struct phash {
  inline size_t operator()(const pair<int, int> &p) const {
    const auto h1 = hash<int>()(p.first);
    const auto h2 = hash<int>()(p.second);
    return h1 ^ (h2 << 1);
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int H, W, M;
  cin >> H >> W >> M;
  unordered_set<pair<int, int>, phash> tg;
  vector<int> sumr(H, 0), sumc(W, 0);
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    tg.insert(make_pair(h - 1, w - 1));
    sumr[h - 1]++;
    sumc[w - 1]++;
  }

  unordered_set<int> maxi, maxj;
  int maxsumr = 0, maxsumc = 0;
  for (int i = 0; i < H; i++) {
    if (sumr[i] > maxsumr) {
      maxi.clear();
      maxsumr = sumr[i];
      maxi.insert(i);
    } else if (sumr[i] == maxsumr) {
      maxi.insert(i);
    }
  }
  for (int j = 0; j < W; j++) {
    if (sumc[j] > maxsumc) {
      maxj.clear();
      maxsumc = sumc[j];
      maxj.insert(j);
    } else if (sumc[j] == maxsumc) {
      maxj.insert(j);
    }
  }
  bool n = true;
  for (auto i : maxi) {
    bool f = false;
    for (auto j : maxj) {
      if (!tg.count(make_pair(i, j))) {
        n = false;
        f = true;
        break;
      }
    }
    if (f) {
      break;
    }
  }
  cout << maxsumr + maxsumc - n << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

struct phash {
  inline size_t operator()(const pair<int, int> &p) const {
    const auto h1 = hash<int>()(p.first);
    const auto h2 = hash<int>()(p.second);
    return h1 ^ (h2 << 1);
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int H, W, M;
  cin >> H >> W >> M;
  unordered_set<pair<int, int>, phash> tg;
  vector<int> sumr(H, 0), sumc(W, 0);
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    tg.insert(make_pair(h - 1, w - 1));
    sumr[h - 1]++;
    sumc[w - 1]++;
  }

  int max = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int a;
      if ((a = sumr[i] + sumc[j] - tg.count(make_pair(i, j))) > max) {
        max = a;
      }
    }
  }
  cout << max << "\\n";
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int fc(vector<int> &hc, vector<int> &ph) {
  int mx = 0;
  rep(i, hc.size()) mx = max(mx, hc.at(i));
  rep(i, hc.size()) {
    if (hc.at(i) == mx)
      ph.push_back(i);
  }
  return mx;
}

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  set<pair<int, int>> se;
  vector<int> hc(h, 0), wc(w, 0);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    hc.at(a)++;
    wc.at(b)++;
    se.insert({a, b});
  }
  vector<int> ph, pw;
  int mh = fc(hc, ph);
  int mw = fc(wc, pw);
  int ans = mh + mw - 1;
  for (auto i : ph) {
    for (auto j : pw) {
      if (se.find({i, j}) == se.end()) {
        ans++;
        goto SKIP;
      }
    }
  }
SKIP:
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int fc(vector<int> &hc, vector<int> &ph) {
  int mx = 0;
  rep(i, hc.size()) mx = max(mx, hc.at(i));
  rep(i, hc.size()) {
    if (hc.at(i) == mx)
      ph.push_back(i);
  }
  return mx;
}

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  set<pair<int, int>> se;
  vector<int> hc(h, 0), wc(w, 0);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    hc.at(a)++;
    wc.at(b)++;
    se.insert({a, b});
  }
  vector<pair<int, int>> pwc(w);
  rep(i, w) pwc.at(i) = {wc.at(i), i};
  sort(pwc.rbegin(), pwc.rend());
  int ans = 0;
  rep(i, h) {
    int bf = 0, index = 0;
    while (1) {
      if (index == pwc.size())
        break;
      int add = hc.at(i);
      add += pwc.at(index).first;
      if (se.find({i, pwc.at(index).second}) != se.end())
        add--;
      if (add <= bf)
        break;
      ans = max(ans, add);
      index++;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
using ll = long long;

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  vector<ll> h(M), w(M);
  vector<pair<ll, ll>> row(H + 1), col(W + 1);
  vector<ll> r(H + 1, 0), c(W + 1, 0);
  for (ll i = 0; i < M; i++) {
    cin >> h.at(i) >> w.at(i);
    r.at(h.at(i))++;
    c.at(w.at(i))++;
  }
  for (ll i = 1; i <= H; i++) {
    row.at(i) = make_pair(r.at(i), i);
  }
  for (ll i = 1; i <= W; i++) {
    col.at(i) = make_pair(c.at(i), i);
  }
  sort(row.begin(), row.end());
  sort(col.begin(), col.end());
  ll maxrow = row.at(H).first;
  vector<ll> mr;
  for (ll i = H; i > 0; i--) {
    if (row.at(i).first == maxrow)
      mr.push_back(row.at(i).second);
    else
      break;
  }
  ll maxcol = col.at(W).first;
  vector<ll> mc;
  for (ll i = W; i > 0; i--) {
    if (col.at(i).first == maxcol)
      mc.push_back(col.at(i).second);
    else
      break;
  }
  ll minus = 1;
  ll plus = 0;
  ll a = mr.size();
  ll b = mc.size();
  for (ll i = 0; i < M; i++) {
    if (r.at(h.at(i)) == maxrow && c.at(w.at(i)) == maxcol) {
      plus += 1;
    }
  }
  if (plus < a * b)
    minus = 0;
  cout << maxrow + maxcol - minus << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
using ll = long long;

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  vector<ll> h(M), w(M);
  vector<pair<ll, ll>> row(H + 1), col(W + 1);
  vector<ll> r(H + 1, 0), c(W + 1, 0);
  vector<vector<ll>> map(H + 1, vector<ll>(W + 1, 0));
  for (ll i = 0; i < M; i++) {
    cin >> h.at(i) >> w.at(i);
    r.at(h.at(i))++;
    c.at(w.at(i))++;
    map.at(h.at(i)).at(w.at(i))++;
  }
  for (ll i = 1; i <= H; i++) {
    row.at(i) = make_pair(r.at(i), i);
  }
  for (ll i = 1; i <= W; i++) {
    col.at(i) = make_pair(c.at(i), i);
  }
  sort(row.begin(), row.end());
  sort(col.begin(), col.end());
  ll maxrow = row.at(H).first;
  vector<ll> mr;
  for (ll i = H; i > 0; i--) {
    if (row.at(i).first == maxrow)
      mr.push_back(row.at(i).second);
    else
      break;
  }
  ll maxcol = col.at(W).first;
  vector<ll> mc;
  for (ll i = W; i > 0; i--) {
    if (col.at(i).first == maxcol)
      mc.push_back(col.at(i).second);
    else
      break;
  }
  ll minus = 1;
  ll a = mr.size();
  ll b = mc.size();
  for (ll i = 0; i < a; i++) {
    for (ll j = 0; j < b; j++) {
      ll mmm = 0;
      if (map.at(mr.at(i)).at(mc.at(j)) == 1)
        mmm = 1;
      if (mmm == 0)
        minus = 0;
    }
  }
  cout << maxrow + maxcol - minus << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// __uint128_t
const ll MOD = 1e9 + 7;

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  map<ll, ll> MH, MW;
  vector<pair<ll, ll>> V(M);
  for (int i = 0; i < M; i++) {
    ll h, w;
    cin >> h >> w;
    MH[h]++;
    MW[w]++;
    V[i] = {h, w};
  }
  ll ans = 0;
  ll MAX1 = 0, MAX2 = 0;
  set<ll> VH, VW;
  for (auto p : MH)
    MAX1 = max(MAX1, p.second);
  for (auto p : MH)
    if (p.second == MAX1)
      VH.insert(p.first);

  for (auto p : MW)
    MAX2 = max(MAX2, p.second);
  for (auto p : MW)
    if (p.second == MAX2)
      VW.insert(p.first);

  ll cnt = 0, SIZE = VH.size() * VW.size();
  for (auto p : V) {
    ll h = p.first, w = p.second;
    if (VH.find(h) != VH.end() && VW.find(w) != VW.end())
      cnt++;
  }

  if (cnt == SIZE)
    cout << MAX1 + MAX2 - 1 << endl;
  else
    cout << MAX1 + MAX2 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// __uint128_t
const ll MOD = 1e9 + 7;

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  map<ll, ll> MH, MW;
  vector<pair<ll, ll>> V(H + 1);
  for (int i = 0; i < M; i++) {
    ll h, w;
    cin >> h >> w;
    MH[h]++;
    MW[w]++;
    V[i] = {h, w};
  }
  ll ans = 0;
  ll MAX1 = 0, MAX2 = 0;
  set<ll> VH, VW;
  for (auto p : MH)
    MAX1 = max(MAX1, p.second);
  for (auto p : MH)
    if (p.second == MAX1)
      VH.insert(p.first);

  for (auto p : MW)
    MAX2 = max(MAX2, p.second);
  for (auto p : MW)
    if (p.second == MAX2)
      VW.insert(p.first);

  ll cnt = 0, SIZE = VH.size() * VW.size();
  for (auto p : V) {
    ll h = p.first, w = p.second;
    if (VH.find(h) != VH.end() && VW.find(w) != VW.end())
      cnt++;
  }

  if (cnt == SIZE)
    cout << MAX1 + MAX2 - 1 << endl;
  else
    cout << MAX1 + MAX2 << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void solve() {
  ll H, W, M;
  cin >> H >> W >> M;
  map<ll, ll> row, col;
  vector<P> items(M);
  set<P> used;
  rep(i, M) cin >> items[i].first >> items[i].second;
  rep(i, M) {
    P p = items[i];
    row[p.first]++;
    col[p.second]++;
    used.insert(p);
  }
  ll m = 0;
  rep2(i, 1, W + 1) { m = max(m, col[i]); }
  vector<ll> tar;
  rep2(i, 1, W + 1) {
    if (col[i] == m) {
      tar.push_back(i);
    }
  }
  ll ans = 0;
  rep2(i, 1, H + 1) {
    if (row[i] + m - 1 < ans)
      continue;
    for (ll t : tar) {
      ll x = row[i];
      x += m;
      if (used.count({i, t})) {
        x--;
      }
      ans = max(ans, x);
    }
  }
  cout << ans << '\\n';
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void solve() {
  ll H, W, M;
  cin >> H >> W >> M;
  map<ll, ll> row, col;
  vector<P> items(M);
  set<P> used;
  rep(i, M) cin >> items[i].first >> items[i].second;
  rep(i, M) {
    P p = items[i];
    row[p.first]++;
    col[p.second]++;
    used.insert(p);
  }
  ll m = 0;
  rep2(i, 1, W + 1) { m = max(m, col[i]); }
  vector<ll> tar;
  rep2(i, 1, W + 1) {
    if (col[i] == m) {
      tar.push_back(i);
    }
  }
  ll ans = 0;
  rep2(i, 1, H + 1) {
    for (ll t : tar) {
      ll x = row[i];
      x += m;
      if (used.count({i, t})) {
        x--;
      }
      ans = max(ans, x);
    }
  }
  cout << ans << '\\n';
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
bool vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size())
    return true; // 発見できた時
  else
    return false; // 発見できなかった時
}
struct edge {
  ll to, cost;
};

int main() {
  int H, W;
  cin >> H >> W;
  int M;
  cin >> M;
  vector<int> h(M), w(M);
  REP(i, M) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
  }
  vector<ll> x(H, 0), y(W, 0);
  REP(i, M) {
    x[h[i]]++;
    y[w[i]]++;
  }
  ll ans = 0;
  ll max_x = *max_element(all(x));
  ll max_y = *max_element(all(y));
  vector<ll> p, q;
  REP(i, H) if (x[i] == max_x) p.push_back(i);
  REP(i, W) if (y[i] == max_y) q.push_back(i);
  ans = max_x + max_y;
  bool flag = false;
  ll cnt = 0;
  REP(i, M) {
    if (binary_search(all(p), h[i]) && binary_search(all(q), w[i])) {
      cnt++;
    }
  }
  if (p.size() * q.size() == cnt)
    cout << ans - 1 << endl;
  else
    cout << ans << endl;
}

Error: TLE

Fixed submission:

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
bool vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size())
    return true; // 発見できた時
  else
    return false; // 発見できなかった時
}
struct edge {
  ll to, cost;
};

int main() {
  int H, W;
  cin >> H >> W;
  int M;
  cin >> M;
  vector<int> h(M), w(M);
  REP(i, M) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
  }
  vector<ll> x(H, 0), y(W, 0);
  REP(i, M) {
    x[h[i]]++;
    y[w[i]]++;
  }
  ll ans = 0;
  ll max_x = *max_element(all(x));
  ll max_y = *max_element(all(y));
  vector<ll> p, q;
  REP(i, H) if (x[i] == max_x) p.push_back(i);
  REP(i, W) if (y[i] == max_y) q.push_back(i);
  ans = max_x + max_y;
  bool flag = false;
  ll cnt = 0;
  REP(i, M) {
    if (vector_finder(p, h[i]) && vector_finder(q, w[i])) {
      cnt++;
    }
  }
  if (p.size() * q.size() == cnt)
    cout << ans - 1 << endl;
  else
    cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ff first
#define ss second
#define int long long
#define all(x) (x).begin(), (x).end()
#define what_is(x) cerr << #x << " is " << x << endl;
#define eb emplace_back
#define pii pair<int, int>

using namespace std;

const int mxn = 300005;
vector<int> fx(mxn, 0), fy(mxn, 0);
void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  set<pii> s;
  int x, y;
  while (m--) {
    cin >> x >> y;
    fx[x]++, fy[y]++;
    s.insert({x, y});
  }
  int mx = *max_element(fx.begin(), fx.end()),
      my = *max_element(fy.begin(), fy.end());
  vector<int> xx, yy;
  for (int i = 1; i <= h; i++) {
    if (fx[i] == mx) {
      xx.eb(i);
    }
  }
  for (int i = 1; i <= w; i++) {
    if (fy[i] == my) {
      yy.eb(i);
    }
  }
  int res = 0;
  for (auto &i : xx) {
    for (auto &j : yy) {
      if (res == 0)
        res = max(res, mx + my - (s.count({i, j}) == 1));
      if (s.count({i, j}) == 0) {
        res = max(res, mx + my);
        break;
      }
    }
  }
  cout << res;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  // int t;
  // cin >> t;
  // while (t--)
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ff first
#define ss second
#define int long long
#define all(x) (x).begin(), (x).end()
#define what_is(x) cerr << #x << " is " << x << endl;
#define eb emplace_back
#define pii pair<int, int>

using namespace std;

const int mxn = 300005;
vector<int> fx(mxn, 0), fy(mxn, 0);
void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  set<pii> s;
  int x, y;
  while (m--) {
    cin >> x >> y;
    fx[x]++, fy[y]++;
    s.insert({x, y});
  }
  int mx = *max_element(fx.begin(), fx.end()),
      my = *max_element(fy.begin(), fy.end());
  vector<int> xx, yy;
  for (int i = 1; i <= h; i++) {
    if (fx[i] == mx) {
      xx.eb(i);
    }
  }
  for (int i = 1; i <= w; i++) {
    if (fy[i] == my) {
      yy.eb(i);
    }
  }
  int res = 0;
  for (auto &i : xx) {
    for (auto &j : yy) {
      res = max(res, mx + my - (s.count({i, j}) == 1));
    }
  }
  cout << res;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  // int t;
  // cin >> t;
  // while (t--)
  solve();
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m, k, r, c, maxic, maxir;
  cin >> n >> m >> k;
  vector<int> row(300001, 0);
  vector<int> col(300001, 0);
  map<pair<int, int>, int> chk;
  for (int i = 0; i < k; i++) {
    cin >> r >> c;
    row[r - 1]++;
    col[c - 1]++;
    chk[{r - 1, c - 1}] = 1;
  }
  int ans = 0;
  int maxi = -1;

  r = 0;
  c = 0;
  maxic = -1;
  maxir = -1;
  for (int i = 0; i < max(m, n); i++) {
    if (row[i] > maxir) {
      maxir = row[i];
      r = i;
    } else if (row[i] == maxir && chk[{i, c}] == 0) {
      maxir = row[i];
      r = i;
    }

    if (col[i] > maxic) {
      maxic = col[i];
      c = i;
    } else if (col[i] == maxic && chk[{r, i}] == 0) {
      maxic = col[i];
      c = i;
    }
  }

  if (chk[{r, c}] == 1)
    ans = maxir + maxic - 1;
  else
    ans = maxir + maxic;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int rec(vector<pair<int, int>> &row, vector<pair<int, int>> &col, int i, int j,
        map<pair<int, int>, int> &chk, int prev) {
  if (i == row.size() || j == col.size())
    return 0;
  int curr;
  if (chk[{row[i].second, col[j].second}] == 1)
    curr = row[i].first + col[j].first - 1;
  else
    curr = row[i].first + col[j].first;
  if (prev > curr)
    return prev;
  if (chk[{row[i].second, col[j].second}] == 0)
    return curr;

  int l = rec(row, col, i + 1, j, chk, curr);
  int r = rec(row, col, i, j + 1, chk, max(curr, l));
  return max(l, r);
}

int main() {
  int n, m, k, r, c;
  cin >> n >> m >> k;
  vector<pair<int, int>> row(300001);
  vector<pair<int, int>> col(300001);
  for (int i = 0; i < 300001; i++) {
    row[i] = {0, 0};
    col[i] = {0, 0};
  }
  map<pair<int, int>, int> chk;
  for (int i = 0; i < k; i++) {
    cin >> r >> c;
    row[r - 1].first++;
    row[r - 1].second = r - 1;
    col[c - 1].first++;
    col[c - 1].second = c - 1;
    chk[{r - 1, c - 1}] = 1;
  }
  int ans = 0;
  sort(row.begin(), row.end());
  reverse(row.begin(), row.end());
  sort(col.begin(), col.end());
  reverse(col.begin(), col.end());
  ans = rec(row, col, 0, 0, chk, 0);
  cout << ans << endl;
  return 0;
}