alexjercan/bugnet huggingface dataset:
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <cstdio>
using namespace std;

inline int getint() {
  int x = 0, c = getchar();
  while (c > 47 && c < 58) {
    x = x * 10 + c - 48;
    c = getchar();
  }
  return x;
}

constexpr int MAX = 300000;
long bomb[MAX];
int posr[MAX], posc[MAX];

int main() {
  int H = getint();
  int W = getint();
  int M = getint();

  long h, w, ans, x, y;
  int mxr = 0;
  int mxc = 0;
  int ctrow = 0;
  int ctcol = 0;
  for (int i = 0; i < M; ++i) {
    h = getint() - 1, w = getint() - 1;
    bomb[i] = h + (w << 20);

    x = ++posr[h], y = ++posc[w];

    if (x == mxr)
      ++ctrow;
    else if (x > mxr)
      mxr = x, ctrow = 1;

    if (y == mxc)
      ++ctcol;
    else if (y > mxc)
      mxc = y, ctcol = 1;
  }
  ans = mxr + mxc;

  long ct = ctrow;
  ct *= ctcol;
  if (ct > M) {
    printf("%d\\n", ans);
    return 0;
  }

  for (int i = 0; i < M; ++i) {
    long tmp = bomb[i] >> 20;
    if (posr[bomb[i] - (tmp << 20)] == mxr && posc[tmp] == mxc)
      --ct;
  }
  if (ct <= 0)
    --ans;
  printf("%d\\n", ans);
  return 0;
}


Error: -11

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <cstdio>
using namespace std;

inline int getint() {
  int x = 0, c = getchar();
  while (c > 47 && c < 58) {
    x = x * 10 + c - 48;
    c = getchar();
  }
  return x;
}

constexpr int MAX = 300000;
long res[MAX * 2];

