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

#include <algorithm>
#include <set>
#include <stdio.h>
#include <vector>
#define MN 300000
typedef std::pair<int, int> pii;

int h, w, m;
int cx[MN + 5], cy[MN + 5], mx = 0, my = 0;
std::set<pii> s;
std::vector<int> vx, vy;

int main() {
  scanf("%d%d%d", &h, &w, &m);
  for (int i = 1, x, y; i <= m; i++) {
    scanf("%d%d", &x, &y);
    cx[x]++;
    cy[y]++;
    s.insert(pii(x, y));
  }
  for (int i = 1; i <= h; i++)
    mx = std::max(mx, cx[i]);
  for (int i = 1; i <= w; i++)
    my = std::max(my, cy[i]);
  for (int i = 1; i <= h; i++)
    if (cx[i] == mx)
      vx.push_back(i);
  for (int i = 1; i <= w; i++)
    if (cy[i] == my)
      vy.push_back(i);
  for (int i = 0; i < vx.size(); i++)
    for (int j = 0; j < vy.size(); j++)
      if (s.find(pii(vx[i], vy[j])) == s.end()) {
        printf("%d\\n", mx + my);
        return 0;
      }
  printf("%d\\n", mx + my - 1);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <set>
#include <stdio.h>
#define MN 300000
typedef std::pair<int, int> pii;

int h, w, m;
int cx[MN + 5], cy[MN + 5], mx = 0, my = 0;
std::set<pii> s;

int main() {
  scanf("%d%d%d", &h, &w, &m);
  for (int i = 1, x, y; i <= m; i++) {
    scanf("%d%d", &x, &y);
    cx[x]++;
    cy[y]++;
    s.insert(pii(x, y));
  }
  for (int i = 1; i <= h; i++)
    mx = std::max(mx, cx[i]);
  for (int i = 1; i <= w; i++)
    my = std::max(my, cy[i]);
  for (int i = 1; i <= h; i++)
    if (cx[i] == mx) {
      for (int j = 1; j <= w; j++) {
        if (cy[j] == my) {
          if (s.find(pii(i, j)) == s.end()) {
            printf("%d\\n", mx + my);
            return 0;
          }
        }
      }
    }
  printf("%d\\n", mx + my - 1);
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
// cout << setprecision(20) <<

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<ll> hbomb(h, 0);
  vector<ll> wbomb(w, 0);
  map<pair<ll, ll>, ll> mp;
  for (int i = 0; i < m; ++i) {
    ll hi, wi;
    cin >> hi >> wi;
    hbomb[hi - 1] += 1;
    wbomb[wi - 1] += 1;
    pair<ll, ll> p = make_pair(hi - 1, wi - 1);
    mp[p] = 1;
  }

  vector<pair<ll, ll>> h_pair(h);
  for (int i = 0; i < h; ++i) {
    h_pair[i] = make_pair(hbomb[i], i);
  }
  sort(h_pair.begin(), h_pair.end());

  vector<pair<ll, ll>> w_pair(w);
  for (int i = 0; i < w; ++i) {
    w_pair[i] = make_pair(wbomb[i], i);
  }
  sort(w_pair.begin(), w_pair.end());

  ll ans = 0;
  ll h_start = h_pair[h_pair.size() - 1].first;
  ll w_start = w_pair[w_pair.size() - 1].first;
  ll max_h_count = 0;
  ll max_w_count = 0;
  vector<ll> h_cand;
  vector<ll> w_cand;
  for (int i = h_pair.size() - 1; i >= 0; --i) {
    if (h_pair[i].first != h_start) {
      break;
    }
    max_h_count++;
    h_cand.push_back(h_pair[i].second);
  }
  for (int j = w_pair.size() - 1; j >= 0; j--) {
    if (w_pair[j].first != w_start) {
      break;
    }
    max_w_count++;
    w_cand.push_back(w_pair[j].second);
  }

  if (max_h_count * max_w_count > m) {
    ans = h_start + w_start;
  } else {
    ll cand_max = h_start + w_start - 1;
    for (auto i : h_cand) {
      for (auto j : w_cand) {
        if (mp.count(make_pair(i, j)) == 0) {
          cand_max = max(cand_max, hbomb[i] + wbomb[j]);
        }
      }
    }
    ans = cand_max;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
// cout << setprecision(20) <<

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<ll> hbomb(h, 0);
  vector<ll> wbomb(w, 0);
  vector<pair<ll, ll>> board(m);
  for (int i = 0; i < m; ++i) {
    ll hi, wi;
    cin >> hi >> wi;
    hbomb[hi - 1] += 1;
    wbomb[wi - 1] += 1;
    board[i] = make_pair(hi - 1, wi - 1);
  }

  vector<pair<ll, ll>> h_pair(h);
  for (int i = 0; i < h; ++i) {
    h_pair[i] = make_pair(hbomb[i], i);
  }
  sort(h_pair.begin(), h_pair.end());

  vector<pair<ll, ll>> w_pair(w);
  for (int i = 0; i < w; ++i) {
    w_pair[i] = make_pair(wbomb[i], i);
  }
  sort(w_pair.begin(), w_pair.end());

  ll ans = 0;
  ll h_start = h_pair[h_pair.size() - 1].first;
  ll w_start = w_pair[w_pair.size() - 1].first;
  for (int i = h_pair.size() - 1; i >= 0; i--) {
    ll cur_h_val = h_pair[i].first;
    if (cur_h_val + 1 < h_start) {
      break;
    }
    for (int j = w_pair.size() - 1; j >= 0; j--) {
      ll cur_w_val = w_pair[j].first;
      if (cur_w_val + 1 < w_start) {
        break;
      }
      ll score = cur_h_val + cur_w_val;
      ll x = h_pair[i].second;
      ll y = w_pair[j].second;
      for (auto k : board) {
        if (k.first == x && k.second == y) {
          score--;
        }
      }
      ans = max(ans, score);
    }
  }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\\n'
#define pii pair<ll ll, ll ll>
#define all(v) (v).begin(), (v).end()
using namespace std;

void solve() {
  ll h, w, m, x, y;
  cin >> h >> w >> m;
  std::vector<pair<ll, ll>> col(w + 1), row(h + 1);
  for (int i = 0; i <= w; i++)
    col[i].second = i;
  for (int i = 0; i <= h; i++)
    row[i].second = i;

  map<pair<ll, ll>, bool> lol;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    lol[{x, y}] = 1;
    row[x].first++;
    col[y].first++;
  }
  sort(all(col));
  reverse(all(col));
  sort(all(row));
  reverse(all(row));
  ll ans = 0;
  for (int i = 0; i < row.size() and row[i].first == row[0].first; i++)
    for (int j = 0; j < col.size() and col[j].first == col[0].first; j++) {

      ans = max(ans, row[i].first + col[j].first -
                         lol[{row[i].second, col[j].second}]);
      if (!lol[{row[i].second, col[j].second}]) {
        cout << ans;
        return;
      }
    }
  cout << ans;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll TESTS = 1;
  // cin >> TESTS;
  while (TESTS--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\\n'
#define pii pair<ll ll, ll ll>
#define all(v) (v).begin(), (v).end()
using namespace std;

void solve() {
  ll h, w, m, x, y;
  cin >> h >> w >> m;
  std::vector<pair<ll, ll>> col(w + 1), row(h + 1);
  for (int i = 0; i <= w; i++)
    col[i].second = i;
  for (int i = 0; i <= h; i++)
    row[i].second = i;

  map<pair<ll, ll>, bool> lol;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    lol[{x, y}] = 1;
    row[x].first++;
    col[y].first++;
  }
  sort(all(col));
  reverse(all(col));
  sort(all(row));
  reverse(all(row));
  ll ans = 0;
  for (int i = 0; i < row.size() and row[i].first == row[0].first; i++)
    for (int j = 0; j < col.size() and col[j].first == col[0].first; j++)
      ans = max(ans, row[i].first + col[j].first -
                         lol[{row[i].second, col[j].second}]);
  cout << ans;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll TESTS = 1;
  // cin >> TESTS;
  while (TESTS--) {
    solve();
  }
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

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

using i_i = pair<int, int>;

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

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

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

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

Error: 0

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<vector<bool>> m(H, vector<bool>(W, false));
  vector<int> cnt_h(H, 0);
  vector<int> cnt_w(W, 0);
  vector<pair<int, int>> tar_h; // cnt, row
  vector<pair<int, int>> tar_w; // cnt, col
  rep(i, M) {
    int h, w;
    cin >> h >> w;
    h--, w--;
    m[h][w] = true;
    cnt_h[h]++;
    cnt_w[w]++;
    tar_h.push_back(make_pair(cnt_h[h], h));
    tar_w.push_back(make_pair(cnt_w[w], w));
  }

  auto comp = [](pair<int, int> a, pair<int, int> b) { return a > b; };

  sort(ALL(tar_h), comp);
  sort(ALL(tar_w), comp);
  int max_h = tar_h[0].first;
  int max_w = tar_w[0].first;

  for (auto it = tar_h.begin(); it != tar_h.end();) {
    if (it->first != max_h)
      it = tar_h.erase(it);
    else
      it++;
  }
  for (auto it = tar_w.begin(); it != tar_w.end();) {
    if (it->first != max_w)
      it = tar_w.erase(it);
    else
      it++;
  }

  int ans = max_h + max_w;

  int x, y;
  for (auto ph : tar_h) {
    for (auto pw : tar_w) {
      if (ph.first == max_h && pw.first == max_w) {
        y = ph.second;
        x = pw.second;
        if (!m[y][x])
          break;
      }
    }
  }

  if (m[y][x])
    ans--;
  cout << ans << endl;

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

#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (ll i = 0; i < n; i++)
#define IREP(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define all(v) v.begin(), v.end()
#define SENTINEL 2000000000
#define NIL -1
using namespace std;
typedef long long ll;

const ll MAX = 510000;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
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;
  set<pair<int, int>> s;
  map<int, int> gyou;
  map<int, int> retsu;
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    s.insert(make_pair(a, b));
    gyou[a]++;
    retsu[b]++;
  }
  int maxh = -1;
  vector<pair<int, int>> q1;
  for (auto &i : gyou) {
    if (maxh < i.second) {
      maxh = i.second;
    }
  }
  for (auto &i : gyou) {
    if (maxh == i.second) {
      q1.push_back(i);
    }
  }
  vector<pair<int, int>> q2;
  int maxw = -1;
  for (auto &i : retsu) {
    if (maxw < i.second) {
      maxw = i.second;
    }
  }
  for (auto &i : retsu) {
    if (maxw == i.second) {
      q2.push_back(i);
    }
  }
  REP(x, q1.size()) {
    pair<int, int> i = q1[x];
    REP(y, q2.size()) {
      pair<int, int> j = q2[y];
      if (s.find(make_pair(i.first, j.first)) == s.end()) {
        cout << maxh + maxw << endl;
        return 0;
      }
    }
  }
  cout << maxh + maxw - 1 << endl;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (ll i = 0; i < n; i++)
#define IREP(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define all(v) v.begin(), v.end()
#define SENTINEL 2000000000
#define NIL -1
using namespace std;
typedef long long ll;

const ll MAX = 510000;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

vector<vector<bool>> s(100001, vector<bool>(100001));
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  map<int, int> gyou;
  map<int, int> retsu;
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    s[a][b] = true;
    gyou[a]++;
    retsu[b]++;
  }
  int maxh = -1;
  for (auto &i : gyou) {
    if (maxh < i.second) {
      maxh = i.second;
    }
  }
  int maxw = -1;
  for (auto &i : retsu) {
    if (maxw < i.second) {
      maxw = i.second;
    }
  }
  for (auto &i : gyou) {
    if (maxh == i.second) {
      for (auto &j : retsu) {
        if (maxw == j.second) {
          if (s[i.first][j.first]) {
            cout << maxh + maxw << endl;
            return 0;
          }
        }
      }
    }
  }
  cout << maxh + maxw - 1 << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
const ll INF = 1 << 18;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> y(h, 0);
  vector<int> x(w, 0);
  vector<P> v;
  int ymax = 0;
  int xmax = 0;
  while (m > 0) {
    int i, j;
    cin >> i >> j;
    i--;
    j--;
    v.push_back(P(i, j));
    y[i]++;
    x[j]++;
    if (ymax < y[i])
      ymax = y[i];
    if (xmax < x[j])
      xmax = x[j];
    m--;
  }

  vector<int> yitr;
  vector<int> xitr;
  for (int i = 0; i < h; i++) {
    if (ymax == y[i])
      yitr.push_back(i);
  }
  for (int i = 0; i < w; i++) {
    if (xmax == x[i])
      xitr.push_back(i);
  }
  ll num = yitr.size() * xitr.size();
  ll cnt = 0;
  for (P p : v) {
    int i = p.first;
    int j = p.second;
    if (y[i] == ymax && x[j] == xmax)
      cnt++;
  }
  int ans = ymax + xmax - 1;
  if (cnt < num) {
    ans++;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
const ll INF = 1 << 18;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> y(h, 0);
  vector<int> x(w, 0);
  vector<vector<int>> yx(h, vector<int>(w, 0));
  int xmax = 0;
  int ymax = 0;
  while (m) {
    m--;
    int i, j;
    cin >> i >> j;
    i--;
    j--;
    y[i]++;
    if (y[i] > ymax) {
      ymax = y[i];
    }
    x[j]++;
    if (x[i] > xmax) {
      xmax = x[i];
    }
    yx[i][j]++;
  }
  vector<int> yitr(0);
  vector<int> xitr(0);
  for (int i = 0; i < h; i++) {
    if (y[i] == ymax)
      yitr.push_back(i);
  }
  for (int j = 0; j < w; j++) {
    if (x[j] == xmax)
      xitr.push_back(j);
  }
  bool f = false;
  for (int i : yitr) {
    for (int j : xitr) {
      if (yx[i][j] == 0) {
        f = true;
        break;
      }
    }
    if (f)
      break;
  }

  if (f) {
    cout << ymax + xmax << endl;
  } else {
    cout << ymax + xmax - 1 << endl;
  }
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
int main() {

  auto cmp = [](auto a, auto b) { return a.first > b.first; };

  int n, m, k;
  cin >> n >> m >> k;

  vector<pair<int, int>> row(n), col(m);

  for (int i = 0; i < n; i++) {
    row[i].second = i;
  }

  for (int j = 0; j < m; j++) {
    col[j].second = j;
  }

  set<pair<int, int>> st;

  for (int i = 0; i < k; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    st.insert({u, v});
    row[u].first++;
    col[v].first++;
  }

  sort(row.begin(), row.end(), cmp);
  sort(col.begin(), col.end(), cmp);

  int mx = 0;

  for (auto [count1, x] : row) {
    bool flag = 0;
    for (auto [count2, y] : col) {
      if (!flag) {
        int mymax = count1 + count2;
        if (st.find({x, y}) == st.end()) {
          flag = 1;
        } else {
          mymax--;
        }

        mx = max(mx, mymax);
      } else
        break;
    }
  }

  cout << mx << endl;

  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
int main() {

  int n, m, k;
  cin >> n >> m >> k;

  vector<vector<bool>> a(n, vector<bool>(m, 0));

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

  for (int i = 0; i < k; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    a[u][v] = 1;
  }

  int mx = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j]) {
        row[i]++;
        col[j]++;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j])
        mx = max(mx, row[i] + col[j] - 1);
      else
        mx = max(mx, row[i] + col[j]);
    }
  }

  cout << mx << endl;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int A, B, C;
  cin >> A >> B >> C;
  vector<int> D(A);
  vector<int> E(B);
  vector<int> d(A);
  vector<int> e(B);
  map<pair<int, int>, int> F;
  for (int i = 0; i < C; i++) {
    int a, b;
    cin >> a >> b;
    D[a - 1]++;
    E[b - 1]++;
    F[make_pair(a - 1, b - 1)] = 1;
  }
  d = D;
  e = E;
  sort(D.begin(), D.end());
  sort(E.begin(), E.end());
  vector<int> Z(0, -1);
  vector<int> z(0, -1);
  int Y = 0, y = 0;
  for (int i = 0; i < A; i++)
    if (D[A - 1] == d[i]) {
      Y++;
      Z.push_back(i);
      Z.resize(Y);
    }
  for (int i = 0; i < B; i++)
    if (E[B - 1] == e[i]) {
      y++;
      z.push_back(i);
      z.resize(y);
    }
  for (int i = 0; i < Y; i++) {
    for (int j = 0; j < y; j++) {
      if (F[pair(Z[i], z[j])] == 0) {
        cout << D[A - 1] + E[B - 1] << endl;
        return 0;
      }
    }
  }
  cout << D[A - 1] + E[B - 1] - 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int A, B, C;
  cin >> A >> B >> C;
  vector<int> D(A);
  vector<int> E(B);
  vector<int> d(A);
  vector<int> e(B);
  vector<vector<bool>> F(A, vector<bool>(B, true));
  for (int i = 0; i < C; i++) {
    int a, b;
    cin >> a >> b;
    D[a - 1]++;
    E[b - 1]++;
    F[a - 1][b - 1] = false;
  }
  d = D;
  e = E;
  sort(D.begin(), D.end());
  sort(E.begin(), E.end());
  vector<int> Z(0, -1);
  vector<int> z(0, -1);
  int Y = 0, y = 0;
  for (int i = 0; i < A; i++)
    if (D[A - 1] == d[i]) {
      Y++;
      Z.push_back(i);
      Z.resize(Y);
    }
  for (int i = 0; i < B; i++)
    if (E[B - 1] == e[i]) {
      y++;
      z.push_back(i);
      z.resize(y);
    }
  for (int i = 0; i < Y; i++) {
    for (int j = 0; j < y; j++) {
      if (F[Z[i]][z[j]]) {
        cout << D[A - 1] + E[B - 1] << endl;
        return 0;
      }
    }
  }
  cout << D[A - 1] + E[B - 1] - 1 << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define MOD 1000000007

