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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll hor[300005], ver[300005];

int main() {

  ll n, m, t, res = 0;
  cin >> n >> m >> t;
  set<pair<ll, ll>> s;
  set<ll> px, py;
  while (t--) {
    ll x, y;
    cin >> x >> y;
    hor[x]++;
    ver[y]++;
    s.insert({x, y});
  }

  ll mx = *max_element(hor, hor + 300005);
  ll my = *max_element(ver, ver + 300005);

  for (ll i = 1; i <= 300005; i++) {
    if (hor[i] == mx)
      px.insert(i);
    if (ver[i] == my)
      py.insert(i);
  }

  for (auto i : px) {
    for (auto j : py) {
      ll tmp = hor[i] + ver[j];
      if (s.find({i, j}) != s.end())
        tmp -= 1;
      res = max(res, tmp);
    }
  }

  cout << res;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll hor[300005], ver[300005];

int main() {

  ll n, m, t, res = 0;
  cin >> n >> m >> t;
  set<pair<ll, ll>> s;
  set<ll> px, py;
  while (t--) {
    ll x, y;
    cin >> x >> y;
    hor[x]++;
    ver[y]++;
    s.insert({x, y});
  }

  ll mx = *max_element(hor, hor + 300005);
  ll my = *max_element(ver, ver + 300005);

  for (ll i = 1; i <= 300005; i++) {
    if (hor[i] == mx)
      px.insert(i);
    if (ver[i] == my)
      py.insert(i);
  }

  for (auto i : px) {
    for (auto j : py) {
      ll tmp = hor[i] + ver[j];
      if (s.find({i, j}) != s.end())
        tmp -= 1;
      res = max(res, tmp);
      if (res == mx + my) {
        cout << res;
        return 0;
      }
    }
  }

  cout << res;
}
Problem p02580 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python3


def main():
    H, W, M = map(int, input().split())
    bomb = [list(map(int, input().split())) for i in range(M)]
    H_bomb = [0] * (H + 1)
    W_bomb = [0] * (W + 1)
    for b in bomb:
        H_bomb[b[0]] += 1
        W_bomb[b[1]] += 1
    ans = max(H_bomb) + max(W_bomb)
    H_bomb_index = [i for i, v in enumerate(H_bomb) if v == max(H_bomb)]
    W_bomb_index = [i for i, v in enumerate(W_bomb) if v == max(W_bomb)]
    x_point = []
    for h in H_bomb_index:
        for w in W_bomb_index:
            x_point.append([h, w])
    flag = True
    for x in x_point:
        if x not in bomb:
            flag = False
    if flag:
        ans -= 1
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3


def main():
    H, W, M = map(int, input().split())
    bomb = [list(map(int, input().split())) for i in range(M)]
    H_bomb = [0] * (H + 1)
    W_bomb = [0] * (W + 1)
    for b in bomb:
        H_bomb[b[0]] += 1
        W_bomb[b[1]] += 1
    max_h, max_w = max(H_bomb), max(W_bomb)
    cnt = H_bomb.count(max_h) * W_bomb.count(max_w)
    for b in bomb:
        if H_bomb[b[0]] == max_h and W_bomb[b[1]] == max_w:
            cnt -= 1
    print(max_h + max_w + bool(cnt) - 1)


if __name__ == "__main__":
    main()
Problem p02580 (Python)

Buggy submission (status: Runtime Error):
H, W, M = map(int, input().split())

B = [list(map(int, input().split())) for _ in range(M)]

bombs_row = [0] * H
bombs_col = [0] * W

for b in B:
    bombs_row[b[0] - 1] += 1
    bombs_col[b[1] - 1] += 1

row_max = max(bombs_row)
col_max = max(bombs_col)

row_max_count = bombs_row.count(row_max)
col_max_count = bombs_col.count(col_max)

ans = row_max + col_max

possible_point = row_max_count * col_max_count

bombed_point = 0

for b in B:
    if bombs_row[b[0]] == row_max and bombs_col[b[1]] == col_max:
        bombed_point += 1

if bombed_point == possible_point:
    ans -= 1

print(ans)


Fixed submission:
H, W, M = map(int, input().split())

B = [list(map(int, input().split())) for _ in range(M)]

bombs_row = [0] * H
bombs_col = [0] * W

for b in B:
    bombs_row[b[0] - 1] += 1
    bombs_col[b[1] - 1] += 1

row_max = max(bombs_row)
col_max = max(bombs_col)

row_max_count = bombs_row.count(row_max)
col_max_count = bombs_col.count(col_max)

ans = row_max + col_max

possible_point = row_max_count * col_max_count

bombed_point = 0

for b in B:
    if bombs_row[b[0] - 1] == row_max and bombs_col[b[1] - 1] == col_max:
        bombed_point += 1

if bombed_point == possible_point:
    ans -= 1

print(ans)
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using ll = long long;
#define FOR(i, k, n) for (ll i = (k); i < (n); i++)
#define FORe(i, k, n) for (ll i = (k); i <= (n); i++)
#define FORr(i, k, n) for (ll i = (k)-1; i > (n); i--)
#define FORre(i, k, n) for (ll i = (k)-1; i >= (n); i--)
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORre(i, n, 0)
#define ALL(x) (x).begin(), (x).end()
#define ALLr(x) (x).rbegin(), (x).rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;

const int INF = 1001001001;

using P = pair<ll, ll>;

int main(void) {
  ll H, W, M;
  cin >> H >> W >> M;
  vector<ll> h(M), w(M);
  map<P, ll> mp;
  REP(i, M) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
    mp[P(h[i], w[i])]++;
  }
  vector<ll> a(H, 0), b(W, 0);
  REP(i, M) a[h[i]]++, b[w[i]]++;

  ll c = *max_element(ALL(a));
  ll d = *max_element(ALL(b));
  vector<ll> p, q;
  REP(i, H) {
    if (a[i] == c)
      p.emplace_back(i);
    if (b[i] == d)
      q.emplace_back(i);
  }
  ll n = p.size(), m = q.size();
  ll ans = c + d - 1;
  REP(i, n) REP(j, m) {
    mp[P(p[i], q[j])];
    if (!mp[P(p[i], q[j])]) {
      ans = c + d;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
#define FOR(i, k, n) for (ll i = (k); i < (n); i++)
#define FORe(i, k, n) for (ll i = (k); i <= (n); i++)
#define FORr(i, k, n) for (ll i = (k)-1; i > (n); i--)
#define FORre(i, k, n) for (ll i = (k)-1; i >= (n); i--)
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORre(i, n, 0)
#define ALL(x) (x).begin(), (x).end()
#define ALLr(x) (x).rbegin(), (x).rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;

const int INF = 1001001001;

using P = pair<ll, ll>;

int main(void) {
  ll H, W, M;
  cin >> H >> W >> M;
  vector<ll> h(M), w(M);
  map<P, ll> mp;
  REP(i, M) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
    mp[P(h[i], w[i])]++;
  }
  vector<ll> a(H, 0), b(W, 0);
  REP(i, M) a[h[i]]++, b[w[i]]++;

  ll c = *max_element(ALL(a));
  ll d = *max_element(ALL(b));
  vector<ll> p, q;
  REP(i, H) if (a[i] == c) p.emplace_back(i);
  REP(i, W) if (b[i] == d) q.emplace_back(i);

  ll n = p.size(), m = q.size();
  ll ans = c + d - 1;
  REP(i, n) REP(j, m) {
    mp[P(p[i], q[j])];
    if (!mp[P(p[i], q[j])]) {
      ans = c + d;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main(void) {
  int H, W, M;
  cin >> H >> W >> M;
  // map<int,int> X, Y;
  // map< pair<int,int> ,int> mp;
  vector<vector<int>> X(H);
  vector<int> Y(W);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    X[x].emplace_back(y);
    Y[y]++;
    // X[x]++;
    // Y[y]++;
    // mp[make_pair(x, y)]++;
  }

  multiset<int> mst;
  for (auto e : Y) {
    mst.insert(e);
  }

  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (auto y : X[i]) {
      ans = max(ans, (int)X[i].size() + Y[y] - 1);
    }
    for (auto y : X[i]) {
      mst.erase(mst.find(Y[y]));
    }
    ans = max(ans, (int)X[i].size() + *mst.rbegin());
    for (auto y : X[i]) {
      mst.insert(Y[y]);
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main(void) {
  int H, W, M;
  cin >> H >> W >> M;
  // map<int,int> X, Y;
  // map< pair<int,int> ,int> mp;
  vector<vector<int>> X(H);
  vector<int> Y(W);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    X[x].emplace_back(y);
    Y[y]++;
    // X[x]++;
    // Y[y]++;
    // mp[make_pair(x, y)]++;
  }

  multiset<int> mst;
  for (auto e : Y) {
    mst.insert(e);
  }

  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (auto y : X[i]) {
      ans = max(ans, (int)X[i].size() + Y[y] - 1);
    }
    for (auto y : X[i]) {
      mst.erase(mst.find(Y[y]));
    }
    if (mst.size())
      ans = max(ans, (int)X[i].size() + *mst.rbegin());
    for (auto y : X[i]) {
      mst.insert(Y[y]);
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define MOD 1000000007

using namespace std;
using ll = long long;

int mp[1005][1005] = {};

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> height(h), width(w);
  set<pair<int, int>> block;
  rep(i, m) {
    int hei, wid;
    cin >> hei >> wid;
    hei--;
    wid--;
    block.emplace(hei, wid);
    height[hei]++;
    width[wid]++;
  }
  int mh = 0, mw = 0;
  rep(i, h) mh = max(mh, height[i]);
  rep(i, w) mw = max(mw, width[i]);
  int ans = mh + mw;
  rep(i, h) {
    if (height[i] != mh)
      continue;
    rep(j, w) {
      if (width[j] != mw)
        continue;
      if (block.count(pair<int, int>(i, j)))
        continue;
      cout << ans << endl;
      return 0;
    }
  }
  cout << ans - 1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define MOD 1000000007

using namespace std;
using ll = long long;

int mp[1005][1005] = {};

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> height(h), width(w);
  set<pair<int, int>> block;
  rep(i, m) {
    int hei, wid;
    cin >> hei >> wid;
    hei--;
    wid--;
    block.emplace(hei, wid);
    height[hei]++;
    width[wid]++;
  }
  int mh = 0, mw = 0;
  rep(i, h) mh = max(mh, height[i]);
  rep(i, w) mw = max(mw, width[i]);
  int ans = mh + mw;
  vector<int> tate, yoko;
  rep(i, h) if (mh == height[i]) tate.push_back(i);
  rep(i, w) if (mw == width[i]) yoko.push_back(i);
  for (int i : tate) {
    for (int j : yoko) {
      if (block.count(pair<int, int>(i, j)))
        continue;
      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 rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;

int main() {
  int H, W, m;
  cin >> H >> W >> m;

  vector<int> a(H), b(W);
  set<pair<int, int>> bomb;
  rep(i, m) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    bomb.insert({h, w});
    a[h]++;
    b[w]++;
  }

  int res1 = 0, res2 = 0;
  int ans = 0;
  rep(i, H) res1 = max(res1, a[i]);
  rep(i, W) res2 = max(res2, b[i]);
  vector<int> p, q;
  rep(i, H) if (a[i] == res1) p.pb(i);
  rep(i, W) if (b[i] == res2) q.pb(i);
  rep(i, p.size()) {
    rep(j, q.size()) {
      if (bomb.count({p[i], q[j]}))
        ans = max(ans, res1 + res2 - 1);
      else
        ans = max(ans, res1 + res2);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;

int main() {
  int H, W, m;
  cin >> H >> W >> m;

  vector<int> a(H), b(W);
  set<pair<int, int>> bomb;
  rep(i, m) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    bomb.insert({h, w});
    a[h]++;
    b[w]++;
  }

  int res1 = 0, res2 = 0;
  int ans = 0;
  rep(i, H) res1 = max(res1, a[i]);
  rep(i, W) res2 = max(res2, b[i]);
  vector<int> p, q;
  rep(i, H) if (a[i] == res1) p.pb(i);
  rep(i, W) if (b[i] == res2) q.pb(i);
  if (p.size() * q.size() > m) {
    cout << res1 + res2 << endl;
    return 0;
  }
  rep(i, p.size()) {
    rep(j, q.size()) {
      if (bomb.count({p[i], q[j]}))
        ans = max(ans, res1 + res2 - 1);
      else
        ans = max(ans, res1 + res2);
    }
  }
  cout << ans << endl;
}
Problem p02580 (C++)

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

#define ll long long
#define fi first
#define se second
#define inf 1e15

#define N 100005

int row[N];
int col[N];

vector<int> rowv, colv;

set<pair<int, int>> bomb;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin>>t;
  while (t--) {

    int n, m, k;
    cin >> n >> m >> k;

    int rowh = 0, colh = 0;
    for (int i = 0; i < k; i++) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;

      bomb.insert({x, y});

      row[x]++;
      col[y]++;

      rowh = max(rowh, row[x]);
      colh = max(colh, col[y]);
    }

    for (int i = 0; i < n; i++) {
      if (row[i] == rowh) {
        rowv.push_back(i);
      }
    }
    for (int i = 0; i < m; i++) {
      if (col[i] == colh) {
        colv.push_back(i);
      }
    }

    ll ans = rowh + colh - 1;

    for (auto i : rowv) {
      for (auto j : colv) {

        if (bomb.find({i, j}) == bomb.end()) {
          ans++;
          goto end;
        }
      }
    }

  end:
    cout << ans << "\n";
  }
  return 0;
}


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

#define ll long long
#define fi first
#define se second
#define inf 1e15

#define N 300005

int row[N];
int col[N];

vector<int> rowv, colv;

set<pair<int, int>> bomb;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin>>t;
  while (t--) {

    int n, m, k;
    cin >> n >> m >> k;

    int rowh = 0, colh = 0;
    for (int i = 0; i < k; i++) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;

      bomb.insert({x, y});

      row[x]++;
      col[y]++;

      rowh = max(rowh, row[x]);
      colh = max(colh, col[y]);
    }

    for (int i = 0; i < n; i++) {
      if (row[i] == rowh) {
        rowv.push_back(i);
      }
    }
    for (int i = 0; i < m; i++) {
      if (col[i] == colh) {
        colv.push_back(i);
      }
    }

    ll ans = rowh + colh - 1;

    for (auto i : rowv) {
      for (auto j : colv) {

        if (bomb.find({i, j}) == bomb.end()) {
          ans++;
          goto end;
        }
      }
    }

  end:
    cout << ans << "\n";
  }
  return 0;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; (ll)(a) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
string YES[2] = {"NO", "YES"};
string yes[2] = {"No", "Yes"};

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<ll> hc(h, 0), wc(w, 0);
  map<pair<ll, ll>, ll> mp;
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    x--, y--;
    hc[x] += 1;
    wc[y] += 1;
    mp[{x, y}] = 1;
  }
  ll hm = MAX(hc), wm = MAX(wc);
  set<ll> ht, wt;
  rep(i, h) if (hm == hc[i]) ht.insert(i);
  rep(i, w) if (wm == wc[i]) wt.insert(i);
  ll res = hm + wm - 1;
  for (auto x : ht) {
    for (auto y : wt) {
      if (mp[{x, y}])
        continue;
      res = hm + wm;
    }
  }
  cout << res << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; (ll)(a) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
string YES[2] = {"NO", "YES"};
string yes[2] = {"No", "Yes"};

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<ll> hc(h, 0), wc(w, 0);
  map<pair<ll, ll>, ll> mp;
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    x--, y--;
    hc[x] += 1;
    wc[y] += 1;
    mp[{x, y}] = 1;
  }
  ll hm = MAX(hc), wm = MAX(wc);
  set<ll> ht, wt;
  rep(i, h) if (hm == hc[i]) ht.insert(i);
  rep(i, w) if (wm == wc[i]) wt.insert(i);
  ll res = hm + wm - 1;
  for (auto x : ht) {
    for (auto y : wt) {
      if (mp[{x, y}])
        continue;
      cout << res + 1 << endl;
      return 0;
    }
  }
  cout << res << endl;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
// AtCoder template
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)

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

  map<pair<int, int>, int> mp;
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> row(h), col(w);

  rep(i, h) row[i] = make_pair(0, i);
  rep(j, w) col[j] = make_pair(0, j);

  int ht, wt;
  rep(i, m) {
    cin >> ht >> wt;
    --ht;
    --wt;
    mp[make_pair(ht, wt)] = 1;
    row[ht].first++;
    col[wt].first++;
  }

  sort(row.begin(), row.end(), greater<pair<int, int>>());
  sort(col.begin(), col.end(), greater<pair<int, int>>());

  int rt = row[0].first, ct = col[0].first, cnt = 0;
  int ans = rt + ct - mp[make_pair(row[0].second, col[0].second)];
  for (int i = 0; row[i].first == rt && cnt <= m + 1; ++i) {
    for (int j = 0; col[j].first == ct && cnt <= m + 1; ++j) {
      ht = row[i].second, wt = col[j].second;
      int tmp = row[i].first + col[j].first - mp[make_pair(ht, wt)];
      if (ans < tmp) {
        ans = tmp;
        break;
      }
      if (mp[make_pair(ht, wt)] == 1)
        cnt++;
    }
  }

  cout << ans << endl;
}

Fixed submission:
// AtCoder template
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)

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

  map<pair<int, int>, int> mp;
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> row(h), col(w);

  rep(i, h) row[i] = make_pair(0, i);
  rep(j, w) col[j] = make_pair(0, j);

  int ht, wt;
  rep(i, m) {
    cin >> ht >> wt;
    --ht;
    --wt;
    mp[make_pair(ht, wt)] = 1;
    row[ht].first++;
    col[wt].first++;
  }

  sort(row.begin(), row.end(), greater<pair<int, int>>());
  sort(col.begin(), col.end(), greater<pair<int, int>>());

  int rt = row[0].first, ct = col[0].first, cnt = 0;
  int ans = rt + ct - mp[make_pair(row[0].second, col[0].second)];
  for (int i = 0; row[i].first == rt && cnt <= m + 1; ++i) {
    for (int j = 0; col[j].first == ct && cnt <= m + 1; ++j) {
      ht = row[i].second, wt = col[j].second;
      int tmp = row[i].first + col[j].first - mp[make_pair(ht, wt)];
      if (ans < tmp) {
        ans = tmp;
        break;
      }
      cnt++;
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
ll H, W, M;
ll Y[1005];
ll X[1005];
int main() {
  cin >> H >> W >> M;
  vector<P> A(M);
  for (ll i = 0; i < M; i++) {
    ll y, x;
    cin >> y >> x;
    P p(y, x);
    A[i] = p;
    Y[y]++;
    X[x]++;
  }
  ll ymax = 0;
  ll xmax = 0;
  for (ll i = 1; i <= H; i++)
    if (ymax < Y[i])
      ymax = Y[i];
  for (ll i = 1; i <= W; i++)
    if (xmax < X[i])
      xmax = X[i];
  set<ll> Symax;
  set<ll> Sxmax;
  for (ll i = 1; i <= H; i++)
    if (Y[i] == ymax)
      Symax.insert(i);
  for (ll i = 1; i <= W; i++)
    if (X[i] == xmax)
      Sxmax.insert(i);
  ll ans = ymax + xmax;
  ll sum = 0;
  for (ll i = 0; i < M; i++) {
    ll y = A[i].first;
    ll x = A[i].second;
    if (Symax.count(y) && Sxmax.count(x))
      sum++;
  }
  ll a = Symax.size();
  ll b = Sxmax.size();
  if (sum == a * b)
    ans--;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
ll H, W, M;
ll Y[300005];
ll X[300005];
int main() {
  cin >> H >> W >> M;
  vector<P> A(M);
  for (ll i = 0; i < M; i++) {
    ll y, x;
    cin >> y >> x;
    P p(y, x);
    A[i] = p;
    Y[y]++;
    X[x]++;
  }
  ll ymax = 0;
  ll xmax = 0;
  for (ll i = 1; i <= H; i++)
    if (ymax < Y[i])
      ymax = Y[i];
  for (ll i = 1; i <= W; i++)
    if (xmax < X[i])
      xmax = X[i];
  set<ll> Symax;
  set<ll> Sxmax;
  for (ll i = 1; i <= H; i++)
    if (Y[i] == ymax)
      Symax.insert(i);
  for (ll i = 1; i <= W; i++)
    if (X[i] == xmax)
      Sxmax.insert(i);
  ll ans = ymax + xmax;
  ll sum = 0;
  for (ll i = 0; i < M; i++) {
    ll y = A[i].first;
    ll x = A[i].second;
    if (Symax.count(y) && Sxmax.count(x))
      sum++;
  }
  ll a = Symax.size();
  ll b = Sxmax.size();
  if (sum == a * b)
    ans--;
  cout << ans << endl;
  return 0;
}
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

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

  int h, w, m;
  cin >> h >> w >> m;

  vector<int> col(w, 0);
  vector<int> row(h, 0);

  set<pair<int, int>> s;
  rep(i, m) {
    int h, w;
    cin >> h >> w;
    col[--w]++;
    row[--h]++;
    s.insert(make_pair(h, w));
  }
  int max_val = 0;
  rep(i, h) { max_val = max(max_val, row[i]); }
  vector<int> max_idy;
  rep(i, h) {
    if (row[i] == max_val) {
      max_idy.push_back(i);
    }
  }
  int max_val2 = 0;
  rep(i, h) { max_val2 = max(max_val2, col[i]); }
  vector<int> max_idx;
  rep(i, w) {
    if (col[i] == max_val2) {
      max_idx.push_back(i);
    }
  }
  int res = 0;
  for (auto e1 : max_idy)
    for (auto e2 : max_idx) {
      auto it = s.find(make_pair(e1, e2));
      int flag = 0;
      if (it != s.end()) {
        res = max(res, max_val + max_val2 - 1);
      } else {
        res = max(res, max_val + max_val2);
        flag = 1;
        break;
      }
      if (flag)
        break;
    }

  wcout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

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

  int h, w, m;
  cin >> h >> w >> m;

  vector<int> col(w, 0);
  vector<int> row(h, 0);

  set<pair<int, int>> s;
  rep(i, m) {
    int h, w;
    cin >> h >> w;
    col[--w]++;
    row[--h]++;
    s.insert(make_pair(h, w));
  }
  int max_val = 0;
  rep(i, h) { max_val = max(max_val, row[i]); }
  vector<int> max_idy;
  rep(i, h) {
    if (row[i] == max_val) {
      max_idy.push_back(i);
    }
  }
  int max_val2 = 0;
  rep(i, w) { max_val2 = max(max_val2, col[i]); }
  vector<int> max_idx;
  rep(i, w) {
    if (col[i] == max_val2) {
      max_idx.push_back(i);
    }
  }
  int res = 0;
  for (auto e1 : max_idy)
    for (auto e2 : max_idx) {
      auto it = s.find(make_pair(e1, e2));
      int flag = 0;
      if (it != s.end()) {
        res = max(res, max_val + max_val2 - 1);
      } else {
        res = max(res, max_val + max_val2);
        flag = 1;
        break;
      }
      if (flag)
        break;
    }

  wcout << res << endl;
}
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000007
using namespace std;

int main() {
  long H, W, m;
  cin >> H >> W >> m;
  vector<pair<long, long>> hw;
  vector<long> hi(H), wi(W);
  rep(i, int(hi.size())) {
    hi[i] = 0;
    wi[i] = 0;
  }
  long h1, w1;
  rep(i, m) {
    cin >> h1 >> w1;
    h1--;
    w1--;
    hw.push_back({h1, w1});
    hi[h1]++;
    wi[w1]++;
  }
  long maxh = 0, maxw = 0;
  rep(i, int(hi.size())) if (maxh < hi[i]) maxh = hi[i];
  rep(i, int(wi.size())) if (maxw < wi[i]) maxw = wi[i];
  long ans = maxh + maxw;
  vector<int> hcan, wcan;
  rep(i, int(hi.size())) if (maxh == hi[i]) {
    hcan.push_back(i);
    hi[i] = -INF;
  }
  rep(i, int(wi.size())) if (maxw == wi[i]) {
    wcan.push_back(i);
    wi[i] = -INF;
  }
  long tmp = 0;
  rep(i, m) {
    if (hi[hw[i].first] == -INF && wi[hw[i].second] == -INF) {
      tmp++;
    }
  }
  if (tmp == int(hcan.size()) * int(wcan.size()))
    ans--;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000007
using namespace std;

int main() {
  long H, W, m;
  cin >> H >> W >> m;
  vector<pair<long, long>> hw;
  vector<long> hi(H), wi(W);
  rep(i, int(hi.size())) hi[i] = 0;
  rep(i, int(wi.size())) wi[i] = 0;
  long h1, w1;
  rep(i, m) {
    cin >> h1 >> w1;
    h1--;
    w1--;
    hw.push_back({h1, w1});
    hi[h1]++;
    wi[w1]++;
  }
  long maxh = 0, maxw = 0;
  rep(i, int(hi.size())) if (maxh < hi[i]) maxh = hi[i];
  rep(i, int(wi.size())) if (maxw < wi[i]) maxw = wi[i];
  long ans = maxh + maxw;
  vector<int> hcan, wcan;
  rep(i, int(hi.size())) if (maxh == hi[i]) {
    hcan.push_back(i);
    hi[i] = -INF;
  }
  rep(i, int(wi.size())) if (maxw == wi[i]) {
    wcan.push_back(i);
    wi[i] = -INF;
  }
  long tmp = 0;
  rep(i, m) {
    if (hi[hw[i].first] == -INF && wi[hw[i].second] == -INF) {
      tmp++;
    }
  }
  if (tmp == int(hcan.size()) * int(wcan.size()))
    ans--;
  cout << ans << endl;
  return 0;
}
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  const int SIZE = 30010;
  int H, W, M, h[SIZE], w[SIZE], x[SIZE], y[SIZE];
  cin >> H >> W >> M;
  for (int i = 1; i <= H; ++i) {
    x[i] = 0;
  }
  for (int i = 1; i <= W; ++i) {
    y[i] = 0;
  }
  for (int i = 0; i < M; ++i) {
    cin >> h[i] >> w[i];
    x[h[i]]++;
    y[w[i]]++;
  }

  int x_max = 0;
  for (int i = 1; i <= H; ++i) {
    x_max = max(x_max, x[i]);
  }

  int x_sum = 0;
  for (int i = 1; i <= H; ++i) {
    if (x[i] == x_max)
      x_sum++;
  }

  int y_max = 0;
  for (int i = 1; i <= W; ++i) {
    y_max = max(y_max, y[i]);
  }

  int y_sum = 0;
  for (int i = 1; i <= W; ++i) {
    if (y[i] == y_max)
      y_sum++;
  }

  int m_sum = 0;
  for (int i = 0; i < M; ++i) {
    if (x[h[i]] == x_max && y[w[i]] == y_max)
      m_sum++;
  }

  if (m_sum == x_sum * y_sum) {
    cout << x_max + y_max - 1 << endl;
  } else {
    cout << x_max + y_max << endl;
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  const int SIZE = 300010;
  int H, W, M, h[SIZE], w[SIZE], x[SIZE], y[SIZE];
  cin >> H >> W >> M;
  for (int i = 1; i <= H; ++i) {
    x[i] = 0;
  }
  for (int i = 1; i <= W; ++i) {
    y[i] = 0;
  }
  for (int i = 0; i < M; ++i) {
    cin >> h[i] >> w[i];
    x[h[i]]++;
    y[w[i]]++;
  }

  int x_max = 0;
  for (int i = 1; i <= H; ++i) {
    x_max = max(x_max, x[i]);
  }

  int x_sum = 0;
  for (int i = 1; i <= H; ++i) {
    if (x[i] == x_max)
      x_sum++;
  }

  int y_max = 0;
  for (int i = 1; i <= W; ++i) {
    y_max = max(y_max, y[i]);
  }

  int y_sum = 0;
  for (int i = 1; i <= W; ++i) {
    if (y[i] == y_max)
      y_sum++;
  }

  int m_sum = 0;
  for (int i = 0; i < M; ++i) {
    if (x[h[i]] == x_max && y[w[i]] == y_max)
      m_sum++;
  }

  if (m_sum == x_sum * y_sum) {
    cout << x_max + y_max - 1 << endl;
  } else {
    cout << x_max + y_max << endl;
  }
  return 0;
}
Problem p02580 (C++)

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

using namespace std;

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

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

#endif

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

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


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

using namespace std;

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

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

#endif

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

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

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

using namespace std;

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

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

#endif

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

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


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

using namespace std;

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

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

#endif

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

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

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

int h, w, m, sz;
vector<int> ob[300005];

int dat[400005];

void upd(int pos, int val) {
  pos += sz - 1;
  dat[pos] += val;
  while (pos > 1) {
    pos >>= 1;
    dat[pos] = max(dat[pos << 1], dat[pos << 1 | 1]);
  }
}

int qry(int bk, int l, int r, int ql, int qr) {
  if (qr < l || r < ql)
    return 0;
  if (ql <= l && r <= qr)
    return dat[bk];
  return max(qry(bk << 1, l, (l + r) >> 1, ql, qr),
             qry(bk << 1 | 1, ((l + r) >> 1) + 1, r, ql, qr));
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> h >> w >> m;
  sz = 1;
  while (sz < w)
    sz <<= 1;
  while (m--) {
    int x, y;
    cin >> x >> y;
    ob[x].emplace_back(y);
    upd(y, 1);
  }
  int ans = 0;
  for (int i = 1; i <= h; i++) {
    for (int &j : ob[i])
      upd(j, -1);
    ans = max(ans,
              (int)ob[i].size() +
                  qry(1, 1, sz, 1,
                      sz)); // cerr<<ob[i].size()<<' '<<qry(1,1,sz,1,sz)<<endl;
    for (int &j : ob[i])
      upd(j, 1);
  }
  cout << ans << endl;

  return 0;
}

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

int h, w, m, sz;
vector<int> ob[300005];

int dat[1200005];

void upd(int pos, int val) {
  pos += sz - 1;
  dat[pos] += val;
  while (pos > 1) {
    pos >>= 1;
    dat[pos] = max(dat[pos << 1], dat[pos << 1 | 1]);
  }
}

int qry(int bk, int l, int r, int ql, int qr) {
  if (qr < l || r < ql)
    return 0;
  if (ql <= l && r <= qr)
    return dat[bk];
  return max(qry(bk << 1, l, (l + r) >> 1, ql, qr),
             qry(bk << 1 | 1, ((l + r) >> 1) + 1, r, ql, qr));
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> h >> w >> m;
  sz = 1;
  while (sz < w)
    sz <<= 1;
  while (m--) {
    int x, y;
    cin >> x >> y;
    ob[x].emplace_back(y);
    upd(y, 1);
  }
  int ans = 0;
  for (int i = 1; i <= h; i++) {
    for (int &j : ob[i])
      upd(j, -1);
    ans = max(ans,
              (int)ob[i].size() +
                  qry(1, 1, sz, 1,
                      sz)); // cerr<<ob[i].size()<<' '<<qry(1,1,sz,1,sz)<<endl;
    for (int &j : ob[i])
      upd(j, 1);
  }
  cout << ans << endl;

  return 0;
}
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
#define int long long
  int H, W, M;
  cin >> H >> W >> M;
  vector<vector<int>> G(H, vector<int>(W, 0));
  vector<int> hh(H);
  vector<int> ww(W);
  vector<pair<int, int>> memo(M);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    memo.at(i) = make_pair(a, b);
    hh.at(a)++;
    ww.at(b)++;
  }
  int mh = 0;
  int mw = 0;
  for (int i = 0; i < H; i++) {
    mh = max(mh, hh.at(i));
  }
  for (int i = 0; i < W; i++) {
    mw = max(mw, ww.at(i));
  }
  int counth = 0;
  int countw = 0;
  for (int i = 0; i < H; i++) {
    if (mh == hh.at(i)) {
      counth++;
    }
  }
  for (int i = 0; i < W; i++) {
    if (mw == ww.at(i)) {
      countw++;
    }
  }
  int count = 0;
  for (int i = 0; i < M; i++) {
    int h = memo.at(i).first;
    int w = memo.at(i).second;
    if (hh.at(h) == mh && ww.at(w) == mw) {
      count++;
    }
  }
  if (count == counth * countw) {
    cout << mh + mw - 1 << endl;
  } else {
    cout << mh + mw << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
#define int long long
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> hh(H);
  vector<int> ww(W);
  vector<pair<int, int>> memo(M);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    memo.at(i) = make_pair(a, b);
    hh.at(a)++;
    ww.at(b)++;
  }
  int mh = 0;
  int mw = 0;
  for (int i = 0; i < H; i++) {
    mh = max(mh, hh.at(i));
  }
  for (int i = 0; i < W; i++) {
    mw = max(mw, ww.at(i));
  }
  int counth = 0;
  int countw = 0;
  for (int i = 0; i < H; i++) {
    if (mh == hh.at(i)) {
      counth++;
    }
  }
  for (int i = 0; i < W; i++) {
    if (mw == ww.at(i)) {
      countw++;
    }
  }
  int count = 0;
  for (int i = 0; i < M; i++) {
    int h = memo.at(i).first;
    int w = memo.at(i).second;
    if (hh.at(h) == mh && ww.at(w) == mw) {
      count++;
    }
  }
  if (count == counth * countw) {
    cout << mh + mw - 1 << endl;
  } else {
    cout << mh + mw << endl;
  }
}
Problem p02580 (C++)

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

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  // vector<vi> masu(h,vi(w,0));
  map<pair<int, int>, int> mp;
  vi tate_count(h, 0);
  vi yoko_count(w, 0);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    yoko_count[b]++;
    tate_count[a]++;
    pair<int, int> kani = make_pair(a, b);
    mp[kani]++;
  }
  int maxi_t = 0;
  for (int i = 0; i < h; i++) {
    maxi_t = max(maxi_t, tate_count[i]);
  }
  int maxi_y = 0;
  for (int i = 0; i < w; i++) {
    maxi_y = max(maxi_y, yoko_count[i]);
  }
  int ans = maxi_y + maxi_t;
  vi yoko, tate;
  for (int i = 0; i < h; i++) {
    if (maxi_t == tate_count[i])
      tate.push_back(i);
  }
  for (int i = 0; i < w; i++) {
    if (maxi_y == yoko_count[i])
      yoko.push_back(i);
  }
  bool flag = false;
  for (auto x : yoko) {
    for (auto y : tate) {
      pair<int, int> ebi = make_pair(y, x);
      if (mp[ebi] == 0)
        flag = true;
    }
  }
  if (!flag)
    ans--;
  cout << ans << endl;
}

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

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  // vector<vi> masu(h,vi(w,0));
  map<pair<int, int>, int> mp;
  vi tate_count(h, 0);
  vi yoko_count(w, 0);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    yoko_count[b]++;
    tate_count[a]++;
    pair<int, int> kani = make_pair(a, b);
    mp[kani]++;
  }
  int maxi_t = 0;
  for (int i = 0; i < h; i++) {
    maxi_t = max(maxi_t, tate_count[i]);
  }
  int maxi_y = 0;
  for (int i = 0; i < w; i++) {
    maxi_y = max(maxi_y, yoko_count[i]);
  }
  int ans = maxi_y + maxi_t;
  vi yoko, tate;
  for (int i = 0; i < h; i++) {
    if (maxi_t == tate_count[i])
      tate.push_back(i);
  }
  for (int i = 0; i < w; i++) {
    if (maxi_y == yoko_count[i])
      yoko.push_back(i);
  }
  bool flag = false;
  if (tate.size() * yoko.size() > 1e9) {
    cout << ans << endl;
    return 0;
  }
  for (auto x : yoko) {
    for (auto y : tate) {
      pair<int, int> ebi = make_pair(y, x);
      if (mp[ebi] == 0)
        flag = true;
    }
  }
  if (!flag)
    ans--;
  cout << ans << endl;
}
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;