int main() {
  int H = getint();
  int W = getint();
  int M = getint();

  int h, w, ans, x, y;
  int mxr = 0;
  int mxc = 0;
  int ctrow = 0;
  int ctcol = 0;
  for (int i = 0; i < M; ++i) {
    h = getint() - 1, w = getint() - 1;
    res[i + 2 * MAX] = h, res[i + 3 * MAX] = w;
    x = ++res[h], y = ++res[w + MAX];

    if (x == mxr)
      ++ctrow;
    else if (x > mxr)
      mxr = x, ctrow = 1;

    if (y == mxc)
      ++ctcol;
    else if (y > mxc)
      mxc = y, ctcol = 1;
  }
  ans = mxr + mxc;

  long ct = ctrow;
  ct *= ctcol;
  if (ct > M) {
    printf("%d\\n", ans);
    return 0;
  }

  for (int i = 0; i < M; ++i)
    if (res[res[i + 2 * MAX]] == mxr && res[res[i + 3 * MAX] + MAX] == mxc)
      --ct;
  if (ct <= 0)
    --ans;
  printf("%d\\n", ans);
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

int H, W, M;
int cntH[int(3e5 + 5)], cntW[int(3e5 + 5)];
Pii PH[int(3e5 + 5)], PW[int(3e5 + 5)];
map<Pii, bool> mp;

int main() {
  cin >> H >> W >> M;
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    cntH[h]++;
    cntW[w]++;
    mp[{h, w}] = true;
  }

  for (int i = 0; i <= H; i++)
    PH[i] = {cntH[i], i};
  sort(PH, PH + H + 1, greater<Pii>());
  for (int i = 0; i <= W; i++)
    PW[i] = {cntW[i], i};
  sort(PW, PW + W + 1, greater<Pii>());

  int h = 0, w = 0;
  while (PH[0].fi == PH[h].fi)
    h++;
  while (PW[0].fi == PW[w].fi)
    w++;

  int ans = PH[0].fi + PW[0].fi - 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (!mp[{PH[i].se, PW[j].se}]) {
        cout << ans + 1 << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

int H, W, M;
int cntH[int(3e5 + 5)], cntW[int(3e5 + 5)];
Pii PH[int(3e5 + 5)], PW[int(3e5 + 5)];
map<Pii, bool> mp;

int main() {
  cin >> H >> W >> M;
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    cntH[h]++;
    cntW[w]++;
    mp[{h, w}] = true;
  }

  for (int i = 0; i <= H; i++)
    PH[i] = {cntH[i], i};
  sort(PH, PH + H + 1, greater<Pii>());
  for (int i = 0; i <= W; i++)
    PW[i] = {cntW[i], i};
  sort(PW, PW + W + 1, greater<Pii>());

  int h = 0, w = 0;
  while (PH[0].fi == PH[h].fi)
    h++;
  while (PW[0].fi == PW[w].fi)
    w++;

  int ans = PH[0].fi + PW[0].fi - 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (!mp[{PH[i].se, PW[j].se}]) {
        ans = PH[0].fi + PW[0].fi;
      }
    }
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>

using namespace std;

int main(int argc, const char *argv[]) {
  //
  int h, w, m;
  cin >> h >> w >> m;

  vector<pair<int, int>> tgts(m);
  vector<int> cnt_row(h, 0), cnt_col(w, 0);
  for (int i = 0; i < m; ++i) {
    cin >> tgts[i].first >> tgts[i].second;
    tgts[i].first--;
    tgts[i].second--;
    cnt_row[tgts[i].first]++;
    cnt_col[tgts[i].second]++;
  }

  sort(tgts.begin(), tgts.end());

  int max_col = *max_element(cnt_col.begin(), cnt_col.end());
  int max_row = *max_element(cnt_row.begin(), cnt_row.end());

  int cnt1 = 0, cnt2 = 0;
  vector<int> vr, vc;
  for (int i = 0; i < cnt_row.size(); i++) {
    if (cnt_row[i] == max_row) {
      vr.push_back(i);
    }
  }

  for (int i = 0; i < cnt_col.size(); i++) {
    if (cnt_col[i] == max_col) {
      vc.push_back(i);
    }
  }

  for (auto r : vr) {
    for (auto c : vc) {
      auto p = make_pair(r, c);
      if (!binary_search(tgts.begin(), tgts.end(), p)) {
        cout << max_col + max_row << '\\n';
        return 0;
      }
    }
  }

  cout << max_col + max_row - 1 << '\\n';

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main(int argc, const char *argv[]) {
  //
  int h, w, m;
  cin >> h >> w >> m;

  vector<pair<int, int>> tgts(m);
  vector<int> cnt_row(h, 0), cnt_col(w, 0);
  for (int i = 0; i < m; ++i) {
    cin >> tgts[i].first >> tgts[i].second;
    cnt_row[tgts[i].first]++;
    cnt_col[tgts[i].second]++;
  }

  sort(tgts.begin(), tgts.end());

  int max_col = *max_element(cnt_col.begin(), cnt_col.end());
  int max_row = *max_element(cnt_row.begin(), cnt_row.end());

  int cnt1 = 0, cnt2 = 0;
  vector<int> vr, vc;
  for (int i = 0; i < cnt_row.size(); i++) {
    if (cnt_row[i] == max_row) {
      vr.push_back(i);
    }
  }

  for (int i = 0; i < cnt_col.size(); i++) {
    if (cnt_col[i] == max_col) {
      vc.push_back(i);
    }
  }

  for (auto r : vr) {
    for (auto c : vc) {
      auto p = make_pair(r, c);
      if (!binary_search(tgts.begin(), tgts.end(), p)) {
        cout << max_col + max_row << '\\n';
        return 0;
      }
    }
  }

  cout << max_col + max_row - 1 << '\\n';

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 25;
const int MOD = 1e9 + 7;
const int MAX = 3 * 1e5;
int h, w, m, x[MAX], y[MAX], r[MAX], c[MAX], mr, sr, mc, sc;
int main() {
  cin >> h >> w >> m;
  rep(i, m) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
    r[x[i]]++;
    c[y[i]]++;
  }

  rep(i, h) {
    if (r[i] > mr) {
      mr = r[i];
      sr = 1;
    } else if (mr == r[i])
      sr++;
  }
  rep(i, w) {
    if (mc < c[i]) {
      mc = c[i];
      sc = 1;
    } else if (mc == c[i])
      sc++;
  }

  int s = 0;
  rep(i, m) {
    if (r[x[i]] == mr && c[y[i]] == mc)
      s++;
  }
  cout << mr + mc - (s == sr * sc) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 25;
const int MOD = 1e9 + 7;
const int MAX = 3 * 1e5 + 100;
int gyou[MAX], retu[MAX];
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<vector<int>> bomb(h + 100, vector<int>(w + 100));
  int x, y;
  rep(i, m) {
    cin >> x >> y;
    bomb[x - 1][y - 1] = 1;
    gyou[x - 1]++;
    retu[y - 1]++;
  }
  int gyou_max = 0, gyou_idx = 0;
  rep(i, h) {
    if (gyou[i] > gyou_max) {
      gyou_max = gyou[i];
      gyou_idx = i;
    }
  }

  int ans = gyou_max;
  rep(j, w) { ans = max(ans, gyou_max + retu[j] - bomb[gyou_idx][j]); }
  cout << ans << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> cnth(h, 0), cntw(w, 0), hh, ww;
  map<pii, int> mp;
  int maxh = 0, maxw = 0;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    cnth[x - 1]++;
    cntw[y - 1]++;
    mp[{x - 1, y - 1}]++;
  }
  for (int i = 0; i < h; i++)
    maxh = max(maxh, cnth[i]);
  for (int i = 0; i < w; i++)
    maxw = max(maxw, cntw[i]);
  for (int i = 0; i < h; i++)
    if (cnth[i] == maxh)
      hh.push_back(i);
  for (int i = 0; i < w; i++)
    if (cntw[i] == maxw)
      ww.push_back(i);
  for (auto i : hh)
    for (auto j : ww)
      if (!mp[{i, j}]) {
        cout << maxh + maxw;
        return;
      }
  cout << maxh + maxw - 1;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> cnth(h, 0), cntw(w, 0), hh, ww;
  int maxh = 0, maxw = 0, minus = 1;
  bool ok = 0;
  vector<vector<bool>> ex(h, vector<bool>(w, 0));
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    cnth[x - 1]++;
    cntw[y - 1]++;
    ex[x - 1][y - 1] = 1;
  }
  for (int i = 0; i < h; i++)
    maxh = max(maxh, cnth[i]);
  for (int i = 0; i < w; i++)
    maxw = max(maxw, cntw[i]);
  for (int i = 0; i < h; i++)
    if (cnth[i] == maxh)
      hh.push_back(i);
  for (int i = 0; i < w; i++)
    if (cntw[i] == maxw)
      ww.push_back(i);
  for (int i : hh) {
    for (int j : ww) {
      if (!ex[i][j]) {
        minus = 0;
        ok = 1;
        break;
      }
    }
    if (ok)
      break;
  }
  cout << maxh + maxw - minus;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  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 < n; i++)
