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

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

void Main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> sh(h), sw(w);
  map<pair<int, int>, int> ss;
  for (int i = 0; i < h; i++)
    sh[i].second = i;
  for (int i = 0; i < w; i++)
    sw[i].second = i;
  for (int i = 0; i < m; i++) {
    int y, x;
    cin >> y >> x;
    y--;
    x--;
    sh[y].first++;
    sw[x].first++;
    ss[make_pair(y, x)] = 1;
  }
  sort(sh.rbegin(), sh.rend());
  sort(sw.rbegin(), sw.rend());

  for (auto i : sh) {
    if (i.first != sh[0].first)
      break;
    for (auto j : sw) {
      if (j.first != sw[0].first)
        break;
      if (ss.count(make_pair(i.second, j.second)) == 0) {
        cout << sh[0].first + sw[0].first << endl;
        return;
      }
    }
  }

  cout << sh[0].first + sw[0].first - 1 << endl;
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  Main();
  return 0;
}


Error: TLE

Fixed submission:

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

void Main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> sh(h), sw(w);
  map<pair<int, int>, int> ss;
  for (int i = 0; i < h; i++)
    sh[i].second = i;
  for (int i = 0; i < w; i++)
    sw[i].second = i;
  for (int i = 0; i < m; i++) {
    int y, x;
    cin >> y >> x;
    y--;
    x--;
    sh[y].first++;
    sw[x].first++;
    ss[make_pair(y, x)] = 1;
  }
  sort(sh.rbegin(), sh.rend());
  sort(sw.rbegin(), sw.rend());

  int ans = 0;
  for (auto i : sh) {
    if (i.first != sh[0].first)
      break;
    for (auto j : sw) {
      if (j.first != sw[0].first)
        break;
      ans = max(ans, i.first + j.first - ss[make_pair(i.second, j.second)]);
    }
  }

  cout << ans << endl;
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  Main();
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

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

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

  int cou_h[h], cou_w[w];
  for (int i = 0; i < h; i++) {
    cou_h[i] = 0;
  }
  for (int i = 0; i < w; i++) {
    cou_w[i] = 0;
  }
  map<pair<int, int>, int> ma;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    auto itr = ma.find(make_pair(a, b));
    if (itr == ma.end()) {
      pair<int, int> c = make_pair(a, b);
      ma[c] = 1;
    }

    cou_h[a]++;
    cou_w[b]++;
  }

  int ans = -1;
  vector<int> v_h;
  for (int i = 0; i < h; i++) {
    if (ans < cou_h[i]) {
      ans = cou_h[i];
      v_h.clear();
      v_h.push_back(i);
    } else if (ans == cou_h[i]) {
      v_h.push_back(i);
    }
  }

  int k = -1;
  vector<int> v_w;
  for (int i = 0; i < w; i++) {
    if (k < cou_w[i]) {
      k = cou_w[i];
      v_w.clear();
      v_w.push_back(i);
    } else if (k == cou_w[i]) {
      v_w.push_back(i);
    }
  }

  int che = 0;
  int size_h = v_h.size();
  int size_w = v_w.size();
  for (int i = 0; i < size_h; i++) {
    for (int j = 0; j < size_w; j++) {
      auto itr = ma.find(make_pair(v_h[i], v_w[j]));
      if (itr == ma.end()) {
        che = 1;
        break;
      }
    }

    if (che == 1) {
      break;
    }
  }

  if (che == 1) {
    cout << ans + k << endl;
  } else {
    cout << ans + k - 1 << endl;
  }
}

Error: WA

