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

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  ll h, w, m;
  cin >> h >> w >> m;
  vector<pii> yx(m);
  rep(i, m) cin >> yx[i].first >> yx[i].second;
  set<ll> st;
  VL smh(h + 3), smw(w + 3);
  for (auto &z : yx) {
    st.insert(z.first * w + z.second);
    smw[z.second]++;
    smh[z.first]++;
  }

  auto ck = [&](ll a, ll b) { return st.count(a * w + b) > 0; };

  vector<pii> srh, srw;
  rep(i, h + 1) srh.emplace_back(smh[i], i);
  rep(i, w + 1) srw.emplace_back(smw[i], i);

  sort(srh.rbegin(), srh.rend());
  sort(srw.rbegin(), srw.rend());

  ll mx = srh[0].first + srw[0].first;
  int ct = 0;
  rep(i, sz(srh)) {
    rep(j, sz(srw)) {
      ll kei = srh[i].first + srw[j].first;
      if (kei < mx)
        break;
      if (!ck(srh[i].second, srw[j].second)) {
        cout << mx << "\\n";
        return 0;
      }
      if (++ct > m + 1) {
        cout << mx << "\\n";
        return 0;
      }
    }
  }

  cout << mx - 1 << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int h, w, m;
  cin >> h >> w >> m;
  vector<pii> yx(m);
  rep(i, m) cin >> yx[i].first >> yx[i].second;
  set<ll> st;
  VL smh(h + 3), smw(w + 3);
  for (auto &z : yx) {
    st.insert(z.first * w + z.second);
    smw[z.second]++;
    smh[z.first]++;
  }

  vector<pii> srth(h);
  rep(i, m) srth[i] = pii(smh[i], i);
  sort(srth.rbegin(), srth.rend());

  ll ans = 0;
  rep(i, m) {
    ll cnd = smw[i];
    rep(j, 2) {

      ll wq = cnd + srth[j].first - st.count(i * w + srth[j].second);
      ans = max(ans, wq);
    }
  }
  cout << ans << "\\n";

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

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
// const long long MOD = 998244353;
const long long INF_LL = 1e18 + 11;
const int INF_int = 1e9 + 11;
#define ll long long
#define mp std::make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define lrep(i, n) for (long long i = 0; i < n; i++)
#define All(a) a.begin(), a.end()
#define Debug_Output_ALL(a)                                                    \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl;
#define pii pair<int, int>
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  map<int, int> xvec;
  map<int, int> yvec;
  map<pii, int> isthere;
  int xmax, ymax;
  xmax = ymax = 0;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    xvec[x]++;
    yvec[y]++;
    xmax = max(xmax, xvec[x]);
    ymax = max(ymax, yvec[y]);
    isthere[mp(x, y)] = 1;
  }
  vector<int> x_candidate, y_candidate;
  for (auto x : xvec) {
    if (x.second == xmax)
      x_candidate.push_back(x.first);
  }
  for (auto y : yvec) {
    if (y.second == ymax)
      y_candidate.push_back(y.first);
  }
  bool fl = false;
  rep(i, x_candidate.size()) {
    rep(j, y_candidate.size()) {
      int nowx = x_candidate[i];
      int nowy = y_candidate[j];
      if (isthere[mp(nowx, nowy)] == 0) {
        fl = true;
        break;
      }
    }
    if (fl)
      break;
  }

  int ans;
  if (fl)
    ans = xmax + ymax;
  else
    ans = xmax + ymax - 1;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
// const long long MOD = 998244353;
const long long INF_LL = 1e18 + 11;
const int INF_int = 1e9 + 11;
#define ll long long
#define mp std::make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define lrep(i, n) for (long long i = 0; i < n; i++)
#define All(a) a.begin(), a.end()
#define Debug_Output_ALL(a)                                                    \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl;
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  int field[h][w];
  rep(i, h) rep(j, w) field[i][j] = 0;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    field[x][y] = 1;
  }
  vector<int> xvec(h);
  vector<int> yvec(w);
  rep(i, h) {
    xvec[i] = 0;
    rep(j, w) xvec[i] += field[i][j];
  }
  rep(j, w) {
    yvec[j] = 0;
    rep(i, h) yvec[j] += field[i][j];
  }
  int ans = 0;
  rep(i, h) rep(j, w) { ans = max(ans, xvec[i] + yvec[j] - field[i][j]); }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
