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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Pine1999
 */

#include <bits/stdc++.h>

using namespace std;

class EBomber {
public:
  void solve(istream &in, ostream &out) {
    int h, w, m;
    in >> h >> w >> m;
    set<pair<int, int>> st;
    vector<int> col(h), row(w);
    for (int i = 0; i < m; ++i) {
      int hh, ww;
      in >> hh >> ww;
      --hh;
      --ww;
      st.insert({hh, ww});
      ++col[hh];
      ++row[ww];
    }
    int max_col = *max_element(col.begin(), col.end());
    int max_row = *max_element(row.begin(), row.end());
    vector<int> max_h, max_w;
    for (int i = 0; i < h; ++i) {
      if (col[i] == max_col) {
        max_h.push_back(i);
      }
    }
    for (int i = 0; i < w; ++i) {
      if (row[i] == max_row) {
        max_w.push_back(i);
      }
    }
    for (int hh : max_h) {
      for (int ww : max_w) {
        if (st.find({hh, ww}) == st.end()) {
          out << max_col + max_row << '\\n';
          return;
        }
      }
    }
    out << max_col + max_row - 1 << '\\n';
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  EBomber solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int h, w;
  cin >> h >> w;
  int m;
  cin >> m;
  vector<int> hh(m), ww(m), col(h), row(w);
  set<pair<int, int>> st;
  for (int i = 0; i < m; ++i) {
    cin >> hh[i] >> ww[i];
    --hh[i];
    --ww[i];
    ++col[hh[i]];
    ++row[ww[i]];
    st.insert({hh[i], ww[i]});
  }
  int max_col = *max_element(col.begin(), col.end());
  int max_row = *max_element(row.begin(), row.end());
  int ans = max_col + max_row;
  vector<int> max_h, max_w;
  for (int i = 0; i < m; ++i) {
    if (col[hh[i]] == max_col) {
      max_h.push_back(hh[i]);
    }
    if (row[ww[i]] == max_row) {
      max_w.push_back(ww[i]);
    }
  }
  for (int _h : max_h) {
    for (int _w : max_w) {
      if (st.find({_h, _w}) == st.end()) {
        cout << ans << '\\n';
        return 0;
      }
    }
  }
  cout << ans - 1 << '\\n';
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  map<P, bool> mp;
  vector<int> r(H, 0);
  vector<int> c(W, 0);
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    mp[{x, y}] = true;
    r.at(x)++;
    c.at(y)++;
  }

  int maxR = 0;
  rep(i, H) {
    if (r.at(i) > maxR)
      maxR = r.at(i);
  }
  vector<int> list1;
  rep(i, H) {
    if (r.at(i) == maxR)
      list1.push_back(i);
  }

  int maxC = 0;
  rep(i, W) {
    if (c.at(i) > maxC)
      maxC = c.at(i);
  }
  vector<int> list2;
  rep(i, W) {
    if (c.at(i) == maxC)
      list2.push_back(i);
  }

  int ans = maxR + maxC;
  for (auto x : list1) {
    for (auto y : list2) {
      if (!mp.count({x, y})) {
        cout << ans << endl;
        return 0;
      }
    }
  }

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

Error: 0

Fixed submission:

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

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<vector<bool>> t(H, vector<bool>(W, false));
  vector<int> th(H, 0);
  vector<int> tw(W, 0);
  rep(i, M) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    t.at(h).at(w) = true;
    th.at(h)++;
    tw.at(w)++;
  }

  int maxh = 0;
  rep(i, H) {
    if (th.at(i) > maxh)
      maxh = th.at(i);
  }
  vector<int> list1;
  rep(i, H) {
    if (th.at(i) == maxh)
      list1.push_back(i);
  }

  int maxw = 0;
  rep(i, W) {
    if (tw.at(i) > maxw)
      maxw = tw.at(i);
  }
  vector<int> list2;
  rep(i, W) {
    if (tw.at(i) == maxw)
      list2.push_back(i);
  }

  int ans = maxh + maxw;
  if (ans >= H + W) {
    cout << H + W - 1 << endl;
    return 0;
  }
  for (auto x : list1) {
    for (auto y : list2) {
      if (t.at(x).at(y) == false) {
        cout << ans << endl;
        return 0;
      }
    }
  }

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

#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
/*-------------------------------------------*/