using namespace std;
using ll = long long;

int mp[1005][1005] = {};

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> height(h), width(w);
  set<pair<int, int>> block;
  rep(i, m) {
    int hei, wid;
    cin >> hei >> wid;
    hei--;
    wid--;
    block.emplace(hei, wid);
    height[hei]++;
    width[wid]++;
  }
  int mh = 0, mw = 0;
  rep(i, h) mh = max(mh, height[i]);
  rep(i, w) mw = max(mw, width[i]);
  int ans = mh + mw;
  vector<int> tate, yoko;
  rep(i, h) if (mh == height[i]) tate.push_back(i);
  rep(i, w) if (mw == width[i]) yoko.push_back(i);
  for (int i : tate) {
    for (int j : yoko) {
      if (block.count(pair<int, int>(i, j)))
        continue;
      cout << ans << endl;
      return 0;
    }
  }
  cout << ans - 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define MOD 1000000007

using namespace std;
using ll = long long;

int mp[1005][1005] = {};

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> height(h), width(w);
  set<pair<int, int>> block;
  rep(i, m) {
    int hei, wid;
    cin >> hei >> wid;
    hei--;
    wid--;
    block.emplace(hei, wid);
    height[hei]++;
    width[wid]++;
  }
  int mh = 0, mw = 0;
  rep(i, h) mh = max(mh, height[i]);
  rep(i, w) mw = max(mw, width[i]);
  int ans = mh + mw;
  rep(i, h) {
    if (height[i] != mh)
      continue;
    rep(j, w) {
      if (width[j] != mw)
        continue;
      if (block.count(pair<int, int>(i, j)))
        continue;
      cout << ans << endl;
      return 0;
    }
  }
  cout << ans - 1 << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define tmp(x) std::cout << "& " << (x) << " &\\n"
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
typedef long long ll;

