Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define sqr(x) ((x) * (x))
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define ld long double
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define debuge cerr << "isok" << endl
#define debug(x) cerr << #x << "=" << x << endl
#define SS second
#define FF first
#define ls (k << 1)
#define rs (k << 1 | 1)
#define inf 0x3f3f3f3f
#define clr(a, x) memset(a, x, sizeof(a))
#define cpy(a, x) memcpy(a, x, sizeof(a))
#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout);
using namespace std;

const int N = 205, M = 100005, mod = 1e9 + 7;
template <class T> inline void gmin(T &x, const T &y) {
  if (x > y)
    x = y;
}
template <class T> inline void gmax(T &x, const T &y) {
  if (x < y)
    x = y;
}
inline void ch(int &x, int y) { x = (x + y) % mod; }
inline void read(int &x) {
  x = 0;
  char ch = getchar(), rev = 0;
  while (ch > '9' || ch < '0')
    rev = (ch == '-'), ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x = rev ? -x : x;
}
inline int exp(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1)
      ans = (ll)ans * x % mod;
    x = (ll)x * x % mod;
    y >>= 1;
  }
  return ans;
}

int n, m, ans, a[N], cnt[N], tag[N];

int main() {
#ifdef rqgao2014
  freopen("input.txt", "r", stdin);
#endif
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), cnt[a[i]]++;
  for (int i = 1; i <= n; i++)
    for (int j = i; j > i - cnt[i] && j; j--)
      tag[j]++;
  for (int i = 1; i <= n; i++)
    if (tag[i])
      ans++;
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    cnt[a[x]]--;
    if (a[x] - cnt[a[x]] > 0) {
      tag[a[x] - cnt[a[x]]]--;
      if (!tag[a[x] - cnt[a[x]]])
        ans--;
    }
    if (y - cnt[y] > 0) {
      if (!tag[y - cnt[y]])
        ans++;
      tag[y - cnt[y]]++;
    }
    cnt[y]++;
    a[x] = y;
    printf("%d\n", n - ans);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define sqr(x) ((x) * (x))
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define ld long double
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define debuge cerr << "isok" << endl
#define debug(x) cerr << #x << "=" << x << endl
#define SS second
#define FF first
#define ls (k << 1)
#define rs (k << 1 | 1)
#define inf 0x3f3f3f3f
#define clr(a, x) memset(a, x, sizeof(a))
#define cpy(a, x) memcpy(a, x, sizeof(a))
#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout);
using namespace std;

const int N = 1000005, M = 100005, mod = 1e9 + 7;
template <class T> inline void gmin(T &x, const T &y) {
  if (x > y)
    x = y;
}
template <class T> inline void gmax(T &x, const T &y) {
  if (x < y)
    x = y;
}
inline void ch(int &x, int y) { x = (x + y) % mod; }
inline void read(int &x) {
  x = 0;
  char ch = getchar(), rev = 0;
  while (ch > '9' || ch < '0')
    rev = (ch == '-'), ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x = rev ? -x : x;
}
inline int exp(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1)
      ans = (ll)ans * x % mod;
    x = (ll)x * x % mod;
    y >>= 1;
  }
  return ans;
}

int n, m, ans, a[N], cnt[N], tag[N];

int main() {
#ifdef rqgao2014
  freopen("input.txt", "r", stdin);
#endif
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), cnt[a[i]]++;
  for (int i = 1; i <= n; i++)
    for (int j = i; j > i - cnt[i] && j; j--)
      tag[j]++;
  for (int i = 1; i <= n; i++)
    if (tag[i])
      ans++;
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    cnt[a[x]]--;
    if (a[x] - cnt[a[x]] > 0) {
      tag[a[x] - cnt[a[x]]]--;
      if (!tag[a[x] - cnt[a[x]]])
        ans--;
    }
    if (y - cnt[y] > 0) {
      if (!tag[y - cnt[y]])
        ans++;
      tag[y - cnt[y]]++;
    }
    cnt[y]++;
    a[x] = y;
    printf("%d\n", n - ans);
  }
  return 0;
}