#define all(v) v.begin(), v.end()
typedef int64_t lint;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  int a = 0, b = 0, c = 0, x, y, z;
  vector<int> vh(H);
  vector<int> vw(W);
  vector<vector<int>> vv(H);
  rep(i, M) {
    cin >> x >> y;
    x--, y--;
    vh.at(x)++, vw.at(y)++;
    vv.at(x).push_back(y);
  }

  vector<int> bh, bw;
  rep(i, H) { chmax(a, vh.at(i)); }
  rep(i, H) {
    if (vh.at(i) == a)
      bh.push_back(i);
  }
  rep(i, W) { chmax(b, vw.at(i)); }
  rep(i, W) {
    if (vw.at(i) == b)
      bw.push_back(i);
  }

  c = a + b;
  rep(i, H) { sort(all(vv.at(i))); }
  rep(i, bh.size()) {
    rep(j, bw.size()) {
      bool f = true;
      x = 0, y = vv.at(bh.at(i)).size();
      rep(k, vv.at(bh.at(i)).size()) {
        if (x == y)
          break;
        z = (x + y) / 2;
        if (vv.at(bh.at(i)).at(z) == bw.at(j)) {
          f = false;
          break;
        } else if (vv.at(bh.at(i)).at(z) > bw.at(j)) {
          y = z;
        } else {
          x = z;
        }
      }
      if (f) {
        std::cout << c << '\\n';
        return 0;
      }
    }
  }
  std::cout << c - 1 << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef int64_t lint;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  int a = 0, b = 0, c = 0, x, y, z;
  vector<int> vh(H);
  vector<int> vw(W);
  vector<vector<int>> vv(H);
  rep(i, M) {
    cin >> x >> y;
    x--, y--;
    vh.at(x)++, vw.at(y)++;
    vv.at(x).push_back(y);
  }

  vector<int> bh, bw;
  rep(i, H) { chmax(a, vh.at(i)); }
  rep(i, H) {
    if (vh.at(i) == a)
      bh.push_back(i);
  }
  rep(i, W) { chmax(b, vw.at(i)); }
  rep(i, W) {
    if (vw.at(i) == b)
      bw.push_back(i);
  }
  c = a + b;
  rep(i, bh.size()) {
    rep(j, bw.size()) {
      bool f = true;
      rep(k, vv.at(bh.at(i)).size()) {
        if (vv.at(bh.at(i)).at(k) == bw.at(j))
          f = false;
      }
      if (f) {
        std::cout << c << '\\n';
        return 0;
      }
    }
  }
  std::cout << c - 1 << '\\n';
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <utility>
#include <vector>