int h, w;
int m;

int cnt1[300001], cnt2[300001];
vector<pair<int, int>> vec;

vector<int> a, b;

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

  cin >> h >> w >> m;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    cnt1[x]++;
    cnt2[y]++;
    vec.emplace_back(x, y);
  }

  sort(ALL(vec));

  int mxa = -1;
  rep(i, h) chmax(mxa, cnt1[i]);
  rep(i, h) if (cnt1[i] == mxa) a.push_back(i);
  int mxb = -1;
  rep(j, w) chmax(mxb, cnt2[j]);
  rep(j, w) if (cnt2[j] == mxb) b.push_back(j);

  for (int i : a)
    for (int j : b)
      if (!binary_search(ALL(vec), make_pair(i, j))) {
        cout << mxa + mxb << endl;
        return 0;
      }

  cout << mxa + mxb - 1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
/*-------------------------------------------*/

int h, w;
int m;

int cnt1[300001], cnt2[300001];
vector<pair<int, int>> vec;

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

  cin >> h >> w >> m;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    cnt1[x]++;
    cnt2[y]++;
    vec.emplace_back(x, y);
  }

  sort(ALL(vec));

  int mx = -1;
  rep(i, h) rep(j, w) chmax(
      mx, cnt1[i] + cnt2[j] - binary_search(ALL(vec), pair<int, int>(i, j)));

  cout << mx << endl;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  int m;
  cin >> m;
  int mxr = 0, mxc = 0;
  vector<int> row(h), col(w);
  vector<pair<int, int>> v;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    mxr = max(mxr, ++row[x]);
    mxc = max(mxc, ++col[y]);
    v.emplace_back(x, y);
  }
  int inter = 0;
  for (auto [x, y] : v) {
    if (row[x] == mxr and col[y] == mxc)
      ++inter;
  }
  int cnt_mxr = count(row.begin(), row.end(), mxr);
  int cnt_mxc = count(col.begin(), col.end(), mxc);
  cout << (mxr + mxc - (inter == 1ll * cnt_mxr * cnt_mxc)) << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  int m;
  cin >> m;
  vector<int> row(h), col(w);
  map<pair<int, int>, bool> inter;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    ++row[x];
    ++col[y];
    inter[{x, y}] = true;
  }
  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      ans = max(ans, row[i] + col[j] - inter[{i, j}]);
    }
  }
  cout << ans << "\\n";
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define REP(i, p, n) for (ll i = p; i < ll(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long;
using pll = pair<ll, ll>;

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

  map<ll, ll> NX;
  map<ll, ll> NY;
  set<pll> P;

  ll xn = 0, yn = 0;

  rep(i, M) {
    ll x, y;
    cin >> x >> y;
    --x;
    --y;

    P.insert(pll(x, y));

    if (!NX[x]) {
      NX[x] = 1;
    } else {
      ++NX[x];
    }
    if (!NY[y]) {
      NY[y] = 1;
    } else {
      ++NY[y];
    }

    xn = max(xn, NX[x]);
    yn = max(yn, NY[y]);
  }

  vector<ll> xs;
  vector<ll> ys;

  for (const auto &kv : NX) {
    if (kv.second == xn) {
      xs.emplace_back(kv.first);
    }
  }
  for (const auto &kv : NY) {
    if (kv.second == yn) {
      ys.emplace_back(kv.first);
    }
  }

  const auto find = [&]() {
    for (const auto &x : xs)
      for (const auto &y : ys) {
        const auto pos = pll(x, y);
        if (P.find(pos) == P.end()) {
          return true;
        }
      }

    return false;
  };

  cout << xn + yn - (find() ? 0 : 1) << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define REP(i, p, n) for (ll i = p; i < ll(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long;
using pll = pair<ll, ll>;

const ll MAXN = 3 * 100000;

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

  set<pll> bombs;

  map<ll, ll> NX;
  map<ll, ll> NY;

  ll xn = 0, yn = 0;

  rep(i, M) {
    ll x, y;
    cin >> x >> y;
    --x;
    --y;

    bombs.insert(pll(x, y));

    if (NX[x]) {
      ++NX[x];
    } else {
      NX[x] = 1;
    }
    if (NY[y]) {
      ++NY[y];
    } else {
      NY[y] = 1;
    }
    xn = max(xn, NX[x]);
    yn = max(yn, NY[y]);
  }

  vector<pll> xs, ys;
  for (const auto &kv : NX) {
    xs.emplace_back(kv.second, kv.first);
  }
  for (const auto &kv : NY) {
    ys.emplace_back(kv.second, kv.first);
  }
  sort(xs.begin(), xs.end(), greater<pll>());
  sort(ys.begin(), ys.end(), greater<pll>());

  ll ans = 0;

  for (const auto &xx : xs)
    for (const auto &yy : ys) {
      const auto xxn = xx.first;
      const auto yyn = yy.first;
      if (xxn + yyn < xn + yn - 1) {
        continue;
      }

      const auto pos = pll(xx.second, yy.second);
      const auto sub = (bombs.find(pos) != bombs.end()) ? 1 : 0;

      ans = max(ans, xxn + yyn - sub);
    }

  cout << ans << endl;

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

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

int MX[333333], MY[333333];
int h, w, m;
int cnt;
int nx, ny;
set<int> SX, SY;
set<pair<int, int>> vis;

int main() {
  scanf("%d%d%d", &h, &w, &m);
  int sbx, sby;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &sbx, &sby);
    vis.insert(make_pair(sbx, sby));
    MX[sbx]++;
    MY[sby]++;
    if (MX[sbx] > nx) {
      nx = MX[sbx];
      SX.clear();
      SX.insert(sbx);
    } else if (MX[sbx] == nx)
      SX.insert(sbx);

    if (MY[sby] > ny) {
      ny = MY[sby];
      SY.clear();
      SY.insert(sby);
    } else if (MY[sby] == ny)
      SY.insert(sby);
  }

  for (auto i : SX) {
    for (auto j : SY) {
      cnt = MX[i] + MY[j];
      if (vis.count(make_pair(i, j)) == 1)
        cnt--;
      else {
        printf("%d\\n", cnt);
        return 0;
      }
    }
  }
  printf("%d\\n", cnt);
  return 0;
}


Error: TLE

Fixed submission:

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

int MX[333333], MY[333333];
int h, w, m;
int cnt;
int nx, ny;
set<int> SX, SY;
set<pair<int, int>> vis;

int main() {
  scanf("%d%d%d", &h, &w, &m);
  int sbx, sby;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &sbx, &sby);
    vis.insert(make_pair(sbx, sby));
    MX[sbx]++;
    MY[sby]++;
    if (MX[sbx] > nx) {
      nx = MX[sbx];
      SX.clear();
      SX.insert(sbx);
    } else if (MX[sbx] == nx)
      SX.insert(sbx);

    if (MY[sby] > ny) {
      ny = MY[sby];
      SY.clear();
      SY.insert(sby);
    } else if (MY[sby] == ny)
      SY.insert(sby);
  }

  for (auto i : SX) {
    for (auto j : SY) {
      int Now = MX[i] + MY[j];
      if (vis.count(make_pair(i, j)) == 1)
        Now--;
      cnt = max(cnt, Now);
    }
  }
  printf("%d\\n", cnt);
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> hcnt(h, 0), wcnt(w, 0);
  vector<vector<int>> bomb(h);
  int a, b;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    --a;
    --b;
    bomb[a].push_back(b);
    ++hcnt[a];
    ++wcnt[b];
  }
  for (int i = 0; i < h; ++i)
    sort(bomb[i].begin(), bomb[i].end());
  vector<int> hi(0), wi(0);
  int hmaxnum = 1;
  for (int i = 0; i < h; ++i) {
    if (hcnt[i] == hmaxnum)
      hi.push_back(i);
    else if (hcnt[i] < hmaxnum)
      continue;
    else {
      hmaxnum = hcnt[i];
      hi = {i};
    }
  }
  int wmaxnum = 1;
  for (int i = 0; i < w; ++i) {
    if (wcnt[i] == wmaxnum)
      wi.push_back(i);
    else if (wcnt[i] < wmaxnum)
      continue;
    else {
      wmaxnum = wcnt[i];
      wi = {i};
    }
  }
  for (int ha : hi)
    for (int wa : wi) {
      auto it = lower_bound(bomb[ha].begin(), bomb[ha].end(), wa);
      if (*it != wa) {
        cout << hmaxnum + wmaxnum << endl;
        return 0;
      }
    }
  cout << hmaxnum + wmaxnum - 1 << endl;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> hcnt(300005, 0), wcnt(300005, 0);
  vector<vector<bool>> bomb(300005, vector<bool>(300005, false));
  int a, b;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    --a;
    --b;
    bomb[a][b] = true;
    ++hcnt[a];
    ++wcnt[b];
  }
  vector<int> hi(0), wi(0);
  int hmaxnum = 1;
  for (int i = 0; i < h; ++i) {
    if (hcnt[i] == hmaxnum)
      hi.push_back(i);
    else if (hcnt[i] < hmaxnum)
      continue;
    else {
      hmaxnum = hcnt[i];
      hi = {i};
    }
  }
  int wmaxnum = 1;
  for (int i = 0; i < w; ++i) {
    if (wcnt[i] == wmaxnum)
      wi.push_back(i);
    else if (wcnt[i] < wmaxnum)
      continue;
    else {
      wmaxnum = wcnt[i];
      wi = {i};
    }
  }
  for (int ha : hi)
    for (int wa : wi) {
      if (!bomb[ha][wa]) {
        cout << hmaxnum + wmaxnum << endl;
        return 0;
      }
    }
  cout << hmaxnum + wmaxnum - 1 << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<ll> memo_h(h), memo_w(w);
  map<ll, bool> table;

  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    memo_h[x]++;
    memo_w[y]++;
    table[x * mod + y] = true;
  }

  ll max_h = 0, max_w = 0;
  rep(i, h) { max_h = max(max_h, memo_h[i]); }
  rep(i, w) { max_w = max(max_w, memo_w[i]); }

  vector<ll> ch, cw;
  rep(i, h) {
    if (memo_h[i] == max_h) {
      ch.push_back(i);
    }
  }
  rep(i, w) {
    if (memo_w[i] == max_w) {
      cw.push_back(i);
    }
  }

  ll sum = max_h + max_w;
  ll counter = 0;
  for (auto fh : ch) {
    for (auto fw : cw) {
      if (!table[fh * mod + fw]) {
        cout << sum << endl;
        return 0;
      }
      if (counter > 2 * m) {
        cout << sum - 1 << endl;
        return 0;
      }
      counter++;
    }
  }
  cout << sum - 1 << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<ll> memo_h(h), memo_w(w);
  map<ll, bool> table;

  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    memo_h[x]++;
    memo_w[y]++;
    table[x * mod + y] = true;
  }

  vector<ll> ih(h), iw(w);
  iota(ih.begin(), ih.end(), 0);
  iota(iw.begin(), iw.end(), 0);

  sort(ih.begin(), ih.end(), [&](ll x, ll y) { return memo_h[x] > memo_h[y]; });
  sort(iw.begin(), iw.end(), [&](ll x, ll y) { return memo_h[x] > memo_h[y]; });

  vector<ll> ch, cw;
  rep(i, h) {
    if (memo_h[ih[i]] == memo_h[ih[0]]) {
      ch.push_back(ih[i]);
    }
  }
  rep(i, w) {
    if (memo_w[iw[i]] == memo_w[iw[0]]) {
      cw.push_back(iw[i]);
    }
  }

  ll sum = memo_h[ih[0]] + memo_w[iw[0]];
  ll counter = 0;
  for (auto fh : ch) {
    for (auto fw : cw) {
      if (table[fh * mod + fw]) {
        continue;
      } else {
        cout << sum << endl;
        return 0;
      }
      if (counter > 2 * m) {
        cout << sum - 1 << endl;
        return 0;
      }
      counter++;
    }
  }
  cout << sum - 1 << endl;

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

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

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> hi(m), wi(m);
  vector<int> hn(h, 0), wn(w, 0);
  rep(i, m) {
    cin >> hi[i] >> wi[i];
    hi[i]--;
    wi[i]--;
    hn[hi[i]]++;
    wn[wi[i]]++;
  }
  int hnmax = 0;
  int wnmax = 0;
  rep(i, m) {
    hnmax = max(hnmax, hn[hi[i]]);
    wnmax = max(wnmax, wn[wi[i]]);
  }
  set<int> hindex, windex;
  set<int> bomb;
  rep(i, m) {
    if (hnmax == hn[hi[i]]) {
      hindex.insert(hi[i]);
    }
    if (wnmax == wn[wi[i]]) {
      windex.insert(wi[i]);
    }
    int id = hi[i] + h * wi[i];
    bomb.insert(id);
  }
  int now = 0;
  for (set<int>::iterator hit = hindex.begin(); hit != hindex.end(); hit++)
    for (set<int>::iterator wit = windex.begin(); wit != windex.end(); wit++) {
      int hi, wi;
      hi = *hit;
      wi = *wit;
      now = hn[hi] + wn[wi];
      int id = hi + h * wi;
      set<int>::iterator bid;
      bid = bomb.find(id);
      if (bid == bomb.end()) {
        cout << now << endl;
        return 0;
      }
    }
  now--;
  cout << now << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  int hi, wi;
  map<P, int> mp;
  vector<int> hn(h, 0), wn(w, 0);
  rep(i, m) {
    cin >> hi >> wi;
    hi--;
    wi--;
    hn[hi]++;
    wn[wi]++;
    P pa;
    pa.first = hi;
    pa.second = wi;
    mp[pa] = 1;
  }
  int ans = 0;
  rep(i, h) rep(j, w) {
    int now = hn[i] + wn[j];
    P pa;
    pa.first = i;
    pa.second = j;
    if (mp[pa] == 1)
      now--;
    ans = max(ans, now);
  }
  cout << ans << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define __                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