int n, m, k;
map<pair<int, int>, int> mp;
const int MAXN = 3e5 + 50;
struct node {
  int ord;
  int cnt;
} r[MAXN], c[MAXN];

bool cmp(struct node &a, struct node &b) { return a.cnt > b.cnt; }

inline void readInt(int &x) {
  static char c;
  c = getchar();
  static int minus;
  minus = 1;
  for (x = 0; !(c >= '0' && c <= '9'); c = getchar())
    if (c == '-')
      minus = -1;
  for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = getchar())
    ;
  x *= minus;
}

int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    int x, y;
    readInt(x);
    readInt(y);
    mp[{x, y}] = 1;
    r[x].ord = x;
    r[x].cnt++;
    c[y].ord = y;
    c[y].cnt++;
  }
  sort(r, r + MAXN, cmp);
  sort(c, c + MAXN, cmp);

  int mxr = r[0].cnt;
  int mxc = c[0].cnt;
  int res = mxr + mxc;
  int i = 0;
  bool can = false;
  while (r[i].cnt == mxr && !can) {
    int j = 0;
    while (c[j].cnt == mxc && !can) {
      if (!mp[{r[i].ord, c[j].ord}])
        can = true;
      j++;
    }
    i++;
  }
  if (can)
    cout << res << endl;
  else
    cout << res - 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
int n, m, k;
map<pair<int, int>, int> mp;
const int MAXN = 3e5 + 50;
struct node {
  int ord;
  int cnt;
} r[MAXN], c[MAXN];

bool cmp(struct node &a, struct node &b) { return a.cnt > b.cnt; }