Fixed submission:

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

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

  int cou_h[h], cou_w[w];
  for (int i = 0; i < h; i++) {
    cou_h[i] = 0;
  }
  for (int i = 0; i < w; i++) {
    cou_w[i] = 0;
  }

  int flag[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      flag[i][j] = 0;
    }
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    flag[a][b] = 1;

    cou_h[a]++;
    cou_w[b]++;
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << flag[i][j] << " ";
    }
    cout << endl;
  }

  int ans = -1;
  vector<int> v_h;
  for (int i = 0; i < h; i++) {
    if (ans < cou_h[i]) {
      ans = cou_h[i];
      v_h.clear();
      v_h.push_back(i);
    } else if (ans == cou_h[i]) {
      v_h.push_back(i);
    }
  }

  int k = -1;
  vector<int> v_w;
  for (int i = 0; i < w; i++) {
    if (k < cou_w[i]) {
      k = cou_w[i];
      v_w.clear();
      v_w.push_back(i);
    } else if (k == cou_w[i]) {
      v_w.push_back(i);
    }
  }

  int che = 0;
  int size_h = v_h.size();
  int size_w = v_w.size();
  for (int i = 0; i < size_h; i++) {
    for (int j = 0; j < size_w; j++) {
      if (flag[v_h[i]][v_w[j]] == 0) {
        che = 1;
        break;
      }
    }

    if (che == 1) {
      break;
    }
  }

  if (che == 1) {
    cout << ans + k << endl;
  } else {
    cout << ans + k - 1 << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;

void solve() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<pair<int, int>> tag;
  vector<int> tag_h(H, 0), tag_w(W, 0);
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    tag.push_back(make_pair(h, w));
    tag_h[h]++;
    tag_w[w]++;
  }

  sort(tag.begin(), tag.end());
  int tag_h_max = 0, tag_w_max = 0;
  for (int i = 0; i < H; i++) {
    tag_h_max = max(tag_h_max, tag_h[i]);
  }
  for (int i = 0; i < W; i++) {
    tag_w_max = max(tag_w_max, tag_w[i]);
  }

  vector<int> a_h, a_w;
  for (int i = 0; i < H; i++) {
    if (tag_h[i] == tag_h_max) {
      a_h.push_back(i);
    }
  }
  for (int i = 0; i < W; i++) {
    if (tag_w[i] == tag_w_max) {
      a_w.push_back(i);
    }
  }

  for (int i = 0; i < (int)a_h.size(); i++) {
    for (int j = 0; j < (int)a_w.size(); j++) {
      pair<int, int> p = make_pair(a_h[i], a_w[j]);
      if (!binary_search(tag.begin(), tag.end(), p)) {
        cout << tag_h_max + tag_w_max << endl;
        return;
      };
    }
  }
  cout << tag_h_max + tag_w_max - 1 << endl;
}

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

void solve() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<pair<int, int>> tag;
  vector<int> tag_h(H, 0), tag_w(W, 0);
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    tag.push_back(make_pair(h, w));
    tag_h[h]++;
    tag_w[w]++;
  }

  int tag_h_max = 0, tag_w_max = 0;
  for (int i = 0; i < H; i++) {
    tag_h_max = max(tag_h_max, tag_h[i]);
  }
  for (int i = 0; i < W; i++) {
    tag_w_max = max(tag_w_max, tag_w[i]);
  }

  vector<int> a_h, a_w;
  for (int i = 0; i < H; i++) {
    if (tag_h[i] == tag_h_max) {
      a_h.push_back(i);
    }
  }
  for (int i = 0; i < W; i++) {
    if (tag_w[i] == tag_w_max) {
      a_w.push_back(i);
    }
  }

  for (int i = 0; i < (int)a_h.size(); i++) {
    for (int j = 0; j < (int)a_w.size(); j++) {
      pair<int, int> p = make_pair(a_h[i], a_w[j]);
      if (find(tag.begin(), tag.end(), p) == tag.end()) {
        cout << tag_h_max + tag_w_max << endl;
        return;
      };
    }
  }
  cout << tag_h_max + tag_w_max - 1 << endl;
}

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

#include <bits/stdc++.h>

const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < int(b); i++)
#define FORD(i, a, b) for (int i = b - 1; i >= int(a); i--)
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

