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

#include <climits>
#include <functional>
#include <iostream>
#include <vector>

template <typename T, typename F> struct SegmentTree {
private:
  // using F = std::function<T(T,T)>;
  int n;
  F f;
  T ti;
  std::vector<T> dat;

public:
  SegmentTree(){};
  SegmentTree(F f, T ti) : f(f), ti(ti) {}
  void build(int n_) {
    n = n_;
    dat.assign(2 * n, ti);
  }
  void build(const std::vector<T> &v) {
    int n_ = v.size();
    build(n_);
    for (int i = 0; i < n; ++i)
      dat[n + i] = v[i];
    for (int i = n - 1; i >= 0; --i)
      dat[i] = f(dat[2 * i + 0], dat[2 * i + 1]);
  }
  void set_val(int k, T x) {
    dat[k += n] = x;
    while (k > 0) {
      k = k / 2;
      dat[k] = f(dat[2 * k + 0], dat[2 * k + 1]);
    }
  }
  T query(int a, int b) {
    if (a == b)
      return ti;
    T vl = ti, vr = ti;
    for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        vl = f(vl, dat[l++]);
      if (r & 1)
        vr = f(dat[--r], vr);
    }
    return f(vl, vr);
  }
};

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  map<pair<int, int>, int> cnt;
  map<int, vector<int>> X;
  map<int, int> Y;
  vector<int> XX, YY;
  for (int i = 0; i < M; ++i) {
    int x, y;
    cin >> x >> y;
    XX.emplace_back(x);
    YY.emplace_back(y);
    ++cnt[{x, y}];
    X[x].emplace_back(y);
    ++Y[y];
  }
  sort(XX.begin(), XX.end());
  XX.erase(unique(XX.begin(), XX.end()), XX.end());
  sort(YY.begin(), YY.end());
  YY.erase(unique(YY.begin(), YY.end()), YY.end());
  int ans = 0;
  SegmentTree rmq([](int a, int b) { return max(a, b); }, 0);
  int m = Y.size();
  vector<int> ini(m);
  for (int i = 0; i < m; ++i) {
    ini[i] = Y[YY[i]];
    Y[YY[i]] = i;
  }
  rmq.build(ini);
  for (auto x : XX) {
    for (auto y : X[x]) {
      int i = Y[y];
      int v = rmq.query(i, i + 1);
      rmq.set_val(i, v - 1);
    }
    int s = X[x].size();
    ans = max(ans, rmq.query(0, m) + s);
    for (auto y : X[x]) {
      int i = Y[y];
      int v = rmq.query(i, i + 1);
      rmq.set_val(i, v + 1);
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  map<pair<int, int>, int> cnt;
  map<int, int> X, Y;
  vector<int> XX, YY;
  for (int i = 0; i < M; ++i) {
    int x, y;
    cin >> x >> y;
    XX.emplace_back(x);
    YY.emplace_back(y);
    ++cnt[{x, y}];
    ++X[x];
    ++Y[y];
  }
  sort(XX.begin(), XX.end());
  XX.erase(unique(XX.begin(), XX.end()), XX.end());
  sort(YY.begin(), YY.end());
  YY.erase(unique(YY.begin(), YY.end()), YY.end());
  int ans = 0;
  for (auto x : XX) {
    for (auto y : YY) {
      ans = max(ans, X[x] + Y[y] - cnt[{x, y}]);
    }
  }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n;
int m;
int k;
int a[300005], b[300005];
int p[300005];
int l[300005];
int totp[300005];
int totl[300005];
int maxp = 0;
int maxl = 0;
int cntl;
int cntp;
int ans = 0;
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d %d", &a[i], &b[i]);
    p[a[i]]++;
    l[b[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (p[i] > maxp) {
      maxp = p[i];
      cntp = i;
    }
  }
  for (int i = 1; i <= k; i++) {
    if (a[i] == cntp) {
      totl[b[i]]++;
    }
  }
  for (int i = 1; i <= m; i++) {
    if (l[i] - totl[i] > maxl) {
      maxl = l[i] - totl[i];
    }
  }
  ans = max(ans, maxl + maxp);
  maxl = 0;
  maxp = 0;
  for (int i = 1; i <= m; i++) {
    if (l[i] > maxl) {
      maxl = l[i];
      cntl = i;
    }
  }
  for (int i = 1; i <= k; i++) {
    if (b[i] == cntl) {
      totp[a[i]]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (p[i] - totp[i] > maxp) {
      maxp = p[i] - totp[i];
    }
  }
  ans = max(ans, maxl + maxp);
  printf("%d", ans);
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n;
int m;
int k;
int a, b;
int p[300005];
int l[300005];
int maxp = 0;
int maxl = 0;
int px;
int lx;
int cntl;
int cntp;
int ans = 0;
vector<int> vis[300005];
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 1; i <= k; i++) {
    scanf("%d %d", &a, &b);
    p[a]++;
    l[b]++;
    vis[a][b] = 1;
    if (p[a] > maxp) {
      maxp = p[a];
      cntp = 0;
      px = a;
    }

    if (l[b] > maxl) {
      maxl = l[b];
      cntl = 0;
      lx = b;
    }
    if (lx == b && px == a) {
      ans = maxp + maxl - 1;
    } else {
      ans = maxp + maxl;
    }
  }

  printf("%d", ans);
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
ll H, W, M;
ll Y[300005];
ll X[300005];
int main() {
  cin >> H >> W >> M;
  vector<P> A(M);
  for (ll i = 0; i < M; i++) {
    ll y, x;
    cin >> y >> x;
    P p(y, x);
    A[i] = p;
    Y[y]++;
    X[x]++;
  }
  ll ymax = 0;
  ll xmax = 0;
  for (ll i = 1; i <= H; i++)
    if (ymax < Y[i])
      ymax = Y[i];
  for (ll i = 1; i <= W; i++)
    if (xmax < X[i])
      xmax = X[i];
  set<ll> Symax;
  set<ll> Sxmax;
  for (ll i = 1; i <= H; i++)
    if (Y[i] == ymax)
      Symax.insert(i);
  for (ll i = 1; i <= W; i++)
    if (X[i] == xmax)
      Sxmax.insert(i);
  ll ans = ymax + xmax;
  ll sum = 0;
  for (ll i = 0; i < M; i++) {
    ll y = A[i].first;
    ll x = A[i].second;
    if (Symax.count(y) && Sxmax.count(x))
      sum++;
  }
  ll a = Symax.size();
  ll b = Sxmax.size();
  if (sum == a * b)
    ans--;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
ll H, W, M;
ll Y[1005];
ll X[1005];
int main() {
  cin >> H >> W >> M;
  vector<P> A(M);
  for (ll i = 0; i < M; i++) {
    ll y, x;
    cin >> y >> x;
    P p(y, x);
    A[i] = p;
    Y[y]++;
    X[x]++;
  }
  ll ymax = 0;
  ll xmax = 0;
  for (ll i = 1; i <= H; i++)
    if (ymax < Y[i])
      ymax = Y[i];
  for (ll i = 1; i <= W; i++)
    if (xmax < X[i])
      xmax = X[i];
  set<ll> Symax;
  set<ll> Sxmax;
  for (ll i = 1; i <= H; i++)
    if (Y[i] == ymax)
      Symax.insert(i);
  for (ll i = 1; i <= W; i++)
    if (X[i] == xmax)
      Sxmax.insert(i);
  ll ans = ymax + xmax;
  ll sum = 0;
  for (ll i = 0; i < M; i++) {
    ll y = A[i].first;
    ll x = A[i].second;
    if (Symax.count(y) && Sxmax.count(x))
      sum++;
  }
  ll a = Symax.size();
  ll b = Sxmax.size();
  if (sum == a * b)
    ans--;
  cout << ans << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define FIN                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef set<ii> sii;
typedef set<int> si;
typedef priority_queue<int> pqi;
typedef priority_queue<ii> pqii;

const int INF = 1000000010;
const ll MOD = 1000000007;

int main() {
  FIN

      int h,
      w, m, x, y, i, mx = 0, my = 0, c, r;
  cin >> h >> w >> m;
  sii its;
  vi sh(h), sw(w);
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    --x, --y;
    sh[x]++;
    sw[y]++;
    its.insert(ii(x, y));
  }
  for (i = 0; i < h; i++) {
    if (i == 0 or sh[mx] < sh[i])
      mx = i;
  }
  for (i = 0; i < w; i++) {
    if (i == 0 or sw[my] < sw[i])
      my = i;
  }
  si xs, ys;
  for (i = 0; i < h; i++) {
    if (sh[mx] == sh[i])
      xs.insert(i);
  }
  for (i = 0; i < w; i++) {
    if (sw[my] == sw[i])
      ys.insert(i);
  }
  c = 0;
  for (auto it = its.begin(); it != its.end(); it++) {
    if (xs.find((*it).first) != xs.end() and ys.find((*it).second) != ys.end())
      c++;
  }
  r = sh[mx] + sw[my];
  if (c == ((int)xs.size() * (int)ys.size()))
    r--;

  cout << r << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FIN                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef set<ii> sii;
typedef set<int> si;
typedef priority_queue<int> pqi;
typedef priority_queue<ii> pqii;
typedef queue<ii> qii;

const int INF = 1000000010;
const ll MOD = 1000000007;

int main() {
  FIN

      int h,
      w, m, x, y, c = 0, r, i, j;
  cin >> h >> w >> m;
  vvi tb(h, vi(w));
  vi sh(h), sw(w);
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    tb[--x][--y] = 1;
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      sh[i] += tb[i][j];
      sw[j] += tb[i][j];
    }
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      r = sh[i] + sw[j];
      if (tb[i][j] == 1)
        r--;
      c = max(c, r);
    }
  }
  cout << c << "\\n";

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

#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>;

/*
まず素直に, 最も多く爆弾が置かれている行と列の交点に爆弾を置けば,
最も多く爆破できる. このとき, 設置点に既に爆弾が置かれていた場合,
(行*列)ではその地点を重複して数えてしまうので, (行*列-1)が爆破できる数になる.
逆に, 最も多く爆弾が置かれている行と列の交点で, かつそこに爆弾
が置かれていない場所に爆弾を置けば, (行*列)個の爆弾を爆破でき,
これが最大値となる.
*/

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> hs(h), ws(w); // その行(列)に何個爆弾があるか
  set<P> s;
  rep(i, m) {
    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> mhs, mws;
  rep(i, h) if (hs[i] == mh) mhs.push_back(i); // 最も多く爆弾が置かれている行
  rep(j, w) if (ws[j] == mw) mws.push_back(j); // 最も多く爆弾が置かれている列

  int ans =
      mh +
      mw; // 理論上の最大値. 最大行と最大列の交点で, そこに爆弾が置かれていない
  for (auto i : mhs)
    for (auto j : mws) {
      if (s.count(P(i, j)))
        continue;          // 交点に爆弾が置かれていたらcontinue
      cout << ans << endl; // そうでなければ理論上の最大値が答え
      return 0;
    }

  // 最大行と最大列の交点で, そこに爆弾が置かれていないような地点がなかった場合
  cout << ans - 1 << endl;
}

Error: WA

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() {
  ll H, W, M;
  cin >> H >> W >> M;
  vector<vector<char>> field(H, vector<char>(W, '.'));
  rep(i, M) {
    ll x, y;
    cin >> x >> y;
    field[--x][--y] = '#';
  }

  ll ans = 0;
  for (ll i = 0; i < H; i++) {
    ll bomb = 0;
    if (field[i][i] == '#')
      bomb--;
    rep(k, H) {
      if (field[k][i] == '#')
        bomb++;
    }
    rep(l, W) {
      if (field[i][l] == '#')
        bomb++;
    }
    ans = max(ans, bomb);
  }

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

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

int H, W, M;

void solve(ll &ans, vector<int> &Hmaxpos, vector<int> &Wmaxpos,
           map<P, int> &bomb) {
  int cnt = 1;
  for (auto x : Hmaxpos) {
    for (auto y : Wmaxpos) {
      P p = make_pair(x, y);
      if (bomb[p] == 0)
        return;
      if (cnt == M) {
        --ans;
        return;
      }
      ++cnt;
    }
  }
  --ans;
  return;
}

int main() {
  cin >> H >> W >> M;
  vector<int> h(M), w(M);
  rep(i, M) cin >> h[i] >> w[i];

  vector<int> cntH(H + 1, 0), cntW(W + 1, 0);
  map<P, int> bomb;
  rep(i, M) {
    ++cntH[h[i]];
    ++cntW[w[i]];
    P p = make_pair(h[i], w[i]);
    ++bomb[p];
  }
  vector<int> Hmaxpos, Wmaxpos;
  int Hmax = -1;
  int Wmax = -1;
  rep(i, H + 1) {
    if (cntH[i] < Hmax)
      continue;
    if (cntH[i] > Hmax)
      Hmaxpos.clear();
    Hmax = cntH[i];
    Hmaxpos.push_back(i);
  }
  rep(i, W + 1) {
    if (cntW[i] < Wmax)
      continue;
    if (cntW[i] > Wmax)
      Wmaxpos.clear();
    Wmax = cntW[i];
    Wmaxpos.push_back(i);
  }
  ll ans = Hmax + Wmax;
  solve(ans, Hmaxpos, Wmaxpos, bomb);
  cout << ans << endl;
}

Error: 0

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 H, W, M;

void solve(ll &ans, vector<int> &Hmaxpos, vector<int> &Wmaxpos,
           vector<vector<bool>> &bomb) {
  int cnt = 1;
  for (auto x : Hmaxpos) {
    for (auto y : Wmaxpos) {
      if (bomb[x][y] == false)
        return;
      if (cnt == M) {
        --ans;
        return;
      }
      ++cnt;
    }
  }
  --ans;
  return;
}

int main() {
  cin >> H >> W >> M;
  vector<int> h(M), w(M);
  rep(i, M) cin >> h[i] >> w[i];

  vector<int> cntH(H + 1, 0), cntW(W + 1, 0);
  vector<vector<bool>> bomb(H + 1, vector<bool>(W + 1, false));
  rep(i, M) {
    ++cntH[h[i]];
    ++cntW[w[i]];
    bomb[h[i]][w[i]] = true;
  }
  vector<int> Hmaxpos, Wmaxpos;
  int Hmax = -1;
  int Wmax = -1;
  rep(i, H + 1) {
    if (cntH[i] < Hmax)
      continue;
    if (cntH[i] > Hmax)
      Hmaxpos.clear();
    Hmax = cntH[i];
    Hmaxpos.push_back(i);
  }
  rep(i, W + 1) {
    if (cntW[i] < Wmax)
      continue;
    if (cntW[i] > Wmax)
      Wmaxpos.clear();
    Wmax = cntW[i];
    Wmaxpos.push_back(i);
  }
  ll ans = Hmax + Wmax;
  solve(ans, Hmaxpos, Wmaxpos, bomb);
  cout << ans << endl;
}
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++)
using ll = long long;

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> h(M, 0), w(M, 0);
  vector<int> x(H, 0), y(W, 0);
  set<pair<int, int>> v;
  rep(i, M) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
    x[h[i]]++;
    y[w[i]]++;
    v.insert({h[i], w[i]});
  }

  int h_m = 0;
  rep(i, H) h_m = max(h_m, x[i]);
  int v_m = 0;
  rep(i, W) v_m = max(v_m, y[i]);

  vector<int> a;
  rep(i, H) if (x[i] == h_m) a.push_back(i);
  vector<int> b;
  rep(i, W) if (y[i] == v_m) b.push_back(i);

  for (auto i : a) {
    for (auto j : b) {
      if (v.find({i, j}) == v.end()) {
        cout << h_m + v_m << endl;
        return 0;
      }
    }
  }

  cout << h_m + v_m - 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> h(M, 0), w(M, 0);
  vector<int> x(H, 0), y(W, 0);
  set<pair<int, int>> v;
  rep(i, M) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
    x[h[i]]++;
    y[w[i]]++;
    v.insert({h[i], w[i]});
  }

  int h_m = 0;
  rep(i, H) h_m = max(h_m, x[i]);
  int v_m = 0;
  rep(i, W) v_m = max(v_m, y[i]);

  vector<int> a;
  rep(i, H) if (x[i] == h_m) a.push_back(i);
  vector<int> b;
  rep(i, W) if (y[i] == v_m) b.push_back(i);

  bool flag = false;
  for (auto i : a) {
    for (auto j : b) {
      if (v.find({i, j}) == v.end()) {
        flag = true;
      }
    }
  }

  if (flag)
    cout << h_m + v_m << endl;
  else
    cout << h_m + v_m - 1 << endl;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
using namespace std;
typedef long long ll;
ll t = 0, n, m;
ll y, z;
ll i, j, k, h, w;
struct hhh {
  ll x, y;
  friend bool operator<(const hhh &n1, const hhh &n2) {
    if (n1.x != n2.x)
      return n1.x < n2.x;
    else {
      return n1.y < n2.y;
    }
  }
} p[1000005], p2[1000005];
set<hhh> ma;
ll a[10000005], x1[1000005], y1[1000005], x2[1000005], y2[1000005];
int main() {
  ll j, ma1 = -10000000000, ma2 = -10000000000, k, d, flag = 0, ans, t;

  ans = 0;
  t = 1;
  k = 1;
  scanf("%lld %lld %lld", &h, &w, &m);
  for (i = 1; i <= m; i++) {
    scanf("%lld %lld", &p[i].x, &p[i].y);
    ma.insert(p[i]);
    x1[p[i].x]++;
    y1[p[i].y]++;
  }
  // printf("*%lld\\n",x1[1]);
  for (i = 1; i <= h; i++) {
    ma1 = max(ma1, x1[i]);
  }

  for (i = 1; i <= w; i++) {
    ma2 = max(ma2, y1[i]);
  }
  for (i = 1; i <= h; i++) {
    if (ma1 == x1[i]) {
      x2[k++] = i;
    }
  }
  for (i = 1; i <= w; i++) {
    if (ma2 == y1[i]) {
      y2[t++] = i;
    }
  }
  ans = ma1 + ma2;
  ll o = 1;
  flag = 0;
  for (i = 1; i < k; i++) {
    for (j = 1; j < t; j++) {

      p2[o].x = x2[i];
      p2[o++].y = y2[j];

      // printf("**%lld %lld\\n",p2[o-1].x,p2[o-1].y);
      if (ma.count(p2[o - 1]) == 0) {
        // printf("****%lld %lld\\n",p2[o-1].x,p2[o-1].y);
        flag = 1;
        break;
      }
    }
    if (flag == 1)
      break;
  }
  if (flag == 0)
    ans--;
  // printf("%lld %lld\\n",ma1,ma2);

  printf("%lld\\n", ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
using namespace std;
typedef long long ll;
ll t = 0, n, m;
ll y, z;
ll i, j, k, h, w;
struct hhh {
  ll x, y;

} p[1000005], p2[1000005];
set<hhh> ma;
ll a[10000005], x1[1000005], y1[1000005], x2[1000005], y2[1000005];
int main() {
  ll j, ma1 = -10000000000, ma2 = -10000000000, k, d, flag = 0, ans, t;

  ans = 0;
  t = 1;
  k = 1;
  scanf("%lld %lld %lld", &h, &w, &m);
  for (i = 1; i <= m; i++) {
    scanf("%lld %lld", &p[i].x, &p[i].y);

    x1[p[i].x]++;
    y1[p[i].y]++;
  }
  // printf("*%lld\\n",x1[1]);
  for (i = 1; i <= h; i++) {
    ma1 = max(ma1, x1[i]);
  }

  for (i = 1; i <= w; i++) {
    ma2 = max(ma2, y1[i]);
  }
  for (i = 1; i <= h; i++) {
    if (ma1 == x1[i]) {
      x2[k++] = i;
    }
  }
  for (i = 1; i <= w; i++) {
    if (ma2 == y1[i]) {
      y2[t++] = i;
    }
  }
  ans = ma1 + ma2;
  ll o = 1;
  for (i = 1; i < k; i++) {
    for (j = 1; j < t; j++) {
      flag = 0;
      p2[o].x = x2[i];
      p2[o++].y = y2[j];
      //  printf("**%lld %lld\\n",p2[o-1].x,p2[o-1].y);
      for (ll l = 1; l <= m; l++) { // printf("****%lld %lld\\n",p[l].x,p[l].y);
        if (p2[o - 1].x == p[l].x && p2[o - 1].y == p[l].y) {
          flag = 1;
          break;
        }
      }
      if (flag == 0) {
        break;
      }
    }
    if (flag == 0) {
      break;
    }
  }
  if (flag == 1)
    ans--;
  // printf("%lld %lld\\n",ma1,ma2);

  printf("%lld\\n", ans);

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

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

typedef unsigned long long ULL;
typedef long long LL;

using namespace std;

typedef struct {
  int num;
  int code;
} POS;
int max(int a, int b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}
bool check(POS a, POS b) {
  if (a.num > b.num) {
    return true;
  } else {
    return false;
  }
}
int process();
int main() { cout << process() << endl; }

int process() {
  int H, W, M;
  POS *row;
  POS *col;
  int max_row, max_col;
  LL *pos;
  int h, w, i, j;
  int e_max;
  int max;
  int temp;
  cin >> H >> W >> M;
  row = new POS[H];
  col = new POS[W];
  pos = new LL[M];

  for (i = 0; i < H; i++) {
    row[i].code = i;
  }
  for (j = 0; j < W; j++) {
    col[j].code = j;
  }
  for (i = 0; i < M; i++) {
    cin >> h >> w;
    row[h - 1].num++;
    col[w - 1].num++;
    pos[i] = row[h - 1].code * H + col[w - 1].code;
  }
  sort(row, row + H, check);
  sort(col, col + W, check);
  sort(pos, pos + M);
  max_row = row[0].num;
  max_col = col[0].num;
  e_max = max_row + max_col;
  max = 0;
  for (i = 0; i < H && row[i].num == max_row; i++) {
    for (j = 0; j < W && col[j].num == max_col; j++) {
      temp = row[i].num + col[j].num -
             binary_search(pos, pos + M, row[i].code * H + col[j].code);
      if (max < temp) {
        max = temp;
      }
      if (max == e_max) {
        return max;
      }
    }
  }
  return max;
}

Error: MLE

Fixed submission:

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

typedef unsigned long long ULL;
typedef long long LL;

using namespace std;

typedef struct {
  int num;
  int code;
} POS;
int max(int a, int b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}
bool check(POS a, POS b) {
  if (a.num > b.num) {
    return true;
  } else {
    return false;
  }
}
int process();
int main() { cout << process() << endl; }

int process() {
  int H, W, M;
  POS *row;
  POS *col;
  int max_row, max_col;
  bool **pos;
  int h, w, i, j;
  int e_max;
  int max;
  int temp;
  cin >> H >> W >> M;
  row = new POS[H];
  col = new POS[W];
  pos = new bool *[H];
  for (i = 0; i < H; i++) {
    pos[i] = new bool[W];
  }

  for (i = 0; i < H; i++) {
    row[i].code = i;
  }
  for (j = 0; j < W; j++) {
    col[j].code = j;
  }
  for (i = 0; i < M; i++) {
    cin >> h >> w;
    pos[(h - 1)][w - 1] = 1;
    row[h - 1].num++;
    col[w - 1].num++;
  }
  sort(row, row + H, check);
  sort(col, col + W, check);
  max_row = row[0].num;
  max_col = col[0].num;
  e_max = max_row + max_col;
  max = 0;
  for (i = 0; i < H && row[i].num == max_row; i++) {
    for (j = 0; j < W && col[j].num == max_col; j++) {
      temp = row[i].num + col[j].num - pos[row[i].code][col[j].code];
      if (max < temp) {
        max = temp;
      }
      if (max == e_max) {
        return max;
      }
    }
  }
  return max;
}