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

#include <bits/stdc++.h>
// pbds
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using orderedSet =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/// use less_equal in pbds template to work as
/// multiset...................................................
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// BFS+DFS
#define boundary(i, j) (i >= 0 && i < n && j >= 0 && j < m)
int X[] = {-1, 1, 0, 0};
int Y[] = {0, 0, 1, -1};
int cnt1[300005], cnt2[300005];
vector<int> v1, v2;
vector<int> r, c;
set<pii> mp;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, m;
  cin >> h >> w >> m;
  int mx1 = 0, mx2 = 0;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    mp.insert({x, y});
    ++cnt1[x];
    ++cnt2[y];
    mx1 = max(mx1, cnt1[x]);
    mx2 = max(mx2, cnt2[y]);
  }

  for (int i = 1; i <= h; i++) {
    if (cnt1[i] == mx1)
      r.pb(i);
  }
  for (int i = 1; i <= w; i++) {
    if (cnt2[i] == mx2)
      c.pb(i);
  }
  int ans = mx1 + mx2 - 1;
  for (int i : r) {
    for (int j : c) {

      if (mp.find({i, j}) == mp.end()) {
        ans = mx1 + mx2;
        break;
      }
    }
    if (ans == mx1 + mx2)
      break;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// pbds
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using orderedSet =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/// use less_equal in pbds template to work as
/// multiset...................................................
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// BFS+DFS
#define boundary(i, j) (i >= 0 && i < n && j >= 0 && j < m)
int X[] = {-1, 1, 0, 0};
int Y[] = {0, 0, 1, -1};
int cnt1[300005], cnt2[300005];
vector<int> v1, v2;
vector<int> r, c;
set<pii> mp;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, m;
  cin >> h >> w >> m;
  int mx1 = 0, mx2 = 0;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    mp.insert({x, y});
    v1.pb(x);
    v2.pb(y);
    ++cnt1[x];
    ++cnt2[y];
    mx1 = max(mx1, cnt1[x]);
    mx2 = max(mx2, cnt2[y]);
  }

  for (int i : v1) {
    if (cnt1[i] == mx1)
      r.pb(i);
  }
  for (int i : v2) {
    if (cnt2[i] == mx2)
      c.pb(i);
  }
  int ans = mx1 + mx2 - 1;
  for (int i : r) {
    for (int j : c) {

      if (mp.find({i, j}) == mp.end()) {
        ans = mx1 + mx2;
        break;
      }
    }
    if (ans == mx1 + mx2)
      break;
  }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
const int inf = 1001001001;

//
//..#..
//..#..
// ##.#.
//.###.
//...#.

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

  map<int, int> imp, jmp;
  set<pair<int, int>> st;
  rep(i, m) {
    int H, W;
    cin >> H >> W;
    st.insert({H, W});
  }

  int imx = 0;
  int jmx = 0;
  for (auto p : st) {
    imx = max(imx, ++imp[p.first]);
    jmx = max(jmx, ++jmp[p.second]);
  }

  vector<int> im(0), jm(0);
  for (auto p : imp) {
    if (p.second == imx)
      im.push_back(p.first);
  }
  for (auto p : jmp) {
    if (p.second == jmx)
      jm.push_back(p.first);
  }

  bool flag = 0;
  for (auto i : im) {
    for (auto j : jm) {
      if (st.find({i, j}) == st.end()) {
        cout << imx + jmx << endl;
        return 0;
      }
    }
  }
  cout << imx + jmx - 1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
const int inf = 1001001001;

//
//..#..
//..#..
// ##.#.
//.###.
//...#.

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

  map<int, int> imp, jmp;
  set<pair<int, int>> st;
  rep(i, m) {
    int H, W;
    cin >> H >> W;
    st.insert({H, W});
  }

  int imx = 0;
  int jmx = 0;
  ;
  for (auto p : st) {
    imx = max(imx, ++imp[p.first]);
    jmx = max(jmx, ++jmp[p.second]);
  }

  vector<int> im(0), jm(0);
  for (auto p : imp) {
    if (p.second == imx)
      im.push_back(p.first);
  }
  for (auto p : jmp) {
    if (p.second == jmx)
      jm.push_back(p.first);
  }

  bool flag = 0;
  rep(i, im.size()) {
    rep(j, jm.size()) {
      if (st.find({im[i], jm[j]}) == st.end())
        flag = 1;
    }
  }
  if (flag)
    cout << imx + jmx << endl;
  else
    cout << imx + jmx - 1 << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