void scan(ll &n) { scanf("%lld", &n); }
void scan(ll &n, ll &m) { scanf("%lld%lld", &n, &m); }
void scan(vector<ll> &v) {
  ll n = v.size();
  for (ll i = 0; i < n; i++)
    scanf("%lld", &v[i]);
}
const int N = 3 * 1e5 + 32;
ll row[N], col[N];
map<pair<ll, ll>, ll> mp;
void solve() {

  ll n, m, u, v, i, z;
  ll mxc, mxr;
  mxc = mxr = 0;
  cin >> n >> m >> z;
  ll ans = 0;
  for (i = 0; i < z; i++) {
    cin >> u >> v;
    mp[{u, v}]++;
    row[u]++, col[v]++;
    mxc = max(mxc, col[v]);
    mxr = max(mxr, row[u]);
  }
  vector<ll> vc, vr;
  for (i = 0; i <= n; i++) {
    if (row[i] == mxr)
      vr.pb(i);
  }
  for (i = 0; i <= m; i++)
    if (col[i] == mxc)
      vc.pb(i);

  n = vr.size(), m = vc.size();
  ll mm = 1;
  for (i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++) {
      if (mp[{vr[i], vc[j]}] == 0) {
        mm = 0;
      }
    }
    if (!mm)
      break;
  }
  cout << mxc + mxr - mm << endl;
}

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);

  ll t = 1;
  // cin >> t ;
  for (ll cz = 1; cz <= t; cz++) {
    // cout << "Case " <<cz <<": " ;
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define __                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

void scan(ll &n) { scanf("%lld", &n); }
void scan(ll &n, ll &m) { scanf("%lld%lld", &n, &m); }
void scan(vector<ll> &v) {
  ll n = v.size();
  for (ll i = 0; i < n; i++)
    scanf("%lld", &v[i]);
}
const int N = 3 * 1e5 + 32;
ll row[N], col[N];
map<pair<ll, ll>, ll> mp;
void solve() {

  ll n, m, u, v, i, z;
  ll mxc, mxr;
  mxc = mxr = 0;
  cin >> n >> m >> z;
  ll ans = 0;
  for (i = 0; i < z; i++) {
    cin >> u >> v;
    mp[{u, v}]++;
    row[u]++, col[v]++;
    mxc = max(mxc, col[v]);
    mxr = max(mxr, row[u]);
  }
  vector<ll> vc, vr;
  for (i = 0; i <= n; i++) {
    if (row[i] == mxr)
      vr.pb(i);
  }
  for (i = 0; i <= m; i++)
    if (col[i] == mxc)
      vc.pb(i);

  n = vr.size(), m = vc.size();
  for (i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++) {
      ans = max(ans, mxc + mxr - mp[{vr[i], vc[j]}]);
    }
  }
  cout << ans << endl;
}

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);

  ll t = 1;
  // cin >> t ;
  for (ll cz = 1; cz <= t; cz++) {
    // cout << "Case " <<cz <<": " ;
    solve();
  }
  return 0;
}