#define ALL(x) (x).begin(), (x).end()

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> vh(h);
  vector<int> vw(w);
  vector<pair<int, int>> vpii;
  REP(i, m) {
    int y, x;
    cin >> y >> x;
    y--;
    x--;
    vh[y]++;
    vw[x]++;
    vpii.push_back({y, x});
  }

  int hmax = -1;
  set<int> sh;
  REP(i, h) {
    if (hmax < vh[i]) {
      sh.clear();
      sh.insert(i);
      hmax = vh[i];
    } else if (hmax == vh[i]) {
      sh.insert(i);
    }
  }
  int wmax = -1;
  set<int> sw;
  REP(i, w) {
    if (wmax < vw[i]) {
      sw.clear();
      sw.insert(i);
      wmax = vw[i];
    } else if (wmax == vw[i]) {
      sw.insert(i);
    }
  }
  int ans = hmax + wmax;
  int ccnt = 0;
  REP(i, m) {
    if (sh.find(vpii[i].first) != sh.end() &&
        sw.find(vpii[i].second) != sw.end()) {
      ccnt++;
    }
  }
  if ((ll)sh.size() * (ll)sw.size() == ccnt)
    ans--;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < int(b); i++)
#define FORD(i, a, b) for (int i = b - 1; i >= int(a); i--)
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

#define ALL(x) (x).begin(), (x).end()

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> vh(h);
  vector<int> vw(w);
  set<pair<int, int>> spii;
  REP(i, m) {
    int y, x;
    cin >> y >> x;
    y--;
    x--;
    vh[y]++;
    vw[x]++;
    spii.insert({y, x});
  }
  int ans = 0;
  REP(i, h) {
    REP(j, w) {
      int able = vh[i] + vw[j];
      if (spii.find({i, j}) != spii.end())
        able--;
      ans = max(ans, able);
    }
  }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

// ABC176E - Bomber

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

typedef unsigned long long int ull;
typedef long long int ll;

int main() {
  int H, W, M;
  cin >> H >> W >> M;

  int h[M], w[M];
  map<int, int> row;
  map<int, int> col;
  set<pair<int, int>> bomb;
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
    row[h[i]]++;
    col[w[i]]++;
    bomb.insert(make_pair(h[i], w[i]));
  }

  int r_max = 0;
  for (int i = 1; i <= H; i++) {
    if (r_max < row[i]) {
      r_max = row[i];
    }
  }
  int c_max = 0;
  for (int i = 1; i <= W; i++) {
    if (c_max < col[i]) {
      c_max = col[i];
    }
  }

  set<int> r_max_num;
  for (int i = 1; i <= H; i++) {
    if (r_max == row[i]) {
      r_max_num.insert(i);
    }
  }
  set<int> c_max_num;
  for (int i = 1; i <= W; i++) {
    if (c_max == col[i]) {
      c_max_num.insert(i);
    }
  }

  if (r_max_num.size() * c_max_num.size() > M) {
    cout << r_max + c_max << endl;
    return 0;
  }

  for (set<int>::iterator i = r_max_num.begin(); i != r_max_num.end(); i++) {
    for (set<int>::iterator j = c_max_num.begin(); j != c_max_num.end(); j++) {
      if (bomb.find(make_pair(*i, *j)) == bomb.end()) {
        cout << r_max + c_max << endl;
        return 0;
      }
    }
  }

  cout << r_max + c_max - 1 << endl;

  return 0;
}


Error: TLE

Fixed submission:

// ABC176E - Bomber

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

typedef unsigned long long int ull;
typedef long long int ll;

