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

// Never stop trying
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

typedef string str;
typedef long long ll;
#define int ll
typedef double db;
typedef long double ld;

typedef pair<int, int> pi;
#define fi first
#define se second

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;

#define pb push_back
#define eb emplace_back
#define pf push_front

#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\\n"

const int MOD = 1e9 + 7; // 998244353
const ll INF = 1e18;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; // right left down up

void IO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

const int MX = 3e5 + 10;
int R, C, M;

vi adj[MX + 1];  // sharing same  X-cord
int cnt[MX + 1]; // cnt[i]= nb having same Y-cord
set<pi> s;

int32_t main() {
  boost;
  IO();

  for (int i = 0; i <= MX; i++)
    cnt[i] = 0;

  cin >> R >> C >> M;
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    cnt[y]++;
  }

  for (int i = 1; i <= MX; i++)
    s.insert({cnt[i], i});

  int ans = 0;
  for (int i = 1; i <= MX; i++) { // ignite these Xs
    int x = sz(adj[i]);
    for (auto j : adj[i]) {
      s.erase({cnt[j], j});
      cnt[j]--;
      s.insert({cnt[j], j});
    }

    auto it = s.end();
    it--;
    x += (*it).fi;

    for (auto j : adj[i]) {
      s.erase({cnt[j], j});
      cnt[j]++;
      s.insert({cnt[j], j});
    }
    ans = max(ans, x);
  }

  cout << ans << endl;

  return 0;
}


Error: WA

Fixed submission:

// Never stop trying
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

typedef string str;
typedef long long ll;
#define int ll
typedef double db;
typedef long double ld;

typedef pair<int, int> pi;
#define fi first
#define se second

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;

#define pb push_back
#define eb emplace_back
#define pf push_front

#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\\n"

const int MOD = 1e9 + 7; // 998244353
const ll INF = 1e18;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; // right left down up

void IO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

const int MX = 3e5 + 10;
int R, C, M;

vi adj[MX];  // sharing same  X-cord
int cnt[MX]; // cnt[i]= nb having same Y-cord
set<pi> s;

int32_t main() {
  boost;
  IO();

  memset(cnt, 0, MX);
  cin >> R >> C >> M;
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    cnt[y]++;
  }

  for (int i = 1; i <= MX; i++)
    s.insert({cnt[i], i});

  int ans = 0;
  for (int i = 1; i <= MX; i++) { // ignite these Xs
    int x = sz(adj[i]);
    for (auto j : adj[i]) {
      s.erase({cnt[j], j});
      cnt[j]--;
      s.insert({cnt[j], j});
    }

    auto it = s.end();
    it--;
    x += (*it).fi;

    for (auto j : adj[i]) {
      s.erase({cnt[j], j});
      cnt[j]++;
      s.insert({cnt[j], j});
    }
    ans = max(ans, x);
  }

  cout << ans << endl;

  return 0;
}