int main() {
  int h, w, n;
  cin >> h >> w >> n;
  vector<int> hs(h), ws(w);
  set<P> s;
  rep(i, n) {
    int r, c;
    cin >> r >> c;
    r--;
    c--;
    hs[r]++;
    ws[c]++;
    s.emplace(r, c);
  }

  int mh = 0, mw = 0;
  rep(i, h) mh = max(mh, hs[i]);
  rep(j, w) mw = max(mw, ws[j]);
  vector<int> is, js;
  rep(i, h) if (mh == hs[i]) is.push_back(i);
  rep(j, w) if (mw == hs[j]) js.push_back(j);

  int ans = mh + mw;
  for (int i : is) {
    for (int j : js) {
      if (s.count(P(i, j)))
        continue;
      cout << ans << endl;
      return 0;
    }
  }
  ans--;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;

int main() {
  int h, w, n;
  cin >> h >> w >> n;
  vector<int> hs(h), ws(w);
  set<P> s;
  rep(i, n) {
    int r, c;
    cin >> r >> c;
    r--;
    c--;
    hs[r]++;
    ws[c]++;
    s.emplace(r, c);
  }

  int mh = 0, mw = 0;
  rep(i, h) mh = max(mh, hs[i]);
  rep(j, w) mw = max(mw, ws[j]);
  vector<int> is, js;
  rep(i, h) if (mh == hs[i]) is.push_back(i);
  rep(j, w) if (mw == ws[j]) js.push_back(j);

  int ans = mh + mw;
  for (int i : is) {
    for (int j : js) {
      if (s.count(P(i, j)))
        continue;
      cout << ans << endl;
      return 0;
    }
  }
  ans--;
  cout << ans << endl;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz size()
#define ll long long
#define all(_v) _v.begin(), _v.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pvllvll pair<vector<ll>, vector<ll>>
#define ld long double
#define veci vector<int>
#define vecll vector<ll>

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;

const int MAXN = 3e5 + 10;
int cnt1[MAXN], cnt2[MAXN];
void solve() {
  int h, w, n;
  cin >> h >> w >> n;
  set<pii> a;
  for (int i = 1; i <= n; ++i) {
    int x, y;
    cin >> x >> y;
    cnt1[x]++;
    cnt2[y]++;
    a.insert(mp(x, y));
  }
  int mx = *max_element(cnt1 + 1, cnt1 + h + 1);
  int mx2 = *max_element(cnt2 + 1, cnt2 + w + 1);
  veci x, y;
  for (int i = 1; i <= h; ++i)
    if (cnt1[i] == mx)
      x.pb(i);
  for (int i = 1; i <= w; ++i)
    if (cnt2[i] == mx2)
      y.pb(i);
  int ans = 0;
  for (auto v : x)
    for (auto u : y)
      ans = max(ans, cnt1[v] + cnt2[u] - (int)a.count(mp(v, u)));
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  /// cin >> T;
  while (T--)
    solve(), cout << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz size()
#define ll long long
#define all(_v) _v.begin(), _v.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pvllvll pair<vector<ll>, vector<ll>>
#define ld long double
#define veci vector<int>
#define vecll vector<ll>

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;

const int MAXN = 3e5 + 10;
int cnt1[MAXN], cnt2[MAXN];
void solve() {
  int h, w, n;
  cin >> h >> w >> n;
  set<pii> a;
  for (int i = 1; i <= n; ++i) {
    int x, y;
    cin >> x >> y;
    cnt1[x]++;
    cnt2[y]++;
    a.insert(mp(x, y));
  }
  int mx = *max_element(cnt1 + 1, cnt1 + h + 1);
  int mx2 = *max_element(cnt2 + 1, cnt2 + w + 1);
  veci x, y;
  for (int i = 1; i <= h; ++i)
    if (cnt1[i] == mx)
      x.pb(i);
  for (int i = 1; i <= w; ++i)
    if (cnt2[i] == mx2)
      y.pb(i);
  int ans = 0;
  for (auto v : x)
    for (auto u : y) {
      int ch = a.count(mp(v, u));
      ans = max(ans, cnt1[v] + cnt2[u] - ch);
      if (!ch)
        break;
    }
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  /// cin >> T;
  while (T--)
    solve(), cout << '\n';
  return 0;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1.0)
#define P pair<int, int>
#define PiP pair<ll, pair<ll, ll>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<int> a(h, 0), b(w, 0);
  map<P, int> mp;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x]++;
    b[y]++;
    mp[{x, y}]++;
  }
  int max_a = *max_element(all(a)), max_b = *max_element(all(b));
  int ans = max_a + max_b;
  rep(i, h) {
    rep(j, w) {
      if (mp[{i, j}] == 0 && max_b == b[j] && max_a == a[i]) {
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans - 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1.0)
#define P pair<int, int>
#define PiP pair<ll, pair<ll, ll>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<int> a(h, 0), b(w, 0);
  map<P, int> mp;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x]++;
    b[y]++;
    mp[{x, y}]++;
  }
  int max_a = *max_element(all(a)), max_b = *max_element(all(b));
  int ans = max_a + max_b;
  vector<int> v1, v2;
  rep(i, h) if (max_a == a[i]) v1.push_back(i);
  rep(i, w) if (max_b == b[i]) v2.push_back(i);
  rep(i, v1.size()) {
    rep(j, v2.size()) {
      if (mp[{v1[i], v2[j]}] == 0) {
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans - 1 << endl;
}