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

#include <bits/stdc++.h>
using namespace std;
// typedef long long int;
typedef vector<int> vi;
#define endl '\\n'
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define fast_io ios_base::sync_with_stdio(0);
#define show(x) cout << x << endl;
#define pret(x)                                                                \\
  cout << x << endl;                                                           \\
  return 0;
#define disp(x) cout << x << " ";
#define let(x, y) cout << x << " " << y << endl;
int MOD = 1e9 + 7;
const int maxn = 3e5 + 5;

int r[maxn], c[maxn];
vi matrix[maxn];

int32_t main() {
  // fast_io;
  int n, m;
  cin >> n >> m;
  int t;
  cin >> t;
  vector<pair<int, int>> v;
  while (t--) {
    int x, y;
    scanf("%d%d", &x, &y);
    // cin >> x >> y;
    matrix[x].emplace_back(y);
    v.emplace_back(x, y);
    r[x]++;
    c[y]++;
  }
  int ans = 0;
  for (const auto &it : v) {
    ans = max(ans, r[it.first] + c[it.second] - 1);
  }
  set<pair<int, int>> s;
  for (int i = 1; i <= m; ++i) {
    if (c[i] > 0) {
      s.insert({c[i], i});
      // update(i, c[i]);
    }
  }
  for (int i = 1; i < n + 1; ++i) {
    for (const auto &it : matrix[i]) {
      // update(it, -c[it]);
      s.erase(s.find({c[it], it}));
    }

    if (!s.empty())
      ans = max(ans, r[i] + (*(s.rbegin())).first);
    for (const auto &it : matrix[i]) {
      // update(it, c[it]);
      s.insert({c[it], it});
    }
  }
  show(ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// typedef long long int;
typedef vector<int> vi;
#define endl '\\n'
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define fast_io ios_base::sync_with_stdio(0);
#define show(x) cout << x << endl;
#define pret(x)                                                                \\
  cout << x << endl;                                                           \\
  return 0;
#define disp(x) cout << x << " ";
#define let(x, y) cout << x << " " << y << endl;
int MOD = 1e9 + 7;
const int maxn = 3e5 + 5;

int r[maxn], c[maxn];
vi matrix[maxn];

int32_t main() {
  // fast_io;
  int n, m;
  cin >> n >> m;
  int t;
  cin >> t;
  vector<pair<int, int>> v;
  while (t--) {
    int x, y;
    scanf("%d%d", &x, &y);
    // cin >> x >> y;
    matrix[x].emplace_back(y);
    v.emplace_back(x, y);
    r[x]++;
    c[y]++;
  }
  int ans = 0;
  for (const auto &it : v) {
    ans = max(ans, r[it.first] + c[it.second] - 1);
  }
  multiset<int> s;
  for (int i = 1; i <= m; ++i) {
    if (c[i] > 0) {
      s.insert(c[i]);
      // update(i, c[i]);
    }
  }
  for (int i = 1; i < n + 1; ++i) {
    for (const auto &it : matrix[i]) {
      // update(it, -c[it]);
      s.erase(s.find(c[it]));
    }

    if (s.empty())
      continue;
    ans = max(ans, r[i] + *(s.rbegin()));
    for (const auto &it : matrix[i]) {
      // update(it, c[it]);
      s.insert(c[it]);
    }
  }
  show(ans);
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> r(h), c(w);
  map<pair<int, int>, bool> mp;
  REP(i, m) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    mp[{x, y}] = true;
    r[x]++, c[y]++;
  }
  int ans = 0;
  int mh = 0, mw = 0;
  vector<int> hh, ww;
  REP(i, h) {
    if (mh == r[i]) {
      hh.push_back(i);
    } else if (mh < r[i]) {
      mh = r[i];
      hh.clear();
      hh.push_back(i);
    }
  }
  REP(i, w) {
    if (mw == c[i]) {
      ww.push_back(i);
    } else if (mw < c[i]) {
      mw = c[i];
      ww.clear();
      ww.push_back(i);
    }
  }
  for (int i : hh) {
    for (int j : ww) {
      if (!mp[{i, j}]) {
        cout << mh + mw << endl;
        return 0;
      }
    }
  }
  cout << mh + mw - 1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> r(h), c(w);
  map<pair<int, int>, bool> mp;
  REP(i, m) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    mp[{x, y}] = true;
    r[x]++, c[y]++;
  }
  int ans = 0;
  REP(i, h) {
    REP(j, w) {
      if (mp.count({i, j})) {
        chmax(ans, r[i] + c[j] - 1);
      } else {
        chmax(ans, r[i] + c[j]);
      }
    }
  }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  int h, w, m;
  cin >> h >> w >> m;
  set<pair<int, int>> points;
  vector<int> row(h + 1, 0), col(w + 1, 0);
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    points.insert({x, y});
    row[x]++, col[y]++;
  }
  int r = 0, c = 0;
  for (int i = 1; i <= h; i++)
    r = max(r, row[i]);
  for (int i = 1; i <= w; i++)
    c = max(c, col[i]);
  int ans = r + c - 1;
  vector<int> a, b;
  for (int i = 1; i <= h; i++)
    if (row[i] == r)
      a.push_back(i);
  for (int i = 1; i <= w; i++)
    if (col[i] == c)
      b.push_back(i);
  for (int i = 0; i < (int)a.size(); i++) {
    for (int j = 0; j < (int)b.size(); j++) {
      if (!points.count({a[i], b[j]})) {
        cout << ans + 1 << "\\n";
        return 0;
      }
    }
  }
  cout << ans << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

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

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

  int h, w, m;
  cin >> h >> w >> m;
  map<int, set<pair<int, int>>> row, col;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    row[x].insert({x, y});
    col[y].insert({x, y});
  }
  int r = 0, c = 0;
  for (auto p : row)
    r = max(r, (int)p.second.size());
  for (auto p : col)
    c = max(c, (int)p.second.size());
  vector<int> ridx, cidx;
  for (auto p : row)
    if (p.second.size() == r)
      ridx.push_back(p.first);
  for (auto p : col)
    if (p.second.size() == c)
      cidx.push_back(p.first);
  int ans = -1;
  for (int i = 0; i < ridx.size(); i++) {
    for (int j = 0; j < cidx.size(); j++) {
      set<pair<int, int>> res;
      for (auto x : row[ridx[i]])
        res.insert(x);
      for (auto x : col[cidx[j]])
        res.insert(x);
      ans = max(ans, (int)res.size());
    }
  }
  cout << ans << "\\n";

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