typedef std::pair<int, int> ii;

int H, W, M;
int SH[300005];
int SW[300005];

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  std::cout.tie(NULL);

  std::cin >> H >> W >> M;
  for (int i = 0; i < H; ++i)
    SH[i] = 0;
  for (int j = 0; j < W; ++j)
    SW[j] = 0;
  std::set<ii> targets;
  for (int i = 0; i < M; ++i) {
    int h, w;
    std::cin >> h >> w;
    h--;
    w--;
    SH[h]++;
    SW[w]++;
    targets.insert({h, w});
  }

  std::vector<ii> SSH, SSW;
  for (int h = 0; h < H; ++h)
    SSH.push_back({SH[h], h});
  for (int w = 0; w < W; ++w)
    SSW.push_back({SW[w], w});
  std::sort(SSH.begin(), SSH.end());
  std::sort(SSW.begin(), SSW.end());

  int max_ans = SSH[H - 1].first + SSW[W - 1].first;
  int ans = 0;
  for (int i = H - 1; i >= 0; --i) {
    if (max_ans - (SSH[i].first + SSW[W - 1].first) >= 2)
      break;
    for (int j = W - 1; j >= 0; --j) {
      if (max_ans - (SSH[i].first + SSW[j].first) >= 2)
        break;
      int cnt = SSH[i].first + SSW[j].first;
      if (cnt <= ans)
        break;
      if (targets.find({SSH[i].second, SSW[j].second}) != targets.end())
        cnt--;
      // std::cout << SSH[i].first << "+" << SSW[j].first << "-" <<
      // int(targets.find({SSH[i].second, SSW[j].second}) != targets.end()) <<
      // "=" << cnt << "\\n";
      ans = std::max(ans, cnt);
    }
  }

  std::cout << ans << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <utility>
#include <vector>

typedef std::pair<int, int> ii;

int H, W, M;
int SH[300005];
int SW[300005];

