prithivMLmods/Coder-Stat huggingface dataset:
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

int main(void) {
  int h, w;

  cin >> h >> w;

  ll m;
  cin >> m;

  map<ll, ll> r, c;
  map<P, bool> flag;
  ll r_max = 0, c_max = 0;
  ll ri, ci;
  vector<int> a(m), b(m);

  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    flag[{a[i], b[i]}] = true;
    r[a[i]]++;
    r_max = max(r_max, r[a[i]]);
    c[b[i]]++;
    c_max = max(c_max, c[b[i]]);
  }

  set<ll> x, y;

  for (int i = 1; i <= h; i++) {
    if (r[i] == r_max) {
      x.insert(i);
    }
  }

  for (int i = 1; i <= w; i++) {
    if (c[i] == c_max) {
      y.insert(i);
    }
  }

  bool z = false;

  for (auto &&i : x) {
    for (auto &&j : y) {
      if (flag[{i, j}] == false) {
        z = true;
      }
    }
  }

  if (z) {
    cout << r_max + c_max << endl;
  } else {
    cout << r_max + c_max - 1 << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

int main(void) {
  int h, w;

  cin >> h >> w;

  ll m;
  cin >> m;

  map<ll, ll> r, c;
  map<P, bool> flag;
  ll r_max = 0, c_max = 0;
  ll ri, ci;
  vector<int> a(m), b(m);

  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    flag[{a[i], b[i]}] = true;
    r[a[i]]++;
    r_max = max(r_max, r[a[i]]);
    c[b[i]]++;
    c_max = max(c_max, c[b[i]]);
  }

  set<ll> x, y;

  for (int i = 1; i <= h; i++) {
    if (r[i] == r_max) {
      x.insert(i);
    }
  }

  for (int i = 1; i <= w; i++) {
    if (c[i] == c_max) {
      y.insert(i);
    }
  }

  bool z = false;

  for (auto &&i : x) {
    for (auto &&j : y) {
      if (flag[{i, j}] == false) {
        z = true;
        break;
      }
    }
  }

  if (z) {
    cout << r_max + c_max << endl;
  } else {
    cout << r_max + c_max - 1 << endl;
  }
}
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

const int maxn = 1e5 + 10;

int x, y, r[maxn], c[maxn];
vector<int> rr, cc;
set<pair<int, int>> st;
void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  int mr = 0, mc = 0;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    st.insert(make_pair(x, y));
    r[x]++;
    c[y]++;
    if (mr < r[x]) {
      mr = r[x];
      rr.clear();
      rr.pb(x);
    } else if (mr == r[x]) {
      rr.pb(x);
    }
    if (mc < c[y]) {
      mc = c[y];
      cc.clear();
      cc.pb(y);
    } else if (mc == c[y]) {
      cc.pb(y);
    }
  }
  for (int i : rr)
    for (int j : cc) {
      if (st.count(make_pair(i, j)))
        continue;
      else {
        cout << mr + mc;
        return;
      }
    }
  cout << mr + mc - 1;
}
signed main() {
  solve();
  return 0;
}


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

#define pb push_back

const int maxn = 3e5 + 10;

int x, y, r[maxn], c[maxn];
vector<int> rr, cc;
set<pair<int, int>> st;
void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  int mr = 0, mc = 0;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    st.insert(make_pair(x, y));
    r[x]++;
    c[y]++;
    if (mr < r[x]) {
      mr = r[x];
      rr.clear();
      rr.pb(x);
    } else if (mr == r[x]) {
      rr.pb(x);
    }
    if (mc < c[y]) {
      mc = c[y];
      cc.clear();
      cc.pb(y);
    } else if (mc == c[y]) {
      cc.pb(y);
    }
  }
  for (int i : rr)
    for (int j : cc) {
      if (st.count(make_pair(i, j)))
        continue;
      else {
        cout << mr + mc;
        return;
      }
    }
  cout << mr + mc - 1;
}
signed main() {
  solve();
  return 0;
}
Problem p02580 (C++)

