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

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
#define rep(i, n) for (i32 i = 0; i < (i32)(n); i++)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using P = pair<i64, i64>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  i64 h, w, m;
  cin >> h >> w >> m;
  set<P> st;
  i64 max_row = 0, max_col = 0;
  map<i64, i64> mp_row, mp_col;
  rep(i, m) {
    i64 H, W;
    cin >> H >> W;
    H--;
    W--;
    mp_row[H]++;
    mp_col[W]++;
    st.insert(P(H, W));
  }
  for (auto v : mp_row) {
    if (v.second > max_row) {
      max_row = v.second;
    }
  }
  for (auto v : mp_col) {
    if (v.second > max_col) {
      max_col = v.second;
    }
  }
  set<i64> row, col;
  i64 cnt_row = 0, cnt_col = 0;
  rep(i, h) {
    if (mp_row[i] == max_row)
      row.insert(i);
  }
  rep(i, w) {
    if (mp_col[i] == max_col)
      col.insert(i);
  }
  i64 ans = max_row + max_col;
  i64 cnt = 0;
  for (auto v : st) {
    if (row.count(v.first) && col.count(v.second))
      cnt++;
  }
  if (cnt == row.size() * col.size())
    ans--;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
#define rep(i, n) for (i32 i = 0; i < (i32)(n); i++)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using P = pair<i64, i64>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  i64 h, w, m;
  cin >> h >> w >> m;
  set<P> st;
  i64 max_row = 0, max_col = 0;
  vector<i64> mp_row(h), mp_col(w);
  rep(i, m) {
    i64 H, W;
    cin >> H >> W;
    H--;
    W--;
    mp_row[H]++;
    mp_col[W]++;
    st.insert(P(H, W));
  }
  for (auto v : mp_row) {
    if (v > max_row) {
      max_row = v;
    }
  }
  for (auto v : mp_col) {
    if (v > max_col) {
      max_col = v;
    }
  }
  vector<i64> row, col;
  rep(i, h) {
    if (mp_row[i] == max_row)
      row.emplace_back(i);
  }
  rep(i, w) {
    if (mp_col[i] == max_col)
      col.emplace_back(i);
  }
  i64 ans = 0;
  for (auto u : row) {
    for (auto v : col) {
      i64 diff = 0;
      if (st.count(P(u, v)))
        diff++;
      ans = max(mp_row[u] + mp_col[v] - diff, ans);
    }
  }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: 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);
  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;
}

Error: 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);
    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, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define P pair<int, int>
#define PI 3.141592653589793
const int INF = 1001001001;
const ll MX = 1e18;
const int mod = 1000000007;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> a(h);
  vector<int> b(w);
  map<P, int> mp;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x]++;
    b[y]++;
    P p = make_pair(x, y);
    mp[p]++;
  }

  vector<int> aa = a;
  vector<int> bb = b;
  sort(aa.rbegin(), aa.rend());
  sort(bb.rbegin(), bb.rend());
  int mh = aa[0], mw = bb[0];

  vector<int> c;
  vector<int> d;
  rep(i, h) {
    if (a[i] == mh) {
      c.push_back(i);
    }
  }
  rep(i, w) {
    if (b[i] == mw) {
      d.push_back(i);
    }
  }
  rep(i, c.size()) rep(j, d.size()) {
    P p = make_pair(c[i], d[j]);
    if (mp[p] == 0) {
      cout << mh + mw << endl;
      return 0;
    }
  }
  cout << mh + mw - 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define P pair<int, int>
#define PI 3.141592653589793
const int INF = 1001001001;
const ll MX = 1e18;
const int mod = 1000000007;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> a(h);
  vector<int> b(w);
  vector<vector<int>> map(h, vector<int>(w));
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x]++;
    b[y]++;
    map[x][y]++;
  }
  int ans = 0;
  vector<int> aa = a;
  vector<int> bb = b;
  sort(aa.rbegin(), aa.rend());
  sort(bb.rbegin(), bb.rend());
  int mh = aa[0], mw = bb[0];
  vector<int> c;
  vector<int> d;
  rep(i, h) {
    if (a[i] == mh) {
      c.push_back(i);
    }
  }
  rep(i, w) {
    if (b[i] == mw) {
      d.push_back(i);
    }
  }
  rep(i, c.size()) rep(j, d.size()) {
    if (map[c[i]][d[j]] == 0) {
      cout << mh + mw << endl;
      return 0;
    }
  }
  cout << mh + mw - 1 << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using ll = long long;

