Problem p03667 (C++)

Buggy submission (status: Runtime Error):
// satyaki3794
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define MOD (1000000007LL)
#define LEFT(n) (2 * (n))
#define RIGHT(n) (2 * (n) + 1)

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

ll pwr(ll base, ll p, ll mod = MOD) {
  ll ans = 1;
  while (p) {
    if (p & 1)
      ans = (ans * base) % mod;
    base = (base * base) % mod;
    p /= 2;
  }
  return ans;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

const int N = 100002;
int n, arr[N], cnt[N], balls[N];

int main() {

  // ios_base::sync_with_stdio(0);
  // cin.tie(0);

  int q;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d", &a);
    balls[i] = a;
    cnt[a]++;
  }

  for (int i = 1; i <= n; i++) {
    arr[max(0, i - cnt[i] + 1)]++;
    arr[i + 1]--;
  }

  int uncovered = 0;
  for (int i = 1; i <= n; i++) {
    arr[i] += arr[i - 1];
    uncovered += (arr[i] == 0);
  }

  // cout<<uncovered<<endl;
  // for(int i=1;i<=n;i++)	cout<<arr[i]<<" ";cout<<endl;

  // q=1;
  while (q--) {

    int a, b;
    scanf("%d%d", &a, &b);

    if (balls[a] == b) {
      printf("%d\n", uncovered);
      continue;
    }

    int z = a;
    a = balls[a];

    if (a - cnt[a] + 1 > 0) {
      arr[a - cnt[a] + 1]--;
      if (arr[a - cnt[a] + 1] == 0)
        uncovered++;
    }
    // cout<<ans<<endl;
    if (b - cnt[b] > 0) {
      arr[b - cnt[b]]++;
      if (arr[b - cnt[b]] == 1)
        uncovered--;
    }

    balls[z] = b;
    cnt[a]--;
    cnt[b]++;

    // for(int i=1;i<=n;i++)	cout<<balls[i]<<" ";cout<<endl;
    // for(int i=1;i<=n;i++)	cout<<cnt[i]<<" ";cout<<endl;

    printf("%d\n", uncovered);
  }

  return 0;
}


Fixed submission:
// satyaki3794
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define MOD (1000000007LL)
#define LEFT(n) (2 * (n))
#define RIGHT(n) (2 * (n) + 1)

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

ll pwr(ll base, ll p, ll mod = MOD) {
  ll ans = 1;
  while (p) {
    if (p & 1)
      ans = (ans * base) % mod;
    base = (base * base) % mod;
    p /= 2;
  }
  return ans;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

const int N = 200002;
int n, arr[N], cnt[N], balls[N];

int main() {

  // ios_base::sync_with_stdio(0);
  // cin.tie(0);

  int q;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d", &a);
    balls[i] = a;
    cnt[a]++;
  }

  for (int i = 1; i <= n; i++) {
    arr[max(0, i - cnt[i] + 1)]++;
    arr[i + 1]--;
  }

  int uncovered = 0;
  for (int i = 1; i <= n; i++) {
    arr[i] += arr[i - 1];
    uncovered += (arr[i] == 0);
  }

  // cout<<uncovered<<endl;
  // for(int i=1;i<=n;i++)	cout<<arr[i]<<" ";cout<<endl;

  // q=1;
  while (q--) {

    int a, b;
    scanf("%d%d", &a, &b);

    if (balls[a] == b) {
      printf("%d\n", uncovered);
      continue;
    }

    int z = a;
    a = balls[a];

    if (a - cnt[a] + 1 > 0) {
      arr[a - cnt[a] + 1]--;
      if (arr[a - cnt[a] + 1] == 0)
        uncovered++;
    }
    // cout<<ans<<endl;
    if (b - cnt[b] > 0) {
      arr[b - cnt[b]]++;
      if (arr[b - cnt[b]] == 1)
        uncovered--;
    }

    balls[z] = b;
    cnt[a]--;
    cnt[b]++;

    // for(int i=1;i<=n;i++)	cout<<balls[i]<<" ";cout<<endl;
    // for(int i=1;i<=n;i++)	cout<<cnt[i]<<" ";cout<<endl;

    printf("%d\n", uncovered);
  }

  return 0;
}