Buggy submission (status: 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(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;
}

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(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;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
#define all(a) a.begin(), a.end()
using ll = long long;
const int INF = 1 << 30;
const ll INFll = 1LL << 62;
const int mod = int(1e9) + 7;
using P = pair<ll, ll>;
using ld = long double;

int main() {
  int H, W, m;
  cin >> H >> W >> m;
  int mxh = 0, mxw = 0;
  ll MX = 300010;
  vector<int> ch(MX, 0);
  vector<int> cw(MX, 0);
  map<P, bool> mp;
  for (int i = 0; i < m; ++i) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    ch[h]++;
    cw[w]++;
    mxh = max(mxh, ch[h]);
    mxw = max(mxw, cw[w]);
    mp[{h, w}] = true;
  }
  vector<int> x;
  vector<int> y;
  for (int i = 0; i <= H; ++i) {
    if (mxh == ch[i])
      x.push_back(i);
  }
  for (int j = 0; j <= W; ++j) {
    if (mxw == cw[j])
      y.push_back(j);
  }
  ll ans = mxh + mxw;
  bool exist = false;
  for (auto i : x) {
    for (auto j : y) {
      if (!mp[{i, j}])
        exist = true;
    }
  }
  if (!exist)
    ans--;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define all(a) a.begin(), a.end()
using ll = long long;
const int INF = 1 << 30;
const ll INFll = 1LL << 62;
const int mod = int(1e9) + 7;
using P = pair<ll, ll>;
using ld = long double;

int main() {
  int H, W, m;
  cin >> H >> W >> m;
  int mxh = 0, mxw = 0;
  ll MX = 300010;
  vector<int> ch(MX, 0);
  vector<int> cw(MX, 0);
  map<P, bool> mp;
  for (int i = 0; i < m; ++i) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    ch[h]++;
    cw[w]++;
    mxh = max(mxh, ch[h]);
    mxw = max(mxw, cw[w]);
    mp[{h, w}] = true;
  }
  vector<int> x;
  vector<int> y;
  for (int i = 0; i <= H; ++i) {
    if (mxh == ch[i])
      x.push_back(i);
  }
  for (int j = 0; j <= W; ++j) {
    if (mxw == cw[j])
      y.push_back(j);
  }
  ll ans = mxh + mxw;
  bool exist = false;
  ll k = x.size() * y.size(); // 交点の数
  if (k > m) {
    cout << ans << endl;
    return 0;
  }
  for (auto i : x) {
    for (auto j : y) {
      if (!mp[{i, j}])
        exist = true;
    }
  }
  if (!exist)
    ans--;
  cout << ans << endl;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
#define int long long
map<int, int> row, col;
vector<int> a, b;
map<pair<int, int>, int> mp;
signed main() {
  int n, m, k;
  cin >> n >> m >> k;
  int maxn_row = -1, maxn_col = -1;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    row[x]++, col[y]++;
    maxn_row = max(maxn_row, row[x]);
    maxn_col = max(maxn_col, col[y]);
    mp[make_pair(x, y)] = 1;
  }
  for (int i = 1; i <= n; i++)
    if (maxn_row == row[i])
      a.push_back(i);
  for (int i = 1; i <= m; i++)
    if (maxn_col == col[i])
      b.push_back(i);
  int maxn = 0;
  for (int i = 0; i < a.size(); i++)
    for (int j = 0; j < b.size(); j++)
      maxn = max(maxn, row[a[i]] + col[b[j]] - mp[make_pair(a[i], b[j])]);
  cout << maxn;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define int long long
map<int, int> row, col;
vector<int> a, b;
map<pair<int, int>, int> mp;
signed main() {
  int n, m, k;
  cin >> n >> m >> k;
  int maxn_row = -1, maxn_col = -1;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    row[x]++, col[y]++;
    maxn_row = max(maxn_row, row[x]);
    maxn_col = max(maxn_col, col[y]);
    mp[make_pair(x, y)] = 1;
  }
  for (int i = 1; i <= n; i++)
    if (maxn_row == row[i])
      a.push_back(i);
  for (int i = 1; i <= m; i++)
    if (maxn_col == col[i])
      b.push_back(i);
  int maxn = 0;
  sort(a.begin(), a.end(), greater<int>()),
      sort(b.begin(), b.end(), greater<int>());

  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < b.size(); j++) {
      if (!mp[make_pair(a[i], b[j])]) {
        cout << max(maxn, row[a[i]] + col[b[j]]);
        return 0;
      } else {
        maxn = max(maxn, row[a[i]] + col[b[j]] - 1);
      }
    }
  }
  cout << maxn;
  return 0;
}
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int row[30020];
int maxi[30020];
int col[30020];
vector<pii> cols;
map<pii, int> m;
int main() {
  int H, W, M, ans = 0;
  scanf("%d%d%d", &H, &W, &M);
  for (int i = 0, x, y; i < M; i++) {
    scanf("%d%d", &x, &y);
    m[{x, y}] = 1;
    row[x]++;
    col[y]++;
  }
  for (int i = 1; i <= W; i++) {
    cols.push_back({col[i], i});
  }
  sort(cols.begin(), cols.end(), greater<pii>());
  for (int i = 1; i <= H; i++) {
    for (int j = 0; j < W; j++) {
      if (!m.count({i, cols[j].second})) {
        maxi[i] = max(maxi[i], cols[j].first + row[i]);
        break;
      } else {
        maxi[i] = max(maxi[i], cols[j].first + row[i] - 1);
      }
    }
    ans = max(ans, maxi[i]);
  }
  printf("%d", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int row[300020];
int maxi[300020];
int col[300020];
vector<pii> cols;
map<pii, int> m;
int main() {
  int H, W, M, ans = 0;
  scanf("%d%d%d", &H, &W, &M);
  for (int i = 0, x, y; i < M; i++) {
    scanf("%d%d", &x, &y);
    m[{x, y}] = 1;
    row[x]++;
    col[y]++;
  }
  for (int i = 1; i <= W; i++) {
    cols.push_back({col[i], i});
  }
  sort(cols.begin(), cols.end(), greater<pii>());
  for (int i = 1; i <= H; i++) {
    for (int j = 0; j < W; j++) {
      if (!m.count({i, cols[j].second})) {
        maxi[i] = max(maxi[i], cols[j].first + row[i]);
        break;
      } else {
        maxi[i] = max(maxi[i], cols[j].first + row[i] - 1);
      }
    }
    ans = max(ans, maxi[i]);
  }
  printf("%d", ans);
}
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\n'
typedef pair<int, int> P;
typedef long long ll;
set<P> Check;
int H[30005], W[30005];
int main() {
  // cin.tie(0);
  // ios_base::sync_with_stdio(false);
  int h, w, m;
  cin >> h >> w;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    Check.insert(make_pair(x, y));
    H[x]++;
    W[y]++;
  }
  int hmax = 0, wmax = 0;
  for (int x = 1; x <= h; x++) {
    if (H[x] > hmax)
      hmax = H[x];
  }
  for (int y = 1; y <= w; y++) {
    if (W[y] > wmax)
      wmax = W[y];
  }
  // cout << "max " << hmax <<  " " << wmax <<endl;
  int hnum = 0, wnum = 0;
  vector<int> hval, wval;
  for (int x = 1; x <= h; x++) {
    if (H[x] == hmax) {
      hval.push_back(x);
      hnum++;
      // cout << x << " ";
    }
  }
  // cout <<endl;
  for (int y = 1; y <= w; y++) {
    if (W[y] == wmax) {
      wval.push_back(y);
      wnum++;
      // cout << y << " ";
    }
  }
  // cout << endl;
  // cout << "cal" << endl;
  for (int i = 0; i < hnum; i++) {
    for (int j = 0; j < wnum; j++) {
      // cout <<hval[i] << " " << wval[j] <<endl;
      if (Check.count(make_pair(hval[i], wval[j])) == 0) {
        cout << hmax + wmax;
        // cout << "hello";
        return 0;
      }
    }
  }
  cout << hmax + wmax - 1;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\n'
typedef pair<int, int> P;
typedef long long ll;
set<P> Check;
int H[300005], W[300005];
int main() {
  // cin.tie(0);
  // ios_base::sync_with_stdio(false);
  int h, w, m;
  cin >> h >> w;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    Check.insert(make_pair(x, y));
    H[x]++;
    W[y]++;
  }
  int hmax = 0, wmax = 0;
  for (int x = 1; x <= h; x++) {
    if (H[x] > hmax)
      hmax = H[x];
  }
  for (int y = 1; y <= w; y++) {
    if (W[y] > wmax)
      wmax = W[y];
  }
  // cout << "max " << hmax <<  " " << wmax <<endl;
  int hnum = 0, wnum = 0;
  vector<int> hval, wval;
  for (int x = 1; x <= h; x++) {
    if (H[x] == hmax) {
      hval.push_back(x);
      hnum++;
      // cout << x << " ";
    }
  }
  // cout <<endl;
  for (int y = 1; y <= w; y++) {
    if (W[y] == wmax) {
      wval.push_back(y);
      wnum++;
      // cout << y << " ";
    }
  }
  // cout << endl;
  // cout << "cal" << endl;
  for (int i = 0; i < hnum; i++) {
    for (int j = 0; j < wnum; j++) {
      // cout <<hval[i] << " " << wval[j] <<endl;
      if (Check.count(make_pair(hval[i], wval[j])) == 0) {
        cout << hmax + wmax;
        // cout << "hello";
        return 0;
      }
    }
  }
  cout << hmax + wmax - 1;
  return 0;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

bool bad(const pair<int, int> &p1, const pair<int, int> &p2) {
  return p1.first > p2.first;
}

void solve() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> row(H, 0);
  vector<int> col(W, 0);
  set<pair<int, int>> st;
  for (int i = 0; i < M; ++i) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    row[h]++;
    col[w]++;
    st.insert({h, w});
  }
  set<pair<int, int>, greater<pair<int, int>>> st1;
  set<pair<int, int>, greater<pair<int, int>>> st2;
  for (int i = 0; i < H; ++i) {
    if (row[i] == 0)
      continue;
    st1.emplace(row[i], i);
  }
  for (int i = 0; i < W; ++i) {
    if (col[i] == 0)
      continue;
    st2.emplace(col[i], i);
  }

  int ans = 0;
  for (auto it1 = st1.begin(); it1 != st1.end(); ++it1) {
    if (bad(*st1.begin(), *it1))
      break;
    for (auto it2 = st2.begin(); it2 != st2.end(); ++it2) {
      if (bad(*st2.begin(), *it2))
        break;
      int temp = it1->first + it2->first;
      if (st.count({it1->second, it2->second})) {
        temp--;
      }
      ans = max(ans, temp);
    }
  }
  cout << ans << endl;
}

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

  int t = 1;
  while (t--) {
    solve();
  }
}


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