using namespace std;
const int maxn = 3e5 + 100;
int n, t;
int h, w, m;
vector<int> hh[maxn];
vector<int> ww[maxn];
set<pair<int, int>> s;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> m;
  int xx = 0, yy = 0;
  rep(i, 1, m) {
    int x, y;
    cin >> x >> y;
    hh[x].push_back(y);
    ww[y].push_back(x);
    xx = max(xx, (int)hh[x].size());
    yy = max(yy, (int)ww[y].size());
    s.insert(make_pair(x, y));
  }
  vector<int> ax, ay;
  rep(i, 1, h) if (hh[i].size() == xx) ax.push_back(i);
  rep(i, 1, w) if (ww[i].size() == yy) ay.push_back(i);
  int ans = xx + yy - 1;
  rep(i, 0, ax.size() - 1) {
    rep(j, 0, ay.size() - 1) {
      if (!s.count(make_pair(ax[i], ay[j]))) {
        cout << ans + 1;
        return 0;
      }
    }
  }
  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define tmp(x) std::cout << "& " << (x) << " &\\n"
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
typedef long long ll;

using namespace std;
const int maxn = 3e5 + 100;
int n, t;
int h, w, m;
vector<int> hh[maxn];
vector<int> ww[maxn];
set<pair<int, int>> s;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> m;

  rep(i, 1, m) {
    int x, y;
    cin >> x >> y;
    hh[x].push_back(y);
    ww[y].push_back(x);
    s.insert(make_pair(x, y));
  }
  int ans = 0;
  rep(i, 1, h) {
    rep(j, 1, w) {
      ans = max(ans,
                (int)(hh[i].size() + ww[j].size() - s.count(make_pair(i, j))));
    }
  }
  cout << ans;

  return 0;
}