int main() {
  int H, W, M;
  cin >> H >> W >> M;

  int h[M], w[M];
  map<int, int> row;
  map<int, int> col;
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
    row[h[i]]++;
    col[w[i]]++;
  }

  int r_max = 0;
  for (int i = 1; i <= H; i++) {
    if (r_max < row[i]) {
      r_max = row[i];
    }
  }
  int c_max = 0;
  for (int i = 1; i <= W; i++) {
    if (c_max < col[i]) {
      c_max = col[i];
    }
  }

  int r_max_num = 0;
  for (int i = 1; i <= H; i++) {
    if (r_max == row[i]) {
      r_max_num++;
    }
  }
  int c_max_num = 0;
  for (int i = 1; i <= W; i++) {
    if (c_max == col[i]) {
      c_max_num++;
    }
  }

  if (r_max_num * c_max_num > M) {
    cout << r_max + c_max << endl;
    return 0;
  }

  map<pair<int, int>, int> table;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if ((row[i] == r_max) && (col[j] == c_max)) {
        table[make_pair(i, j)] = 1;
      }
    }
  }
  for (int k = 0; k < M; k++) {
    table[make_pair(h[k], w[k])] = 0;
  }

  for (map<pair<int, int>, int>::iterator it = table.begin(); it != table.end();
       it++) {
    pair<pair<int, int>, int> t = *it;
    if (t.second == 1) {
      cout << r_max + c_max << endl;
      return 0;
    }
  }

  cout << r_max + c_max - 1 << endl;

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

// ABC176E - Bomber

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

typedef unsigned long long int ull;
typedef long long int ll;

int main() {
  int H, W, M;
  cin >> H >> W >> M;

  int h[M], w[M];
  map<int, int> row;
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
    row[h[i]]++;
  }
  ull r_max = 0;
  int max_row = 0;
  for (int i = 1; i <= H; i++) {
    if (r_max < row[i]) {
      max_row = i;
      r_max = row[i];
    }
  }

  map<int, int> col;
  for (int i = 0; i < M; i++) {
    if (h[i] != max_row) {
      col[w[i]]++;
    }
  }
  ull c_max = 0;
  for (int i = 1; i <= W; i++) {
    if (c_max < col[i]) {
      c_max = col[i];
    }
  }

  ull ans1 = r_max + c_max;

  map<int, int> col2;
  for (int i = 0; i < M; i++) {
    col2[w[i]]++;
  }
  ull c_max2 = 0;
  int max_col2 = 0;
  for (int i = 1; i <= H; i++) {
    if (c_max2 < col2[i]) {
      max_col2 = i;
      c_max2 = col2[i];
    }
  }

  map<int, int> row2;
  for (int i = 0; i < M; i++) {
    if (w[i] != max_col2) {
      row2[h[i]]++;
    }
  }
  ull r_max2 = 0;
  for (int i = 1; i <= W; i++) {
    if (r_max2 < row2[i]) {
      r_max2 = row2[i];
    }
  }

  ull ans2 = r_max2 + c_max2;

  cout << max(ans1, ans2) << endl;

  return 0;
}


Error: TLE

Fixed submission:

// ABC176E - Bomber

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

typedef unsigned long long int ull;
typedef long long int ll;

int main() {
  int H, W, M;
  cin >> H >> W >> M;

  int h[M], w[M];
  map<int, int> row;
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
    row[h[i]]++;
  }
  ull r_max = 0;
  for (int i = 1; i <= H; i++) {
    if (r_max < row[i]) {
      r_max = row[i];
    }
  }
  vector<int> max_row;
  for (int i = 1; i <= H; i++) {
    if (r_max == row[i]) {
      max_row.push_back(i);
    }
  }

  ull c_max = 0;
  for (int i = 0; i < max_row.size(); i++) {
    map<int, int> col;
    for (int j = 0; j < M; j++) {
      if (h[j] != max_row[i]) {
        col[w[j]]++;
      }
    }
    for (int j = 1; j <= W; j++) {
      if (c_max < col[j]) {
        c_max = col[j];
      }
    }
  }

  cout << r_max + c_max << endl;

  return 0;
}
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;
typedef long long ll;
const int MOD = (int)1e9 + 7;
const double PI = 3.14159265358979323846;
template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

