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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using ll = long long;
using namespace std;

constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9 + 7);

int main(void) {
  int H, W, m;
  cin >> H >> W >> m;

  vector<int> row(H, 0), col(W, 0);
  set<pair<int, int>> st;
  for (int i = 0; i < m; ++i) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    row[h]++;
    col[w]++;
    st.insert({h, w});
  }

  int maxRow = *max_element(row.begin(), row.end());
  int maxCol = *max_element(col.begin(), col.end());

  vector<int> r, c;
  for (int i = 0; i < (int)row.size(); ++i) {
    if (row[i] == maxRow)
      r.push_back(i);
  }

  for (int i = 0; i < (int)col.size(); ++i) {
    if (col[i] == maxCol)
      c.push_back(i);
  }

  int ans = maxRow + maxCol - 1;
  for (auto &i : r) {
    for (auto &j : c) {
      if (st.find({i, j}) == st.end()) {
        cout << ans + 1 << endl;
        return 0;
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using ll = long long;
using namespace std;

constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9 + 7);

int main(void) {
  int H, W, m;
  cin >> H >> W >> m;

  vector<vector<int>> grid(H, vector<int>(W, 0));
  vector<int> row(H, 0), col(W, 0);
  for (int i = 0; i < m; ++i) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;

    grid[h][w] = 1;
    row[h]++;
    col[w]++;
  }

  int ans = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      int x = 0;

      if (grid[i][j])
        x = 1;

      ans = max(ans, row[i] + col[j] - x);
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

typedef long long ll;

ll cntH[300001], cntW[300001];

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  map<pair<ll, ll>, bool> mines;
  for (ll m = 0; m < M; m++) {
    ll h, w;
    cin >> h >> w;
    cntH[h]++;
    cntW[w]++;
    mines[pair<ll, ll>(h, w)] = true;
  }

  ll idxH = 1;
  for (ll h = 2; h <= H; h++) {
    if (cntH[h] > cntH[idxH]) {
      idxH = h;
    }
  }
  vector<ll> candidateH;
  for (ll h = 1; h <= H; h++) {
    if (cntH[h] == cntH[idxH]) {
      candidateH.push_back(h);
    }
  }

  ll idxW = 1;
  for (ll w = 2; w <= W; w++) {
    if (cntW[w] > cntW[idxW]) {
      idxW = w;
    }
  }
  vector<ll> candidateW;
  for (ll w = 1; w <= W; w++) {
    if (cntW[w] == cntW[idxW]) {
      candidateW.push_back(w);
    }
  }

  ll cnt = cntH[idxH] + cntW[idxW];
  for (ll ch = 0; ch < candidateH.size(); ch++) {
    for (ll cw = 0; cw < candidateW.size(); cw++) {
      if (!mines[pair<ll, ll>(candidateH[ch], candidateW[cw])]) {
        cout << cnt << endl;
        return 0;
      }
    }
  }

  cout << cnt - 1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

typedef long long ll;

ll cntH[300001], cntW[300001];

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  map<pair<ll, ll>, bool> mines;
  for (ll m = 0; m < M; m++) {
    ll h, w;
    cin >> h >> w;
    cntH[h]++;
    cntW[w]++;
    mines[pair<ll, ll>(h, w)] = true;
  }

  ll mx = 0;
  for (ll h = 1; h <= H; h++) {
    for (ll w = 1; w <= W; w++) {
      ll cnt = cntH[h] + cntW[w];
      if (mines[pair<ll, ll>(h, w)]) {
        cnt -= 1;
      }

      mx = max(mx, cnt);
    }
  }

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

#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define loop(i, x, n) for (ll i = x; i < n; i++)
#define loopr(i, x, n) for (ll i = n - 1; i >= x; i--)
#define S(v) sort(v.begin(), v.end());
#define ff first
#define ss second
map<ll, bool> empty;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n, m, k;
    cin >> n >> m >> k;
    vector<ll> row(n, 0), col(m, 0);
    set<ll> x, y;
    map<pair<ll, ll>, bool> mp;
    vector<pair<ll, ll>> v(k);
    loop(i, 0, k) {
      ll a, b;
      cin >> a >> b;
      row[a - 1]++;
      col[b - 1]++;
      mp[make_pair(a - 1, b - 1)] = 1;
      v[i] = make_pair(a - 1, b - 1);
    }
    ll mx1 = *max_element(row.begin(), row.end()),
       mx2 = *max_element(col.begin(), col.end());
    ll ans = mx1 + mx2 - 1;
    loop(i, 0, n) {
      if (row[i] == mx1)
        x.insert(i);
    }
    loop(i, 0, m) {
      if (col[i] == mx2)
        y.insert(i);
    }
    ll sum = 0;
    for (auto p : v) {
      // cout<<p.first<<" "<<p.second<<endl;
      if (x.find(p.first) != x.end() && y.find(p.second) != y.end())
        sum++;
    }
    // cout<<sum<<endl;
    // for(auto i:x)cout<<i<<" ";cout<<endl;
    // for(auto i:y)cout<<i<<" ";cout<<endl;
    // cout<<mx1<<" "<<mx2;
    cout << (sum - x.size() * y.size() == 0 ? ans : ans + 1);
  }
}
/*

1 5 4 1 2 2 4

*/


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define loop(i, x, n) for (ll i = x; i < n; i++)
#define loopr(i, x, n) for (ll i = n - 1; i >= x; i--)
#define S(v) sort(v.begin(), v.end());
#define ff first
#define ss second
map<ll, bool> empty;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n, m, k;
    cin >> n >> m >> k;
    vector<ll> row(n, 0), col(m, 0), x, y;
    map<pair<ll, ll>, bool> mp;
    loop(i, 0, k) {
      ll a, b;
      cin >> a >> b;
      row[a - 1]++;
      col[b - 1]++;
      mp[make_pair(a - 1, b - 1)] = 1;
    }
    ll mx1 = *max_element(row.begin(), row.end()),
       mx2 = *max_element(col.begin(), col.end());
    ll ans = mx1 + mx2 - 1;
    loop(i, 0, n) {
      if (row[i] == mx1)
        x.push_back(i);
    }
    loop(i, 0, m) {
      if (col[i] == mx2)
        y.push_back(i);
    }
    loop(i, 0, x.size()) {
      loop(j, 0, y.size()) {
        if (mp[make_pair(x[i], y[j])] == 0)
          ans = max(ans, mx1 + mx2);
      }
    }
    // for(auto i:x)cout<<i<<" ";cout<<endl;
    // for(auto i:y)cout<<i<<" ";cout<<endl;
    // cout<<mx1<<" "<<mx2;
    cout << ans << endl;
  }
}
/*

1 5 4 1 2 2 4

*/
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
  int H, W, M;
  cin >> H >> W >> M;
  int x, y, i, j;
  set<pair<int, int>> mySet;
  set<pair<int, int>>::iterator sit;
  vector<int> LineBom(H + 1, 0);
  vector<int> ColBom(W + 1, 0);
  int MaxLineBom = 0;
  int MaxColBom = 0;
  for (i = 0; i < M; i++) {
    cin >> x >> y;
    LineBom[x]++;
    if (MaxLineBom < LineBom[x]) {
      MaxLineBom = LineBom[x];
    }
    ColBom[y]++;
    if (MaxColBom < ColBom[y]) {
      MaxColBom = ColBom[y];
    }
    mySet.insert(pair<int, int>(x, y));
  }
  vector<int> MaxLine;
  for (i = 1; i <= H; i++) {
    if (MaxLineBom == LineBom[i]) {
      MaxLine.push_back(i);
    }
  }
  vector<int> MaxCol;
  for (j = 1; j <= W; j++) {
    if (MaxColBom == ColBom[j]) {
      MaxCol.push_back(j);
    }
  }
  int MaxBom = MaxLineBom + MaxColBom;
  for (i = 0; i < MaxLine.size(); i++) {
    for (j = 0; j < MaxCol.size(); j++) {
      sit = mySet.find(pair<int, int>(MaxLine[i], MaxCol[j]));
      if (sit == mySet.end()) {
        cout << MaxBom << endl;
        return 0;
      }
    }
  }
  cout << MaxBom - 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
  int H, W, M;
  cin >> H >> W >> M;
  int x, y, i, j;
  set<pair<int, int>> mySet;
  set<pair<int, int>>::iterator sit;
  vector<int> LineBom(H + 1, 0);
  vector<int> ColBom(W + 1, 0);
  int MaxLineBom = 0;
  int MaxColBom = 0;
  for (i = 0; i < M; i++) {
    cin >> x >> y;
    LineBom[x]++;
    if (MaxLineBom < LineBom[x]) {
      MaxLineBom = LineBom[x];
    }
    ColBom[y]++;
    if (MaxColBom < ColBom[y]) {
      MaxColBom = ColBom[y];
    }
    mySet.insert(pair<int, int>(x, y));
  }
  vector<int> MaxLine;
  for (i = 1; i <= H; i++) {
    if (MaxLineBom == LineBom[i]) {
      MaxLine.push_back(i);
    }
  }
  vector<int> MaxCol;
  for (j = 1; j <= W; j++) {
    if (MaxColBom == ColBom[j]) {
      MaxCol.push_back(j);
    }
  }
  int MaxBom = MaxLineBom + MaxColBom;
  int MaxBomNums = 0;
  for (i = 0; i < MaxLine.size(); i++) {
    for (j = 0; j < MaxCol.size(); j++) {
      sit = mySet.find(pair<int, int>(MaxLine[i], MaxCol[j]));
      int maxBom;
      if (sit != mySet.end()) {
        maxBom = MaxBom - 1;
      } else {
        maxBom = MaxBom;
      }
      if (MaxBomNums < maxBom) {
        MaxBomNums = maxBom;
      }
    }
  }
  cout << MaxBomNums << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#define maxn 300010
