Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll int
#define int long long
#define ld long double
#define ff first
#define ss string
#define se second
#define endl "\\n"
#define alt(v) v.begin(), v.end()
#define ralt(v) v.rbegin(), v.rend()
#define pb emplace_back
#define mp make_pair
#define fab(i, a, b) for (int i = (a); i < (b); i++)
#define fba(i, a, b) for (int i = (b); i >= (a); i--)
int mod = 1000000007;
// int mod=998244353;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
int dx1[] = {-2, -2, -2, -2, -2, 2, 2, 2, 2, 2,
             -1, -1, -1, -1, 1,  1, 1, 1, 0, 0};
int dy1[] = {0,  -1, -2, 1, 2,  0,  -1, -2, 1,  2,
             -1, -2, 1,  2, -1, -2, 1,  2,  -2, 2};

bool test = 0;
const int N = 1e3 + 5;
const int inf = 1e9;
int h, w, m;
void solve() {

  cin >> h >> w >> m;
  map<int, int> row, col;
  vector<pair<int, int>> v;
  for (int i = 1, x, y; i <= m; ++i) {
    cin >> x >> y;
    x--;
    y--;
    row[x]++;
    col[y]++;
    v.pb(mp(x, y));
  }
  int mxrow = 0;
  set<int> r, c;
  for (auto it : row)
    mxrow = max(mxrow, it.se);
  for (auto it : row) {
    if (it.se == mxrow)
      r.insert(it.ff);
  }
  int mxcol = 0;
  for (auto it : col)
    mxcol = max(mxcol, it.se);

  int ans = mxrow + mxcol;

  for (auto it : col) {
    if (it.se == mxcol)
      c.insert(it.ff);
  }

  int cnt = 0;
  for (auto it : v) {
    int x = it.ff;
    int y = it.se;
    if (r.find(x) != r.end() and c.find(y) != c.end())
      cnt++;
  }
  int tot = (int)r.size() * (int)c.size();

  bool ok = false;
  if (cnt < tot)
    ok = true;
  if (!ok)
    ans--;
  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  if (test)
    cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

// heeeyyyy Mooortyyyyyy!!
#include <bits/stdc++.h>
#include <chrono>
#include <random>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define ed end()
#define bg begin()
#define ld long double
#define ll long long
#define ub(v, a) upper_bound(all(v), a)
#define lb(v, a) lower_bound(all(v), a)
#define sz(a) (int)(a.size())
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define tr(v, it) for (auto it = v.begin(); it != v.end(); it++)
#define present(v, x) (v.find(x) != v.end())        // set/map etc.
#define vpresent(v, x) (find(all(v), x) != v.end()) // vector
#define vi vector<int>
#define vs vector<string>
#define vc vector<char>
typedef vector<long long> vll;
typedef vector<pair<ll, ll>> vp;
typedef vector<vll> vvll;
typedef pair<int, int> pi;
const long long max_sz = 1e5 + 1;
const long long mod = 1e9 + 7;
const long long MAXN = 4e18 + 1;
const int N = 500023;
#define PI 3.1415926535897932384626
char flip(char k) {
  if (k == '0')
    return '1';
  return '0';
}
ll sum_digit(ll n) {
  ll a = 0, i;
  for (i = n; i > 0; i = i / 10) {
    a += (i % 10);
  }
  return a;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
bool isPowerOfTwo(int n) {
  if (n == 0)
    return false;
  return (ceil(log2(n)) == floor(log2(n)));
}
// DEBUGGING
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
const int LG = 20;
//__builtin_popcount(n) -> returns number of set bits in n
ll expo(ll a, ll b, ll m); // a^(b) %m
//=======================

// vector<bool> vis;vis.assign(n,false); g.assign(n,{});
// vector<vll> g;
// void ipgraph(ll m){ll  u, v;while(m--){cin>>u>>v; u--,
// v--;g[u].pb(v);g[v].pb(u);}} ll lvl[N], p[N][LG]; ll lca(ll u,ll
// v){if(lvl[u]<lvl[v])swap(u,v);ll i,lg;
// for(lg=0;(1<<lg)<=lvl[u];lg++);lg--;for(i=lg;i>=0;i--){if(lvl[u]-(1<<i)>=lvl[v]){u=p[u][i];}}if(u==v)return
// v;else{for(i=lg;i>=0;i--){if(p[u][i]!=-1 &&
// p[u][i]!=p[v][i]){u=p[u][i];v=p[v][i];}}}return p[u][0];}

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

  int **bomb = new int *[h];
  for (ll i = 0; i < h; i++) {
    bomb[i] = new int[w];
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      bomb[i][j] = 0;
    }
  }

  map<int, int> no_rows, no_columns;

  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    bomb[u][v] = 1;
    no_rows[u]++;
    no_columns[v]++;
  }

  int jhc = 0;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (bomb[i][j] == 1) {
        if (jhc < no_rows[i] + no_columns[j] - 1) {
          jhc = no_rows[i] + no_columns[j] - 1;
        }
      } else {
        if (jhc < no_rows[i] + no_columns[j]) {
          jhc = no_rows[i] + no_columns[j];
        }
      }
    }
  }

  cout << jhc << endl;
}

int main() {

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

ll expo(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  ll p = expo(a, b / 2, m) % m;
  p = (p * p) % m;

  return (b % 2 == 0) ? p : (a * p) % m;
}