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

#include <algorithm>
#include <iostream>
#include <set>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

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

  unordered_map<int, int> cnt_h;
  unordered_map<int, int> cnt_w;
  unordered_map<int, set<int>> idx_h;
  unordered_map<int, set<int>> idx_w;

  vector<pair<int, int>> bombs(M);
  int mx_h = 0;
  int mx_w = 0;
  for (int m = 0; m < M; ++m) {
    int h, w;
    cin >> h >> w;
    --h, --w;
    bombs[m] = {h, w};
    ++cnt_h[h];
    ++cnt_w[w];
    if (mx_h <= cnt_h[h]) {
      mx_h = cnt_h[h];
      idx_h[mx_h].insert(h);
    }
    if (mx_w <= cnt_w[w]) {
      mx_w = cnt_w[w];
      idx_w[mx_w].insert(w);
    }
  }
  int possible_size = idx_h[mx_h].size() * idx_w[mx_w].size();
  if (possible_size > bombs.size()) {
    cout << mx_h + mx_w << endl;
  } else {
    int hit = 0;
    for (const pair<int, int> &bomb : bombs) {
      if (idx_h[mx_h].count(bomb.first) != 0 &&
          idx_w[mx_w].count(bomb.second) != 0)
        ++hit;
    }
    if (possible_size == hit) {
      cout << mx_h + mx_w - 1 << endl;
    } else {
      cout << mx_h + mx_w << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

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

  unordered_map<int, int> cnt_h;
  unordered_map<int, int> cnt_w;
  unordered_map<int, vector<int>> idx_h;
  unordered_map<int, vector<int>> idx_w;

  vector<pair<int, int>> bombs(M);
  int mx_h = 0;
  int mx_w = 0;
  for (int m = 0; m < M; ++m) {
    int h, w;
    cin >> h >> w;
    --h, --w;
    bombs[m] = {h, w};
    ++cnt_h[h];
    ++cnt_w[w];
    if (mx_h <= cnt_h[h]) {
      if (mx_h < cnt_h[h])
        idx_h[mx_h - 1].resize(0);
      mx_h = cnt_h[h];
      idx_h[mx_h].push_back(h);
    }
    if (mx_w <= cnt_w[w]) {
      if (mx_w < cnt_w[w])
        idx_w[mx_w - 1].resize(0);
      mx_w = cnt_w[w];
      idx_w[mx_w].push_back(w);
    }
  }
  set<pair<int, int>> possible;
  for (int h = 0, size_h = idx_h[mx_h].size(); h < size_h; ++h) {
    for (int w = 0, size_w = idx_w[mx_w].size(); w < size_w; ++w) {
      possible.insert({idx_h[mx_h][h], idx_w[mx_w][w]});
      //      cout << "{" << idx_h[mx_h][h] << ", " << idx_w[mx_w][w] << "}" <<
      //      endl;
    }
  }
  if (possible.size() > bombs.size()) {
    cout << mx_h + mx_w << endl;
  } else {
    int hit = 0;
    for (const pair<int, int> &bomb : bombs) {
      if (possible.count({bomb.first, bomb.second}))
        ++hit;
    }
    if (possible.size() == hit) {
      cout << mx_h + mx_w - 1 << endl;
    } else {
      cout << mx_h + mx_w << endl;
    }
  }
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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;
  vector<int> rowNum(h, 0);
  vector<int> columnNum(w, 0);
  set<pair<int, int>> s;
  for (int i = 0; i < m; i++) {
    int H, W;
    cin >> H >> W;
    H--;
    W--;
    rowNum[H]++;
    columnNum[W]++;
    s.insert({H, W});
  }
  int rowNumMax = -1;
  int columnNumMax = -1;
  for (int i = 0; i < h; i++) {
    chmax(rowNumMax, rowNum[i]);
  }
  for (int i = 0; i < w; i++) {
    chmax(columnNumMax, columnNum[i]);
  }
  vector<int> rowNumMaxIndex;
  vector<int> columnNumMaxIndex;
  for (int i = 0; i < h; i++) {
    if (rowNumMax == rowNum[i]) {
      rowNumMaxIndex.push_back(i);
    }
  }
  for (int i = 0; i < w; i++) {
    if (columnNumMax == columnNum[i]) {
      columnNumMaxIndex.push_back(i);
    }
  }
  for (auto y : rowNumMaxIndex) {
    for (auto x : columnNumMaxIndex) {
      if (s.find({y, x}) == s.end()) {
        cout << rowNumMax + columnNumMax << endl;
        return 0;
      }
    }
  }
  cout << rowNumMax + columnNumMax - 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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;
  vector<int> H(m), W(m);
  vector<int> rowNum(h, 0);
  vector<int> columnNum(w, 0);
  for (int i = 0; i < m; i++) {
    cin >> H[i] >> W[i];
    H[i]--;
    W[i]--;
    rowNum[H[i]]++;
    columnNum[W[i]]++;
  }
  int rowNumMax = -1;
  int columnNumMax = -1;
  for (int i = 0; i < h; i++) {
    chmax(rowNumMax, rowNum[i]);
  }
  for (int i = 0; i < w; i++) {
    chmax(columnNumMax, columnNum[i]);
  }
  vector<bool> rowNumMaxIndex(h, false);
  vector<bool> columnNumMaxIndex(w, false);
  for (int i = 0; i < h; i++) {
    if (rowNumMax == rowNum[i]) {
      rowNumMaxIndex[i] = true;
    }
  }
  for (int i = 0; i < w; i++) {
    if (columnNumMax == columnNum[i]) {
      columnNumMaxIndex[i] = true;
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (!(rowNumMaxIndex[i] && columnNumMaxIndex[j]))
        continue;
      bool exist = false;
      for (ll k = 0; k < m; k++) {
        if (i == H[k] && j == W[k]) {
          exist = true;
        }
      }
      if (!exist) {
        cout << rowNumMax + columnNumMax << endl;
        return 0;
      }
    }
  }
  cout << rowNumMax + columnNumMax - 1 << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const long long INF = 1e15;

int main() {
  ll h, w;
  cin >> h >> w;
  ll m;
  cin >> m;
  map<ll, ll> cw, ch;
  map<pair<int, int>, bool> sur;
  for (ll i = 0; i < m; i++) {
    ll y, x;
    cin >> y >> x;
    y--;
    x--;
    cw[x]++;
    ch[y]++;
    sur[{y, x}] = true;
  }

  ll mh = -1, mw = -1;
  ll zh, zw;

  vector<ll> lh, lw;

  for (ll i = 0; i < h; i++) {
    if (mh < ch[i]) {
      mh = ch[i];
      zh = i;
      vector<ll> emp;
      swap(lh, emp);
      lh.push_back(zh);
    } else if (mh == ch[i])
      lh.push_back(i);
  }
  for (ll i = 0; i < w; i++) {
    if (mw < cw[i]) {
      mw = cw[i];
      zw = i;
      vector<ll> emp;
      swap(lw, emp);
      lw.push_back(zw);
    } else if (mw == cw[i])
      lw.push_back(i);
  }

  ll ans = -1;

  for (ll i = 0; i < (ll)lh.size(); i++) {
    for (ll j = 0; j < (ll)lw.size(); j++) {
      ll d = 0;
      if (!sur[{lh[i], lw[j]}]) {
        cout << mh + mw << endl;
        return 0;
      } else
        ans = max(ans, mh + mw - 1);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const long long INF = 1e15;

int main() {
  int h, w;
  cin >> h >> w;
  int m;
  cin >> m;
  map<int, int> cw, ch;
  vector<vector<char>> sur(h, vector<char>(w, '.'));
  for (int i = 0; i < m; i++) {
    int y, x;
    cin >> y >> x;
    y--;
    x--;
    cw[x]++;
    ch[y]++;
    sur[y][x] = '#';
  }

  int mh = -1, mw = -1;
  int zh, zw;

  vector<int> lh, lw;

  for (int i = 0; i < h; i++) {
    if (mh < ch[i]) {
      mh = ch[i];
      zh = i;
      vector<int> emp;
      swap(lh, emp);
      lh.push_back(zh);
    } else if (mh == ch[i])
      lh.push_back(i);
  }
  for (int i = 0; i < w; i++) {
    if (mw < cw[i]) {
      mw = cw[i];
      zw = i;
      vector<int> emp;
      swap(lw, emp);
      lw.push_back(zw);
    } else if (mw == cw[i])
      lw.push_back(i);
  }

  int ans = -1;

  for (int i = 0; i < (int)lh.size(); i++) {
    for (int j = 0; j < (int)lw.size(); j++) {
      int d = 0;
      if (sur[lh[i]][lw[j]] == '#')
        d = -1;
      ans = max(ans, d + mh + mw);
    }
  }

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

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<ll, int>;

#define INF 1001001001

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  map<P, bool> bbk;
  vector<int> cnth(h), cntw(w);
  int maxh = 0, maxw = 0;
  rep(i, m) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    bbk[{s, t}] = true;
    cnth[s]++;
    cntw[t]++;
    maxh = max(maxh, cnth[s]);
    maxw = max(maxw, cntw[t]);
  }
  vector<int> idxh, idxw;
  rep(i, h) if (cnth[i] == maxh) idxh.push_back(i);
  rep(i, w) if (cntw[i] == maxw) idxw.push_back(i);
  int be = true;
  for (auto ih : idxh) {
    for (auto iw : idxw) {
      if (!bbk[{ih, iw}]) {
        be = false;
        break;
      }
    }
  }

  int ans = maxh + maxw;
  if (be)
    ans--;
  cout << ans << endl;

  return 0;
}

Error: 0

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;
using P = pair<ll, int>;

#define INF 1001001001

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<vector<bool>> b(h, vector<bool>(w));
  vector<int> cnth(h), cntw(w);
  int maxh = 0, maxw = 0;
  rep(i, m) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    b[s][t] = true;
    cnth[s]++;
    cntw[t]++;
    maxh = max(maxh, cnth[s]);
    maxw = max(maxw, cntw[s]);
  }
  vector<int> idxh, idxw;
  rep(i, h) if (cnth[i] == maxh) idxh.push_back(i);
  rep(i, w) if (cntw[i] == maxw) idxw.push_back(i);
  int be = true;
  for (auto ih : idxh) {
    for (auto iw : idxw) {
      if (!b[ih][iw]) {
        be = false;
        break;
      }
    }
  }

  int ans = maxh + maxw;
  if (be)
    ans--;
  cout << ans << endl;

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

#include <bits/stdc++.h>

using namespace std;

const int INF = 2e9 + 3;
const int N = 1e3 + 3;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

const double PI = acos(-1);

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  pair<int, int> p[m];
  map<int, int> row, col;
  int max_rep_row = 0, max_rep_col = 0;
  for (int i = 0; i < m; i++) {
    int r, c;
    cin >> r >> c;
    p[i] = make_pair(r, c);
    row[r]++;
    col[c]++;
    max_rep_row = max(max_rep_row, row[r]);
    max_rep_col = max(max_rep_col, col[c]);
  }
  int cnt_max_row = 0, cnt_max_col = 0;
  for (auto r : row)
    cnt_max_row += r.second == max_rep_row;
  for (auto c : col)
    cnt_max_col += c.second == max_rep_col;

  int cnt_max_matches = 0;
  for (auto target : p) {
    if (row[target.first] == max_rep_row && col[target.second] == max_rep_col)
      cnt_max_matches++;
  }
  if (cnt_max_matches == 1ll * cnt_max_row * cnt_max_col)
    cout << max_rep_row + max_rep_col - 1;
  else
    cout << max_rep_row + max_rep_col;
}

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int INF = 2e9 + 3;
const int N = 1e3 + 3;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

const double PI = acos(-1);

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  pair<int, int> p[m];
  map<int, int> row, col;
  map<pair<int, int>, bool> was;
  for (auto x : p) {
    cin >> x.first >> x.second;
    row[x.first]++;
    col[x.second]++;
    was[{x.first, x.second}] = 1;
  }
  int mx = 0;
  for (auto x : row)
    mx = max(mx, x.second);
  vector<int> rows;
  for (auto x : row)
    if (x.second == mx)
      rows.push_back(x.first);
  mx = 0;
  for (auto x : col)
    mx = max(mx, x.second);
  vector<int> cols;
  for (auto x : col)
    if (x.second == mx)
      cols.push_back(x.first);
  int ans = 0;
  int cnt = 0;
  for (int r : rows) {
    for (int c : cols) {
      int val = row[r] + col[c] - was[{r, c}];
      ans = max(ans, val);
      cnt++;
      if (cnt > 100000000) {
        cout << ans;
        return;
      }
    }
  }
  cout << ans;
}

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

// tt89 ;)
#include <bits/stdc++.h>
using namespace std;
// typedef long long ll;
#define int long long
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
void testcase(int t) { cout << "Case #" << t << ": "; }
void online() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
const int mod = 1e9 + 7;

int n, m, ans;
set<pair<int, int>> s;
vector<int> row, col, x, y;

int getAns() {
  int ans = 0;
  for (int i : x) {
    for (int j : y) {
      int c = row[i] + col[j];
      if (s.find({i, j}) != s.end())
        c--;
      else
        return c;
      ans = max(ans, c);
    }
  }
  return ans;
}

signed main() {
  fastIO;
  cin >> n >> m;
  ans = 0;
  row.resize(n, 0);
  col.resize(m, 0);
  int t;
  cin >> t;

  while (t--) {
    int a, b;
    cin >> a >> b;
    row[a - 1]++;
    col[b - 1]++;
    s.insert({a - 1, b - 1});
  }
  //    vector<int> x,y;
  int a = 0, b = 0;

  for (int i = 0; i < n; i++) {
    if (row[i] == a)
      x.push_back(i);
    else if (row[i] > a) {
      x = {i};
      a = row[i];
    }
  }
  for (int i = 0; i < m; i++) {
    if (col[i] == b)
      y.push_back(i);
    else if (col[i] > b) {
      y = {i};
      b = col[i];
    }
  }
  int ans = getAns();
  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

// tt89 ;)
#include <bits/stdc++.h>
using namespace std;
// typedef long long ll;
#define int long long
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
void testcase(int t) { cout << "Case #" << t << ": "; }
void online() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
const int mod = 1e9 + 7;

int n, m, ans;
set<pair<int, int>> s;
vector<int> row, col;
signed main() {
  fastIO;
  cin >> n >> m;
  ans = 0;
  row.resize(n, 0);
  col.resize(m, 0);
  int t;
  cin >> t;

  while (t--) {
    int a, b;
    cin >> a >> b;
    row[a - 1]++;
    col[b - 1]++;
    s.insert({a - 1, b - 1});
  }
  vector<int> x, y;
  int a = 0, b = 0;

  for (int i = 0; i < n; i++) {
    if (row[i] == a)
      x.push_back(i);
    else if (row[i] > a) {
      x = {i};
      a = row[i];
    }
  }
  for (int i = 0; i < m; i++) {
    if (col[i] == b)
      y.push_back(i);
    else if (col[i] > b) {
      y = {i};
      b = col[i];
    }
  }
  int ans = 0;

  for (int i : x) {
    for (int j : y) {
      int c = row[i] + col[j];
      if (s.find({i, j}) != s.end())
        c--;
      ans = max(ans, c);
    }
  }
  cout << ans;

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

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  V<int> row(300010, 0), col(300010, 0);
  map<P<ll, ll>, int> mp;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    row[x]++;
    col[y]++;
    mp[{x, y}]++;
  }

  int mr = 0, mc = 0;
  for (int i = 1; i < 300010; i++) {
    mr = max(mr, row[i]);
    mc = max(mc, col[i]);
  }

  V<int> r, c;
  for (int i = 1; i < 300010; i++) {
    if (mr == row[i])
      r.push_back(i);
    if (mc == col[i])
      c.push_back(i);
  }

  int ans = mr + mc - 1;
  for (auto rr : r) {
    for (auto cc : c) {
      if (!mp[{rr, cc}]) {
        cout << ans + 1 << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  V<int> row(m + 1, 0), col(m + 1, 0);
  map<P<ll, ll>, int> mp;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    row[x]++;
    col[y]++;
    mp[{x, y}]++;
  }

  int mr = 0, mc = 0;
  for (int i = 1; i < m; i++) {
    mr = max(mr, row[i]);
    mc = max(mc, col[i]);
  }

  V<int> r, c;
  for (int i = 1; i < m; i++) {
    if (mr == row[i])
      r.push_back(i);
    if (mc == col[i])
      c.push_back(i);
  }

  int ans = mr + mc - 1;
  for (auto rr : r) {
    for (auto cc : c) {
      if (!mp[{rr, cc}]) {
        cout << ans + 1 << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;

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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define int long long

using namespace std;

const int INF = 1e17 + 7;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  set<pair<int, int>> s;
  map<int, int> c, r;
  map<int, vector<int>> ch;
  for (int i = 0; i < k; ++i) {
    pair<int, int> t;
    cin >> t.first >> t.second;
    ++c[t.first];
    ++r[t.second];
    ch[t.second].push_back(t.first);
    s.insert(t);
  }
  for (auto &t : ch)
    sort(t.second.begin(), t.second.end());
  vector<int> a, b;
  int mx = 0, ans = 0;
  for (auto t : r)
    mx = max(mx, t.second);
  for (auto t : r)
    if (t.second == mx)
      a.push_back(t.first);
  ans += mx;
  mx = 0;
  for (auto t : c)
    mx = max(mx, t.second);
  for (auto t : c)
    if (t.second == mx)
      b.push_back(t.first);
  ans += mx;
  for (auto x : a) {
    int cnt = 0, it = 0;
    for (int i = 0; i < ch[x].size(); ++i) {
      while (it < b.size() && b[it] < ch[x][i])
        ++it;
      if (it == b.size())
        break;
      if (ch[x][i] == b[it])
        ++cnt;
    }
    if (cnt < b.size()) {
      cout << ans;
      return;
    }
  }
  cout << ans - 1;
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define int long long

using namespace std;

const int INF = 1e17 + 7;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  set<pair<int, int>> s;
  map<int, int> c, r;
  map<int, vector<int>> ch;
  for (int i = 0; i < k; ++i) {
    pair<int, int> t;
    cin >> t.first >> t.second;
    ++c[t.first];
    ++r[t.second];
    ch[t.second].push_back(t.first);
    s.insert(t);
  }
  set<int> a, b;
  int mx = 0, ans = 0;
  for (auto t : r)
    mx = max(mx, t.second);
  for (auto t : r)
    if (t.second == mx)
      a.insert(t.first);
  ans += mx;
  mx = 0;
  for (auto t : c)
    mx = max(mx, t.second);
  for (auto t : c)
    if (t.second == mx)
      b.insert(t.first);
  ans += mx;
  for (auto x : a) {
    int cnt = 0;
    for (auto t : ch[x]) {
      if (find(b.begin(), b.end(), t) != b.end()) {
        ++cnt;
      }
    }
    if (cnt < b.size()) {
      cout << ans;
      return;
    }
  }
  cout << ans - 1;
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();

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

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

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> h(M), w(M);
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
  }

  map<int, int> Hmap;
  map<int, int> Wmap;
  set<pair<int, int>> BOMB;
  for (int i = 0; i < M; i++) {
    if (Hmap.find(h[i]) == Hmap.end())
      Hmap[h[i]] = 1;
    else
      Hmap[h[i]]++;

    if (Wmap.find(w[i]) == Wmap.end())
      Wmap[w[i]] = 1;
    else
      Wmap[w[i]]++;

    BOMB.insert(make_pair(h[i], w[i]));
  }
  vector<pair<int, int>> Hcount, Wcount;
  for (auto it = Hmap.begin(); it != Hmap.end(); ++it) {
    Hcount.emplace_back(it->second, it->first);
  }
  for (auto it = Wmap.begin(); it != Wmap.end(); ++it) {
    Wcount.emplace_back(it->second, it->first);
  }
  sort(Hcount.begin(), Hcount.end(), greater<pair<int, int>>());
  sort(Wcount.begin(), Wcount.end(), greater<pair<int, int>>());
  int wmax = Wcount[0].first;
  int ans = 0;
  for (auto u : Hcount) {
    if (ans >= u.first + wmax)
      break;
    for (auto v : Wcount) {
      if (ans >= u.first + v.first)
        break;

      int newans = u.first + v.first;
      if (BOMB.find(make_pair(u.second, v.second)) != BOMB.end())
        newans--;
      ans = max(ans, newans);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> h(M), w(M);
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
  }

  map<int, int> Hmap;
  map<int, int> Wmap;
  set<pair<int, int>> BOMB;
  for (int i = 0; i < M; i++) {
    if (Hmap.find(h[i]) == Hmap.end())
      Hmap[h[i]] = 1;
    else
      Hmap[h[i]]++;

    if (Wmap.find(w[i]) == Wmap.end())
      Wmap[w[i]] = 1;
    else
      Wmap[w[i]]++;

    BOMB.insert(make_pair(h[i], w[i]));
  }
  vector<pair<int, int>> Hcount, Wcount;
  for (auto it = Hmap.begin(); it != Hmap.end(); ++it) {
    Hcount.emplace_back(it->second, it->first);
  }
  for (auto it = Wmap.begin(); it != Wmap.end(); ++it) {
    Wcount.emplace_back(it->second, it->first);
  }
  sort(Hcount.begin(), Hcount.end(), greater<pair<int, int>>());
  sort(Wcount.begin(), Wcount.end(), greater<pair<int, int>>());
  int wmax = Wcount[0].first;
  int ans = 0;
  for (auto u : Hcount) {
    if (ans > u.first + wmax)
      break;
    for (auto v : Wcount) {
      if (ans > u.first + v.first)
        break;

      int newans = u.first + v.first;
      if (BOMB.find(make_pair(u.second, v.second)) != BOMB.end())
        newans--;
      ans = max(ans, newans);
    }
  }
  cout << ans << endl;
  return 0;
}