using namespace std;
struct ty {
  int i, j;
} a[maxn];
int H, W, m;
int h[maxn], w[maxn];
int A[maxn], B[maxn];
#define PII pair<int, int>
map<PII, int> s;
pair<int, int> tmp;
bool zw(int i, int j) {
  pair<int, int> x;
  x.first = i;
  x.second = j;
  if (s[x] == 1)
    return 1;
  return 0;
}
int main() {
  scanf("%d %d %d", &H, &W, &m);
  int hmax = 0, wmax = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &a[i].i, &a[i].j);
    tmp.first = a[i].i;
    tmp.second = a[i].j;
    s[tmp] = 1;
    h[a[i].i]++;
    w[a[i].j]++;
    hmax = max(hmax, h[a[i].i]);
    wmax = max(wmax, w[a[i].j]);
  }
  // printf("hmax: %d wmax :%d\\n",hmax,wmax);
  int j = 1;
  int len1, len2;
  for (int i = 1; i <= H; i++) {
    if (hmax == h[i])
      A[j++] = i;
  }
  len1 = j - 1;
  j = 1;
  for (int i = 1; i <= W; i++) {
    if (wmax == w[i])
      B[j++] = i;
  }
  len2 = j - 1;
  for (int i = 1; i <= len1; i++)
    for (int j = 1; j <= len2; j++) {
      if (zw(A[i], B[j]) == 0) {
        printf("%d\\n", hmax + wmax);
        return 0;
      }
    }
  printf("%d\\n", hmax + wmax - 1);
}