bool bad(const pair<int, int> &p1, const pair<int, int> &p2) {
  return p1.first > p2.first;
}

void solve() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> row(H, 0);
  vector<int> col(W, 0);
  set<pair<int, int>> st;
  for (int i = 0; i < M; ++i) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    row[h]++;
    col[w]++;
    st.insert({h, w});
  }
  set<pair<int, int>, greater<pair<int, int>>> st1;
  set<pair<int, int>, greater<pair<int, int>>> st2;
  for (int i = 0; i < H; ++i) {
    if (row[i] == 0)
      continue;
    st1.emplace(row[i], i);
  }
  for (int i = 0; i < W; ++i) {
    if (col[i] == 0)
      continue;
    st2.emplace(col[i], i);
  }

  int ans = 0;
  for (auto it1 = st1.begin(); it1 != st1.end(); ++it1) {
    if (bad(*st1.begin(), *it1))
      break;
    for (auto it2 = st2.begin(); it2 != st2.end(); ++it2) {
      if (bad(*st2.begin(), *it2))
        break;
      int temp = it1->first + it2->first;
      if (st.count({it1->second, it2->second})) {
        temp--;
      }
      if (ans > temp) {
        cout << ans << endl;
        return;
      }
      ans = max(ans, temp);
    }
  }
  cout << ans << endl;
}

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

  int t = 1;
  while (t--) {
    solve();
  }
}
Problem p02580 (C++)

