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

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

const ll INF = 1LL << 60;
const ll MOD = ll(1e9) + 7;

int main() {
  ll H, W, M;
  cin >> H >> W >> M;

  set<pair<ll, ll>> S;

  vector<ll> hnum(H, 0);
  vector<ll> wnum(W, 0);

  for (ll i = 0; i < M; i++) {
    ll a, b;
    cin >> a >> b;
    hnum.at(a - 1)++;
    wnum.at(b - 1)++;

    S.emplace(a, b);
  }

  ll hmax = 0;
  ll wmax = 0;
  vector<ll> hmaxindex;
  vector<ll> wmaxindex;

  for (ll i = 0; i < H; i++) {
    if (hnum.at(i) > hmax) {
      hmax = hnum.at(i);

      hmaxindex.clear();
      hmaxindex.shrink_to_fit();
      hmaxindex.emplace_back(i + 1);
    } else if (hnum.at(i) == hmax) {
      hmaxindex.emplace_back(i + 1);
    }
  }

  for (ll i = 0; i < W; i++) {
    if (wnum.at(i) > wmax) {
      wmax = wnum.at(i);

      wmaxindex.clear();
      wmaxindex.shrink_to_fit();
      wmaxindex.emplace_back(i + 1);
    } else if (wnum.at(i) == wmax) {
      wmaxindex.emplace_back(i + 1);
    }
  }

  ll ans = wmax + hmax - 1;

  for (ll i = 0; i < hmaxindex.size(); i++) {
    for (ll j = 0; j < wmaxindex.size(); j++) {

      bool is_dup = false;

      if (S.find({hmaxindex.at(i), wmaxindex.at(j)}) != S.end())
        is_dup = true;

      if (!is_dup) {
        ans = wmax + hmax;
        cout << ans << endl;
        return 0;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

const ll INF = 1LL << 60;
const ll MOD = ll(1e9) + 7;

int main() {
  ll H, W, M;
  cin >> H >> W >> M;

  vector<vector<ll>> S(M, vector<ll>(2));

  vector<ll> hnum(H, 0);
  vector<ll> wnum(W, 0);

  for (ll i = 0; i < M; i++) {
    cin >> S.at(i).at(0) >> S.at(i).at(1);
    hnum.at(S.at(i).at(0) - 1)++;
    wnum.at(S.at(i).at(1) - 1)++;
  }

  ll hmax = 0;
  ll wmax = 0;
  vector<ll> hmaxindex;
  vector<ll> wmaxindex;

  for (ll i = 0; i < H; i++) {
    if (hnum.at(i) > hmax) {
      hmax = hnum.at(i);

      hmaxindex.clear();
      hmaxindex.shrink_to_fit();
      hmaxindex.push_back(i + 1);
    } else if (hnum.at(i) == hmax) {
      hmaxindex.push_back(i + 1);
    }
  }

  for (ll i = 0; i < W; i++) {
    if (wnum.at(i) > wmax) {
      wmax = wnum.at(i);

      wmaxindex.clear();
      wmaxindex.shrink_to_fit();
      wmaxindex.push_back(i + 1);
    } else if (wnum.at(i) == wmax) {
      wmaxindex.push_back(i + 1);
    }
  }

  ll ans = wmax + hmax - 1;

  for (ll i = 0; i < hmaxindex.size(); i++) {
    for (ll j = 0; j < wmaxindex.size(); j++) {

      bool is_dup = false;
      for (ll k = 0; k < M; k++) {

        if (S.at(k).at(0) == hmaxindex.at(i) &&
            S.at(k).at(1) == wmaxindex.at(j)) {
          is_dup = true;
          break;
        }
      }

      if (!is_dup) {
        ans = wmax + hmax;
        cout << ans << endl;
        return 0;
      }
    }
  }

  cout << ans << endl;

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

#pragma GCC optimize("O2")

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

#define HW(x, y) (((long long)x << 32) + y)

static int s_cmp(const void *lhs, const void *rhs) {
  long long l = *(const long long *)lhs;
  long long r = *(const long long *)rhs;

  if (l < r) {
    return -1;
  } else if (l > r) {
    return 1;
  }

  return 0;
}

#define S_PRIME 9999991

int H;
int W;
int M;
int h[300000 + 1] = {
    0,
};
int w[300000 + 1] = {
    0,
};
vector<long long> hw;
int max_m = 0;

int main() {

  cin >> H >> W >> M;

  int max_h = 0;
  int max_w = 0;
  for (int i = 1; i <= M; i++) {
    int hi;
    int wi;

    cin >> hi >> wi;
    h[hi]++;
    if (max_h < h[hi]) {
      max_h = h[hi];
    }
    w[wi]++;
    if (max_w < w[wi]) {
      max_w = w[wi];
    }

    hw.push_back(HW(hi, wi));
  }
  int max_m = max_h + max_w;

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

  vector<int> max_i;
  vector<int> max_j;
  for (int i = 1; i <= ((H > W) ? H : W); i++) {
    if (max_h == h[i]) {
      max_i.push_back(i);
    }

    if (max_w == w[i]) {
      max_j.push_back(i);
    }
  }

  if (max_i.size() * max_j.size() > M) {
    cout << max_m << endl;
    return 0;
  }

  for (vector<int>::iterator it_i = max_i.begin(); it_i != max_i.end();
       it_i++) {
    for (vector<int>::iterator it_j = max_j.begin(); it_j != max_j.end();
         it_j++) {
      if (!binary_search(hw.begin(), hw.end(), HW(*it_i, *it_j))) {
        cout << max_m << endl;
        return 0;
      }
    }
  }

  // bomb on target (add both in h and in w)
  max_m--;

  cout << max_m << endl;

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O2")

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

#define HW(x, y) (((long long)x << 19) + y)

static int s_cmp(const void *lhs, const void *rhs) {
  long long l = *(const long long *)lhs;
  long long r = *(const long long *)rhs;

  if (l < r) {
    return -1;
  } else if (l > r) {
    return 1;
  }

  return 0;
}

#define S_PRIME 9999991

int H;
int W;
int M;
int h[300000 + 1] = {
    0,
};
int w[300000 + 1] = {
    0,
};
char hw[9999991] = {
    0,
};
int max_m = 0;

int main() {

  cin >> H >> W >> M;

  for (int i = 1; i <= M; i++) {
    int hi;
    int wi;

    cin >> hi >> wi;
    h[hi]++;
    w[wi]++;

    hw[HW(hi, wi) % S_PRIME] = 1;
  }

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      int m = h[i] + w[j];
      long long ij = HW(i, j);
      if (hw[ij % S_PRIME] != 0) {
        // bomb on target (add both in h and in w)
        m--;
      }

      if (m > max_m) {
        max_m = m;
      }
    }
  }

  cout << max_m << endl;

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

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
// #include <bits/stdc++.h>

using namespace std;
typedef pair<long int, long int> P;

int main() {
  long int H, W, M;
  cin >> H >> W >> M;
  long int h[M], w[M];
  set<P> s;
  vector<long int> x(H), y(W);
  for (long int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
    x[h[i]]++;
    y[w[i]]++;
    s.insert(P(h[i], w[i]));
  }
  long int maxh = 0, maxw = 0;
  for (long int i = 1; i < H; i++) {
    if (x[i] > x[maxh])
      maxh = i;
  }
  for (long int i = 1; i < W; i++) {
    if (y[i] > y[maxw])
      maxw = i;
  }
  vector<long int> xmax, ymax;
  long int nx = 0, ny = 0;
  for (long int i = 0; i < H; i++) {
    if (x[i] == x[maxh]) {
      xmax.push_back(i);
      nx++;
    }
  }
  for (long int i = 0; i < W; i++) {
    if (y[i] == y[maxw]) {
      ymax.push_back(i);
      ny++;
    }
  }
  bool flag = false;
  for (int i = 0; i < nx; i++) {
    for (int j = 0; j < ny; j++) {
      auto ite = s.find(P(xmax[i], ymax[j]));
      if (ite == s.end()) {
        flag = true;
        break;
      }
    }
  }
  if (flag)
    cout << x[maxh] + y[maxw] << endl;
  else
    cout << x[maxh] + y[maxw] - 1 << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
// #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  int h[M], w[M];
  vector<int> x(H), y(W);
  vector<vector<bool>> A(H, vector<bool>(W, false));
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
    A[h[i]][w[i]] = true;
    x[h[i]]++;
    y[w[i]]++;
  }
  int n = 0;
  for (int i = 1; i < H; i++) {
    if (x[i] > x[i - 1])
      n = i;
  }
  vector<int> vx;
  for (int i = 0; i < H; i++) {
    if (x[i] == x[n])
      vx.push_back(i);
  }
  int m = 0;
  for (int i = 1; i < W; i++) {
    if (y[i] > y[i - 1])
      m = i;
  }
  vector<int> vy;
  for (int i = 0; i < W; i++) {
    if (y[i] == y[m])
      vy.push_back(i);
  }
  int lx = vx.size();
  int ly = vy.size();
  long int ans = 0;
  for (int i = 0; i < lx; i++) {
    for (int j = 0; j < ly; j++) {
      if (A[vx[i]][vy[j]] == false) {
        ans--;
        break;
      }
    }
  }
  ans += x[n] + y[m];
  cout << ans << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define Int int64_t
#define ll long long
#define pb push_back
using namespace std;
const int MAXN = 500005;
Int N, M, K;

int main() {
  Int N, M;
  cin >> N >> M >> K;
  set<pair<int, int>> both;
  for (int i = 0; i < K; ++i) {
    int u, v;
    cin >> u >> v;
    both.insert({u, v});
  }
  map<int, int> rows;
  map<int, int> cols;
  map<pair<int, int>, int> common;
  priority_queue<pair<int, int>> pq;
  for (auto &itr : both) {
    rows[itr.first]++;
    cols[itr.second]++;
    common[{itr.first, itr.second}]++;
  }
  Int a = 0, b = 0;
  Int fromRows, fromCols;
  for (auto &itr : rows) {
    if (itr.second > a) {
      a = itr.second;
      fromRows = itr.first;
    }
  }
  for (auto &itr : cols) {
    if (itr.second > b) {
      b = itr.second;
      fromCols = itr.first;
    }
  }
  vector<int> poss_rows, poss_cols;
  for (auto itr : rows) {
    if (itr.second == a) {
      poss_rows.pb(itr.first);
    }
  }
  for (auto itr : cols) {
    if (itr.second == b) {
      poss_cols.pb(itr.first);
    }
  }
  int ans = rows[fromRows] + cols[fromCols] - 1;
  bool commonFind = false;
  for (auto x : poss_rows) {
    for (auto y : poss_cols) {
      if (!common[{x, y}]) {
        ++ans;
        commonFind = true;
        break;
      }
    }
    if (commonFind)
      break;
  }
  cout << ans << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define Int int64_t
#define ll long long
#define pb push_back
using namespace std;
const int MAXN = 500005;
Int N, M, K;

int main() {
  Int N, M;
  cin >> N >> M >> K;
  set<pair<int, int>> both;
  for (int i = 0; i < K; ++i) {
    int u, v;
    cin >> u >> v;
    both.insert({u, v});
  }
  map<int, int> rows;
  map<int, int> cols;
  map<pair<int, int>, int> common;
  priority_queue<pair<int, int>> pq;
  for (auto &itr : both) {
    rows[itr.first]++;
    cols[itr.second]++;
    common[{itr.first, itr.second}]++;
  }
  Int a = 0, b = 0;
  Int fromRows, fromCols;
  for (auto &itr : rows) {
    if (itr.second > a) {
      a = itr.second;
      fromRows = itr.first;
    }
  }
  for (auto &itr : cols) {
    if (itr.second > b) {
      b = itr.second;
      fromCols = itr.first;
    }
  }
  vector<int> poss_rows, poss_cols;
  for (auto itr : rows) {
    if (itr.second == a) {
      poss_rows.pb(itr.first);
    }
  }
  for (auto itr : cols) {
    if (itr.second == b) {
      poss_cols.pb(itr.first);
    }
  }
  int ans = 0;
  for (auto x : poss_rows) {
    for (auto y : poss_cols) {
      ans = max(ans, rows[x] + cols[y] - common[{x, y}]);
    }
  }
  cout << ans << "\\n";
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
  Author: @yash_31
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#define ll long long int
#define eb emplace_back
#define fatafat                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define MOD 1000000007
#define ft first
#define sd second
#define deb(x) std::cout << #x << ":" << x << endl;
#define piii pair<int, pair<int, int>>
#define endl "\\n"

using namespace std;

const int mlt = (1 << 18) + 69;
const ll inf = 1e18;
// TTBDC

int main() {
  fatafat int h, w, m, x, y;
  cin >> h >> w >> m;
  map<int, int> mc, mr;
  map<pair<int, int>, int> mp;
  set<int> rs, cs;
  int mxr = 0, mxc = 0;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    mc[y]++;
    mr[x]++;
    mp[{x, y}]++;
    if (mc[y] > mxc) {
      mxc = mc[y];
      cs.clear();
      cs.insert(y);
    } else if (mc[y] == mxc)
      cs.insert(y);
    if (mr[x] > mxr) {
      mxr = mr[x];
      rs.clear();
      rs.insert(x);
    } else if (mr[x] == mxr)
      rs.insert(x);
  }
  for (int i1 : cs) {
    for (int i2 : rs) {
      if (mp[{i2, i1}] == 0) {
        cout << mc[i1] + mr[i2];
        return 0;
      }
    }
  }
  cout << mxr + mxc - 1;
  return 0;
}

Error: TLE

Fixed submission:

/*
  Author: @yash_31
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#define ll long long int
#define eb emplace_back
#define fatafat                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define MOD 1000000007
#define ft first
#define sd second
#define deb(x) std::cout << #x << ":" << x << endl;
#define piii pair<int, pair<int, int>>
#define endl "\\n"

using namespace std;

const int mlt = (1 << 18) + 69;
const ll inf = 1e18;
// TTBDC

int main() {
  fatafat int h, w, m, x, y;
  cin >> h >> w >> m;
  map<int, int> mc, mr;
  map<pair<int, int>, int> mp;
  set<int> rs, cs;
  int mxr = 0, mxc = 0;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    mc[y]++;
    mr[x]++;
    mp[{x, y}]++;
    if (mc[y] > mxc) {
      mxc = mc[y];
      cs.clear();
      cs.insert(y);
    } else if (mc[y] == mxc)
      cs.insert(y);
    if (mr[x] > mxr) {
      mxr = mr[x];
      rs.clear();
      rs.insert(x);
    } else if (mr[x] == mxr)
      rs.insert(x);
  }
  int ans = 0;
  for (int i1 : cs) {
    for (int i2 : rs) {
      ans = max(ans, mc[i1] + mr[i2] - mp[{i2, i1}]);
    }
  }
  cout << ans;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
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;
}
const int INF = 1001001001;
const int mod = 1e9 + 7;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int64_t> h(H), w(W);
  set<pair<int, int>> q;
  for (int i = 0; i < M; i++) {
    int y, x;
    cin >> y >> x;
    y--;
    x--;
    h[y]++;
    w[x]++;
    q.insert(make_pair(y, x));
  }
  int64_t MXh = *max_element(h.begin(), h.end());
  int64_t MXw = *max_element(w.begin(), w.end());
  vector<int> hmx, wmx;
  for (int i = 0; i < H; i++) {
    if (h[i] == MXh) {
      hmx.push_back(i);
    }
  }
  for (int j = 0; j < W; j++) {
    if (w[j] == MXw) {
      wmx.push_back(j);
    }
  }
  for (int i : hmx) {
    for (int j : wmx) {
      if (!q.count(make_pair(i, j))) {
        cout << MXh + MXw << endl;
        return 0; // 全部使っていたとするときのループが最悪のケースだが、それはMケースのみ
      }
    }
  }
  cout << MXh + MXw - 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
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;
}
const int INF = 1001001001;
const int mod = 1e9 + 7;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int64_t> h(H), w(W);
  vector<vector<bool>> use(H, vector<bool>(W, false));
  for (int i = 0; i < M; i++) {
    int y, x;
    cin >> y >> x;
    y--;
    x--;
    h[y]++;
    w[x]++;
    use[y][x] = true;
  }
  int64_t MXh = *max_element(h.begin(), h.end());
  int64_t MXw = *max_element(w.begin(), w.end());
  vector<int> hmx, wmx;
  for (int i = 0; i < H; i++) {
    if (h[i] == MXh) {
      hmx.push_back(i);
    }
  }
  for (int j = 0; j < W; j++) {
    if (w[j] == MXw) {
      wmx.push_back(j);
    }
  }
  int64_t cnt = 0;
  for (int i : hmx) {
    for (int j : wmx) {
      if (!use[i][j] || cnt >= M) {
        cout << MXh + MXw << endl;
        return 0; // 全部使っていたとするときのループが最悪のケースだが、それはMケースのみ
      }
      cnt++;
    }
  }
  cout << MXh + MXw - 1 << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
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;
}

const int mod = 1000000007;
const int INF = 1001001001;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> h(M), w(M);
  vector<int> t(H), y(W);
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
    t[h[i]]++;
    y[w[i]]++;
  }
  int64_t YMX = *max_element(t.begin(), t.end());
  int64_t XMX = *max_element(y.begin(), y.end());
  int64_t cnt = 0;
  for (int i = 0; i < M; i++) {
    if (t[h[i]] == YMX && y[w[i]] == XMX) {
      cnt++;
    }
  }
  int64_t X = 0, Y = 0;
  for (int i = 0; i < H; i++) {
    if (t[i] == YMX) {
      Y++;
    }
  }
  for (int i = 0; i < W; i++) {
    if (y[i] == XMX) {
      X++;
    }
  }

  int64_t ans = YMX + XMX;
  if (Y * X > cnt) {
    cout << ans << endl;
  } else {
    cout << ans - 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
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;
}

const int mod = 1000000007;
const int INF = 1001001001;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> h(M), w(M);
  vector<int> t(H), y(W);
  map<pair<int, int>, int> k;
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
    k[make_pair(h[i], w[i])] = 1;
    t[h[i]]++;
    y[w[i]]++;
  }
  int64_t YMX = *max_element(t.begin(), t.end());
  int64_t XMX = *max_element(y.begin(), y.end());
  vector<int> X, Y;
  for (int i = 0; i < M; i++) {
    if (t[h[i]] == YMX) {
      Y.push_back(h[i]);
    }
    if (y[w[i]] == XMX) {
      X.push_back(w[i]);
    }
  }

  int64_t ans = YMX + XMX;
  for (auto x : X) {
    for (auto y : Y) {
      if (k[make_pair(y, x)] != 1) {
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans - 1 << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long
#define endl "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const int mod = 1e9 + 7;
const int MAX = 3e5 + 1;

unordered_set<ll> st;
int R[MAX], C[MAX];

ll hashFunction(int x, int y) { return ((x * 1LL * 100000000) + y); }

void solve() {
  int rows, cols, targets;
  cin >> rows >> cols >> targets;
  int h, w;
  for (int i = 0; i < targets; ++i) {
    cin >> h >> w;
    R[h] += 1;
    C[w] += 1;
    st.insert(hashFunction(h, w));
  }
  int mxR = 0, mxC = 0;
  for (int i = 1; i <= rows; ++i) {
    mxR = max(R[i], mxR);
  }
  for (int i = 1; i <= cols; ++i) {
    mxC = max(C[i], mxC);
  }

  vector<int> ff, ss;
  for (int i = 1; i <= rows; ++i) {
    if (R[i] == mxR)
      ff.push_back(i);
  }
  for (int i = 1; i <= cols; ++i) {
    if (C[i] == mxC)
      ss.push_back(i);
  }

  int ans = mxR + mxC - 1;
  bool ok = false;
  for (int i = 0; i < ff.size(); ++i) {
    for (int j = 0; j < ss.size(); ++j) {
      if (st.find(hashFunction(ff[i], ss[j])) != st.end()) {
        continue;
      } else {
        ans += 1;
        ok = true;
        break;
      }
    }
    if (ok)
      break;
  }
  cout << ans;
}

int main() {
  IOS int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define endl "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const int mod = 1e9 + 7;
const int MAX = 3e5 + 1;

vector<int> adj[MAX];
int R[MAX], C[MAX];

void solve() {
  int rows, cols, targets;
  cin >> rows >> cols >> targets;
  int h, w;
  for (int i = 0; i < targets; ++i) {
    cin >> h >> w;
    R[h] += 1;
    C[w] += 1;
    adj[h].push_back(w);
  }
  int mxR = 0, mxC = 0;
  for (int i = 1; i <= rows; ++i) {
    mxR = max(R[i], mxR);
  }
  for (int i = 1; i <= cols; ++i) {
    mxC = max(C[i], mxC);
  }

  vector<int> ff, ss;
  for (int i = 1; i <= rows; ++i) {
    if (R[i] == mxR)
      ff.push_back(i);
  }
  for (int i = 1; i <= cols; ++i) {
    if (C[i] == mxC)
      ss.push_back(i);
  }

  int ans = mxR + mxC - 1;
  bool ok = false;
  for (int i = 0; i < ff.size(); ++i) {
    for (int j = 0; j < ss.size(); ++j) {
      ok = false;
      for (int k = 0; k < adj[ff[i]].size(); ++k) {
        if (adj[ff[i]][k] == ss[j]) {
          ok = true;
          break;
        }
      }
      if (!ok) {
        ans += 1;
        break;
      }
    }
    if (!ok)
      break;
  }
  cout << ans;
}

int main() {
  IOS int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
const double pi = 3.14159265358979323846;
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;
  vector<int> BH(H + 1, 0), BW(W + 1, 0);
  map<P, int> b;
  rep(i, M) {
    int h, w;
    cin >> h >> w;
    BH[h]++;
    BW[w]++;
    P a = make_pair(h, w);
    b[a]++;
  }
  int maxh = max_element(BH.begin(), BH.end()) - BH.begin();
  int maxw = max_element(BW.begin(), BW.end()) - BW.begin();
  vector<int> bh, bw;
  REP(i, H) {
    if (BH[maxh] == BH[i]) {
      bh.push_back(i);
    }
  }
  REP(i, W) {
    if (BW[maxw] == BW[i]) {
      bw.push_back(i);
    }
  }
  for (int &e : bh) {
    for (int &el : bw) {
      if (b[make_pair(e, el)] == 0) {
        cout << BH[maxh] + BW[maxw] << endl;
        return 0;
      }
    }
  }

  cout << BH[maxh] + BW[maxw] - b[make_pair(maxh, maxw)] << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define i(x) cin >> x
#define fr(n, x) for (ll n = 0; n < x; n++)
#define pf(x) cout << x << ' '
const ll mx = 1e5 + 4;
const ll mod = 1e9 + 7;
#pragma GCC optimize("O3")

void solv() {
  ll x, y, z;
  cin >> x >> y >> z;
  ll grid[x + 1][y + 1];
  memset(grid, 0, sizeof(grid));
  ll rows[x + 1], cols[y + 1];
  memset(rows, 0, sizeof(rows));
  memset(cols, 0, sizeof(cols));
  fr(n, z) {
    ll a, b;
    cin >> a >> b;
    rows[a]++;
    cols[b]++;
    grid[a][b] = 1;
  }
  ll res = 0;
  for (ll n = 1; n <= x; n++) {
    for (ll k = 1; k <= y; k++) {
      ll ct = rows[n] + cols[k] - (grid[n][k]);
      res = max(res, ct);
    }
  }
  cout << res;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solv();
  }
}