Error: TLE

Fixed submission:

#include <iostream>
#define maxn 300010
using namespace std;
struct ty {
  int i, j;
} a[maxn];
int H, W, m;
int h[maxn], w[maxn];
int A[maxn], B[maxn];
bool zw(int i, int j) {
  for (int k = 1; k <= m; k++) {
    if (a[k].i == i && a[k].j == j)
      return 1;
  }
  return 0;
}
int main() {
  cin >> H >> W >> m;
  int hmax = 0, wmax = 0;
  for (int i = 1; i <= m; i++) {
    cin >> a[i].i >> a[i].j;
    h[a[i].i]++;
    w[a[i].j]++;
    hmax = max(hmax, h[a[i].i]);
    wmax = max(wmax, w[a[i].j]);
  }
  // printf("hmax: %d wmax :%d\\n",hmax,wmax);
  int j = 1;
  int len1, len2;
  for (int i = 1; i <= H; i++) {
    if (hmax == h[i])
      A[j++] = i;
  }
  len1 = j - 1;
  j = 1;
  for (int i = 1; i <= W; i++) {
    if (wmax == w[i])
      B[j++] = i;
  }
  len2 = j - 1;
  for (int i = 1; i <= len1; i++)
    for (int j = 1; j <= len2; j++) {
      if (zw(A[i], B[j]) == 0) {
        printf("%d\\n", hmax + wmax);
        return 0;
      }
    }
  printf("%d\\n", hmax + wmax - 1);
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int h, w, m;
typedef pair<int, int> P;
// 行与列
unordered_map<int, int> m1;
unordered_map<int, int> m2;
map<P, int> c;
int main() {
  vector<int> col, row;
  int ans = 0;
  int max1 = -1, max2 = -1;
  int index1, index2;
  cin >> h >> w >> m;

  int x, y = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    if (!m1.count(x)) {
      m1[x] = 1;
    } else {
      m1[x] += 1;
    }
    if (!m2.count(y)) {
      m2[y] = 1;
    } else {
      m2[y] += 1;
    }
    max1 = max(max1, m1[x]);
    max2 = max(max2, m2[y]);
    c[make_pair(x, y)] = 1;
  }
  unordered_map<int, int>::iterator iter;
  for (iter = m1.begin(); iter != m1.end(); iter++) {
    if (iter->second == max1)
      row.push_back(iter->first);
  }
  for (iter = m2.begin(); iter != m2.end(); iter++) {
    if (iter->second == max2)
      col.push_back(iter->first);
  }
  // for(int i = 0;i<row.size();i++){
  //     cout << row[i] << " ";
  // }
  // cout << endl;
  for (int i = 0; i < row.size(); i++) {
    for (int j = 0; j < col.size(); j++) {
      int num;
      if (c[make_pair(row[i], col[j])] == 1)
        num = max1 + max2 - 1;
      else
        num = max1 + max2;
      ans = max(ans, num);
      if (ans == max1 + max2)
        break;
    }
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int h, w, m;
typedef pair<int, int> P;
// 行与列
unordered_map<int, int> m1;
unordered_map<int, int> m2;
map<P, int> c;
int main() {
  vector<int> col, row;
  int ans = 0;
  int max1 = -1, max2 = -1;
  int index1, index2;
  cin >> h >> w >> m;

  int x, y = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    if (!m1.count(x)) {
      m1[x] = 1;
    } else {
      m1[x] += 1;
    }
    if (!m2.count(y)) {
      m2[y] = 1;
    } else {
      m2[y] += 1;
    }
    max1 = max(max1, m1[x]);
    max2 = max(max2, m2[y]);
    c[make_pair(x, y)] = 1;
  }
  unordered_map<int, int>::iterator iter;
  for (iter = m1.begin(); iter != m1.end(); iter++) {
    if (iter->second == max1)
      row.push_back(iter->first);
  }
  for (iter = m2.begin(); iter != m2.end(); iter++) {
    if (iter->second == max2)
      col.push_back(iter->first);
  }
  // for(int i = 0;i<row.size();i++){
  //     cout << row[i] << " ";
  // }
  // cout << endl;
  for (int i = 0; i < row.size(); i++) {
    for (int j = 0; j < col.size(); j++) {
      int num;
      if (c[make_pair(row[i], col[j])] == 1)
        num = max1 + max2 - 1;
      else
        num = max1 + max2;
      ans = max(ans, num);
    }
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int h, w, m;
typedef pair<int, int> P;
// 行与列
unordered_map<int, int> m1;
unordered_map<int, int> m2;
map<P, int> c;
int main() {
  vector<int> col, row;
  int ans = 0;
  int max1 = -1, max2 = -1;
  int index1, index2;
  cin >> h >> w >> m;

  int x, y = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    if (!m1.count(x)) {
      m1[x] = 1;
    } else {
      m1[x] += 1;
    }
    if (!m2.count(y)) {
      m2[y] = 1;
    } else {
      m2[y] += 1;
    }
    max1 = max(max1, m1[x]);
    max2 = max(max2, m2[y]);
    c[make_pair(x, y)] = 1;
  }
  unordered_map<int, int>::iterator iter;
  for (iter = m1.begin(); iter != m1.end(); iter++) {
    if (iter->second == max1)
      row.push_back(iter->first);
  }
  for (iter = m2.begin(); iter != m2.end(); iter++) {
    if (iter->second == max2)
      col.push_back(iter->first);
  }
  // for(int i = 0;i<row.size();i++){
  //     cout << row[i] << " ";
  // }
  // cout << endl;
  for (int i = 0; i < row.size(); i++) {
    for (int j = 0; j < col.size(); j++) {
      int num;
      if (c[make_pair(row[i], col[j])] == 1)
        num = max1 + max2 - 1;
      else
        num = max1 + max2;
      ans = max(ans, num);
      if (ans == max1 + max2)
        break;
    }
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int h, w, M;
map<P, int> m;
int main() {
  cin >> h >> w >> M;
  vector<int> x(h + 10);
  vector<int> y(w + 10);
  vector<int> rec1, rec2;
  int max1 = -1, max2 = -1;
  for (int i = 0; i < M; i++) {
    int xx, yy;
    scanf("%d%d", &xx, &yy);
    m[P(xx, yy)] = 1;
    x[xx] += 1;
    y[yy] += 1;
    max1 = max(max1, x[xx]);
    max2 = max(max2, y[yy]);
  }
  for (int i = 1; i <= max(h, w); i++) {
    if (x[i] == max1)
      rec1.push_back(i);
  }
  for (int i = 1; i <= max(h, w); i++) {
    if (y[i] == max2)
      rec2.push_back(i);
  }
  for (int i = 0; i < rec1.size(); i++) {
    for (int j = 0; j < rec2.size(); j++) {
      int xx = rec1[i];
      int yy = rec2[j];
      if (!m.count(P(xx, yy))) {
        cout << max1 + max2 << endl;
        return 0;
      }
    }
  }
  cout << max1 + max2 - 1 << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e5 + 10, mod = 1e9 + 7;

void solve() {
  int h, w, m;
  cin >> h >> w >> m;

  int hc[h + 1], wc[w + 1];
  pii pos[m + 1];
  memset(pos, 0, sizeof(pos));
  memset(hc, 0, sizeof(hc));
  memset(wc, 0, sizeof(wc));

  for (int i = 1; i <= m; ++i) {
    int x, y;
    cin >> x >> y;
    pos[i] = {x, y};
    ++hc[x];
    ++wc[y];
  }

  int hmx = 0, hcnt = 0;
  for (int i = 1; i <= h; ++i) {
    if (hc[i] == hmx) {
      ++hcnt;
    }
    if (hc[i] > hmx) {
      hcnt = 1;
      hmx = hc[i];
    }
  }

  int wmx = 0, wcnt = 0;
  for (int i = 1; i <= w; ++i) {
    if (wc[i] == wmx) {
      ++wcnt;
    }
    if (wc[i] > wmx) {
      wcnt = 1;
      wmx = wc[i];
    }
  }

  int ans = hmx + wmx, cnt = 0;
  for (int i = 1; i <= m; ++i) {
    int x = pos[i].first, y = pos[i].second;
    if (hc[x] == hmx && wc[y] == wmx) {
      ++cnt;
    }
  }

  if (hcnt * wcnt == cnt) {
    --ans;
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--)
    solve();
  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e5 + 10, mod = 1e9 + 7;

void solve() {
  int h, w, m;
  cin >> h >> w >> m;

  int a[h + 1][w + 1], hc[h + 1], wc[w + 1];
  memset(a, 0, sizeof(a));
  memset(hc, 0, sizeof(hc));
  memset(wc, 0, sizeof(wc));

  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    a[x][y] = 1;
    ++hc[x];
    ++wc[y];
  }

  int ans = 0;
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      int cur = hc[i] + wc[j];
      if (a[i][j]) {
        --cur;
      }
      ans = max(ans, cur);
    }
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--)
    solve();
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<long long>> vvll;
typedef long double ld;
typedef std::vector<long double> vld;
typedef std::vector<std::vector<long double>> vvld;
using namespace std;
void yesno(bool state);

void Main() {
  ll H, W, M;
  cin >> H >> W >> M;
  // vector<vector<bool>> A(H,vector<bool>(W));
  // rep(i,H)
  //     rep(j,W)
  //         A[i][j] = false;

  vll h(H, 0), w(W, 0);
  vector<pair<ll, ll>> hw(M);
  rep(m, M) {
    ll i, j;
    cin >> i >> j;
    i--, j--;
    hw[m] = {i, j};
    h[i]++, w[j]++;
  }

  vll::iterator max_iter_h = max_element(h.begin(), h.end());
  ll max_h = *max_iter_h;
  vll::iterator max_iter_w = max_element(w.begin(), w.end());
  ll max_w = *max_iter_w;
  vll max_h_idxs, max_w_idxs;

  ll h_memo = 0, w_memo = 0;
  rep(i, H) if (h[i] == max_h) h_memo++;
  rep(i, W) if (w[i] == max_w) w_memo++;

  ll memo = 0;
  rep(i, M) {
    ll h_ = hw[i].first;
    ll w_ = hw[i].second;
    if (h[h_] == max_h && w[w_] == max_w)
      memo++;
  }

  if (h_memo * w_memo == memo)
    cout << (max_h + max_w - 1) << endl;
  else
    cout << (max_h + max_w) << endl;
}

int main(int argc, char const *argv[]) {
  Main();
  return 0;
}

void yesno(bool state) {
  if (state)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<long long>> vvll;
typedef long double ld;
typedef std::vector<long double> vld;
typedef std::vector<std::vector<long double>> vvld;
using namespace std;
void yesno(bool state);

void Main() {
  ll H, W, M;
  cin >> H >> W >> M;
  vector<vector<bool>> A(H, vector<bool>(W));
  rep(i, H) rep(j, W) A[i][j] = false;

  rep(i, M) {
    ll h, w;
    cin >> h >> w;
    A[h - 1][w - 1] = true;
  }
  vll h(H, 0), w(W, 0);
  rep(i, H) rep(j, W) if (A[i][j]) h[i]++, w[j]++;
  vll::iterator max_iter_h = max_element(h.begin(), h.end());
  ll max_h = *max_iter_h;
  vll::iterator max_iter_w = max_element(w.begin(), w.end());
  ll max_w = *max_iter_w;
  vll max_h_idxs, max_w_idxs;
  rep(i, H) if (h[i] == max_h) max_h_idxs.push_back(i);
  rep(i, W) if (w[i] == max_w) max_w_idxs.push_back(i);

  bool minus = true;
  for (ll i : max_h_idxs)
    for (ll j : max_w_idxs)
      if (!A[i][j])
        minus = false;

  if (minus)
    cout << (max_h + max_w - 1) << endl;
  else
    cout << (max_h + max_w) << endl;
}

int main(int argc, char const *argv[]) {
  Main();
  return 0;
}

void yesno(bool state) {
  if (state)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 1;
int r[N];
int c[N];
int n, m, k;
int rr[N];
int cc[N];
struct node {
  int x, y;
  node(int _x, int _y) { x = _x, y = _y; }
  bool operator<(const node &a) const {
    if (a.x != x)
      return a.x < x;
    else
      return a.y < y;
  };
};
map<node, int> st;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  while (k--) {
    int x, y;
    scanf("%d%d", &x, &y);
    r[x]++;
    c[y]++;
    node s = node(x, y);
    st[s] = 1;
  }
  int maxn = 0;
  int sum = 0;
  int tot = 0;
  for (int i = 0; i < N; i++) {
    if (r[i] == maxn) {
      rr[++tot] = i;
    } else if (r[i] > maxn) {
      tot = 0;
      rr[++tot] = i;
      maxn = r[i];
    }
  }
  sum += maxn;
  maxn = 0;
  int to = 0;
  for (int i = 0; i < N; i++) {
    if (c[i] == maxn) {
      cc[++to] = i;
    } else if (c[i] > maxn) {
      to = 0;
      cc[++to] = i;
      maxn = c[i];
    }
  }
  sum += maxn;
  int f = 0;
  for (int i = 1; i <= tot; i++) {
    for (int j = 1; j <= to; j++) {
      node sk = node(rr[i], cc[j]);
      if (!st[sk]) {
        f = 1;
        break;
      }
    }
    if (f)
      break;
  }
  if (f)
    cout << sum << endl;
  else
    cout << sum - 1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 1;
int r[N];
int c[N];
int n, m, k;
int rr[N];
int cc[N];
struct node {
  int x, y;
  node(int _x, int _y) { x = _x, y = _y; }
  bool operator<(const node &a) const {
    if (a.x != x)
      return a.x < x;
    else
      return a.y < y;
  };
};
map<node, int> st;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  while (k--) {
    int x, y;
    scanf("%d%d", &x, &y);
    r[x]++;
    c[y]++;
    node s = node(x, y);
    st[s] = 1;
  }
  int maxn = 0;
  int sum = 0;
  int tot = 0;
  for (int i = 0; i < N; i++) {
    if (r[i] == maxn) {
      rr[++tot] = i;
    } else if (r[i] > maxn) {
      tot = 0;
      rr[++tot] = i;
      maxn = r[i];
    }
  }
  sum += maxn;
  maxn = 0;
  int to = 0;
  for (int i = 0; i < N; i++) {
    if (c[i] == maxn) {
      cc[++to] = i;
    } else if (c[i] > maxn) {
      to = 0;
      cc[++to] = i;
      maxn = c[i];
    }
  }
  sum += maxn;
  int f = 0;
  for (int i = 1; i <= tot; i++) {
    for (int j = 1; j <= to; j++) {
      node sk = node(rr[i], cc[j]);
      if (!st[sk])
        f = 1;
    }
  }
  if (f)
    cout << sum << endl;
  else
    cout << sum - 1 << endl;
}