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


// Problem : E - Bomber
// Contest : AtCoder - AtCoder Beginner Contest 176
// URL : https://atcoder.jp/contests/abc176/tasks/abc176_e
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define db double
#define mod 1000000007
#define pb push_back
#define pp pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define maxn 300005
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
int inv(int a, int b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
set<pp> s;
vector<int> mp[maxn];
int v[maxn];
void solve() {
  int n, m, w;
  cin >> n >> m >> w;
  rep(i, 0, w) {
    int x, y;
    cin >> x >> y;
    mp[x].pb(y);
    v[y]++;
  }
  for (int i = 1; i <= m; i++)
    s.insert({-v[i], i});
  int ans = -s.begin()->ff;
  for (int i = 1; i <= n; i++) {
    for (auto x : mp[i]) {
      s.erase({-v[x], x});
      v[x]--;
      s.insert({-v[x], x});
    }
    ans = max(ans, (int)mp[i].size() - s.begin()->ff);
    for (auto x : mp[i]) {
      s.erase({-v[x], x});
      v[x]++;
      s.insert({-v[x], x});
    }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


Error: WA

Fixed submission:


// Problem : E - Bomber
// Contest : AtCoder - AtCoder Beginner Contest 176
// URL : https://atcoder.jp/contests/abc176/tasks/abc176_e
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define db double
#define mod 1000000007
#define pb push_back
#define pp pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define maxn 100005
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
int inv(int a, int b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
set<pp> s;
vector<int> mp[maxn];
int v[maxn];
void solve() {
  int n, m, w;
  cin >> n >> m >> w;
  rep(i, 0, w) {
    int x, y;
    cin >> x >> y;
    mp[x].pb(y);
    v[y]++;
  }
  for (int i = 1; i <= m; i++)
    s.insert({-v[i], i});
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (auto x : mp[i]) {
      s.erase({-v[x], x});
      v[x]--;
      s.insert({-v[x], x});
    }
    ans = max(ans, (int)mp[i].size() - s.begin()->ff);
    for (auto x : mp[i]) {
      s.erase({v[x], x});
      v[x]++;
      s.insert({v[x], x});
    }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}