int main(void) {
  int H, W, M;
  cin >> H >> W >> M;
  set<pair<int, int>> st;
  rep(i, M) {
    int tmpa, tmpb;
    cin >> tmpa >> tmpb;
    st.insert({tmpa, tmpb});
  }
  int hmax = 0, wmax = 0;
  map<int, int> hmp, wmp;
  for (auto tmp : st) {
    chmax(hmax, ++hmp[tmp.first]);
    chmax(wmax, ++wmp[tmp.second]);
  }
  vector<int> hmaxpos, wmaxpos;
  for (auto tmp : hmp) {
    if (hmax == tmp.second)
      hmaxpos.push_back(tmp.first);
  }
  for (auto tmp : wmp) {
    if (wmax == tmp.second)
      wmaxpos.push_back(tmp.first);
  }
  int ans = hmax + wmax - 1;
  for (auto h : hmaxpos) {
    if (ans >= hmax + wmax)
      break;
    for (auto w : wmaxpos) {
      if (st.find({h, w}) == st.end()) {
        ans = hmax + wmax;
        break;
      }
    }
  }
  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;
typedef long long ll;
const int MOD = (int)1e9 + 7;
const double PI = 3.14159265358979323846;
template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

int main(void) {
  int H, W, M;
  cin >> H >> W >> M;
  set<pair<int, int>> st;
  rep(i, M) {
    int tmpa, tmpb;
    cin >> tmpa >> tmpb;
    st.insert({tmpa, tmpb});
  }
  int hmax = 0, wmax = 0;
  map<int, int> hmp, wmp;
  for (auto tmp : st) {
    chmax(hmax, ++hmp[tmp.first]);
    chmax(wmax, ++wmp[tmp.second]);
  }
  vector<int> hmaxpos, wmaxpos;
  for (auto tmp : hmp) {
    if (hmax == tmp.second)
      hmaxpos.push_back(tmp.first);
  }
  for (auto tmp : wmp) {
    if (wmax == tmp.second)
      wmaxpos.push_back(tmp.first);
  }
  int ans = hmax + wmax - 1;
  for (auto h : hmaxpos) {
    for (auto w : wmaxpos) {
      if (st.find({h, w}) == st.end())
        ans = hmax + wmax;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1.0)
#define P pair<int, int>
#define PiP pair<ll, pair<ll, ll>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<int> a(h, 0), b(w, 0);
  map<P, int> mp;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x]++;
    b[y]++;
    mp[{x, y}]++;
  }
  int max_a = *max_element(all(a)), max_b = *max_element(all(b));
  int ans = max_a + max_b;
  vector<int> v1, v2;
  rep(i, h) if (max_a == a[i]) v1.push_back(i);
  rep(i, w) if (max_b == b[i]) v2.push_back(i);
  rep(i, v1.size()) {
    rep(j, v2.size()) {
      if (mp[{v1[i], v2[j]}] == 0) {
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans - 1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1.0)
#define P pair<int, int>
#define PiP pair<ll, pair<ll, ll>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<int> a(h, 0), b(w, 0);
  map<P, int> mp;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x]++;
    b[y]++;
    mp[{x, y}]++;
  }
  int max_a = *max_element(all(a)), max_b = *max_element(all(b));
  int ans = max_a + max_b;
  rep(i, h) {
    rep(j, w) {
      if (mp[{i, j}] == 0 && max_b == b[j] && max_a == a[i]) {
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans - 1 << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const long long INF = 1001001001;
const long long MOD = 1000000007;
const double EPS = 1e-10;

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

  int h, w, m;
  cin >> h >> w >> m;
  map<P, int> mp;
  P r[h], c[w];
  for (int i = 0; i < h; i++)
    r[i] = P(0, i);
  for (int j = 0; j < w; j++)
    c[j] = P(0, j);
  for (int i = 0, a, b; i < m; i++) {
    cin >> a >> b;
    a--, b--;
    mp[P(a, b)]++;
    r[a].first++;
    c[b].first++;
  }
  sort(c, c + w, greater<P>());
  int ans = 0;
  for (int i = 0; i < h; i++) {
    int res = r[i].first;
    int t = 0;
    for (int j = 0; j < w; j++) {
      if (mp[P(i, c[j].second)]) {
        t = max(t, c[j].first - 1);
      } else {
        t = max(t, c[j].first);
        break;
      }
      if (c[j].first < t)
        break;
    }
    ans = max(ans, res + t);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const long long INF = 1001001001;
const long long MOD = 1000000007;
const double EPS = 1e-10;

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

  int H, W, m;
  cin >> H >> W >> m;
  P t1[H], t2[W];
  map<P, int> mp;
  for (int i = 0; i < H; i++) {
    t1[i] = P(0, i);
  }
  for (int i = 0; i < W; i++) {
    t2[i] = P(0, i);
  }
  int h[m], w[m];
  for (int i = 0; i < m; i++) {
    cin >> h[i] >> w[i];
    h[i]--, w[i]--;
    mp[P(h[i], w[i])]++;
    t1[h[i]].first++;
    t2[w[i]].first++;
  }
  sort(t2, t2 + W);
  int ans = 0;
  for (int i = 0; i < H; i++) {
    int cnt = 0;
    cnt += t1[i].first;
    int v = t1[i].second;
    int tmp = -1;
    for (int j = 0; j < W; j++) {
      int u = t2[j].second;
      if (tmp == -1) {
        tmp = t2[j].first;
        if (mp[P(v, u)])
          tmp--;
      }
      int t = t2[j].first;
      if (mp[P(v, u)])
        t--;
      tmp = max(tmp, t);
      if (tmp > t2[j].first)
        break;
    }
    ans = max(ans, cnt + tmp);
  }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

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

int h, w, m, sz;
vector<int> ob[300005];

int dat[1200005];

void upd(int pos, int val) {
  pos += sz - 1;
  dat[pos] += val;
  while (pos > 1) {
    pos >>= 1;
    dat[pos] = max(dat[pos << 1], dat[pos << 1 | 1]);
  }
}

int qry(int bk, int l, int r, int ql, int qr) {
  if (qr < l || r < ql)
    return 0;
  if (ql <= l && r <= qr)
    return dat[bk];
  return max(qry(bk << 1, l, (l + r) >> 1, ql, qr),
             qry(bk << 1 | 1, ((l + r) >> 1) + 1, r, ql, qr));
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> h >> w >> m;
  sz = 1;
  while (sz < w)
    sz <<= 1;
  while (m--) {
    int x, y;
    cin >> x >> y;
    ob[x].emplace_back(y);
    upd(y, 1);
  }
  int ans = 0;
  for (int i = 1; i <= h; i++) {
    for (int &j : ob[i])
      upd(j, -1);
    ans = max(ans,
              (int)ob[i].size() +
                  qry(1, 1, sz, 1,
                      sz)); // cerr<<ob[i].size()<<' '<<qry(1,1,sz,1,sz)<<endl;
    for (int &j : ob[i])
      upd(j, 1);
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int h, w, m, sz;
vector<int> ob[300005];

int dat[400005];

void upd(int pos, int val) {
  pos += sz - 1;
  dat[pos] += val;
  while (pos > 1) {
    pos >>= 1;
    dat[pos] = max(dat[pos << 1], dat[pos << 1 | 1]);
  }
}

int qry(int bk, int l, int r, int ql, int qr) {
  if (qr < l || r < ql)
    return 0;
  if (ql <= l && r <= qr)
    return dat[bk];
  return max(qry(bk << 1, l, (l + r) >> 1, ql, qr),
             qry(bk << 1 | 1, ((l + r) >> 1) + 1, r, ql, qr));
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> h >> w >> m;
  sz = 1;
  while (sz < w)
    sz <<= 1;
  while (m--) {
    int x, y;
    cin >> x >> y;
    ob[x].emplace_back(y);
    upd(y, 1);
  }
  int ans = 0;
  for (int i = 1; i <= h; i++) {
    for (int &j : ob[i])
      upd(j, -1);
    ans = max(ans,
              (int)ob[i].size() +
                  qry(1, 1, sz, 1,
                      sz)); // cerr<<ob[i].size()<<' '<<qry(1,1,sz,1,sz)<<endl;
    for (int &j : ob[i])
      upd(j, 1);
  }
  cout << ans << endl;

  return 0;
}