#include <bits/stdc++.h>

using namespace std;

u_int seed = 3;
u_int xor_shift() {
  seed ^= seed << 1;
  seed ^= seed >> 3;
  seed ^= seed << 10;
  return seed;
}

chrono::system_clock::time_point start_time;
int elapsed() {
  auto cur = chrono::system_clock::now();
  return chrono::duration_cast<chrono::milliseconds>(cur - start_time).count();
}

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  start_time = chrono::system_clock::now();
  set<array<int, 2>> bomb;
  vector<int> cnt_h(h), cnt_w(w);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    bomb.insert({--a, --b});
    cnt_h[a]++;
    cnt_w[b]++;
  }

  auto max_ = [](vector<int> &A) {
    return accumulate(A.begin(), A.end(), 0,
                      [](auto &l, auto &r) { return max(l, r); });
  };
  int ma_h = max_(cnt_h);
  int ma_w = max_(cnt_w);

  bool flag = false;
  vector<int> H, W;
  for (int i = 0; i < h; i++) {
    if (cnt_h[i] != ma_h)
      continue;
    H.push_back(i);
  }

  for (int j = 0; j < w; j++) {
    if (cnt_w[j] != ma_w)
      continue;
    W.push_back(j);
  }

  while (elapsed() < 1980) {
    int rh = xor_shift() % H.size();
    int rw = xor_shift() % W.size();
    flag |= !bomb.count({H[rh], W[rw]});
  }

  flag |= H.size() * W.size() > m;
  cout << (ma_h + ma_w - (flag ? 0 : 1)) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<vector<bool>> bomb(h, vector<bool>(w));
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    bomb[--a][--b] = true;
  }

  vector<int> cnt_h(h), cnt_w(w);
  for (int i = 0; i < h; i++) {
    cnt_h[i] = accumulate(bomb[i].begin(), bomb[i].end(), 0);
  }

  for (int j = 0; j < w; j++) {
    for (int i = 0; i < h; i++) {
      cnt_w[j] += bomb[i][j];
    }
  }

  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      ans = max(ans, cnt_h[i] + cnt_w[j] - bomb[i][j]);
  }

  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> cH(H + 1, 0);
  vector<int> cW(W + 1, 0);
  set<pair<int, int>> Q;

  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    cH[h]++;
    cW[w]++;
    Q.insert(pair<int, int>(h, w));
  }

  int mHc = 0;
  for (int i = 1; i <= H; i++) {
    mHc = (mHc < cH[i] ? cH[i] : mHc);
  }
  int mWc = 0;
  for (int i = 1; i <= W; i++) {
    mWc = (mWc < cW[i] ? cW[i] : mWc);
  }
  set<int> sH;
  for (int i = 1; i <= H; i++) {
    if (mHc == cH[i])
      sH.insert(i);
  }
  set<int> sW;
  for (int i = 1; i <= W; i++) {
    if (mWc == cW[i])
      sW.insert(i);
  }

  int dbl = 1;
  for (auto it = sH.begin(); it != sH.end(); it++) {
    for (auto jt = sW.begin(); jt != sW.end(); jt++) {
      if (Q.find(pair<int, int>(*it, *jt)) == Q.end()) {
        dbl = 0;
        break;
      }
    }
  }
  cout << mHc + mWc - dbl << endl;
  return (0);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> cH(H + 1, 0);
  vector<int> cW(W + 1, 0);
  vector<vector<int>> Q(H + 1, vector<int>(W + 1, 0));
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    cH[h]++;
    cW[w]++;
    Q[h][w] = 1;
  }

  int mHc = 0;
  for (int i = 1; i <= H; i++) {
    mHc = (mHc < cH[i] ? cH[i] : mHc);
  }
  int mWc = 0;
  for (int i = 1; i <= W; i++) {
    mWc = (mWc < cW[i] ? cW[i] : mWc);
  }
  set<int> sH;
  for (int i = 1; i <= H; i++) {
    if (mHc == cH[i])
      sH.insert(i);
  }
  set<int> sW;
  for (int i = 1; i <= W; i++) {
    if (mWc == cW[i])
      sW.insert(i);
  }

  int dbl = 1;
  for (auto it = sH.begin(); it != sH.end(); it++) {
    for (auto jt = sW.begin(); jt != sW.end(); jt++) {
      if (Q[*it][*jt] == 0) {
        dbl = 0;
        break;
      }
    }
  }
  cout << mHc + mWc - dbl << endl;
  return (0);
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  set<pair<int, int>> pos;
  int xx[n + 1];
  int yy[m + 1];
  vector<int> x;
  vector<int> y;
  int maxx = 0;
  int maxy = 0;
  for (int i = 1; i <= n; ++i)
    xx[i] = 0;
  for (int i = 1; i <= m; ++i)
    yy[i] = 0;
  for (int i = 1; i <= k; ++i) {
    int a, b;
    cin >> a >> b;
    pos.insert(make_pair(a, b));
    ++xx[a];
    ++yy[b];
    if (xx[a] > maxx) {
      maxx = xx[a];
      x.clear();
      x.push_back(a);
    } else if (xx[a] == maxx)
      x.push_back(a);
    if (yy[b] > maxy) {
      maxy = yy[b];
      y.clear();
      y.push_back(b);
    } else if (yy[b] == maxy)
      y.push_back(b);
  }
  for (int i = 0; i < x.size(); ++i)
    for (int j = 0; j < y.size(); ++j)
      if (pos.count(make_pair(x[i], y[j])) == 0) {
        cout << maxx + maxy << endl;
        return 0;
      }
  cout << maxx + maxy - 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int h, w;
  int m;
  cin >> h >> w;
  cin >> m;
  int x[m + 1];
  int y[m + 1];
  for (int i = 1; i <= m; ++i)
    cin >> x[i] >> y[i];
  int tx[h + 1];
  for (int i = 1; i <= h; ++i)
    tx[i] = 0;
  for (int i = 1; i <= m; ++i)
    ++tx[x[i]];
  int ty[w + 1];
  for (int i = 1; i <= w; ++i)
    ty[i] = 0;
  for (int i = 1; i <= m; ++i)
    ++tx[y[i]];
  int maxvx = 0;
  for (int i = 1; i <= h; ++i)
    maxvx = max(maxvx, tx[i]);
  vector<int> maxx;
  for (int i = 1; i <= h; ++i)
    if (tx[i] == maxvx)
      maxx.push_back(tx[i]);
  int maxvy = 0;
  for (int i = 1; i <= w; ++i)
    maxvy = max(maxvy, ty[i]);
  vector<int> maxy;
  for (int i = 1; i <= w; ++i)
    if (ty[i] == maxvy)
      maxy.push_back(ty[i]);
  for (int i = 1; i <= m; ++i)
    if (!binary_search(maxx.begin(), maxx.end(), x[i]) &&
        !binary_search(maxy.begin(), maxy.end(), y[i])) {
      cout << maxvx + maxvy << endl;
      return 0;
    }
  cout << maxvx + maxvy - 1 << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

