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

#include <algorithm>
#include <bitset>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <deque>
#define pi 3.141592653589793238
#include <chrono>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define MOD 1000000007
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second

#define mt make_tuple
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  fast;
  ll T = 1, i, j;

  // cin >> T;
  while (T--) {
    ll h, w, m;
    cin >> h >> w >> m;
    if (h == 1 || w == 1) {
      cout << m;
      return 0;
    }
    vector<ll> col(w + 1), row(h + 1);
    set<pair<ll, ll>> s;
    for (i = 0; i < m; i++) {
      ll x, y;
      cin >> x >> y;
      row[x]++;
      col[y]++;
      s.insert({x, y});
    }

    vector<pair<ll, ll>> ca, ra;
    for (i = 0; i < h; i++) {
      ra.pb({row[i + 1], i + 1});
    }
    for (i = 0; i < w; i++) {
      ca.pb({col[i + 1], i + 1});
    }
    sort(ra.rbegin(), ra.rend());
    sort(ca.rbegin(), ca.rend());
    vector<ll> r, c;
    for (auto u : ra) {
      r.pb(u.ss);
    }
    for (auto u : ca) {
      c.pb(u.ss);
    }
    ll ans = 0;
    ll csz = c.size();
    ll rsz = r.size();

    for (i = 0; i < rsz; i++) {
      for (j = 0; j < 1; j++) {
        ll val = row[r[i]] + col[c[j]];
        if (s.find({r[i], c[j]}) != s.end()) {
          val--;
        }
        ans = max(ans, val);
      }
    }
    for (i = 0; i < csz; i++) {
      for (j = 0; j < 1; j++) {
        ll val = row[r[j]] + col[c[i]];
        if (s.find({r[j], c[i]}) != s.end()) {
          val--;
        }
        ans = max(ans, val);
      }
    }
    cout << ans;
  }
  return 0;
}
/*
100 100 10
1 3
2 3
12 42
11 11
43 21
23 12
23 54
12 12
1 5
*/

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <deque>
#define pi 3.141592653589793238
#include <chrono>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define MOD 1000000007
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second

#define mt make_tuple
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  fast;
  ll T = 1, i, j;

  // cin >> T;
  while (T--) {
    ll h, w, m;
    cin >> h >> w >> m;
    if (h == 1 && w == 1) {
      cout << 0;
      return 0;
    }
    map<ll, ll> col, row;
    set<pair<ll, ll>> s;
    for (i = 0; i < m; i++) {
      ll x, y;
      cin >> x >> y;
      row[x]++;
      col[y]++;
      s.insert({x, y});
    }
    vector<ll> c, r;
    for (i = 0; i < h; i++) {
      r.pb(i + 1);
    }
    for (i = 0; i < w; i++) {
      c.pb(i + 1);
    }
    sort(r.rbegin(), r.rend(), [&](ll a, ll b) { return row[a] <= row[b]; });
    sort(c.rbegin(), c.rend(), [&](ll a, ll b) { return col[a] <= col[b]; });

    ll ans1, ans2, ans3, ans4;
    ll r1, c1;
    r1 = row[r[0]], c1 = col[c[0]];
    ans1 = r1 + c1;
    if (s.find({r[0], c[0]}) != s.end()) {
      ans1--;
    }
    r1 = row[r[0]], c1 = col[c[1]];
    ans2 = r1 + c1;
    if (s.find({r[0], c[1]}) != s.end()) {
      ans2--;
    }
    r1 = row[r[1]], c1 = col[c[1]];
    ans3 = r1 + c1;
    if (s.find({r[1], c[1]}) != s.end()) {
      ans3--;
    }

    r1 = row[r[1]], c1 = col[c[0]];
    ans4 = r1 + c1;
    if (s.find({r[1], c[0]}) != s.end()) {
      ans4--;
    }
    cout << max({ans1, ans2, ans3, ans4});
  }
  return 0;
}