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

#include "bits/stdc++.h"
using ll = long long;
using ld = long double;
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(ld x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif
#ifndef ONLINE_JUDGE
#define debugt(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debugt(...) 42
#endif
int row[3 * 100000 + 3], col[3 * 100000 + 3];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int h, w, m;
  cin >> h >> w >> m;
  int x, y;
  map<pair<int, int>, int> g;
  map<int, vector<int>> f;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    x--;
    y--;
    row[x]++;
    col[y]++;
    g[{x, y}] = 1;
    f[y].emplace_back(x);
  }
  for (int i = 0; i <= w - 1; i++) {
    sort(f[i].begin(), f[i].end());
  }
  int t = -1;
  for (int i = 0; i <= w - 1; i++) {
    t = max(col[i], t);
  }
  int ans = 0;
  vector<int> v;
  for (int i = 0; i <= w - 1; i++) {
    if (col[i] == t)
      v.emplace_back(i);
  }
  for (int i = 0; i <= h - 1; i++) {
    int res = row[i];
    int j = v[0];
    if (g[{i, j}] == 0) {
      res += t;
    } else {
      if ((int)v.size() == 1) {
        res += (t - 1);
      } else {
        int flag = 0;
        for (auto idx : v) {
          auto it = lower_bound(f[idx].begin(), f[idx].end(), i);
          if (it == f[idx].end() || *it != i) {
            flag = 1;
            break;
          }
        }
        if (flag) {
          res += t;
        } else
          res += (t - 1);
      }
    }
    ans = max(res, ans);
  }
  cout << ans << "\\n";
  return 0;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
using ll = long long;
using ld = long double;
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int h, w, m;
  cin >> h >> w >> m;
  int x, y;
  int a[h + 1][w + 1];
  memset(a, 0, sizeof(a));
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    x--;
    y--;
    a[x][y] = 1;
  }
  vector<int> row(h + 1), col(w + 1);
  for (int i = 0; i <= h - 1; i++) {
    int s = 0;
    for (int j = 0; j <= w - 1; j++) {
      if (a[i][j])
        s++;
    }
    row[i] = s;
  }
  for (int j = 0; j <= w - 1; j++) {
    int s = 0;
    for (int i = 0; i <= h - 1; i++) {
      if (a[i][j])
        s++;
    }
    col[j] = s;
  }
  int ans = 0;
  for (int i = 0; i <= h - 1; i++) {
    for (int j = 0; j <= w - 1; j++) {
      int res = row[i] + col[j];
      if (a[i][j])
        res--;
      ans = max(ans, res);
    }
  }
  cout << ans << "\\n";
  return 0;
}