Fixed submission:

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

int main() {
  int h, w;
  cin >> h >> w;
  ll m;
  cin >> m;
  vector<int> H(m), W(m);
  rep(i, m) cin >> H[i] >> W[i];
  ll ans = 0;
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      ll nh = 0;
      ll nw = 0;
      vector<int> idxs;
      for (int x = 0; x < H.size(); ++x) {
        if (H[x] == i) {
          ++nh;
          idxs.push_back(x);
        }
      }
      for (int x = 0; x < W.size(); ++x) {
        if (count(idxs.begin(), idxs.end(), x) == 0) {
          if (W[x] == j)
            ++nw;
        }
      }
      ans = max(ans, nh + nw);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pii pair<int, int>
#define mp make_pair
const int maxn = 3e5 + 10;
struct Node {
  int num, y;
  bool operator<(const Node &A) const {
    return num == A.num ? y < A.y : num > A.num;
  }
};
int n, m, k;
int row[maxn], line[maxn];
vector<int> boom[maxn];
set<Node> s;

int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1, x, y; i <= k; ++i) {
    scanf("%d%d", &x, &y);
    ++row[x];
    ++line[y];
    boom[x].push_back(y);
  }
  int ans = 0;
  for (int i = 1; i <= m; ++i)
    s.insert((Node){line[i], i});
  for (int i = 1, tmp; i <= n; ++i) {
    tmp = row[i];
    for (int y : boom[i]) {
      auto q = s.find((Node){line[y], y});
      s.erase(q);
      s.insert((Node){line[y] - 1, y});
    }
    tmp += s.begin()->num;
    for (int y : boom[i]) {
      auto q = s.find((Node){line[y] - 1, y});
      s.erase(q);
      s.insert((Node){line[y], y});
    }
    ans = max(ans, tmp);
  }
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pii pair<int, int>
#define mp make_pair
const int maxn = 3e5 + 10;
int n, m, k;
map<pii, int> vis;
int row[maxn], line[maxn];