long long n, m, k, row[300005], col[300005], x[300005], y[300005];
long long rowmax, colmax;
long long rowmaxsize, colmaxsize;
long long resset;
long long result;
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    cin >> x[i] >> y[i];
    row[x[i]]++;
    col[y[i]]++;
  }

  for (int i = 1; i <= n; i++)
    rowmax = max(rowmax, row[i]);
  for (int i = 1; i <= m; i++)
    colmax = max(colmax, col[i]);

  for (int i = 1; i <= n; i++)
    if (row[i] == rowmax)
      rowmaxsize++;

  for (int i = 1; i <= m; i++)
    if (col[i] == colmax)
      colmaxsize++;

  for (int i = 0; i < k; i++) {
    if (row[x[i]] == rowmax && col[y[i]] == colmax)
      resset++;
  }

  if (resset < rowmaxsize * colmaxsize)
    cout << rowmax + colmax << endl;
  else
    cout << rowmax + colmax - 1 << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>

int main() {

  int h, w, m;
  scanf("%d %d %d", &h, &w, &m);

  int row[30000], col[30000];
  int i;
  int rowmax = 0, colmax = 0;
  int rowrow, rowcol, colrow, colcol;
  for (i = 0; i < m; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    row[a]++;
    if (row[a] > rowmax) {
      rowmax = row[a];
      rowrow = a;
      rowcol = b;
    }

    col[b]++;
    if (col[b] > colmax) {
      colmax = col[b];
      colrow = a;
      colcol = b;
    }

    if (rowrow == colrow || rowcol == colcol)
      printf("%d", colmax + rowmax - 1);
    else
      printf("%d", colmax + rowmax);
  }

  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 < n; i++)