using namespace std;

#define amax(a, b) a = max(a, b)

using P = pair<int, int>;
#define fi first
#define se second

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  int cnt_x[h], cnt_y[w];
  fill(cnt_x, cnt_x + h, 0);
  fill(cnt_y, cnt_y + w, 0);
  set<P> st;
  int xmax = 0, ymax = 0;
  for (int i = 0, hi, wi; i < m; i++) {
    cin >> hi >> wi;
    hi--;
    wi--;
    st.insert(P{hi, wi});
    cnt_x[hi]++;
    cnt_y[wi]++;
    amax(xmax, cnt_x[hi]);
    amax(ymax, cnt_y[wi]);
  }
  vector<int> xbests, ybests;
  for (int i = 0; i < h; i++) {
    if (cnt_x[i] == xmax) {
      xbests.push_back(i);
    }
  }
  for (int i = 0; i < w; i++) {
    if (cnt_y[i] == ymax) {
      ybests.push_back(i);
    }
  }
  if ((ll)xbests.size() * ybests.size() > (ll)m) {
    cout << xmax + ymax << '\\n';
  } else {
    bool hole = false;
    for (auto &xi : xbests) {
      for (auto &yi : ybests) {
        if (st.count(P{xi, yi}) == 0)
          hole = true;
      }
    }
    cout << xmax + ymax - (int)(!hole) << '\\n';
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using ll = long long;

using namespace std;

#define amax(a, b) a = max(a, b)

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  int cnt_x[h], cnt_y[w];
  fill(cnt_x, cnt_x + h, 0);
  fill(cnt_y, cnt_y + w, 0);
  bool target[h][w];
  fill(target[0], target[h], false);
  int xmax = 0, ymax = 0;
  for (int i = 0, hi, wi; i < m; i++) {
    cin >> hi >> wi;
    hi--;
    wi--;
    target[hi][wi] = true;
    cnt_x[hi]++;
    cnt_y[wi]++;
    amax(xmax, cnt_x[hi]);
    amax(ymax, cnt_y[wi]);
  }
  vector<int> xbests, ybests;
  for (int i = 0; i < h; i++) {
    if (cnt_x[i] == xmax) {
      xbests.push_back(i);
    }
  }
  for (int i = 0; i < w; i++) {
    if (cnt_y[i] == ymax) {
      ybests.push_back(i);
    }
  }
  if ((ll)xbests.size() * ybests.size() > (ll)m) {
    cout << xmax + ymax << '\\n';
  } else {
    bool hole = false;
    for (auto &xi : xbests) {
      for (auto &yi : ybests) {
        if (!target[xi][yi])
          hole = true;
      }
    }
    cout << xmax + ymax - (int)(!hole) << '\\n';
  }
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#ifdef LOCAL
#define debug(var) cerr << (#var) << " = " << (var) << endl;
#else
#define debug(var)
#endif

void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

const int N = 3e5 + 23;

set<pair<int, int>> s;
pair<int, int> H[N], W[N];

void solve() {
  int h, w, m;
  scanf("%d%d%d", &h, &w, &m);
  for (int i = 0; i < m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    s.emplace(--x, --y);
    ++H[x].first;
    ++W[y].first;
  }
  for (int i = 0; i < h; ++i)
    H[i].second = i;
  for (int i = 0; i < w; ++i)
    W[i].second = i;
  sort(H, H + h, greater<pair<int, int>>());
  sort(W, W + w, greater<pair<int, int>>());
  int ans = 0;
  for (int i = 0; i < h; ++i) {
    if (H[i].first < H[0].first)
      break;
    for (int j = 0; j < w; ++j) {
      if (W[j].first < W[0].first)
        break;
      int cur = H[i].first + W[j].first;
      if (s.count({H[i].second, W[j].second}))
        --cur;
      ans = max(ans, cur);
      if (ans == H[0].first + W[0].first) {
        goto end;
      }
    }
  }
end:;
  printf("%d", ans);
}

int main() {
  init();
  int t = 1; // scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#ifdef LOCAL
#define debug(var) cerr << (#var) << " = " << (var) << endl;
#else
#define debug(var)
#endif

void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

const int N = 3e5 + 23;

set<pair<int, int>> s;
pair<int, int> H[N], W[N];

void solve() {
  int h, w, m;
  scanf("%d%d%d", &h, &w, &m);
  for (int i = 0; i < m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    s.emplace(--x, --y);
    ++H[x].first;
    ++W[y].first;
  }
  for (int i = 0; i < h; ++i)
    H[i].second = i;
  for (int i = 0; i < w; ++i)
    W[i].second = i;
  sort(H, H + h, greater<pair<int, int>>());
  sort(W, W + w, greater<pair<int, int>>());
  int ans = 0;
  for (int i = 0; i < h; ++i) {
    if (H[i].first < H[0].first)
      break;
    for (int j = 0; j < w; ++j) {
      if (W[j].first < W[0].first)
        break;
      int cur = H[i].first + W[j].first;
      if (s.count({H[i].second, W[j].second}))
        --cur;
      ans = max(ans, cur);
    }
  }
  printf("%d", ans);
}

int main() {
  init();
  int t = 1; // scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

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

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define ALL(a) a.begin(), a.end()
#define INIT(a, b) memset(a, b, sizeof(a))
using lint = long long int;

template <typename T> class RangeMinimumQuery {
private:
  int N;
  vector<T> rmq;

public:
  RangeMinimumQuery() {}
  RangeMinimumQuery(vector<T> &ary) { build(ary); }

  void build(vector<T> &ary) {
    N = ary.size();

    int logN = 1;
    for (int k = 1; k < N; k *= 2)
      ++logN;

    int *r = new int[N * logN];
    int *b = r;
    copy(ary.begin(), ary.begin() + N, b);

    for (int k = 1; k < N; k *= 2) {
      copy(b, b + N, b + N);
      b += N;
      for_(i, 0, N - k) b[i] = min(b[i], b[i + k]);
    }

    rmq.assign(N * logN, 0);
    int len = N * logN;
    for_(i, 0, len) rmq[i] = r[i];

    delete[] r;
  }

  T query(int l, int r) { //[l,r]
    int z = r - l, k = 0, e = 1, s;
    s = ((z & 0xffff0000) != 0) << 4;
    z >>= s;
    e <<= s;
    k |= s;
    s = ((z & 0x0000ff00) != 0) << 3;
    z >>= s;
    e <<= s;
    k |= s;
    s = ((z & 0x000000f0) != 0) << 2;
    z >>= s;
    e <<= s;
    k |= s;
    s = ((z & 0x0000000c) != 0) << 1;
    z >>= s;
    e <<= s;
    k |= s;
    s = ((z & 0x00000002) != 0) << 0;
    z >>= s;
    e <<= s;
    k |= s;
    return min(rmq[l + N * k], rmq[r + N * k - e + 1]);
  }
};

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

  vector<int> y(M), x(M);
  for_(i, 0, M) {
    cin >> y[i] >> x[i];
    --y[i];
    --x[i];
  }

  vector<int> sum_y(H, 0), sum_x(W, 0);
  for_(i, 0, M) {
    sum_y[y[i]]++;
    sum_x[x[i]]++;
  }

  int ans = 0;
  for_(i, 0, M) ans = max(ans, sum_y[y[i]] + sum_x[x[i]] - 1);

  //
  vector<vector<int>> X(H, vector<int>());
  for_(i, 0, M) X[y[i]].push_back(x[i]);
  for_(y, 0, H) {
    X[y].push_back(-1);
    X[y].push_back(W);
    sort(ALL(X[y]));
  }

  for_(x, 0, W) sum_x[x] *= -1;
  RangeMinimumQuery<int> RMQ(sum_x);
  for_(y, 0, H) {
    int m = X[y].size();
    for_(i, 0, m - 1) {
      int L = X[y][i] + 1, R = X[y][i + 1] - 1;
      if (L > R)
        continue;
      int a = RMQ.query(L, R);
      ans = max(ans, sum_y[y] - a);
    }
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define ALL(a) a.begin(), a.end()
#define INIT(a, b) memset(a, b, sizeof(a))
using lint = long long int;

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

  vector<vector<int>> bom(H, vector<int>(W, 0));
  for_(i, 0, M) {
    int y, x;
    cin >> y >> x;
    --y;
    --x;
    bom[y][x] += 1;
  }

  vector<int> sum_v(H, 0), sum_h(W, 0);
  for_(x, 0, W) for_(y, 0, H) sum_v[y] += bom[y][x];
  for_(y, 0, H) for_(x, 0, W) sum_h[x] += bom[y][x];

  int ans = 0;
  for_(y, 0, H) for_(x, 0, W) {
    ans = max(ans, sum_v[y] + sum_h[x] - bom[y][x]);
  }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int h, w, m, ans = 0, th = 0, tw = 0;
  cin >> h >> w >> m;
  v1i bh(h, 0), bw(w, 0), mh, mw;
  set<pii> b;
  rep(i, m) {
    int ih, iw;
    cin >> ih >> iw;
    ih--;
    iw--;
    bh[ih]++;
    bw[iw]++;
    b.insert({ih, iw});
  }
  rep(i, h) th = max(th, bh[i]);
  rep(i, w) tw = max(tw, bw[i]);
  rep(i, h) if (th == bh[i]) mh.push_back(i);
  rep(i, w) if (tw == bw[i]) mw.push_back(i);
  ans = th + tw;
  long tc = (long)mh.size() * (long)mw.size();
  if (tc <= (long)m) {
    ans--;
    bool tb = 0;
    rep(i, mh.size()) {
      rep(j, mw.size()) {
        pii tpi = {mh[i], mw[j]};
        if (!b.count(tpi)) {
          ans++;
          tb = 1;
          break;
        }
      }
      if (tb)
        break;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int h, w, m, ans = 0;
  cin >> h >> w >> m;
  v1i bh(h, 0), bw(w, 0);
  v1b b(h * w, 0);
  rep(i, m) {
    int ih, iw;
    cin >> ih >> iw;
    ih--;
    iw--;
    bh[ih]++;
    bw[iw]++;
    b[ih * w + iw] = 1;
  }
  rep(i, h * w) {
    int tmp = bh[i / w] + bw[i % w];
    if (b[i])
      tmp--;
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
unordered_map<int, int> bomb[300100];
struct node {
  int index;
  int cnt;
} xx[300100], yy[300100];
bool cmp(node a, node b) { return a.cnt > b.cnt; }
int main() {
  int n, m;
  cin >> n >> m;
  int num;
  cin >> num;
  for (int i = 1; i <= n; i++) {
    xx[i].index = i;
  }
  for (int i = 1; i <= m; i++) {
    yy[i].index = i;
  }
  for (int i = 1; i <= num; i++) {
    int x, y;
    cin >> x >> y;
    bomb[x][y] = 1;
    xx[x].cnt++;
    yy[y].cnt++;
  }
  sort(xx + 1, xx + n + 1, cmp);
  sort(yy + 1, yy + m + 1, cmp);
  int cntx = n, cnty = m;
  for (int i = 2; i <= n; i++) {
    if (xx[i].cnt < xx[i - 1].cnt) {
      cntx = i - 1;
      break;
    }
  }
  for (int i = 2; i <= m; i++) {
    if (yy[i].cnt < yy[i - 1].cnt) {
      cnty = i - 1;
      break;
    }
  }
  int ans = 0;
  for (int i = 1; i <= cntx; i++) {
    for (int j = 1; j <= cnty; j++) {
      if (bomb[xx[i].index][yy[j].index] == 1) {
        ans = max(ans, xx[i].cnt + yy[j].cnt - 1);
      } else {
        ans = max(ans, xx[i].cnt + yy[j].cnt);
        break;
      }
    }
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
unordered_map<int, int> bomb[300100];
struct node {
  int index;
  int cnt;
} xx[300100], yy[300100];
bool cmp(node a, node b) { return a.cnt > b.cnt; }
int main() {
  int n, m;
  cin >> n >> m;
  int num;
  cin >> num;
  for (int i = 1; i <= n; i++) {
    xx[i].index = i;
  }
  for (int i = 1; i <= m; i++) {
    yy[i].index = i;
  }
  for (int i = 1; i <= num; i++) {
    int x, y;
    cin >> x >> y;
    bomb[x][y] = 1;
    xx[x].cnt++;
    yy[y].cnt++;
  }
  sort(xx + 1, xx + n + 1, cmp);
  sort(yy + 1, yy + m + 1, cmp);
  int cntx = n, cnty = m;
  for (int i = 2; i <= n; i++) {
    if (xx[i].cnt < xx[i - 1].cnt)
      cntx = i - 1;
  }
  for (int i = 2; i <= m; i++) {
    if (yy[i].cnt < yy[i - 1].cnt)
      cnty = i - 1;
  }
  int ans = 0;
  for (int i = 1; i <= cntx; i++) {
    for (int j = 1; j <= cnty; j++) {
      if (bomb[xx[i].index][yy[j].index] == 1) {
        ans = max(ans, xx[i].cnt + yy[j].cnt - 1);
      } else {
        ans = max(ans, xx[i].cnt + yy[j].cnt);
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define ll long long int

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> r(n, 0), c(m, 0);

  set<pair<ll, ll>> st;
  for (ll i = 0; i < k; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    st.insert({a, b});
    r[a]++;
    c[b]++;
  }
  ll maxr = -1, maxc = -1;
  for (ll i = 0; i < n; i++) {
    if (r[i] > maxr) {
      maxr = r[i];
    }
  }

  vector<ll> vr, vc;
  for (ll i = 0; i < n; i++) {
    if (r[i] == maxr) {
      vr.push_back(i);
    }
  }
  for (ll i = 0; i < m; i++) {

    if (c[i] > maxc) {

      maxc = c[i];
    }
  }
  for (ll i = 0; i < m; i++) {

    if (c[i] == maxc) {

      vc.push_back(i);
    }
  }

  ll ans = 0;
  ll n1 = vr.size(), m1 = vc.size();
  for (ll i = 0; i < n1; i++) {
    for (ll j = 0; j < m1; j++) {

      if (st.find({vr[i], vc[j]}) == st.end()) {
        ans = max(ans, r[vr[i]] + c[vc[j]]);
        break;
      } else
        ans = max(ans, r[vr[i]] + c[vc[j]] - 1);
    }
  }

  cout << ans;
}


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define ll long long int

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> r(n, 0), c(m, 0);

  set<pair<ll, ll>> st;
  for (ll i = 0; i < k; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    st.insert({a, b});
    r[a]++;
    c[b]++;
  }
  ll maxr = -1, maxc = -1;
  for (ll i = 0; i < n; i++) {
    if (r[i] > maxr) {
      maxr = r[i];
    }
  }

  vector<ll> vr, vc;
  for (ll i = 0; i < n; i++) {
    if (r[i] == maxr) {
      vr.push_back(i);
    }
  }
  for (ll i = 0; i < m; i++) {

    if (c[i] > maxc) {

      maxc = c[i];
    }
  }
  for (ll i = 0; i < m; i++) {

    if (c[i] == maxc) {

      vc.push_back(i);
    }
  }

  ll ans = 0;
  ll n1 = vr.size(), m1 = vc.size();
  for (ll i = 0; i < n1; i++) {
    for (ll j = 0; j < m1; j++) {

      if (st.find({vr[i], vc[j]}) == st.end())
        ans = max(ans, r[vr[i]] + c[vc[j]]);
      else
        ans = max(ans, r[vr[i]] + c[vc[j]] - 1);
    }
  }

  cout << ans;
}