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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
// ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> h_accum(h);
  vector<int> w_accum(w);
  set<pair<int, int>> st;
  rep(i, m) {
    int hh, ww;
    cin >> hh >> ww;
    --hh, --ww;
    ++h_accum[hh];
    ++w_accum[ww];
    st.insert(make_pair(hh, ww));
  }
  int h_max = *max_element(h_accum.begin(), h_accum.end());
  int w_max = *max_element(w_accum.begin(), w_accum.end());
  vector<int> h_max_v, w_max_v;
  rep(i, h) if (h_accum[i] == h_max) h_max_v.push_back(i);
  rep(i, w) if (w_accum[i] == w_max) w_max_v.push_back(i);
  if ((ll)(h_max_v.size()) * (ll)(w_max_v.size()) > m) {
    cout << h_max + w_max << endl;
    return 0;
  }
  rep(i, (int)(h_max_v.size())) {
    rep(j, (int)(w_max_v.size())) {
      if (st.count(make_pair(h_max_v[i], w_max_v[j])) == 0) {
        cout << h_max + w_max << endl;
        return 0;
      }
    }
  }
  cout << h_max + w_max - 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
// ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<vector<int>> cnt(h, vector<int>(w, 0));
  vector<pair<int, int>> p(m);
  rep(i, m) {
    int hh, ww;
    cin >> hh >> ww;
    --hh, --ww;
    ++cnt[hh][ww];
    p[i] = make_pair(hh, ww);
  }
  int h_max = 0;
  int w_max = 0;
  vector<int> h_accum(h);
  vector<int> w_accum(w);
  int h_max_cnt = 0;
  int w_max_cnt = 0;
  rep(i, h) {
    int sum = 0;
    rep(j, w) { sum += cnt[i][j]; }
    h_max = max(h_max, sum);
    h_accum[i] = sum;
  }
  rep(j, w) {
    int sum = 0;
    rep(i, h) { sum += cnt[i][j]; }
    w_max = max(w_max, sum);
    w_accum[j] = sum;
  }
  // cout << h_max << " " << w_max << endl;
  rep(i, h) {
    if (h_accum[i] == h_max)
      ++h_max_cnt;
  }
  rep(i, w) {
    if (w_accum[i] == w_max)
      ++w_max_cnt;
  }
  // rep(i, h) cout << h_accum[i] << " ";
  // cout << endl;
  // rep(i, w) cout << w_accum[i] << " ";
  // cout << endl;
  int rest = h_max_cnt * w_max_cnt;
  rep(i, m) {
    if (h_accum[p[i].first] == h_max && w_accum[p[i].second] == w_max) {
      --rest;
    }
  }
  // cout << rest << " " << h_max_cnt << " " << w_max_cnt << endl;
  if (rest <= 0)
    cout << h_max + w_max - 1 << endl;
  else
    cout << h_max + w_max << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
// ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  // vector<vector<bool>> hw(h, vector<bool>(w, false));
  set<pair<int, int>> st;
  vector<int> hnum(h, 0);
  vector<int> wnum(w, 0);
  rep(i, m) {
    int hh, ww;
    cin >> hh >> ww;
    --hh, --ww;
    st.insert(make_pair(hh, ww));
    ++hnum[hh];
    ++wnum[ww];
  }
  int hnum_max = *max_element(hnum.begin(), hnum.end());
  int wnum_max = *max_element(wnum.begin(), wnum.end());
  vector<int> hmax_idx;
  vector<int> wmax_idx;
  rep(i, h) {
    if (hnum[i] == hnum_max)
      hmax_idx.push_back(i);
  }
  rep(i, w) {
    if (wnum[i] == wnum_max)
      wmax_idx.push_back(i);
  }
  if ((ll)(hmax_idx.size()) * (ll)(wmax_idx.size()) > m) {
    cout << hnum_max + wnum_max << endl;
    return 0;
  }
  int ans = 0;
  for (auto hidx : hmax_idx) {
    for (auto widx : wmax_idx) {
      if (st.count(make_pair(hidx, widx)) != 0) {
        ans = max(ans, hnum_max + wnum_max - 1);
      } else {
        ans = max(ans, hnum_max + wnum_max);
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
// ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<vector<bool>> hw(h, vector<bool>(w, false));
  vector<int> hnum(h, 0);
  vector<int> wnum(w, 0);

  rep(i, m) {
    int hh, ww;
    cin >> hh >> ww;
    --hh, --ww;
    hw[hh][ww] = true;
    ++hnum[hh];
    ++wnum[ww];
  }
  vector<pair<int, int>> hnum_idx;
  vector<pair<int, int>> wnum_idx;
  rep(i, h) { hnum_idx.emplace_back(hnum[i], i); }
  rep(j, w) { wnum_idx.emplace_back(wnum[j], j); }
  int ans = 0;
  sort(wnum_idx.rbegin(), wnum_idx.rend());
  sort(hnum_idx.rbegin(), hnum_idx.rend());

  rep(i, 2) rep(j, 2) {
    int tmp = wnum_idx[i].first + hnum_idx[j].first;
    if (hw[hnum_idx[j].second][wnum_idx[i].second])
      --tmp;
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

// Sky's the limit :)
#include <bits/stdc++.h>
using namespace std;
#define int long long

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

  int T = 1;
  // cin >> T;
  while (T--) {
    int n, m, k;
    cin >> n >> m >> k;
    map<int, int> mx, my;
    map<pair<int, int>, int> mp;
    for (int i = 0; i < k; i++) {
      int x, y;
      cin >> x >> y;

      mx[x]++, my[y]++;
      mp[{x, y}]++;
    }

    int x = 0, y = 0;
    for (auto &&it : mx)
      x = max(x, it.second);
    for (auto &&it : my)
      y = max(y, it.second);

    vector<int> vx, vy;
    for (auto &&it : mx) {
      if (it.second == x)
        vx.push_back(it.first);
    }
    for (auto &&it : my) {
      if (it.second == y)
        vy.push_back(it.first);
    }

    int ans = x + y - 1;
    for (auto &&x : vx) {
      for (auto &&y : vy) {
        if (mp.find({x, y}) == mp.end()) {
          cout << ++ans << '\\n';
          exit(0);
        }
      }
    }

    cout << ans << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

// Sky's the limit :)
#include <bits/stdc++.h>
using namespace std;
#define int long long

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

  int T = 1;
  // cin >> T;
  while (T--) {
    int n, m, k;
    cin >> n >> m >> k;
    map<int, int> mx, my;
    map<pair<int, int>, int> mp;
    for (int i = 0; i < k; i++) {
      int x, y;
      cin >> x >> y;

      mx[x]++, my[y]++;
      mp[{x, y}]++;
    }

    int x = 0, y = 0;
    for (auto &&it : mx)
      x = max(x, it.second);
    for (auto &&it : my)
      y = max(y, it.second);

    vector<int> vx, vy;
    for (auto &&it : mx) {
      if (it.second == x)
        vx.push_back(it.first);
    }
    for (auto &&it : my) {
      if (it.second == y)
        vy.push_back(it.first);
    }

    int ans = x + y - 1, ok = 0;
    for (auto &&x : vx) {
      for (auto &&y : vy) {
        if (mp.find({x, y}) == mp.end())
          ok = 1;
      }
    }

    cout << ans + ok << '\\n';
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int H, W, M;
set<i_i> st;

void input() {
  cin >> H >> W >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    st.insert({a, b});
  }
}

void solve() {
  map<int, int> mph, mpw;
  int hmaxval = 0;
  int wmaxval = 0;
  vector<int> hmax, wmax;
  for (auto tmp : st) {
    chmax(hmaxval, ++mph[tmp.first]);
    chmax(wmaxval, ++mpw[tmp.second]);
  }
  for (auto tmp : mph) {
    if (hmaxval == tmp.second)
      hmax.push_back(tmp.first);
  }
  for (auto tmp : mpw) {
    if (wmaxval == tmp.second)
      wmax.push_back(tmp.first);
  }
  int ANS = hmaxval + wmaxval - 1;
  for (auto h : hmax) {
    for (auto w : wmax) {
      if (st.find({h, w}) == st.end()) {
        ANS++;
        cout << ANS << endl;
        return;
      }
    }
  }
  cout << ANS << endl;
}

int main() {
  input();
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int H, W, M;
set<i_i> st;

void input() {
  cin >> H >> W >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    st.insert({a, b});
  }
}

void solve() {
  map<int, int> mph, mpw;
  int hmaxval = 0;
  int wmaxval = 0;
  vector<int> hmax, wmax;
  for (auto tmp : st) {
    chmax(hmaxval, ++mph[tmp.first]);
    chmax(wmaxval, ++mpw[tmp.second]);
  }
  for (auto tmp : mph) {
    if (hmaxval == tmp.second)
      hmax.push_back(tmp.first);
  }
  for (auto tmp : mpw) {
    if (wmaxval == tmp.second)
      wmax.push_back(tmp.first);
  }
  int ANS = hmaxval + wmaxval - 1;
  bool isok = false;
  for (auto h : hmax) {
    for (auto w : wmax) {
      if (st.find({h, w}) == st.end()) {
        isok = true;
      }
    }
  }
  if (isok)
    cout << ANS + 1 << endl;
  else
    cout << ANS << endl;
}

int main() {
  input();
  solve();
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef bool boool;
typedef long long ll;
#define vl vector<ll>
#define vb vector<boool>
#define vs vector<string>
#define vp vector<pair<ll, ll>>
#define vvl vector<vector<ll>>
#define vvp vector<vector<pair<ll, ll>>>
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define rep1(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep2(i, s, e) for (long long i = (s); i < (long long)(e); i++)
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  map<int, int> row;
  map<int, int> col;
  vector<map<int, int>> vec(300000);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    row[a]++;
    col[b]++;
    vec[b][a]++;
  }
  int br = 0, bc = 0;
  vector<int> y, x;
  for (auto p : row) {
    if (br < p.second) {
      br = p.second;
      y.clear();
      y.push_back(p.first);
    } else if (br == p.second)
      y.push_back(p.first);
  }
  for (auto p : col) {
    if (bc < p.second) {
      bc = p.second;
      x.clear();
      x.push_back(p.first);
    } else if (bc == p.second)
      x.push_back(p.first);
  }
  for (auto dy : y) {
    for (auto dx : x) {
      if (vec[dx][dy] == 0) {
        cout << br + bc << endl;
        return 0;
      }
    }
  }
  cout << br + bc - 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef bool boool;
typedef long long ll;
#define vl vector<ll>
#define vb vector<boool>
#define vs vector<string>
#define vp vector<pair<ll, ll>>
#define vvl vector<vector<ll>>
#define vvp vector<vector<pair<ll, ll>>>
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define rep1(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep2(i, s, e) for (long long i = (s); i < (long long)(e); i++)
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  unordered_map<int, int> row;
  unordered_map<int, int> col;
  vector<map<int, int>> vec(300000);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    row[a]++;
    col[b]++;
    vec[b][a]++;
  }
  int br = 0, bc = 0;
  vector<int> y, x;
  for (auto p : row) {
    if (br < p.second) {
      br = p.second;
      y.clear();
      y.push_back(p.first);
    } else if (br == p.second)
      y.push_back(p.first);
  }
  for (auto p : col) {
    if (bc < p.second) {
      bc = p.second;
      x.clear();
      x.push_back(p.first);
    } else if (bc == p.second)
      x.push_back(p.first);
  }
  bool isExist = false;
  for (auto dy : y) {
    for (auto dx : x) {
      if (vec[dx][dy] == 0)
        isExist = true;
    }
  }
  if (isExist)
    cout << br + bc << endl;
  else
    cout << br + bc - 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 = int64_t;
using P = pair<ll, ll>;
template <class T> using V = vector<T>;

int main() {
  int h, w, m;
  cin >> h >> w >> m;

  map<int, int> hl, wl;
  map<P, int> b;
  rep(i, m) {
    int hi, wi;
    cin >> hi >> wi;
    hi--;
    wi--;
    hl[hi]++;
    wl[wi]++;
    b[{hi, wi}] = 1;
  }

  V<P> nh(h), nw(w);
  for (auto e : hl)
    nh.push_back({e.second, e.first});
  for (auto e : wl)
    nw.push_back({e.second, e.first});

  sort(nh.rbegin(), nh.rend());
  sort(nw.rbegin(), nw.rend());

  ll ans = 0;
  rep(i, h) {
    if (nh[0].first > nh[i].first)
      break;
    rep(j, w) {
      if (nw[0].first > nw[j].first)
        break;
      ll tmp = nh[i].first + nw[j].first;
      int x = nw[j].second;
      int y = nh[i].second;
      if (b[{y, x}] > 0)
        tmp--;
      ans = max(ans, tmp);
      if (b[{y, x}] == 0)
        break;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int h, w, m;
  cin >> h >> w >> m;

  V<int> hl(h), wl(w);
  map<P, int> b;
  rep(i, m) {
    int hi, wi;
    cin >> hi >> wi;
    hi--;
    wi--;
    hl[hi]++;
    wl[wi]++;
    b[{hi, wi}] = 1;
  }

  V<P> nh(h), nw(w);
  rep(i, h) nh[i] = {hl[i], i};
  rep(i, w) nw[i] = {wl[i], i};

  sort(nh.rbegin(), nh.rend());
  sort(nw.rbegin(), nw.rend());

  ll ans = 0;
  rep(i, h) {
    if (nh[0].first > nh[i].first)
      break;
    rep(j, w) {
      if (nw[0].first > nw[j].first)
        break;
      ll tmp = nh[i].first + nw[j].first;
      int x = nw[j].second;
      int y = nh[i].second;
      if (b[{y, x}] > 0)
        tmp--;
      ans = max(ans, tmp);
    }
  }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 1001001001001001001
#define MAX 2001001

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

using namespace std;

vector<int> ch[300001];
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> b(m);
  vector<pair<int, int>> cw(w + 1, {0, 0});
  rep(i, 0, w + 1) cw[i].second = i;
  rep(i, 0, m) {
    cin >> b[i].first >> b[i].second;
    ch[b[i].first].push_back(b[i].second);
    cw[b[i].second].first++;
  }
  size_t mhsize = 0;
  vector<int> a;
  rep(i, 0, h + 1) {
    if (ch[i].size() > mhsize) {
      mhsize = ch[i].size();
      a.clear();
      a.push_back(i);
    }
    if (ch[i].size() == mhsize) {
      a.push_back(i);
    }
  }
  for (auto n : a)
    sort(ch[n].begin(), ch[n].end());
  sort(cw.rbegin(), cw.rend());
  int mw = cw[0].first;
  int ans = 0;
  int z = mhsize;
  for (auto n : a) {
    rep(i, 0, w + 1) {
      int flag = 0;
      if (mw != cw[i].first)
        break;
      if (binary_search(ch[n].begin(), ch[n].end(), cw[i].second))
        flag++;
      ans = max(ans, z + mw - flag);
      if (ans == z + mw)
        break;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 1001001001001001001
#define MAX 2001001

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

using namespace std;

vector<int> ch[300001];
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> b(m);
  vector<pair<int, int>> cw(w + 1, {0, 0});
  rep(i, 0, w + 1) cw[i].second = i;
  rep(i, 0, m) {
    cin >> b[i].first >> b[i].second;
    ch[b[i].first].push_back(b[i].second);
    cw[b[i].second].first++;
  }
  size_t mhsize = 0;
  int mh;
  vector<int> a;
  rep(i, 0, h + 1) {
    if (ch[i].size() > mhsize) {
      mhsize = ch[i].size();
      a.clear();
      a.push_back(i);
    }
    if (ch[i].size() == mhsize) {
      a.push_back(i);
    }
  }
  for (auto n : a)
    sort(ch[n].begin(), ch[n].end());
  sort(cw.rbegin(), cw.rend());
  int mw = cw[0].first;
  int ans = 0;
  int z = mhsize;
  for (auto n : a) {
    rep(i, 0, w + 1) {
      int flag = 0;
      if (mw != cw[i].first)
        break;
      if (binary_search(ch[n].begin(), ch[n].end(), cw[i].second))
        flag++;
      ans = max(ans, z + mw - flag);
    }
  }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  map<pair<int, int>, int> v;
  vector<int> tate(W, 0);
  vector<int> yoko(H, 0);

  rep(i, M) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    tate[w]++;
    yoko[h]++;
    v[make_pair(h, w)] = 1;
  }

  vector<pair<int, int>> tates;
  rep(j, W) { tates.emplace_back(tate[j], j); }
  sort(ALLOF(tates));
  reverse(ALLOF(tates));

  int ret = 0;
  rep(i, H) {
    int sum = yoko[i];
    int mx = 0;
    rep(j, tates.size()) {
      int w = tates[j].second;
      int t = v.count(make_pair(i, w));
      if (t == 1) {
        mx = max(mx, tates[j].first - 1);
      } else {
        mx = max(mx, tates[j].first);
        break;
      }
    }
    ret = max(ret, sum + mx);
  }

  cout << ret << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<vector<int>> v(H, vector<int>(W, 0));
  rep(i, M) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    v[h][w] = 1;
  }

  vector<int> tate(W, 0);
  vector<int> yoko(H, 0);
  rep(i, H) {
    int sum = 0;
    rep(j, W) { sum += v[i][j]; }
    yoko[i] = sum;
  }
  rep(j, W) {
    int sum = 0;
    rep(i, H) { sum += v[i][j]; }
    tate[j] = sum;
  }

  int ret = 0;
  rep(i, H) {
    rep(j, W) {
      int sum = 0;
      sum += yoko[i] - v[i][j];
      sum += tate[j] - v[i][j];
      sum += v[i][j];
      ret = max(ret, sum);
    }
  }

  cout << ret << endl;

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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

struct A {
  int cnt, idx;
  A() { cnt = 0; }
} x[300001], y[300001];

bool cmp(A a, A b) { return a.cnt > b.cnt; }

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

  int h, w, m;
  cin >> h >> w >> m;
  int p, q;
  unordered_set<ll> us;
  while (m--) {
    cin >> p >> q;
    ++x[p].cnt;
    ++y[q].cnt;
    us.insert(ll(p) * 4 * 100000 + ll(q));
  }
  for (int i = 1; i <= h; ++i)
    x[i].idx = i;
  for (int i = 1; i <= w; ++i)
    y[i].idx = i;
  sort(x + 1, x + h + 1, cmp);
  sort(y + 1, y + w + 1, cmp);
  int i = 1, j = 1;
  int ans = 0;
  for (i = 1; i <= h; ++i) {
    if (x[i].cnt + y[1].cnt <= ans)
      break;
    for (j = 1; j <= w; ++j) {
      if (x[i].cnt + y[j].cnt <= ans)
        break;
      if (!us.count(ll(x[i].idx) * 4 * 100000 + ll(y[j].idx)))
        ans = x[i].cnt + y[j].cnt;
      else if (x[i].cnt + y[j].cnt - 1 > ans)
        ans = x[i].cnt + y[j].cnt - 1;
    }
  }
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

struct A {
  int cnt, idx;
  A() { cnt = 0; }
} x[300001], y[300001];

bool cmp(A a, A b) { return a.cnt > b.cnt; }

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

  int h, w, m;
  cin >> h >> w >> m;
  int p, q;
  unordered_set<ll> us;
  while (m--) {
    cin >> p >> q;
    ++x[p].cnt;
    ++y[q].cnt;
    us.insert(ll(p) * 4 * 100000 + ll(q));
  }
  for (int i = 1; i <= h; ++i)
    x[i].idx = i;
  for (int i = 1; i <= w; ++i)
    y[i].idx = i;
  sort(x + 1, x + h + 1, cmp);
  sort(y + 1, y + w + 1, cmp);
  int i = 1, j = 1;
  int ans = 0;
  for (i = 1; i <= h; ++i) {
    if (x[i].cnt + y[1].cnt < ans)
      break;
    for (j = 1; j <= w; ++j) {
      if (x[i].cnt + y[j].cnt < ans)
        break;
      if (!us.count(ll(x[i].idx) * 4 * 100000 + ll(y[j].idx)))
        ans = x[i].cnt + y[j].cnt;
      else if (x[i].cnt + y[j].cnt - 1 > ans)
        ans = x[i].cnt + y[j].cnt - 1;
    }
  }
  cout << ans << "\\n";
  return 0;
}