int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1, x, y; i <= k; ++i) {
    scanf("%d%d", &x, &y);
    ++row[x];
    ++line[y];
    vis[mp(x, y)] = 1;
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      ans = max(ans, row[i] + line[j] - vis[mp(i, j)]);
  printf("%d\\n", ans);
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> A(H), B(W);
  vector<P> D(0);
  REP(i, M) {
    int a, b;
    cin >> a >> b, a--, b--;
    A[a]++;
    B[b]++;
    D.push_back(P(a, b));
  }
  int ma = *max_element(A.begin(), A.end());
  int mb = *max_element(B.begin(), B.end());
  int al = 0, bl = 0;
  REP(i, A.size()) if (ma == A[i]) al++;
  REP(i, B.size()) if (mb == B[i]) bl++;
  int nme = 0;
  REP(i, D.size()) {
    if (A[D[i].first] == ma && B[D[i].second] == mb)
      nme++;
  }
  cout << ma + mb - ((al * bl - nme) == 0) << endl;
  int a = 10;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
const ll mx = 3 * 1e5 + 10;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<ll> tate(W + 5, 0), yoko(H + 5, 0);
  map<P, int> mp{};
  set<P> st;
  REP(i, M) {
    int a, b;
    cin >> a >> b, a--, b--;
    // mp[P(a, b)]++;
    st.insert(P(a, b));
    tate[b]++;
    yoko[a]++;
  }

  ll ans = -1;
  REP(i, H) REP(j, W) {
    ll tmp = tate[j] + yoko[i];
    auto itr = st.find(P(i, j));
    if (itr != st.end())
      tmp--;
    ans = max(ans, tmp);
  }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int N = 300004;
int cnth[N], cntw[N], x[N], y[N];
map<int, bool> mp[N];

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int h, w, m;
  cin >> h >> w >> m;
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i];
    mp[x[i]][y[i]] = true;
    cnth[x[i]]++, cntw[y[i]]++;
  }
  int mxh = 0, mxw = 0;
  for (int i = 0; i < N; i++) {
    mxh = max(mxh, cnth[i]);
    mxw = max(mxw, cntw[i]);
  }
  vector<int> hall, wall;
  for (int i = 0; i < N; i++) {
    if (cnth[i] == mxh) {
      hall.push_back(i);
    }
    if (cntw[i] == mxw) {
      wall.push_back(i);
    }
  }
  int cnt = 0, mx = 0;
  for (int i = 0; i < hall.size(); i++) {
    for (int j = 0; j < wall.size(); j++) {
      cnt++;
      if (cnt > N * 2)
        break;
      mx = max(mx, mxh + mxw - mp[hall[i]][wall[j]]);
    }
    if (cnt > N * 2)
      break;
  }
  cout << mx;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld double
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define BOOST                                                                  \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define all(v) v.begin(), v.end()
#define sl(a) a.size()
#define mod 1000000007
#define lcm(a, b) (a * b) / __gcd(a, b)
#define MAX1 100006
#define f first
#define s second
#define pi 2 * acos(0.0)
void solve() {
  ll n, m, a, x, y, maxi = 0;
  cin >> n >> m >> a;
  vector<pair<ll, ll>> vp(a);
  ll i = 0;
  while (a--) {
    cin >> x >> y;
    vp[i++] = {x, y};
  }
  sort(all(vp));
  vector<ll> row(n + 1), col(m + 1);
  rep(i, 1, n + 1) {
    ll ans = 0;
    rep(j, 1, m + 1) {
      auto p = make_pair(i, j);
      if (binary_search(vp.begin(), vp.end(), p))
        ans++;
    }
    row[i] = ans;
  }
  rep(j, 1, m + 1) {
    ll ans = 0;
    rep(i, 1, n + 1) {
      auto p = make_pair(i, j);
      if (binary_search(vp.begin(), vp.end(), p))
        ans++;
    }
    col[j] = ans;
  }
  rep(i, 1, n + 1) {
    rep(j, 1, m + 1) {
      auto p = make_pair(i, j);
      if (binary_search(vp.begin(), vp.end(), p))
        maxi = max(maxi, row[i] + col[j] - 1);
      else
        maxi = max(maxi, row[i] + col[j]);
    }
  }
  cout << maxi;
}
int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for getting error from error.txt
  freopen("error.txt", "w", stderr);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  BOOST
  int T = 1; // cin>>T;
  while (T--)
    solve();
  cerr << "Time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs"
       << "\\n";
  cerr << "My CLOCKS_PER_SEC= " << CLOCKS_PER_SEC << "\\n";
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

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

#define pb push_back

const int maxn = 3e5 + 10;

int x, y, r[maxn], c[maxn];
vector<int> rr, cc;
set<pair<int, int>> st;
void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  int mr = 0, mc = 0;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    st.insert(make_pair(x, y));
    r[x]++;
    c[y]++;
    if (mr < r[x]) {
      mr = r[x];
      rr.clear();
      rr.pb(x);
    } else if (mr == r[x]) {
      rr.pb(x);
    }
    if (mc < c[y]) {
      mc = c[y];
      cc.clear();
      cc.pb(y);
    } else if (mc == c[y]) {
      cc.pb(y);
    }
  }
  for (int i : rr)
    for (int j : cc) {
      if (st.count(make_pair(i, j)))
        continue;
      else {
        cout << mr + mc;
        return;
      }
    }
  cout << mr + mc - 1;
}
signed main() {
  solve();
  return 0;
}


Error: WA

Fixed submission:

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

#define pb push_back

const int maxn = 1e5 + 10;