int main() {
  std::cin >> H >> W >> M;
  for (int i = 0; i < H; ++i)
    SH[i] = 0;
  for (int j = 0; j < W; ++j)
    SW[j] = 0;
  std::set<ii> targets;
  for (int i = 0; i < M; ++i) {
    int h, w;
    std::cin >> h >> w;
    h--;
    w--;
    SH[h]++;
    SW[w]++;
    targets.insert({h, w});
  }

  std::vector<ii> SSH, SSW;
  for (int h = 0; h < H; ++h)
    SSH.push_back({SH[h], h});
  for (int w = 0; w < W; ++w)
    SSW.push_back({SW[w], w});
  std::sort(SSH.begin(), SSH.end());
  std::sort(SSW.begin(), SSW.end());

  int max_ans = SSH[H - 1].first + SSW[W - 1].first;
  int ans = 0;
  for (int i = H - 1; i >= 0; --i) {
    for (int j = W - 1; j >= 0; --j) {
      if (max_ans - (SSH[i].first + SSW[j].first) >= 2)
        break;
      int cnt = SSH[i].first + SSW[j].first;
      if (targets.find({SSH[i].second, SSW[j].second}) != targets.end())
        cnt--;
      // std::cout << SSH[i].first << "+" << SSW[j].first << "-" <<
      // int(targets.find({SSH[i].second, SSW[j].second}) != targets.end()) <<
      // "=" << cnt << "\\n";
      ans = std::max(ans, cnt);
    }
  }

  std::cout << ans << "\\n";

  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define F first
#define S second
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define pb push_back
#define fastIO ios::sync_with_stdio(0), cin.tie(0)
#define endl '\\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H), debug_out(T...);
}
int main() {
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#else
#define debug(...) 3
  fastIO;
#endif
  int n, m, q;
  cin >> n >> m >> q;
  pair<int, int> x, y;
  map<int, int> f, c;
  map<pii, bool> par;
  map<int, vector<int>> mp1;
  map<int, vector<int>> mp2;
  x = {0, 0}, y = {0, 0};
  forn(i, q) {
    int h, w;
    cin >> h >> w;
    f[h]++;
    c[w]++;
    par[{h, w}] = 1;
    // debug(h, w, f[h], c[w], x.F, y.S);
    if (f[h] >= x.S) {
      x = {h, f[h]};
      mp1[x.S].pb(x.F);
    }
    if (c[w] >= y.S) {
      y = {w, c[w]};
      mp2[y.S].pb(y.F);
    }
  }
  int ans = 0;
  for (auto p : mp1[x.S]) {
    for (auto k : mp2[y.S]) {
      ans = max(ans, f[p] + c[k] - par[{p, k}]);
      if (!par[{p, k}])
        goto done;
    }
  }
done:;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define F first
#define S second
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define pb push_back
#define fastIO ios::sync_with_stdio(0), cin.tie(0)
#define endl '\\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H), debug_out(T...);
}
int main() {
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#else
#define debug(...) 3
  fastIO;
#endif
  int n, m, q;
  cin >> n >> m >> q;
  pair<int, int> x, y;
  int f[n + 2], c[m + 2];
  memset(f, 0, sizeof f);
  memset(c, 0, sizeof c);
  map<pii, bool> par;
  map<int, vector<int>> mp1;
  map<int, vector<int>> mp2;
  x = {0, 0}, y = {0, 0};
  forn(i, q) {
    int h, w;
    cin >> h >> w;
    f[h]++;
    c[w]++;
    par[{h, w}] = 1;
    // debug(h, w, f[h], c[w], x.F, y.S);
    if (f[h] >= x.S) {
      x = {h, f[h]};
      mp1[x.S].pb(x.F);
    }
    if (c[w] >= y.S) {
      y = {w, c[w]};
      mp2[y.S].pb(y.F);
    }
  }
  int ans = 0;
  for (auto p : mp1[x.S]) {
    for (auto k : mp2[y.S]) {
      ans = max(ans, f[p] + c[k] - par[{p, k}]);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

constexpr llong mod = 1e9 + 7;
constexpr llong inf = mod * mod;

int main() {
  llong H, W, M;
  vector<llong> bh;
  vector<llong> bw;
  map<llong, llong> mh, mw;
  set<lpair> s;

  cin >> H >> W >> M;
  bh.resize(M);
  bw.resize(M);
  for (int i = 0; i < M; i++) {
    cin >> bh[i] >> bw[i];
    mh[bh[i]]++;
    mw[bw[i]]++;
    s.insert(lpair(bh[i], bw[i]));
  }

  llong ah = 0, aw = 0;
  vector<llong> ch, cw;
  for (auto it : mh) {
    if (ah < it.second) {
      ch.clear();
      ah = it.second;
    }
    if (ah == it.second) {
      ch.push_back(it.first);
    }
  }
  for (auto it : mw) {
    if (aw < it.second) {
      cw.clear();
      aw = it.second;
    }
    if (aw == it.second) {
      cw.push_back(it.first);
    }
  }

  llong ans = ah + aw;
  llong same = 1;
  for (auto &h : ch) {
    if (same == 0)
      break;
    for (auto &w : cw) {
      if (!s.count(lpair(h, w))) {
        same = 0;
        break;
      }
    }
  }

  cout << ans - same << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

constexpr llong mod = 1e9 + 7;
constexpr llong inf = mod * mod;

int main() {
  llong H, W, M;
  vector<llong> bh;
  vector<llong> bw;
  map<llong, llong> mh, mw;

  cin >> H >> W >> M;
  bh.resize(M);
  bw.resize(M);
  for (int i = 0; i < M; i++) {
    cin >> bh[i] >> bw[i];
    mh[bh[i]]++;
    mw[bw[i]]++;
  }

  llong ah = 0, aw = 0;
  vector<llong> ch, cw;
  for (auto it : mh) {
    if (ah < it.second) {
      ch.clear();
      ah = it.second;
    }
    if (ah == it.second) {
      ch.push_back(it.first);
    }
  }
  for (auto it : mw) {
    if (aw < it.second) {
      cw.clear();
      aw = it.second;
    }
    if (aw == it.second) {
      cw.push_back(it.first);
    }
  }

  llong ans = ah + aw;
  llong same = 1;
  for (auto &h : ch) {
    if (same == 0)
      break;
    for (auto &w : cw) {
      bool ok = true;
      for (int i = 0; i < M; i++) {
        ok &= (bh[i] != h || bw[i] != w);
      }
      if (ok) {
        same = 0;
        break;
      }
    }
  }

  cout << ans - same << endl;

  return 0;
}