int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    mp[{x, y}] = 1;
    r[x].ord = x;
    r[x].cnt++;
    c[y].ord = y;
    c[y].cnt++;
  }
  sort(r, r + MAXN, cmp);
  sort(c, c + MAXN, cmp);
  int res = 0;
  int mxr = r[0].cnt;
  int mxc = c[0].cnt;
  int i = 0;
  while (r[i].cnt == mxr) {
    int j = 0;
    while (c[j].cnt == mxc) {
      res = max(res, mxr + mxc - mp[{r[i].ord, c[j].ord}]);
      j++;
    }
    i++;
  }
  cout << res << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  set<pair<int, int>> st;
  vector<int> x(300010, 0), y(300010, 0);
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    x[h]++;
    y[w]++;
    st.insert({h, w});
  }

  int xmax = 0, ymax = 0;
  for (int i = 0; i < 300010; i++) {
    xmax = max(xmax, x[i]);
    ymax = max(ymax, y[i]);
  }
  vector<int> argxmax, argymax;
  for (int i = 0; i < 300010; i++) {
    if (x[i] == xmax)
      argxmax.push_back(i);
    if (y[i] == ymax)
      argymax.push_back(i);
  }
  if ((ll)argxmax.size() * (ll)argymax.size() > M) {
    cout << xmax + ymax << endl;
    return 0;
  } else {
    int cand = xmax + ymax - 1;
    for (int n : argxmax) {
      for (int m : argymax) {
        auto it = st.find({n, m});
        if (it == st.end()) {
          cout << xmax + ymax << endl;
          return 0;
        } else {
          st.erase(it);
        }
      }
    }
    cout << cand << endl;
    return 0;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  set<pair<int, int>> st;
  vector<int> x(300010, 0), y(300010, 0);
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    x[h]++;
    y[w]++;
    st.insert({h, w});
  }

  int xmax = 0, ymax = 0;
  for (int i = 0; i < 300010; i++) {
    xmax = max(xmax, x[i]);
    ymax = max(ymax, y[i]);
  }
  vector<int> argxmax, argymax;
  for (int i = 0; i < 300010; i++) {
    if (x[i] == xmax)
      argxmax.push_back(i);
    if (y[i] == ymax)
      argymax.push_back(i);
  }
  if ((int)argxmax.size() * (int)argymax.size() > M) {
    cout << xmax + ymax << endl;
    return 0;
  } else {
    int cand = xmax + ymax - 1;
    for (int n : argxmax) {
      for (int m : argymax) {
        if (st.find({n, m}) == st.end())
          cand = xmax + ymax;
      }
    }
    cout << cand << endl;
    return 0;
  }
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  vector<ll> row(H), col(W);
  set<P> s;
  rep(i, M) {
    ll r, c;
    cin >> r >> c;
    --r;
    --c;
    row[r]++;
    col[c]++;
    s.insert({r, c});
  }
  ll mrow = 0;
  ll mcol = 0;
  rep(i, H) mrow = max(mrow, row.at(i));
  rep(i, W) mcol = max(mcol, col.at(i));

  vector<ll> is, js;
  rep(i, H) if (mrow == row.at(i)) is.push_back(i);
  rep(i, W) if (mcol == col.at(i)) js.push_back(i);

  ll ans = mrow + mcol;
  for (ll i : is) {
    for (ll j : js) {
      // i,jが重ならない(=ansがそのまま)のパターンを探す
      if (s.count({i, j}))
        continue;
      cout << ans << endl;
      return 0;
    }
  }
  cout << --ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<ll> tate(h);
  vector<ll> yoko(w);
  map<P, ll> mp;
  rep(i, m) {
    ll nh, nw;
    cin >> nh >> nw;
    nh--;
    nw--;
    tate.at(nh)++;
    yoko.at(nw)++;
    mp[make_pair(nh, nw)]++;
  }

  ll ans = -1;
  rep(i, h) {
    rep(j, w) { ans = max(ans, tate.at(i) + yoko.at(j) - mp[make_pair(i, j)]); }
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
#define repl(i, l, r) for (int i = l; i < r; ++i)
#define rep(i, n) repl(i, 0, n)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using G = vector<vector<int>>;
const int MOD = 1000000007;
const int INF = 1001001001;
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, -1, 0, 1};

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> hs(h), ws(w);
  set<P> s;
  rep(i, m) {
    int r, c;
    cin >> r >> c;
    --r;
    --c;
    hs[r]++;
    ws[c]++;
    s.emplace(r, c);
  }
  int mh = 0;
  int 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.pb(i);
  rep(j, w) if (mw == ws[j]) js.pb(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;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> hv(m);
  vector<int> wv(m);
  map<int, int> hh;
  map<int, int> ww;
  vector<vector<bool>> mi(h, vector<bool>(w, false));
  rep(i, m) {
    cin >> hv[i] >> wv[i];
    --hv[i];
    --wv[i];
    hh[hv[i]]++;
    ww[wv[i]]++;
    mi[hv[i]][wv[i]] = true;
  }
  int mh = 0;
  int mw = 0;
  vector<int> kh(1, 0);
  vector<int> kw(1, 0);
  for (auto x : hh) {
    if (x.second > mh) {
      mh = x.second;
      kh.clear();
      kh.push_back(x.first);
    } else if (x.second == mh) {
      kh.push_back(x.first);
    }
  }
  for (auto x : ww) {
    if (x.second > mw) {
      mw = x.second;
      kw.clear();
      kw.push_back(x.first);
    } else if (x.second == mw) {
      kw.push_back(x.first);
    }
  }
  bool ok = false;
  bool fin = false;
  if (kh.empty() && kw.empty()) {
    cout << 0 << endl;
    return 0;
  }
  if (kh.empty()) {
    cout << mw << endl;
    return 0;
  }
  if (kw.empty()) {
    cout << mh << endl;
    return 0;
  }
  for (auto y : kh) {
    for (auto z : kw) {
      if (mi[y][z] == false) {
        ok = true;
        fin = true;
        break;
      }
    }
    if (fin)
      break;
  }
  if (ok)
    cout << mh + mw << endl;
  else
    cout << mh + mw - 1 << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

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

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

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

#include <bits/stdc++.h>

using namespace ::std;

long h[300001];
long w[300001];

int main() {
  long n, res = 0, i, j, hi, wi, H, W, M, x, y, a = 0, b = 0, ko = 1;
  set<pair<long, long>> s;
  vector<long> numx, numy;
  cin >> H >> W >> M;
  for (i = 0; i < M; i++) {
    cin >> hi >> wi;
    s.insert(make_pair(hi, wi));
    h[hi]++;
    w[wi]++;
  }
  for (i = 0; i <= H; i++)
    if (a < h[i])
      a = h[i];
  for (i = 0; i <= W; i++)
    if (b < w[i])
      b = w[i];

  for (i = 0; i <= H; i++)
    if (a == h[i])
      numx.push_back(i);
  for (i = 0; i <= W; i++)
    if (b == w[i])
      numy.push_back(i);

  for (i = 0; i < numx.size() && ko == 1; i++) {
    for (j = 0; j < numy.size() && ko == 1; j++) {
      x = numx.at(i);
      y = numy.at(j);
      ko = s.count(make_pair(x, y));
      res = a + b - ko;
    }
  }
  cout << res;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace ::std;

long h[300001];
long w[300001];

int main() {
  long n, res = 0, tmp, i, j, hi, wi, H, W, M, x, y, a = 0, b = 0;
  set<pair<long, long>> s;
  vector<long> numx, numy;
  cin >> H >> W >> M;
  for (i = 0; i < M; i++) {
    cin >> hi >> wi;
    s.insert(make_pair(hi, wi));
    h[hi]++;
    w[wi]++;
  }
  for (i = 0; i <= H; i++)
    if (a < h[i])
      a = h[i];
  for (i = 0; i <= W; i++)
    if (b < w[i])
      b = w[i];

  for (i = 0; i <= H; i++)
    if (a == h[i])
      numx.push_back(i);
  for (i = 0; i <= W; i++)
    if (b == w[i])
      numy.push_back(i);

  for (i = 0; i < numx.size(); i++) {
    for (j = 0; j < numy.size(); j++) {
      x = numx.at(i);
      y = numy.at(j);
      tmp = a + b - s.count(make_pair(x, y));
      res = std::max(tmp, res);
    }
  }
  cout << res;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int iINF = 1001001001;
const long long llINF = 1LL << 60;
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> hor(h, 0), ver(w, 0);
  vector<P> bomb(m);
  rep(i, m) {
    cin >> bomb[i].first >> bomb[i].second;
    bomb[i].first--;
    bomb[i].second--;
    hor[bomb[i].first]++;
    ver[bomb[i].second]++;
  }
  vector<int> hor2 = hor;
  vector<int> ver2 = ver;
  sort(rALL(hor));
  sort(rALL(ver));
  ll ans = hor[0] + ver[0];
  int horNum, verNum;
  horNum = verNum = 0;
  rep(i, h) {
    if (hor[i] == hor[0])
      horNum++;
    else
      break;
  }
  rep(i, w) {
    if (ver[i] == ver[0])
      verNum++;
    else
      break;
  }
  int num = 0;
  rep(i, m) {
    if (hor2[bomb[i].first] == hor[0] && ver2[bomb[i].second] == ver[0])
      num++;
  }
  if (num == horNum * verNum)
    ans--;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int iINF = 1001001001;
const long long llINF = 1LL << 60;
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<P> hor(h, P(0, 0)), ver(w, P(0, 0));
  rep(i, h) hor[i].second = i;
  rep(i, w) ver[i].second = i;
  vector<vector<bool>> bomb(h, vector<bool>(w, false));
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    bomb[a][b] = true;
    hor[a].first++;
    ver[b].first++;
  }
  sort(rALL(hor));
  sort(rALL(ver));
  ll ans = hor[0].first + ver[0].first;
  if (bomb[hor[0].second][ver[0].second] == true &&
      (hor[0] != hor[1] || ver[0] != ver[1]))
    ans--;
  if (hor[0].first != w || ver[0].first != h)
    ans++;
  cout << ans << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long LL;
const int N = 3e5 + 10;
LL n, m, w;
int r[N];
int c[N];
int qr[N], qc[N];
int r_max, c_max;
int main() {
  scanf("%lld%lld%lld", &n, &m, &w);
  for (LL i = 0; i < w; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    r[a]++;
    c[b]++;
    r_max = max(r_max, r[a]);
    c_max = max(c_max, c[b]);
    qr[i] = a;
    qc[i] = b;
  }
  LL R = 0, C = 0;
  for (int i = 1; i <= n; ++i) {
    if (r_max == r[i])
      R++;
  }
  for (int i = 1; i <= m; ++i) {
    if (c_max == c[i])
      C++;
  }
  LL cnt = 0;
  for (LL i = 0; i < w; ++i) {
    int a, b;
    a = qr[i];
    b = qc[i];
    if (r[a] == r_max && c[b] == c_max)
      cnt++;
  }
  LL t = R * C;
  LL res = r_max + c_max;
  if (t == cnt)
    res--;
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <unordered_set>
using namespace std;

const int N = 3e5 + 10;

typedef pair<int, int> PII;

string row[N];
string col[N];
int n, m, w;
unordered_set<string> s;

int main() {
  cin >> n >> m >> w;
  for (int i = 0; i < w; ++i) {
    int a, b;
    cin >> a >> b;
    row[a - 1] += '1';
    col[b - 1] += '1';
    string t;
    t += (a - 1) + '0';
    t += (b - 1) + '0';
    s.insert(t);
  }

  int res = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      int cnt = row[i].size() + col[j].size();
      string t;
      t += i + '0';
      t += j + '0';
      if (s.count(t) != 0)
        cnt--;
      res = max(res, cnt);
    }
  cout << res << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded


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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  map<pair<int, int>, int> m;
  long long int row[h + 5];
  long long int col[w + 5];
  for (int i = 0; i <= h; i++) {
    row[i] = 0;
  }
  for (int i = 0; i <= w; i++) {
    col[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    row[a]++;
    col[b]++;
    m.insert(make_pair(make_pair(a, b), 1));
  }
  long long int maxX = 0, maxY = 0;
  for (int i = 1; i <= h; i++) {
    maxX = max(maxX, row[i]);
  }
  for (int i = 1; i <= w; i++) {
    maxY = max(maxY, col[i]);
  }
  vector<int> candiX;
  vector<int> candiY;
  for (int i = 1; i <= h; i++) {
    if (row[i] == maxX) {
      candiX.push_back(i);
    }
  }
  for (int i = 1; i <= w; i++) {
    if (col[i] == maxY) {
      candiY.push_back(i);
    }
  }

  long long int ans = maxX + maxY - 1;
  bool b = false;
  // cout<<maxX<<"****"<<maxY<<endl;
  for (int i = 0; i < candiX.size(); i++) {
    if (b == true)
      break;
    for (int j = 0; j < candiY.size(); j++) {

      if (m.find(make_pair(candiX[i], candiY[j])) == m.end()) // not found
      {
        ans++;
        // cout<<"Ye chasla for "<<candiX[i]<<" "<<candiY[j]<<endl;
        b = true;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:


#include <iostream>
#include <map>
#include <utility>
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  map<pair<int, int>, int> m;
  long long int row[h + 5];
  long long int col[w + 5];
  for (int i = 0; i <= h; i++) {
    row[i] = 0;
  }
  for (int i = 0; i <= w; i++) {
    col[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    row[a]++;
    col[b]++;
    m.insert(make_pair(make_pair(a, b), 1));
  }
  long long int maxX = 0, maxY = 0;
  for (int i = 1; i <= h; i++) {
    maxX = max(maxX, row[i]);
  }
  for (int i = 1; i <= w; i++) {
    maxY = max(maxY, col[i]);
  }
  long long int ans = maxX + maxY - 1;
  bool b = false;
  // cout<<maxX<<"****"<<maxY<<endl;
  for (int i = 1; i <= h; i++) {
    if (row[i] == maxX)
      for (int j = 1; j <= w; j++) {
        if (b == true)
          break;
        if (col[j] == maxY) {
          if (m.find(make_pair(i, j)) == m.end()) // not found
          {
            ans++;
            // cout<<"Ye chasla for "<<i<<" "<<j<<endl;
            b = true;
            break;
          }
        }
      }
  }
  cout << ans << endl;
  return 0;
}