int x, y, r[maxn], c[maxn];
vector<int> rr, cc;
set<pair<int, int>> st;
void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  int mr = 0, mc = 0;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    st.insert(make_pair(x, y));
    r[x]++;
    c[y]++;
    if (mr < r[x]) {
      mr = r[x];
      rr.clear();
      rr.pb(x);
    } else if (mr == r[x]) {
      rr.pb(x);
    }
    if (mc < c[y]) {
      mc = c[y];
      cc.clear();
      cc.pb(y);
    } else if (mc == c[y]) {
      cc.pb(y);
    }
  }
  for (int i : rr)
    for (int j : cc) {
      if (st.count(make_pair(i, j)))
        continue;
      else {
        cout << mr + mc;
        return;
      }
    }
  cout << mr + mc - 1;
}
signed main() {
  solve();
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1000000007;

void solve() {

  int n, m, b;
  cin >> n >> m >> b;

  vector<int> row(n + 1, 0);
  vector<int> col(m + 1, 0);

  map<pair<int, int>, bool> mp;

  for (int i = 1; i <= b; i++) {
    int r, c;
    cin >> r >> c;
    row[r]++;
    col[c]++;
    mp[{r, c}] = true;
  }

  int mr = 0, mc = 0;

  for (int i = 1; i <= n; i++) {
    mr = max(mr, row[i]);
  }

  for (int j = 1; j <= m; j++) {
    mc = max(mc, col[j]);
  }

  vector<int> rr, cc;

  for (int i = 1; i <= n; i++) {
    if (mr == row[i]) {
      rr.push_back(i);
    }
  }

  for (int j = 1; j <= m; j++) {
    if (mc == col[j]) {
      cc.push_back(j);
    }
  }

  int ans = mr + mc;

  for (auto x : rr) {
    for (auto y : cc) {
      if (mp.find({x, y}) != mp.end()) {
        continue;
      }
      cout << ans;
      return;
    }
  }
  ans--;
  cout << ans;
  return;
}

int main() {

  int y;

  y = 1;
  // cin>>y ;

  while (y--) {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1000000007;

void solve() {

  int n, m, b;
  cin >> n >> m >> b;

  vector<int> row(n + 1, 0);
  vector<int> col(m + 1, 0);

  map<pair<int, int>, bool> mp;

  for (int i = 1; i <= b; i++) {
    int r, c;
    cin >> r >> c;
    row[r]++;
    col[c]++;
    mp[{r, c}] = true;
  }

  int mr = 0, mc = 0;

  for (int i = 1; i <= n; i++) {
    mr = max(mr, row[i]);
  }

  for (int j = 1; j <= m; j++) {
    mc = max(mc, col[j]);
  }

  vector<int> rr, cc;

  for (int i = 1; i <= n; i++) {
    if (mr == row[i]) {
      rr.push_back(i);
    }
  }

  for (int j = 1; j <= m; j++) {
    if (mc == col[j]) {
      cc.push_back(j);
    }
  }

  int ans = 0;

  for (auto x : rr) {
    for (auto y : cc) {
      if (mp.find({x, y}) != mp.end()) {
        ans = max(ans, mr + mc - 1);
      } else
        ans = max(ans, mr + mc);
    }
  }

  cout << ans;
}

int main() {

  int y;

  y = 1;
  // cin>>y ;

  while (y--) {
    solve();
  }
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int M = 3e5 + 5;
int a[M];
int b[M];
map<pair<int, int>, bool> mp;
int main() {
  int n, m, q;
  scanf("%d%d%d", &n, &m, &q);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[x]++;
    b[y]++;
    mp[make_pair(x, y)] = 1;
  }
  int ma = 0, mb = 0;
  for (int i = 1; i <= n; i++) {
    ma = max(ma, a[i]);
  }
  for (int i = 1; i <= m; i++) {
    // printf("%d ",b[i]);
    mb = max(mb, b[i]);
  }
  vector<int> aa, bb;
  for (int i = 1; i <= n; i++) {
    if (a[i] == ma)
      aa.push_back(i);
  }
  for (int i = 1; i <= m; i++) {
    if (b[i] == mb)
      bb.push_back(i);
  }
  // int cnt=0;
  for (auto i : aa) {
    for (auto j : bb) {
      if (mp.count(make_pair(i, j)) == 0) {
        printf("%d\\n", ma + mb);
        return 0;
      }
    }
  }

  // printf("+");
  // printf("%d %d\\n",ma,mb);
  printf("%d\\n", ma + mb - 1);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int M = 3e5 + 5;
int a[M];
int b[M];
map<pair<int, int>, bool> mp;
int main() {
  int n, m, q;
  scanf("%d%d%d", &n, &m, &q);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[x]++;
    b[y]++;
    mp[make_pair(x, y)] = 1;
  }
  int ma = 0, mb = 0;
  for (int i = 1; i <= n; i++) {
    ma = max(ma, a[i]);
  }
  for (int i = 1; i <= m; i++) {
    // printf("%d ",b[i]);
    mb = max(mb, b[i]);
  }

  for (int i = 1; i <= n; i++) {
    if (a[i] != ma)
      continue;
    for (int j = 1; j <= m; j++) {
      if (b[j] != mb)
        continue;
      if (mp.count(make_pair(i, j)) == 0) {
        // printf(":%d %d\\n",i,j);

        printf("%d\\n", ma + mb);
        return 0;
      }
    }
  }
  // printf("+");
  // printf("%d %d\\n",ma,mb);
  printf("%d\\n", ma + mb - 1);
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using pi = pair<int, int>;
const ll INF = 1LL << 60;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> hp(h), wp(w);
  set<pi> s;
  rep(i, m) {
    int y, x;
    cin >> y >> x;
    y--, x--;
    hp[y]++;
    wp[x]++;
    s.insert(pi(y, x));
  }
  int mh = 0, mw = 0;
  rep(i, h) mh = max(mh, hp[i]);
  rep(i, w) mw = max(mw, wp[i]);

  vector<int> hm, wm;
  rep(i, h) if (hp[i] == mh) hm.push_back(i);
  rep(i, w) if (wp[i] == mw) wm.push_back(i);

  ll ans = mh + mw;

  for (int y : hm)
    for (int x : wm) {
      if (s.count(pi(y, x)))
        continue;
      cout << ans << endl;
      return 0;
    }
  cout << --ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using pi = pair<int, int>;
const ll INF = 1LL << 60;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> hp(h), wp(w);
  set<pi> s;
  rep(i, m) {
    int y, x;
    cin >> y >> x;
    y--, x--;
    hp[y]++;
    wp[x]++;
    s.insert(pi(y, x));
  }
  int mh = 0, mw = 0;
  rep(i, h) mh = max(mh, hp[i]);
  rep(i, w) mw = max(mw, wp[i]);

  vector<int> hm, wm;
  rep(i, h) if (hp[i] == mh) hm.push_back(i);
  rep(i, w) if (wp[i] == mw) wm.push_back(i);

  ll ans = mh + mw;

  rep(i, h) rep(j, w) {
    if (s.count(pi(hm[i], wm[j])))
      continue;
    cout << ans << endl;
    return 0;
  }
  cout << --ans << endl;

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

#include <bits/stdc++.h>

using namespace std;
#define int long long

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> row(h), col(w);
  vector<pair<int, int>> pts(m);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    row[x]++;
    col[y]++;
    pts[i] = make_pair(x, y);
  }
  int maxr = -1, maxc = -1;
  for (int x : row) {
    maxr = max(maxr, x);
  }
  for (int x : col) {
    maxc = max(maxc, x);
  }
  int r = 0, c = 0, should = 0;
  for (int x : row) {
    if (x == maxr)
      r++;
  }
  for (int x : col) {
    if (x == maxc)
      c++;
  }
  for (pair<int, int> p : pts) {
    int x = p.first;
    int y = p.second;
    if (row[x] == maxr && col[y] == maxc) {
      should++;
    }
  }
  if (r * c > should) {
    cout << maxr + maxc << '\\n';
  } else {
    cout << maxr + maxc - 1 << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, b;
  cin >> n >> m >> b;
  vector<int> row(n), col(m);
  set<pair<int, int>> s;
  while (b--) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    row[x]++;
    col[y]++;
    s.insert({x, y});
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int g = row[i] + col[j];
      if (s.find({i, j}) != s.end())
        g--;
      ans = max(ans, g);
    }
  }
  cout << ans << '\\n';
  return 0;
}