#define repd(i, n) for (int i = n - 1; i > -1; i--)
#define repran(i, a, b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> P;
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  set<P> c;
  map<int, int> mph, mpw;
  int hmax = 0, wmax = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    c.emplace(a, b);
    mph[a]++;
    mpw[b]++;
    hmax = max(hmax, mph[a]);
    wmax = max(wmax, mpw[b]);
  }
  vector<int> hidx, widx;
  for (auto p : mph) {
    if (p.second == hmax)
      hidx.push_back(p.first);
  }
  for (auto p : mpw) {
    if (p.second == wmax)
      widx.push_back(p.first);
  }
  int ans = hmax + wmax - 1;
  for (int i : hidx) {
    for (int j : widx) {
      if (c.find({i, j}) == c.end()) {
        ans++;
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i > -1; i--)
#define repran(i, a, b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> P;
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<vector<int>> g(h, vector<int>(w, 0));
  vector<int> hs(h, 0), vs(w, 0);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    hs[a]++;
    vs[b]++;
    g[a][b] = 1;
  }
  int tmp = 0;
  int ans = 0;
  rep(i, h) rep(j, w) {
    tmp = hs[i] + vs[j];
    if (g[i][j] == 1)
      tmp--;
    ans = max(ans, tmp);
  }

  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;

#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define len(x) int((x).size())

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define randint(n) uniform_int_distribution<int>(1, (n))(rng)

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

#ifdef ARTHUR_LOCAL
  ifstream cin("input.txt");
#endif

  int h, w, m;
  cin >> h >> w >> m;

  vector<int> r(h + 1), c(w + 1);
  vector<bool> r2(w + 1, 0);

  vector<pair<int, int>> ps;

  for (int i = 1; i <= m; i++) {
    int hi, wi;
    cin >> hi >> wi;

    ps.push_back({hi, wi});

    r[hi]++;
    c[wi]++;
  }

  // cout << len(grid) << " " << len(grid[0]) << endl;

  int maxr = 0;
  for (int i = 1; i <= h; i++) {
    maxr = max(maxr, r[i]);
  }
  int maxc = 0;
  for (int i = 1; i <= w; i++) {
    maxc = max(maxc, c[i]);
  }

  vector<bool> rr(h + 1), cr(w + 1);

  int grs = 0;
  int gcs = 0;

  for (int i = 1; i <= h; i++) {
    if (r[i] == maxr) {
      rr[i] = 1;
      grs++;
    }
  }

  for (int i = 1; i <= w; i++) {
    if (c[i] == maxc) {
      cr[i] = 1;
      gcs++;
    }
  }

  ll things = ll(grs) * ll(gcs);

  for (auto p : ps) {
    if (rr[p.ff] && cr[p.ss])
      things--;
  }

  cout << maxr + maxc - int(things == 0) << endl;
}

