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

#include <algorithm>
#include <iostream>
using namespace std;

int Hs[300010] = {0};
int Ws[300010] = {0};
int hw[300010][2];

int main() {
  int H, W, M, h, w;
  cin >> H >> W >> M;
  for (int i = 0; i < M; i++) {
    cin >> h >> w;
    h -= 1;
    w -= 1;
    Hs[h] += 1;
    Ws[w] += 1;
    hw[i][0] = h;
    hw[i][1] = w;
  }

  int h_max = 0, w_max = 0, num_h_max = 0, num_w_max = 0;
  for (int i = 0; i < H; i++) {
    if (h_max < Hs[i]) {
      h_max = Hs[i];
      num_h_max = 1;
    } else if (h_max == Hs[i]) {
      num_h_max += 1;
    }
  }

  for (int i = 0; i < W; i++) {
    if (w_max < Ws[i]) {
      w_max = Ws[i];
      num_w_max = 1;
    } else if (w_max == Ws[i]) {
      num_w_max += 1;
    }
  }

  int tmp_sum_max = h_max + w_max;
  int num_sum_max_pat = num_h_max * num_w_max;

  int counter = 0;
  for (int i = 0; i < M; i++) {
    h = hw[i][0];
    w = hw[i][1];
    if (tmp_sum_max == Hs[h] + Ws[w])
      counter++;
  }
  int sum_max;
  if (counter == num_sum_max_pat)
    sum_max = tmp_sum_max - 1;
  else
    sum_max = tmp_sum_max;

  cout << sum_max << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int Hs[30010] = {};
int Ws[30010] = {};
int hw[30010][2];

int main() {
  int H, W, M, h, w;
  cin >> H >> W >> M;
  for (int i = 0; i < M; i++) {
    cin >> h >> w;
    h -= 1;
    w -= 1;
    Hs[h] += 1;
    Ws[w] += 1;
    hw[i][0] = h;
    hw[i][1] = w;
  }

  int h_max = 0, w_max = 0, num_h_max = 0, num_w_max = 0;
  for (int i = 0; i < H; i++) {
    if (h_max < Hs[i]) {
      h_max = Hs[i];
      num_h_max = 1;
    } else if (h_max == Hs[i]) {
      num_h_max += 1;
    }
  }

  for (int i = 0; i < W; i++) {
    if (w_max < Ws[i]) {
      w_max = Ws[i];
      num_w_max = 1;
    } else if (w_max == Ws[i]) {
      num_w_max += 1;
    }
  }

  int tmp_sum_max = h_max + w_max;
  int num_sum_max_pat = num_h_max * num_w_max;

  int counter = 0;
  for (int i = 0; i < M; i++) {
    h = hw[i][0];
    w = hw[i][1];
    if (tmp_sum_max == Hs[h] + Ws[w])
      counter++;
  }
  int sum_max;
  // cout << tmp_sum_max << endl;
  // cout << num_h_max << ":" << num_w_max << ":" << counter << endl;
  if (counter == num_sum_max_pat)
    sum_max = tmp_sum_max - 1;
  else
    sum_max = tmp_sum_max;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int Ws[300010];
int Hs[300010];
int hw[300010][2];
int main() {
  int H, W, M, h, w;
  cin >> H >> W >> M;
  for (int i = 0; i < M; i++) {
    cin >> h;
    h = h - 1;
    Hs[h] += 1;
    cin >> w;
    w = w - 1;
    Ws[w] += 1;
    hw[i][0] = h;
    hw[i][1] = w;
  }

  int h_max = -1;
  int w_max = -1;
  int num_h_max = 0, num_w_max = 0;
  vector<int> arg_h_max, arg_w_max;
  for (int i = 0; i < H; i++) {
    if (h_max < Hs[i]) {
      h_max = Hs[i];
      num_h_max = 1;
    } else if (h_max == Hs[i]) {
      num_h_max += 1;
    }
  }
  for (int i = 0; i < W; i++) {
    if (w_max < Ws[i]) {
      w_max = Ws[i];
      num_w_max = 1;
    } else if (w_max == Ws[i]) {
      num_w_max += 1;
    }
  }

  int result = h_max + w_max;
  int count = 0;
  for (int k = 0; k < M; k++) {
    if ((Hs[hw[k][0]] == h_max) && (Ws[hw[k][1]] == w_max)) {
      count++;
    }
  }
  if (count == num_h_max * num_w_max)
    result -= 1;

  cout << result << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int Ws[300010];
int Hs[300010];
int wh[300010][2];

int main() {
  int H, W, M, h, w;
  cin >> H >> W >> M;
  for (int i = 0; i < M; i++) {
    cin >> h;
    h = h - 1;
    Hs[h] += 1;
    cin >> w;
    w = w - 1;
    Ws[w] += 1;
    wh[i][0] = w;
    wh[i][1] = h;
  }

  int h_max = -1;
  int w_max = -1;
  vector<int> arg_h_max, arg_w_max;
  for (int i = 0; i < H; i++) {
    if (h_max < Hs[i])
      h_max = Hs[i];
  }
  for (int i = 0; i < W; i++) {
    if (w_max < Ws[i])
      w_max = Ws[i];
  }
  for (int i = 0; i < H; i++) {
    if (h_max == Hs[i])
      arg_h_max.push_back(i);
  }
  for (int i = 0; i < W; i++) {
    if (w_max == Ws[i])
      arg_w_max.push_back(i);
  }

  int result = h_max + w_max;
  int addition = -1;
  for (int i = 0; i < arg_h_max.size(); i++) {
    h = arg_h_max[i];
    for (int j = 0; j < arg_w_max.size(); j++) {
      w = arg_w_max[j];
      int count = 0;
      for (int k = 0; k < M; k++) {
        if ((wh[k][0] == w) && (wh[k][1] == h)) {
          // cout << h << ":" << w << endl;
          count = count;
        } else
          count++;
      }
      if (count == M) {
        addition = 0;
      }
    }
  }

  cout << result + addition << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <array>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  set<array<int, 2>> field;
  vector<int> rows(h, 0), cols(w, 0);
  for (int i = 0; i < m; ++i) {
    int y, x;
    cin >> y >> x;
    --y;
    --x;
    field.insert({y, x});
    ++rows[y];
    ++cols[x];
  }

  int rmax = -1, cmax = -1;
  vector<int> rmaxes, cmaxes;

  for (int i = 0; i < h; ++i) {
    if (rmax <= rows[i]) {
      if (rmax < rows[i]) {
        rmaxes.clear();
        rmax = rows[i];
      }
      rmaxes.push_back(i);
    }
  }
  for (int i = 0; i < w; ++i) {
    if (cmax <= cols[i]) {
      if (cmax < cols[i]) {
        cmaxes.clear();
        cmax = cols[i];
      }
      cmaxes.push_back(i);
    }
  }

  for (auto y : rmaxes) {
    for (auto x : cmaxes) {
      if (field.find({y, x}) == field.end()) {
        cout << rmax + cmax << endl;
        return 0;
      }
    }
  }

  cout << rmax + cmax - 1 << endl;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<vector<bool>> field(h, vector<bool>(w, false));
  vector<int> rows(h, 0), cols(w, 0);
  for (int i = 0; i < m; ++i) {
    int y, x;
    cin >> y >> x;
    --y;
    --x;
    field[y][x] = true;
    ++rows[y];
    ++cols[x];
  }

  int rmax = -1, cmax = -1;
  vector<int> rmaxes, cmaxes;

  for (int i = 0; i < h; ++i) {
    if (rmax <= rows[i]) {
      if (rmax < rows[i]) {
        rmaxes.clear();
        rmax = rows[i];
      }
      rmaxes.push_back(i);
    }
  }
  for (int i = 0; i < w; ++i) {
    if (cmax <= cols[i]) {
      if (cmax < cols[i]) {
        cmaxes.clear();
        cmax = cols[i];
      }
      cmaxes.push_back(i);
    }
  }

  for (auto y : rmaxes) {
    for (auto x : cmaxes) {
      if (!field[y][x]) {
        cout << rmax + cmax << endl;
        return 0;
      }
    }
  }

  cout << rmax + cmax - 1 << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> P;
typedef string str;
typedef vector<P> vp;
typedef vector<string> vs;
typedef vector<bool> vb;

const ll mod = 1e9;
const ll inf = 1e16;

#define rep(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m - 1; i >= n; i--)
#define fi first
#define se second
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define eb(x) emplace_back(x)
#define pb(x) pop_back(x)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sum(x) accumulate(all(x), 0)
#define pc(x) __builtin_popcount(x)
#define gll greater<ll>()

void solve() {
  ll h, w, m;
  cin >> h >> w >> m;
  vp p(m);
  vll x(h, 0), y(w, 0);
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    --a;
    --b;
    x[a]++;
    y[b]++;
    p[i] = P(a, b);
  }

  ll mx = *max_element(all(x));
  ll my = *max_element(all(y));
  rep(i, 0, h) x[i] = (x[i] == mx ? 1 : 0);
  rep(i, 0, w) y[i] = (y[i] == my ? 1 : 0);

  ll ans = mx + my;

  ll num = sum(x) * sum(y);
  rep(i, 0, m) if (x[p[i].fi] && y[p[i].se]) num--;

  if (num == 0)
    ans--;

  cout << ans << endl;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> P;
typedef string str;
typedef vector<P> vp;
typedef vector<string> vs;
typedef vector<bool> vb;

const ll mod = 1e9;
const ll inf = 1e16;

#define rep(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m - 1; i >= n; i--)
#define fi first
#define se second
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define eb(x) emplace_back(x)
#define pb(x) pop_back(x)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sum(x) accumulate(all(x), 0)
#define pc(x) __builtin_popcount(x)
#define gll greater<ll>()

void solve() {
  ll h, w, m;
  cin >> h >> w >> m;
  set<P> s;
  vll x(h, 0), y(w, 0);
  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    s.insert(P(a, b));
    x[a]++;
    y[b]++;
  }

  ll z = *max_element(all(x));
  vll tate, yoko;
  rep(i, 0, h) if (x[i] == z) tate.eb(i);

  z = *max_element(all(y));
  rep(i, 0, w) if (y[i] == z) yoko.eb(i);

  ll ans = 0;
  for (auto i : tate) {
    for (auto j : yoko) {
      ll tot = x[i] + y[j];
      if (s.find(P(i, j)) != s.end())
        tot--;
      chmax(ans, tot);
    }
  }
  cout << ans << endl;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using ll = long long;
template <class T> using p = pair<T, T>;
template <class T> using vt = vector<T>;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) (int)(x).size()
#define eb emplace_back
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second

signed main() {
  ios::sync_with_stdio(0), cin.tie(0);

  int h, w, m;
  cin >> h >> w >> m;
  int mxh = 0, mxw = 0;
  vt<unordered_set<int>> H(h + 1);
  vt<unordered_set<int>> W(w + 1);
  rep(i, m) {
    int hh, ww;
    cin >> hh >> ww;
    H[hh].insert(ww);
    W[ww].insert(hh);
    mxh = max(sz(H[hh]), mxh);
    mxw = max(sz(W[ww]), mxw);
  }
  bool minus1 = true;

  vt<int> okh, okw;
  for (int i = 1; i <= h; ++i)
    if (sz(H[i]) == mxh)
      okh.pb(i);
  for (int i = 1; i <= w; ++i)
    if (sz(W[i]) == mxw)
      okw.pb(i);

  for (auto i : okh) {
    if (sz(H[i]) < mxh)
      continue;
    for (auto j : okw) {
      if (sz(W[j]) < mxw)
        continue;
      if (!H[i].count(j) || !W[j].count(i)) {
        minus1 = false;
        break;
      }
    }
    if (!minus1)
      break;
  }

  if (minus1)
    cout << mxh + mxw - 1 << endl;
  else
    cout << mxh + mxw << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

using ll = long long;
template <class T> using p = pair<T, T>;
template <class T> using vt = vector<T>;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) (int)(x).size()
#define eb emplace_back
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second

signed main() {
  ios::sync_with_stdio(0), cin.tie(0);

  int h, w, m;
  cin >> h >> w >> m;
  int mxh = 0, mxw = 0;
  vt<unordered_set<int>> H(h + 1);
  vt<unordered_set<int>> W(w + 1);
  rep(i, m) {
    int hh, ww;
    cin >> hh >> ww;
    H[hh].insert(ww);
    W[ww].insert(hh);
    mxh = max(sz(H[hh]), mxh);
    mxw = max(sz(W[ww]), mxw);
  }
  bool minus1 = true;

  for (int i = 1; i <= h; ++i) {
    if (sz(H[i]) < mxh)
      continue;
    for (int j = 1; j <= w; ++j) {
      if (sz(W[j]) < mxw)
        continue;
      if (!H[i].count(j) || !W[j].count(i)) {
        minus1 = false;
        break;
      }
    }
    if (!minus1)
      break;
  }

  if (minus1)
    cout << mxh + mxw - 1 << endl;
  else
    cout << mxh + mxw << endl;

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

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> y(m), x(m);
  map<pair<int, int>, int> bomb;
  for (int i = 0; i < m; i++) {
    cin >> y[i] >> x[i];
    y[i]--;
    x[i]--;
    bomb[make_pair(y[i], x[i])]++;
  }

  map<int, int> ycnt, xcnt;
  for (int i : y)
    ycnt[i]++;
  for (int i : x)
    xcnt[i]++;

  vector<pair<int, int>> yp, xp;
  for (auto i : ycnt)
    yp.push_back(make_pair(i.second, i.first));
  for (auto i : xcnt)
    xp.push_back(make_pair(i.second, i.first));
  sort(yp.begin(), yp.end(), greater<>());
  sort(xp.begin(), xp.end(), greater<>());

  int ans = yp[0].first + xp[0].first;
  if (bomb[make_pair(yp[0].second, xp[0].second)]) {
    ans--;
    for (auto i : yp) {
      if (bomb[make_pair(i.second, xp[0].second)]) {
        ans = max(ans, i.first + xp[0].first - 1);
      } else {
        ans = max(ans, i.first + xp[0].first);
      }
    }
    for (auto i : xp) {
      if (bomb[make_pair(yp[0].second, i.second)]) {
        ans = max(ans, yp[0].first + i.first - 1);
      } else {
        ans = max(ans, yp[0].first + i.first);
      }
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> y(m), x(m);
  map<pair<int, int>, int> bomb;
  for (int i = 0; i < m; i++) {
    cin >> y[i] >> x[i];
    y[i]--;
    x[i]--;
    bomb[make_pair(y[i], x[i])]++;
  }

  map<int, int> ycnt, xcnt;
  for (int i : y)
    ycnt[i]++;
  for (int i : x)
    xcnt[i]++;

  vector<pair<int, int>> yp, xp;
  for (auto i : ycnt)
    yp.push_back(make_pair(i.second, i.first));
  for (auto i : xcnt)
    xp.push_back(make_pair(i.second, i.first));
  sort(yp.begin(), yp.end(), greater<>());
  sort(xp.begin(), xp.end(), greater<>());

  int ans = yp[0].first + xp[0].first;
  if (bomb[make_pair(yp[0].second, xp[0].second)]) {
    ans--;
    for (int i = 0; i < h; i++) {
      if (bomb[make_pair(yp[i].second, xp[0].second)]) {
        ans = max(ans, yp[i].first + xp[0].first - 1);
      } else {
        ans = max(ans, yp[i].first + xp[0].first);
      }
    }
    for (int i = 0; i < w; i++) {
      if (bomb[make_pair(yp[0].second, xp[i].second)]) {
        ans = max(ans, yp[0].first + xp[i].first - 1);
      } else {
        ans = max(ans, yp[0].first + xp[i].first);
      }
    }
  }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll h, w, n, i, j;
  cin >> h >> w >> n;
  vector<ll> r(h + 5, 0);
  vector<ll> c(w + 5, 0);
  vector<pair<ll, ll>> v;
  map<pair<ll, ll>, ll> mp;
  ll ans = 0;
  ll rr = 0, cc = 0;
  ll x, y;
  for (i = 0; i < n; i++) {
    cin >> x >> y;
    r[x]++;
    c[y]++;
    if (r[rr] < r[x])
      rr = x;
    if (c[cc] < c[y])
      cc = y;
    mp[{x, y}] = 1;
    v.push_back({x, y});
  }
  ll temp = 0;
  for (auto i : v) {
    temp = 0;
    temp += r[i.first];
    temp += c[cc];
    if (mp.find({i.first, cc}) != mp.end())
      temp--;
    ans = max(ans, temp);
    temp = 0;
    temp += r[rr];
    temp += c[i.second];
    if (mp.find({rr, i.second}) != mp.end())
      temp--;
    ans = max(ans, temp);
  }
  cout << ans << "\\n";
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll ax[300005], ay[300005];
int main() {
  ll n, m, x, y, z, maxi = 0, i, p, f = 0, j;
  cin >> n >> m >> p;
  map<pair<ll, ll>, ll> mp;
  std::vector<ll> v1;
  std::vector<ll> v2;
  while (p--) {
    cin >> x >> y;
    ax[x]++;
    ay[y]++;
    mp[{x, y}] = 1;
  }
  priority_queue<pair<ll, ll>> pq1;
  priority_queue<pair<ll, ll>> pq2;
  for (i = 1; i <= 300005; i++) {
    pq1.push({ax[i], i});
    pq2.push({ay[i], i});
  }
  priority_queue<pair<ll, ll>> pq3;
  priority_queue<pair<ll, ll>> pq4;
  pair<ll, ll> p1 = pq1.top();
  pair<ll, ll> p2 = pq2.top();
  pq3.push(p1);
  pq4.push(p2);
  pq1.pop();
  pq2.pop();
  z = p1.first;
  maxi = maxi + z;
  while (!pq1.empty() && pq1.top().first == z) {
    v1.push_back(pq1.top().second);
    pq1.pop();
  }
  z = p2.first;
  maxi = maxi + z;
  while (!pq2.empty() && pq2.top().first == z) {
    v2.push_back(pq2.top().second);
    pq2.pop();
  }
  for (i = 0; i < v1.size(); i++) {
    for (j = 0; j < v2.size(); j++) {
      if (mp[{v1[i], v2[j]}] == 0) {
        f = 1;
        break;
      }
    }
  }
  if (f == 1)
    cout << maxi;
  else
    cout << maxi - 1;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <map>
#include <set>
using namespace std;

int H, W, M;
int HH[300005], WW[300005];

pair<int, int> DH[300005], DW[300005];
set<pair<int, int>> B;

int bom(int h, int w) {
  return DH[h].first + DW[w].first -
         (B.count(make_pair(DH[h].second, DW[w].second)) > 0 ? 1 : 0);
}

void solve() {
  for (int i = 0; i < H; i++) {
    DH[i] = make_pair(0, i);
  }
  for (int i = 0; i < W; i++) {
    DW[i] = make_pair(0, i);
  }

  for (int m = 0; m < M; m++) {
    DH[HH[m]].first++;
  }
  for (int m = 0; m < M; m++) {
    DW[WW[m]].first++;
  }

  sort(DH, DH + H, greater<pair<int, int>>());
  sort(DW, DW + H, greater<pair<int, int>>());
  // for (int i = 0; i < H; i++) { printf("DH[%d] = (%d, %d)\\n", i, DH[i].first,
  // DH[i].second); } for (int i = 0; i < W; i++) { printf("DW[%d] = (%d,
  // %d)\\n", i, DW[i].first, DW[i].second); }

  int mh = 0, mw = 0;
  int s = bom(0, 0);
  for (int h = 0; h < H; h++) {
    if (bom(h, 0) >= s - 1) {
      mh = h;
    } else {
      break;
    }
  }
  for (int w = 0; w < W; w++) {
    if (bom(0, w) >= s - 1) {
      mw = w;
    } else {
      break;
    }
  }

  int ans = -1;
  for (int h = 0; h <= mh; h++) {
    ans = max(ans, bom(h, 0));
  }
  for (int w = 0; w <= mw; w++) {
    ans = max(ans, bom(0, w));
  }

  printf("%d\\n", ans);
}

int main() {
  scanf("%d %d %d", &H, &W, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d %d", HH + i, WW + i);
    HH[i]--;
    WW[i]--;
    B.insert(make_pair(HH[i], WW[i]));
  }
  solve();
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
#include <set>
using namespace std;

int H, W, M;
int HH[300005], WW[300005];

pair<int, int> DH[300005], DW[300005];
set<pair<int, int>> B;

int bom(int h, int w) {
  return DH[h].first + DW[w].first -
         (B.count(make_pair(DH[h].second, DW[w].second)) > 0 ? 1 : 0);
}

void solve() {
  for (int i = 0; i < H; i++) {
    DH[i] = make_pair(0, i);
  }
  for (int i = 0; i < W; i++) {
    DW[i] = make_pair(0, i);
  }

  for (int m = 0; m < M; m++) {
    DH[HH[m]].first++;
  }
  for (int m = 0; m < M; m++) {
    DW[WW[m]].first++;
  }

  sort(DH, DH + H, greater<pair<int, int>>());
  sort(DW, DW + H, greater<pair<int, int>>());
  // for (int i = 0; i < H; i++) { printf("DH[%d] = (%d, %d)\\n", i, DH[i].first,
  // DH[i].second); } for (int i = 0; i < W; i++) { printf("DW[%d] = (%d,
  // %d)\\n", i, DW[i].first, DW[i].second); }

  int mh = 0, mw = 0;
  int s = bom(0, 0);
  for (int h = 0; h < H; h++) {
    if (bom(h, 0) >= s - 1) {
      mh = h;
    } else {
      break;
    }
  }
  for (int w = 0; w < W; w++) {
    if (bom(0, w) >= s - 1) {
      mw = w;
    } else {
      break;
    }
  }

  int ans = -1;
  for (int h = 0; h <= mh; h++) {
    for (int w = 0; w <= mw; w++) {
      ans = max(ans, bom(h, w));
    }
  }
  printf("%d\\n", ans);
}

int main() {
  scanf("%d %d %d", &H, &W, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d %d", HH + i, WW + i);
    HH[i]--;
    WW[i]--;
    B.insert(make_pair(HH[i], WW[i]));
  }
  solve();
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()    // 昇順ソート
#define rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define FastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
typedef long long ll;
using P = pair<int, int>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  map<int, int> mpRow, mpCol;
  set<P> target;
  rep(i, m) {
    int hi, wi;
    cin >> hi >> wi;
    hi--;
    wi--;
    mpCol[wi]++;
    mpRow[hi]++;
    target.insert(make_pair(hi, wi));
  }
  int mxRowCnt = 0, mxColCnt = 0;
  for (auto mp : mpRow)
    mxRowCnt = max(mxRowCnt, mp.second);
  for (auto mp : mpCol)
    mxColCnt = max(mxColCnt, mp.second);
  set<int> stRow, stCol;
  for (auto mp : mpRow)
    if (mxRowCnt == mp.second)
      stRow.insert(mp.first);
  for (auto mp : mpCol)
    if (mxColCnt == mp.second)
      stCol.insert(mp.first);
  int ans = mxRowCnt + mxColCnt;
  bool ok = false;
  for (int sr : stRow) {
    for (int sc : stCol) {
      if (target.find(make_pair(sr, sc)) == target.end()) {
        ok = true;
        break;
      }
    }
  }
  if (ok)
    cout << ans << endl;
  else
    cout << ans - 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()    // 昇順ソート
#define rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define FastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
typedef long long ll;
using P = pair<int, int>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  map<int, int> mpRow, mpCol;
  set<P> st;
  rep(i, m) {
    int hi, wi;
    cin >> hi >> wi;
    hi--;
    wi--;
    mpRow[hi]++;
    mpCol[wi]++;
    st.insert(make_pair(hi, wi));
  }

  int ans = 0;
  for (auto mr : mpRow) {
    for (auto mc : mpCol) {
      int sum = mc.second + mr.second;
      if (st.find(make_pair(mr.first, mc.first)) != st.end())
        sum--;
      ans = max(ans, sum);
    }
  }
  cout << ans << endl;
  return 0;
}