Buggy submission (status: 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}]) {
        ans = PH[0].fi + PW[0].fi;
      }
    }
  }
  cout << ans << endl;

  return 0;
}


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}]) {
        cout << ans + 1 << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02580 (C++)

Buggy submission (status: 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;
    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;
}

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;
    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 (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<pair<ll, ll>>>;

// MOD確認

ll H, W, M;

pair<ll, ll> Y[300010];
pair<ll, ll> X[300010];

int main() {
  cin >> H >> W >> M;
  map<pair<ll, ll>, ll> mp;
  rep(i, 300010) {
    Y[i].second = i;
    X[i].second = i;
  }
  rep(i, M) {
    ll h, w;
    cin >> h >> w;
    h--;
    w--;
    mp[make_pair(h, w)]++;
    Y[h].first++;
    X[w].first++;
  }
  ll ans = 0;
  sort(X, X + 300010);
  sort(Y, Y + 300010);
  reverse(X, X + 300010);
  reverse(Y, Y + 300010);
  rep(i, 100) rep(j, M) {
    ll num = Y[i].first + X[j].first;
    if (mp[make_pair(Y[i].second, X[j].second)] >= 1) {
      num--;
    }
    ans = max(ans, num);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<pair<ll, ll>>>;

// MOD確認

ll H, W, M;

pair<ll, ll> Y[300010];
pair<ll, ll> X[300010];

int main() {
  cin >> H >> W >> M;
  map<pair<ll, ll>, ll> mp;
  rep(i, 300010) {
    Y[i].second = i;
    X[i].second = i;
  }
  rep(i, M) {
    ll h, w;
    cin >> h >> w;
    h--;
    w--;
    mp[make_pair(h, w)]++;
    Y[h].first++;
    X[w].first++;
  }
  ll ans = 0;
  sort(X, X + 300010);
  sort(Y, Y + 300010);
  reverse(X, X + 300010);
  reverse(Y, Y + 300010);
  rep(i, 10) rep(j, M) {
    ll num = Y[i].first + X[j].first;
    if (mp[make_pair(Y[i].second, X[j].second)] >= 1) {
      num--;
    }
    ans = max(ans, num);
  }
  rep(i, M) rep(j, 10) {
    ll num = Y[i].first + X[j].first;
    if (mp[make_pair(Y[i].second, X[j].second)] >= 1) {
      num--;
    }
    ans = max(ans, num);
  }
  cout << ans << endl;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// 定数
#define INF 1000000000 // 10^9:極めて大きい値,∞
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

int h, w, m;

int main() {
  cin >> h >> w >> m;
  vector<int> yv(h), xv(w);
  int x, y;
  set<pair<int, int>> s;
  rep(i, m) {
    cin >> y >> x;
    y--;
    x--;
    s.insert(make_pair(y, x));
    yv[y]++;
    xv[x]++;
  }
  int ymax = 0;
  rep(i, h) { ymax = max(ymax, yv[i]); }
  ll ans = ymax;
  int xmax = 0;
  rep(i, w) { xmax = max(xmax, xv[i]); }
  ans += xmax;
  rep(i, h) {
    if (yv[i] == ymax) {
      rep(j, w) {
        if (xv[j] == xmax) {
          if (s.count(make_pair(i, j)) == 0) {
            cout << ans << endl;
            return 0;
          }
        }
      }
    }
  }
  cout << ans - 1 << endl;
  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// 定数
#define INF 1000000000 // 10^9:極めて大きい値,∞
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

int h, w, m;

int main() {
  cin >> h >> w >> m;
  vector<int> yv(h), xv(w);
  int x, y;
  set<pair<int, int>> s;
  rep(i, m) {
    cin >> y >> x;
    y--;
    x--;
    s.insert(make_pair(y, x));
    yv[y]++;
    xv[x]++;
  }
  int ymax = *max_element(yv.begin(), yv.end());
  int xmax = *max_element(xv.begin(), xv.end());
  ll ans = xmax + ymax;
  vector<int> yvv, xvv;

  rep(i, h) if (yv[i] == ymax) yvv.push_back(i);
  rep(i, w) if (xv[i] == xmax) xvv.push_back(i);

  for (int y : yvv) {
    for (int x : xvv) {
      if (s.count(make_pair(y, x)) == 0) {
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans - 1 << endl;
  return 0;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define FASTIO                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
using ll = long long;

int main() {
  FASTIO
  int h, w;
  cin >> h >> w;
  int m;
  cin >> m;
  vector<int> horizontal(w + 1), vertical(h + 1);
  unordered_map<int, unordered_map<int, bool>> mp;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    mp[a][b] = 1;
    horizontal[b]++;
    vertical[a]++;
  }
  int mx = 0;
  vector<int> u, v;
  mx = *max_element(vertical.begin(), vertical.end());
  for (int i = 0; i <= h; i++) {
    if (mx == vertical[i]) {
      u.push_back(i);
    }
  }
  mx = *max_element(horizontal.begin(), horizontal.end());
  for (int i = 0; i <= w; i++) {
    if (mx == horizontal[i]) {
      v.push_back(i);
    }
  }
  for (int i = 0; i < u.size(); i++) {
    for (int j = 0; j < v.size(); j++) {
      mx = max(mx, horizontal[v[j]] + vertical[u[i]] - mp[u[i]][v[j]]);
    }
  }
  cout << mx << endl;
}


Fixed submission:
#include <bits/stdc++.h>

#define FASTIO                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
using ll = long long;

int main() {
  FASTIO
  int h, w;
  cin >> h >> w;
  int m;
  cin >> m;
  vector<int> horizontal(w + 1), vertical(h + 1);
  unordered_map<int, unordered_map<int, bool>> mp;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    mp[a][b] = 1;
    horizontal[b]++;
    vertical[a]++;
  }
  int mx = 0;
  vector<int> u, v;
  mx = *max_element(vertical.begin(), vertical.end());
  for (int i = 0; i <= h; i++) {
    if (mx == vertical[i]) {
      u.push_back(i);
    }
  }
  mx = *max_element(horizontal.begin(), horizontal.end());
  for (int i = 0; i <= w; i++) {
    if (mx == horizontal[i]) {
      v.push_back(i);
    }
  }
  for (int i = 0; i < u.size(); i++) {
    for (int j = 0; j < v.size(); j++) {
      mx = max(mx, horizontal[v[j]] + vertical[u[i]] - mp[u[i]][v[j]]);
      if (!mp[u[i]][v[j]])
        return cout << mx << endl, 0;
    }
  }
  cout << mx << endl;
}
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
#define N 200200
pair<int, int> v[N];
pair<int, int> l[N], c[N];
int i, x, y, ml, mc, h, w, n, ii, jj, j;
long long cntc, cntl, ans;
map<pair<int, int>, bool> sel;
int main() {
  cin >> h >> w >> n;
  for (i = 0; i < n; ++i) {
    cin >> v[i].first >> v[i].second;
    sel[v[i]] = 1;
  }
  for (i = 0; i < h; ++i)
    l[i + 1].second = i + 1;
  for (i = 0; i < w; ++i)
    c[i + 1].second = i + 1;

  for (i = 0; i < n; ++i) {
    ++l[v[i].first].first;
    ++c[v[i].second].first;
  }
  sort(l + 1, l + h + 1);
  sort(c + 1, c + w + 1);
  i = h;
  j = w;
  ans = l[i].first + c[j].first;
  cntl = 0;
  cntc = 0;
  while (i > 0 && l[i].first == l[h].first) {
    --i;
    ++cntl;
  }
  while (j > 0 && c[j].first == c[w].first) {
    --j;
    ++cntc;
  }
  if (cntc * cntl > n) {
    cout << ans;
    return 0;
  } else {
    for (ii = 0; ii < cntl; ++ii)
      for (jj = 0; jj < cntc; ++jj) {
        if (!sel[make_pair(l[h - ii].second, c[w - jj].second)]) {
          cout << ans;
          return 0;
        }
      }
  }
  cout << ans - 1;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define N 300300
pair<int, int> v[N];
pair<int, int> l[N], c[N];
int i, x, y, ml, mc, h, w, n, ii, jj, j;
long long cntc, cntl, ans;
map<pair<int, int>, bool> sel;
int main() {
  cin >> h >> w >> n;
  for (i = 0; i < n; ++i) {
    cin >> v[i].first >> v[i].second;
    sel[v[i]] = 1;
  }
  for (i = 0; i < h; ++i)
    l[i + 1].second = i + 1;
  for (i = 0; i < w; ++i)
    c[i + 1].second = i + 1;

  for (i = 0; i < n; ++i) {
    ++l[v[i].first].first;
    ++c[v[i].second].first;
  }
  sort(l + 1, l + h + 1);
  sort(c + 1, c + w + 1);
  i = h;
  j = w;
  ans = l[i].first + c[j].first;
  cntl = 0;
  cntc = 0;
  while (i > 0 && l[i].first == l[h].first) {
    --i;
    ++cntl;
  }
  while (j > 0 && c[j].first == c[w].first) {
    --j;
    ++cntc;
  }
  if (cntc * cntl > n) {
    cout << ans;
    return 0;
  } else {
    for (ii = 0; ii < cntl; ++ii)
      for (jj = 0; jj < cntc; ++jj) {
        if (!sel[make_pair(l[h - ii].second, c[w - jj].second)]) {
          cout << ans;
          return 0;
        }
      }
  }
  cout << ans - 1;
  return 0;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ar array
#define db double
#define pow pw
const db pi = 3.14159265358979323846;
int pw(int a, int b) {
  int ans = 1;
  while (b) {
    if (b % 2)
      ans *= a;
    a *= a;
    b /= 2;
  }
  return (ans);
}
const int mxn = 3e5 + 7, INF = 1e9 + 7;
int m, n, k;
int r[mxn], c[mxn];
map<pair<int, int>, int> a;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> m >> n >> k;
  for (int i = 0; i < k; i++) {
    int u, v;
    cin >> u >> v;
    r[u]++;
    c[v]++;
    a[{u, v}] = 1;
  }
  int t1 = 0, t2 = 0, ans = 0;
  vector<int> d, d2;
  for (int i = 1; i <= m; i++)
    t1 = max(t1, r[i]);
  for (int i = 1; i <= m; i++)
    if (r[i] == t1)
      d.push_back(i);
  for (int i = 1; i <= n; i++)
    t2 = max(t2, c[i]);
  for (int i = 1; i <= n; i++)
    if (c[i] == t2)
      d2.push_back(i);
  for (int g : d)
    for (int h : d2) {
      ans = max(ans, r[g] + c[h] - a[{g, h}]);
    }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ar array
#define db double
#define pow pw
const db pi = 3.14159265358979323846;
int pw(int a, int b) {
  int ans = 1;
  while (b) {
    if (b % 2)
      ans *= a;
    a *= a;
    b /= 2;
  }
  return (ans);
}
const int mxn = 3e5 + 7, INF = 1e9 + 7;
int m, n, k;
int r[mxn], c[mxn];
map<pair<int, int>, int> a;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> m >> n >> k;
  for (int i = 0; i < k; i++) {
    int u, v;
    cin >> u >> v;
    r[u]++;
    c[v]++;
    a[{u, v}] = 1;
  }
  int t1 = 0, t2 = 0, ans = 0;
  vector<int> d, d2;
  for (int i = 1; i <= m; i++)
    t1 = max(t1, r[i]);
  for (int i = 1; i <= m; i++)
    if (r[i] == t1)
      d.push_back(i);
  for (int i = 1; i <= n; i++)
    t2 = max(t2, c[i]);
  for (int i = 1; i <= n; i++)
    if (c[i] == t2)
      d2.push_back(i);
  for (int g : d)
    for (int h : d2) {
      if (a[{g, h}] == 0) {
        ans = max(ans, r[g] + c[h] - a[{g, h}]);
        cout << ans;
        return 0;
      } else
        ans = max(ans, r[g] + c[h] - a[{g, h}]);
    }
  cout << ans;
  return 0;
}
Problem p02580 (C++)

Buggy submission (status: 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) {
      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;
}

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) {
      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;
}
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  const int MAX = 30000;
  int H, W, M;
  cin >> H >> W >> M;
  int h[MAX], w[MAX] = {0};
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
  }

  // Create unordered map to reference the existence of a bomb later
  // The key cannot be pair as long as I don't implement a has function for pair
  unordered_map<string, int> map;

  int rows[MAX], cols[MAX] = {0};

  int row_best_count = 0;
  int col_best_count = 0;

  for (int i = 0; i < M; i++) {
    string key = to_string(h[i]) + ',' + to_string(w[i]);
    map[key] = 1;

    rows[h[i]]++;
    cols[w[i]]++;

    row_best_count = max(row_best_count, rows[h[i]]);
    col_best_count = max(col_best_count, cols[w[i]]);
  }

  vector<int> best_rows;
  vector<int> best_cols;

  for (int i = 0; i < H; i++) {
    if (rows[i] == row_best_count) {
      best_rows.push_back(i);
    }
  }

  for (int i = 0; i < W; i++) {
    if (cols[i] == col_best_count) {
      best_cols.push_back(i);
    }
  }

  int ans = row_best_count + col_best_count - 1;
  for (int r : best_rows) {
    bool break_signal = false;
    for (int c : best_cols) {
      string key = to_string(r) + ',' + to_string(c);
      if (!map[key]) {
        ans = row_best_count + col_best_count;
        break_signal = true;
        break;
      }
    }

    if (break_signal)
      break;
  }

  cout << ans << endl;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  const int MAX = 300000;
  int H, W, M;
  cin >> H >> W >> M;
  int h[MAX], w[MAX] = {0};
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
  }

  // Create unordered map to reference the existence of a bomb later
  // The key cannot be pair as long as I don't implement a has function for pair
  unordered_map<string, int> map;

  int rows[MAX], cols[MAX] = {0};

  int row_best_count = 0;
  int col_best_count = 0;

  for (int i = 0; i < M; i++) {
    string key = to_string(h[i]) + ',' + to_string(w[i]);
    map[key] = 1;

    rows[h[i]]++;
    cols[w[i]]++;

    row_best_count = max(row_best_count, rows[h[i]]);
    col_best_count = max(col_best_count, cols[w[i]]);
  }

  vector<int> best_rows;
  vector<int> best_cols;

  for (int i = 0; i < H; i++) {
    if (rows[i] == row_best_count) {
      best_rows.push_back(i);
    }
  }

  for (int i = 0; i < W; i++) {
    if (cols[i] == col_best_count) {
      best_cols.push_back(i);
    }
  }

  int ans = row_best_count + col_best_count - 1;
  for (int r : best_rows) {
    bool break_signal = false;
    for (int c : best_cols) {
      string key = to_string(r) + ',' + to_string(c);
      if (!map[key]) {
        ans = row_best_count + col_best_count;
        break_signal = true;
        break;
      }
    }

    if (break_signal)
      break;
  }

  cout << ans << endl;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007

int main() {

  int H, W, M;
  cin >> H >> W >> M;

  vector<int> g(H), r(W);
  map<pair<int, int>, int> mp;
  int gm = 0, rm = 0;

  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    g[h - 1]++;
    r[w - 1]++;
    mp[make_pair(h - 1, w - 1)]++;
    gm = max(gm, g[h - 1]);
    rm = max(rm, r[w - 1]);
  }

  vector<int> cg(0), cr(0);
  for (int i = 0; i < H; i++) {
    if (g[i] == gm)
      cg.push_back(i);
  }

  for (int i = 0; i < W; i++) {
    if (r[i] == rm)
      cr.push_back(i);
  }

  int ans = 0;
  for (int G : cg) {
    for (int R : cr) {
      ans = max(ans, gm + rm - mp[make_pair(G, R)]);
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007

int main() {

  int H, W, M;
  cin >> H >> W >> M;

  vector<int> g(H), r(W);
  map<pair<int, int>, int> mp;
  int gm = 0, rm = 0;

  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    g[h - 1]++;
    r[w - 1]++;
    mp[make_pair(h - 1, w - 1)]++;
    gm = max(gm, g[h - 1]);
    rm = max(rm, r[w - 1]);
  }

  vector<int> cg(0), cr(0);
  for (int i = 0; i < H; i++) {
    if (g[i] == gm)
      cg.push_back(i);
  }

  for (int i = 0; i < W; i++) {
    if (r[i] == rm)
      cr.push_back(i);
  }

  int ans = 0;
  for (int G : cg) {
    for (int R : cr) {
      ans = max(ans, gm + rm - mp[make_pair(G, R)]);
      if (!mp[make_pair(G, R)])
        break;
    }
  }

  cout << ans << endl;
}
Problem p02580 (C++)

Buggy submission (status: 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 (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;
}

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 (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;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int h, w, m;
typedef pair<int, int> P;
// 行与列
unordered_map<int, int> m1;
unordered_map<int, int> m2;
map<P, int> c;
int main() {
  vector<int> col, row;
  int ans = 0;
  int max1 = -1, max2 = -1;
  int index1, index2;
  cin >> h >> w >> m;

  int x, y = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    if (!m1.count(x)) {
      m1[x] = 1;
    } else {
      m1[x] += 1;
    }
    if (!m2.count(y)) {
      m2[y] = 1;
    } else {
      m2[y] += 1;
    }
    max1 = max(max1, m1[x]);
    max2 = max(max2, m2[y]);
    c[make_pair(x, y)] = 1;
  }
  unordered_map<int, int>::iterator iter;
  for (iter = m1.begin(); iter != m1.end(); iter++) {
    if (iter->second == max1)
      row.push_back(iter->first);
  }
  for (iter = m2.begin(); iter != m2.end(); iter++) {
    if (iter->second == max2)
      col.push_back(iter->first);
  }
  // for(int i = 0;i<row.size();i++){
  //     cout << row[i] << " ";
  // }
  // cout << endl;
  for (int i = 0; i < row.size(); i++) {
    for (int j = 0; j < col.size(); j++) {
      int num;
      if (c[make_pair(row[i], col[j])] == 1)
        num = max1 + max2 - 1;
      else
        num = max1 + max2;
      ans = max(ans, num);
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int h, w, m;
typedef pair<int, int> P;
// 行与列
unordered_map<int, int> m1;
unordered_map<int, int> m2;
map<P, int> c;
int main() {
  vector<int> col, row;
  int ans = 0;
  int max1 = -1, max2 = -1;
  int index1, index2;
  cin >> h >> w >> m;

  int x, y = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    if (!m1.count(x)) {
      m1[x] = 1;
    } else {
      m1[x] += 1;
    }
    if (!m2.count(y)) {
      m2[y] = 1;
    } else {
      m2[y] += 1;
    }
    max1 = max(max1, m1[x]);
    max2 = max(max2, m2[y]);
    c[make_pair(x, y)] = 1;
  }
  unordered_map<int, int>::iterator iter;
  for (iter = m1.begin(); iter != m1.end(); iter++) {
    if (iter->second == max1)
      row.push_back(iter->first);
  }
  for (iter = m2.begin(); iter != m2.end(); iter++) {
    if (iter->second == max2)
      col.push_back(iter->first);
  }
  // for(int i = 0;i<row.size();i++){
  //     cout << row[i] << " ";
  // }
  // cout << endl;
  for (int i = 0; i < row.size(); i++) {
    for (int j = 0; j < col.size(); j++) {
      int num;
      if (c[make_pair(row[i], col[j])] == 1)
        num = max1 + max2 - 1;
      else
        num = max1 + max2;
      ans = max(ans, num);
      if (ans == max1 + max2)
        break;
    }
  }
  cout << ans << endl;

  return 0;
}