Error: 0

Fixed submission:

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

using ll = long long;

#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define len(x) int((x).size())

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define randint(n) uniform_int_distribution<int>(1, (n))(rng)

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

#ifdef ARTHUR_LOCAL
  ifstream cin("input.txt");
#endif

  int h, w, m;
  cin >> h >> w >> m;

  vector<int> r(h + 1), c(w + 1);
  vector<bool> r2(w + 1);
  vector<vector<bool>> grid(h + 1, r2);

  for (int i = 1; i <= m; i++) {
    int hi, wi;
    cin >> hi >> wi;

    r[hi]++;
    c[wi]++;

    grid[hi][wi] = 1;
  }

  int maxr = 0;
  for (int i = 1; i <= h; i++) {
    maxr = max(maxr, r[i]);
  }
  int maxc = 0;
  for (int i = 1; i <= w; i++) {
    maxc = max(maxc, c[i]);
  }

  vector<int> rr, cr;

  for (int i = 1; i <= h; i++) {
    if (r[i] == maxr)
      rr.push_back(i);
  }

  for (int i = 1; i <= w; i++) {
    if (c[i] == maxc)
      cr.push_back(i);
  }

  for (auto rrr : rr) {
    for (auto ccc : cr) {
      if (!grid[rrr][ccc]) {
        cout << maxr + maxc << endl;
        return 0;
      }
    }
  }

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> row(h);
  vector<pair<int, int>> column(w);
  unordered_map<int, unordered_map<int, int>> graph;
  for (int i = 0; i < h; i++) {
    row[i].first = 0;
    row[i].second = i;
  }
  for (int i = 0; i < w; i++) {
    column[i].first = 0;
    column[i].second = i;
  }
  for (int i = 0; i < m; i++) {
    int hi, wi;
    cin >> hi >> wi;
    graph[hi - 1][wi - 1] = 1;
    row[hi - 1].first++;
    column[wi - 1].first++;
  }
  sort(row.begin(), row.end());
  // reverse(row.begin(),row.end());
  sort(column.begin(), column.end());
  // reverse(column.begin(), column.end());
  // cout<<row[0].second+1<<" "<<column[0].second+1<<endl;
  int countx = row[h - 1].first;
  int county = column[w - 1].first;
  long long int max = row[h - 1].first + column[w - 1].first;
  if (graph[row[h - 1].second][column[w - 1].second])
    max--;
  int flag = 1;
  int count = 0;
  for (int i = h - 1; i >= 0 and row[i].first == countx; i--) {
    for (int j = w - 1; j >= 0 and column[j].first == county; j--) {
      count++;
      long long int destruction = row[i].first + column[j].first;
      if (graph[row[i].second][column[j].second])
        destruction--;
      if (destruction > max) {
        max = destruction;
        flag = 0;
        break;
      }
      if (count > m) {
        max = row[h - 1].first + column[w - 1].first;
        flag = 0;
        break;
      }
    }
    if (!flag)
      break;
  }

  cout << max << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> row(h);
  vector<pair<int, int>> column(w);
  unordered_map<int, unordered_map<int, int>> graph;
  for (int i = 0; i < h; i++) {
    row[i].first = 0;
    row[i].second = i;
  }
  for (int i = 0; i < w; i++) {
    column[i].first = 0;
    column[i].second = i;
  }
  for (int i = 0; i < m; i++) {
    int hi, wi;
    cin >> hi >> wi;
    graph[hi - 1][wi - 1] = 1;
    row[hi - 1].first++;
    column[wi - 1].first++;
  }
  sort(row.begin(), row.end());
  // reverse(row.begin(),row.end());
  sort(column.begin(), column.end());
  // reverse(column.begin(), column.end());
  // cout<<row[0].second+1<<" "<<column[0].second+1<<endl;
  int countx = row[h - 1].first;
  int county = column[w - 1].first;
  long long int max = row[h - 1].first + column[w - 1].first;
  if (graph[row[h - 1].second][column[w - 1].second])
    max--;
  int flag = 1;
  for (int i = h - 1; i >= 0 and row[i].first == countx; i--) {
    for (int j = w - 1; j >= 0 and column[j].first == county; j--) {
      long long int destruction = row[i].first + column[j].first;
      if (graph[row[i].second][column[j].second])
        destruction--;
      if (destruction > max) {
        max = destruction;
        flag = 0;
        break;
      }
    }
    if (!flag)
      break;
  }

  cout << max << '\\n';
}