prithivMLmods/Coder-Stat huggingface dataset:
Problem p03665 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
#define int long long
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef long double ld;
using namespace std;
int n, a[51], dp[51][50001], p;
int solve(int i, int sum) {
  if (i == n) {
    if (sum % 2 == p)
      return 1;
  }
  if (dp[i][sum] != -1)
    return dp[i][sum];
  int ans = 0;
  ans += solve(i + 1, sum + a[i]);
  ans += solve(i + 1, sum);
  return dp[i][sum] = ans;
}
signed main() {
  cin >> n >> p;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < 5000; j++) {
      dp[i][j] = -1;
    }
  }
  cout << solve(0, 0) << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
#define int long long
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef long double ld;
using namespace std;
int n, a[51], dp[51][50001], p;
int solve(int i, int sum) {
  if (i == n) {
    if (sum % 2 == p)
      return 1;
    return 0;
  }
  if (dp[i][sum] != -1)
    return dp[i][sum];
  int ans = 0;
  ans += solve(i + 1, sum + a[i]);
  ans += solve(i + 1, sum);
  return dp[i][sum] = ans;
}
signed main() {
  cin >> n >> p;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < 5000; j++) {
      dp[i][j] = -1;
    }
  }
  cout << solve(0, 0) << endl;
}
Problem p03665 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using lli = long long;
#define rep(i, n) for (int i = 0; i < n; i++)

lli n, p;

lli mypow(lli a, lli x) {
  lli ret = 1;
  lli b = a;
  while (x) {
    if (x & 1) {
      ret *= a;
    }
    a *= a;
    x >>= 1;
  }
  return ret;
}

int main(void) {
  cin >> n >> p;
  lli c, d;
  c = d = 0;
  rep(i, n) {
    lli a;
    cin >> a;
    if (a & 1)
      d++;
    else
      c++;
  }
  if (p == 1 && d == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (p == 0)
    cout << mypow(2, c) * mypow(2, d - 1) << endl;
  else
    cout << mypow(2, c - 1) * mypow(2, d) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lli = long long;
#define rep(i, n) for (int i = 0; i < n; i++)

lli n, p;

lli mypow(lli a, lli x) {
  lli ret = 1;
  lli b = a;
  while (x > 0) {
    if (x & 1) {
      ret *= a;
    }
    a *= a;
    x >>= 1;
  }
  return ret;
}

int main(void) {
  cin >> n >> p;
  lli c, d;
  c = d = 0;
  rep(i, n) {
    lli a;
    cin >> a;
    if (a & 1)
      d++;
    else
      c++;
  }
  if (p == 1 && d == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (p == 0)
    cout << mypow(2, c) * mypow(2, d - 1) << endl;
  else
    cout << mypow(2, c - 1) * mypow(2, d) << endl;

  return 0;
}
Problem p03665 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <sstream>
#include <stdio.h>
// #define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctype.h>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

typedef long long int lli;
#define rep(i, s, N) for (int i = s; i < N; i++)
#define MOD 1000000007
#define more(a, b) (((a) > (b)) ? (a) : (b))
#define less(a, b) (((a) < (b)) ? (a) : (b))

using namespace std;

int main(void) {
  lli N, P;
  cin >> N >> P;
  lli odd = 0;
  rep(i, 0, N) {
    int tmp;
    cin >> tmp;
    odd += (tmp % 2);
  }
  lli C_even = pow(2, N - odd);

  lli ans = 0, tmp = 0;
  for (lli r = P; r <= odd; r += 2) {
    if (r == 1)
      tmp = odd;
    else
      tmp = ((lli)odd - r + 1) * ((lli)odd - r + 2) * tmp / r / (r - 1);
    ans += tmp;
  }
  ans *= C_even;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <sstream>
#include <stdio.h>
// #define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctype.h>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

typedef long long int lli;
#define rep(i, s, N) for (int i = s; i < N; i++)
#define MOD 1000000007
#define more(a, b) (((a) > (b)) ? (a) : (b))
#define less(a, b) (((a) < (b)) ? (a) : (b))

using namespace std;

int main(void) {
  lli N, P;
  cin >> N >> P;
  lli odd = 0;
  rep(i, 0, N) {
    int tmp;
    cin >> tmp;
    odd += (tmp % 2);
  }
  lli C_even = pow(2, N - odd);

  lli ans = 0, tmp = 0;
  for (lli r = P; r <= odd; r += 2) {
    if (r == 0)
      tmp = 1;
    else if (r == 1)
      tmp = odd;
    else
      tmp = ((lli)odd - r + 1) * ((lli)odd - r + 2) * tmp / r / (r - 1);
    ans += tmp;
  }
  ans *= C_even;
  cout << ans << endl;

  return 0;
}
Problem p03666 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define For(i, x, y) for (int i = ((int)x); i <= ((int)y); i++)
#define Dep(i, y, x) for (int i = ((int)y); i >= ((int)x); i--)
#define Rep(i, x) for (int y, i = head[x]; i; i = E[i].nxt)
using namespace std;

const int inf = 1000000009;

typedef double db;
typedef long long ll;
typedef unsigned int uint;
typedef pair<int, int> pii;

int rd() {
  char c = getchar();
  int t = 0, f = 1;
  while (!isdigit(c))
    f = (c == '-') ? -1 : 1, c = getchar();
  while (isdigit(c))
    t = t * 10 + c - 48, c = getchar();
  return t * f;
}
void wt(int x) {
  if (x < 0)
    putchar('-'), wt(-x);
  else {
    if (x > 9)
      wt(x / 10);
    putchar(x % 10 + 48);
  }
}

ll n, A, B, C, D, dis, p, t, l, r, mid, da, L, R;

int main() {
  n = rd() - 1, A = rd(), B = rd(), C = rd(), D = rd();
  dis = abs(B - A), p = D - C;

  l = 0, r = n / 2;
  while (l <= r) {
    mid = l + r >> 1;
    da = mid * 2 + (n & 1);
    L = da * C - p * ((n - da) / 2);
    R = da * D + p * ((n - da) / 2);
    if (L <= dis && dis <= R)
      return puts("YES"), 0;
    else if (R < dis)
      l = mid + 1;
    else
      r = mid - 1;
  }
  return puts("NO");

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define For(i, x, y) for (int i = ((int)x); i <= ((int)y); i++)
#define Dep(i, y, x) for (int i = ((int)y); i >= ((int)x); i--)
#define Rep(i, x) for (int y, i = head[x]; i; i = E[i].nxt)
using namespace std;

const int inf = 1000000009;

typedef double db;
typedef long long ll;
typedef unsigned int uint;
typedef pair<int, int> pii;

int rd() {
  char c = getchar();
  int t = 0, f = 1;
  while (!isdigit(c))
    f = (c == '-') ? -1 : 1, c = getchar();
  while (isdigit(c))
    t = t * 10 + c - 48, c = getchar();
  return t * f;
}
void wt(int x) {
  if (x < 0)
    putchar('-'), wt(-x);
  else {
    if (x > 9)
      wt(x / 10);
    putchar(x % 10 + 48);
  }
}

ll n, A, B, C, D, dis, p, t, l, r, mid, da, L, R;

int main() {
  n = rd() - 1, A = rd(), B = rd(), C = rd(), D = rd();
  dis = abs(B - A), p = D - C;

  l = 0, r = n / 2;
  while (l <= r) {
    mid = l + r >> 1;
    da = mid * 2 + (n & 1);
    L = da * C - p * ((n - da) / 2);
    R = da * D + p * ((n - da) / 2);
    if (L <= dis && dis <= R)
      return puts("YES"), 0;
    else if (R < dis)
      l = mid + 1;
    else
      r = mid - 1;
  }
  return puts("NO"), 0;
}
Problem p03666 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return (b > 0) ? gcd(b, a % b) : 1; }
int main() {
  ll n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;
  ll gg = a - b;
  int flag = 0;
  for (ll i = 0; i < n; i++) {
    ll x = i, y = n - i - 1;
    ll Min = x * c - y * d;
    ll Max = x * d - y * c;
    if (gg <= Max && gg >= Min) {
      ll gl = gg - Min;
      ll x1 = x % gl, y1 = y % gl;
      if (gcd(x1, y1) == 1) {
        flag = 1;
        break;
      }
    }
  }
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return (b > 0) ? gcd(b, a % b) : 1; }
int main() {
  ll n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;
  ll gg = a - b;
  int flag = 0;
  for (ll i = 0; i < n; i++) {
    ll x = i, y = n - i - 1;
    ll Min = x * c - y * d;
    ll Max = x * d - y * c;
    if (gg <= Max && gg >= Min) {
      ll gl = gg - Min;
      ll x1 = x % (gl - 1), y1 = y % (gl - 1);
      if (gcd(x1, y1) == 1) {
        flag = 1;
        break;
      }
    }
  }
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
Problem p03666 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int N;
ll A, B, C, D;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  cin >> N >> A >> B >> C >> D;
  int yes = 0;
  for (int i = 0; i < N; i++) {
    ll rem = B - (A + C * i - C * (N - 1 - i));
    ll cnt = (abs(rem) + (D - C) - 1) / (D - C);
    if (rem > 0) {
      yes |= cnt <= i;
    } else if (rem < 0) {
      yes |= cnt <= (N - 1 - i);
    } else {
      yes = 1;
    }
  }
  cout << (yes ? "YES" : "NO") << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int N;
ll A, B, C, D;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  cin >> N >> A >> B >> C >> D;
  int yes = 0;
  if (C == D) {
    yes = (B - A) % C == 0;
    yes &= (B - A) / C <= N - 1;
  } else {
    for (int i = 0; i < N; i++) {
      ll rem = B - (A + C * i - C * (N - 1 - i));
      ll cnt = (abs(rem) + (D - C) - 1) / (D - C);
      if (rem > 0) {
        yes |= cnt <= i;
      } else if (rem < 0) {
        yes |= cnt <= (N - 1 - i);
      } else {
        yes = 1;
      }
    }
  }
  cout << (yes ? "YES" : "NO") << endl;

  return 0;
}
Problem p03666 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;
const ll INF = 1ll << 30;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int main() {
  ll n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;

  b = abs(b - a);

  bool ans = false;
  FOR(i, 1, n + 1) {
    ll l = i * c, r = i * d;
    ll cnt = INF;

    if (b < l) {
      cnt = (l - b + d - c - 1) / (d - c) * 2 + i + 1;
    } else if (l <= b && b <= r) {
      cnt = i + 1;
    } else if (r < b) {
      cnt = (b - r + d - c - 1) / (d - c) * 2 + i + 1;
    }

    if (cnt <= n && (n - cnt) % 2 == 0)
      ans = true;
  }

  puts(ans ? "YES" : "NO");

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;
const ll INF = 1ll << 30;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int main() {
  ll n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;

  b = abs(b - a);

  if (c == d) {
    bool ans = false;
    if (d == 0)
      ans = false;
    else if (b % d != 0)
      ans = false;
    else {
      ll cnt = b / d + 1;
      if (cnt <= n && (n - cnt) % 2 == 0)
        ans = true;
    }

    puts(ans ? "YES" : "NO");
    return 0;
  }

  bool ans = false;
  FOR(i, 1, n + 1) {
    ll l = i * c, r = i * d;
    ll cnt = INF;

    if (b < l) {
      cnt = (l - b + d - c - 1) / (d - c) * 2 + i + 1;
    } else if (l <= b && b <= r) {
      cnt = i + 1;
    } else if (r < b) {
      cnt = (b - r + d - c - 1) / (d - c) * 2 + i + 1;
    }

    if (cnt <= n && (n - cnt) % 2 == 0)
      ans = true;
  }

  puts(ans ? "YES" : "NO");

  return 0;
}
Problem p03666 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
inline int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
int main() {
  int n, A, B, C, D;
  cin >> n >> A >> B >> C >> D, --n;
  long long L = A + 1LL * n * C, R = A + 1LL * n * D;
  for (int i = 0; i <= n; i++) {
    if (L <= B && B <= R)
      return puts("YES"), 1;
    L -= C + D, R -= C + D;
  }
  puts("NO");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
inline int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
int main() {
  int n, A, B, C, D;
  cin >> n >> A >> B >> C >> D, --n;
  long long L = A + 1LL * n * C, R = A + 1LL * n * D;
  for (int i = 0; i <= n; i++) {
    if (L <= B && B <= R)
      return puts("YES"), 0;
    L -= C + D, R -= C + D;
  }
  puts("NO");
  return 0;
}
Problem p03666 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;
  if (n % 2)
    cout << 1 / 0 << endl;
  else
    cout << "NO" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;
  for (int i = 0; i < n; i++) {
    if (c * (n - 1 - i) - d * i <= b - a && b - a <= d * (n - 1 - i) - c * i) {
      cout << "YES" << endl;
      exit(0);
    }
  }
  cout << "NO" << endl;
}
Problem p03666 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long
using pi = pair<int, int>;
signed main() {
  int n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;
  b = abs(a - b), a = 0, --n;
  if (b > n * d)
    return cout << "NO", 0;
  else if (b >= n * c)
    return cout << "YES", 0;
  assert(b < n * c);
  // b == m * c + r
  b %= c, d -= c;
  if (n * d >= b && ((b / d) % 2 == 0 || (n - b / d > 2 && d >= c)))
    cout << "YES";
  else
    cout << "NO";
  cerr << "b:" << b << ", d:" << d << ", n:" << n << ", n-b/d:" << n - b / d
       << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long
using pi = pair<int, int>;
signed main() {
  int n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;
  b = abs(a - b), --n;
  for (int x = 0; x <= n; ++x)
    if (c * (n - x) - d * x <= b && b <= d * (n - x) - c * x)
      return cout << "YES", 0;
  cout << "NO";
}
Problem p03666 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>

#define gec getchar
#define FILE(F) freopen(F ".in", "r", stdin), freopen(F ".out", "w", stdout)

typedef long long ll;
template <typename T> inline void read(T &x) {
  x = 0;
  bool f = 0;
  char c = gec();
  for (; c < '0' || c > '9'; c = gec())
    f = (c == '-');
  for (; c >= '0' && c <= '9'; c = gec())
    x = x * 10 + c - '0';
  x = f ? -x : x;
}
namespace IO {
inline void fpr(char *c) { fprintf(stderr, "%s\n", c); }
inline void fpr(int x, char *c) { fprintf(stderr, "[ %s ] : = %d   \n", c, x); }
inline void fpr(ll x, char *c) { fprintf(stderr, "[ %s ] : = %lld \n", c, x); }
inline void fpr(double x, char *c) {
  fprintf(stderr, "[ %s ] : = %.5lf\n", c, x);
}
inline void fpr(long double x, char *c) {
  fprintf(stderr, "[ %s ] : = %.5Lf\n", c, x);
}
} // namespace IO
using namespace IO;

int n;
ll A, B, C, D;
int main() {
#ifndef ONLINE_JUDGE
  FILE("C");
#endif
  read(n);
  read(A);
  read(B);
  read(C);
  read(D);
  B -= A;
  for (int i = 1; i < n - 1; i++) {
    ll L1 = i * C, R1 = i * D;
    ll R2 = -(n - 2 - i + 1) * C, L2 = -(n - 2 - i + 1) * D;
    if (L1 + L2 <= B && B <= R1 + R2) {
      puts("YES");
      return 0;
    }
  }
  puts("NO");
  return 0;
}

Fixed submission:
#include <cstdio>

#define gec getchar
#define FILE(F) freopen(F ".in", "r", stdin), freopen(F ".out", "w", stdout)

typedef long long ll;
template <typename T> inline void read(T &x) {
  x = 0;
  bool f = 0;
  char c = gec();
  for (; c < '0' || c > '9'; c = gec())
    f = (c == '-');
  for (; c >= '0' && c <= '9'; c = gec())
    x = x * 10 + c - '0';
  x = f ? -x : x;
}
namespace IO {
inline void fpr(char *c) { fprintf(stderr, "%s\n", c); }
inline void fpr(int x, char *c) { fprintf(stderr, "[ %s ] : = %d   \n", c, x); }
inline void fpr(ll x, char *c) { fprintf(stderr, "[ %s ] : = %lld \n", c, x); }
inline void fpr(double x, char *c) {
  fprintf(stderr, "[ %s ] : = %.5lf\n", c, x);
}
inline void fpr(long double x, char *c) {
  fprintf(stderr, "[ %s ] : = %.5Lf\n", c, x);
}
} // namespace IO
using namespace IO;

int n;
ll A, B, C, D;
int main() {
  read(n);
  read(A);
  read(B);
  read(C);
  read(D);
  B -= A;
  for (int i = 1; i < n - 1; i++) {
    ll L1 = i * C, R1 = i * D;
    ll R2 = -(n - 2 - i + 1) * C, L2 = -(n - 2 - i + 1) * D;
    if (L1 + L2 <= B && B <= R1 + R2) {
      puts("YES");
      return 0;
    }
  }
  puts("NO");
  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

template <typename T> inline void rd(T &x) {
  int si = 1;
  char c = getchar();
  x = 0;
  while (!isdigit(c))
    si = c == '-' ? -1 : si, c = getchar();
  while (isdigit(c))
    x = x * 10 + c - 48, c = getchar();
  x *= si;
}
template <typename T, typename... Args> inline void rd(T &x, Args &...args) {
  rd(x);
  rd(args...);
}
#define fi first
#define se second
#define mkp std::make_pair
typedef long long ll;
typedef double ff;
typedef std::pair<int, int> pii;
const int kN = 1e5 + 5, kInf = 0x3f3f3f3f;
const ll kMod = 998244353, kInfLL = 0x3f3f3f3f3f3f3f3fLL;

int cover[kN], ans, n, q, buc[kN], a[kN];

void Add(int x) {
  ++buc[x];
  if (x - buc[x] + 1 > 0) {
    if (!cover[x - buc[x] + 1])
      --ans;
    ++cover[x - buc[x] + 1];
  }
}
void Del(int x) {
  if (x - buc[x] + 1 > 0) {
    --cover[x - buc[x] + 1];
    if (!cover[x - buc[x] + 1])
      ++ans;
  }
  --buc[x];
}

int main() {
  int T = 1; // rd(T);
  while (T--) {
    rd(n, q);
    ans = n;
    for (int i = 1; i <= n; ++i) {
      rd(a[i]);
      Add(a[i]);
    }
    while (q--) {
      int x, y;
      rd(x, y);
      Del(a[x]);
      Add(y);
      a[x] = y;
      printf("%d\n", ans);
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

template <typename T> inline void rd(T &x) {
  int si = 1;
  char c = getchar();
  x = 0;
  while (!isdigit(c))
    si = c == '-' ? -1 : si, c = getchar();
  while (isdigit(c))
    x = x * 10 + c - 48, c = getchar();
  x *= si;
}
template <typename T, typename... Args> inline void rd(T &x, Args &...args) {
  rd(x);
  rd(args...);
}
#define fi first
#define se second
#define mkp std::make_pair
typedef long long ll;
typedef double ff;
typedef std::pair<int, int> pii;
const int kN = 2e5 + 5, kInf = 0x3f3f3f3f;
const ll kMod = 998244353, kInfLL = 0x3f3f3f3f3f3f3f3fLL;

int cover[kN], ans, n, q, buc[kN], a[kN];

void Add(int x) {
  ++buc[x];
  if (x - buc[x] + 1 > 0) {
    if (!cover[x - buc[x] + 1])
      --ans;
    ++cover[x - buc[x] + 1];
  }
}
void Del(int x) {
  if (x - buc[x] + 1 > 0) {
    --cover[x - buc[x] + 1];
    if (!cover[x - buc[x] + 1])
      ++ans;
  }
  --buc[x];
}

int main() {
  int T = 1; // rd(T);
  while (T--) {
    rd(n, q);
    ans = n;
    for (int i = 1; i <= n; ++i) {
      rd(a[i]);
      Add(a[i]);
    }
    while (q--) {
      int x, y;
      rd(x, y);
      Del(a[x]);
      Add(y);
      a[x] = y;
      printf("%d\n", ans);
    }
  }
  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

const int MAX_N = 1e5 + 5;
int n, q, ans;
int Arr[MAX_N];
int cnt[MAX_N];
int val[MAX_N];

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> Arr[i];
    cnt[Arr[i]]++;
  }

  for (int i = 1; i <= n; i++)
    for (int j = max(0, i - cnt[i]); j < i; j++)
      val[j]++;

  for (int i = 0; i < n; i++)
    ans += !val[i];

  while (q--) {
    int x, y;
    cin >> x >> y;
    x--;

    if (0 <= Arr[x] - cnt[Arr[x]]) {
      if (val[Arr[x] - cnt[Arr[x]]] == 1)
        ans++;

      val[Arr[x] - cnt[Arr[x]]]--;
    }

    cnt[Arr[x]]--;

    Arr[x] = y;
    cnt[Arr[x]]++;
    if (0 <= Arr[x] - cnt[Arr[x]]) {
      if (val[Arr[x] - cnt[Arr[x]]] == 0)
        ans--;

      val[Arr[x] - cnt[Arr[x]]]++;
    }

    cout << ans << "\n";
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

const int MAX_N = 2e5 + 10;
int n, q, ans;
int Arr[MAX_N];
int cnt[MAX_N];
int val[MAX_N];

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> Arr[i];
    cnt[Arr[i]]++;
  }

  for (int i = 1; i <= n; i++)
    for (int j = max(0, i - cnt[i]); j < i; j++)
      val[j]++;

  for (int i = 0; i < n; i++)
    ans += !val[i];

  while (q--) {
    int x, y;
    cin >> x >> y;
    x--;

    if (0 <= Arr[x] - cnt[Arr[x]]) {
      if (val[Arr[x] - cnt[Arr[x]]] == 1)
        ans++;

      val[Arr[x] - cnt[Arr[x]]]--;
    }

    cnt[Arr[x]]--;

    Arr[x] = y;
    cnt[Arr[x]]++;
    if (0 <= Arr[x] - cnt[Arr[x]]) {
      if (val[Arr[x] - cnt[Arr[x]]] == 0)
        ans--;

      val[Arr[x] - cnt[Arr[x]]]++;
    }

    cout << ans << "\n";
  }
  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
// AtCoder Grand Contest 017
// C - Snuke and Spells

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

const int MAX_N = 200000;
const int MAX_M = 200000;

int N, M;
int A[MAX_N];
int cnt[MAX_N + 1];
int sec_raw[2 * MAX_N + 2];
int *sec = sec_raw + N;

int main() {

  int x, y;
  int idx;
  int ans;

  cin >> N >> M;
  memset(cnt, 0, sizeof(cnt));
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    cnt[A[i]]++;
  }
  memset(sec_raw, 0, sizeof(sec_raw));
  for (int i = 1; i <= N; i++) {
    sec[i - cnt[i] + 1]++;
    sec[i + 1]--;
  }
  ans = 0;
  for (int i = -N + 1; i <= N; i++) {
    sec[i] += sec[i - 1];
    ans += (i >= 1 && sec[i] == 0 ? 1 : 0);
  }

  for (int q = 0; q < M; q++) {
    cin >> x >> y;
    x--;
    idx = A[x] - cnt[A[x]] + 1;
    sec[idx]--;
    ans += (idx >= 1 && sec[idx] == 0 ? 1 : 0);
    cnt[A[x]]--;
    A[x] = y;
    cnt[y]++;
    idx = y - cnt[y] + 1;
    ans -= (idx >= 1 && sec[idx] == 0 ? 1 : 0);
    sec[idx]++;

    cout << ans << endl;
  }

  return 0;
}


Fixed submission:
// AtCoder Grand Contest 017
// C - Snuke and Spells

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

const int MAX_N = 200000;
const int MAX_M = 200000;

int N, M;
int A[MAX_N];
int cnt[MAX_N + 1];
int sec_raw[2 * MAX_N + 2];
int *sec = sec_raw + MAX_N;

int main() {

  int x, y;
  int idx;
  int ans;

  cin >> N >> M;
  memset(cnt, 0, sizeof(cnt));
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    cnt[A[i]]++;
  }
  memset(sec_raw, 0, sizeof(sec_raw));
  for (int i = 1; i <= N; i++) {
    sec[i - cnt[i] + 1]++;
    sec[i + 1]--;
  }
  ans = 0;
  for (int i = -N + 1; i <= N; i++) {
    sec[i] += sec[i - 1];
    ans += (i >= 1 && sec[i] == 0 ? 1 : 0);
  }

  for (int q = 0; q < M; q++) {
    cin >> x >> y;
    x--;
    idx = A[x] - cnt[A[x]] + 1;
    sec[idx]--;
    ans += (idx >= 1 && sec[idx] == 0 ? 1 : 0);
    cnt[A[x]]--;
    A[x] = y;
    cnt[y]++;
    idx = y - cnt[y] + 1;
    ans -= (idx >= 1 && sec[idx] == 0 ? 1 : 0);
    sec[idx]++;

    cout << ans << endl;
  }

  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)

using namespace std;
using llng = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
template <class T> using hset = unordered_set<T>;
template <class Key, class T> using hmap = unordered_map<Key, T>;

int main() {
  int N, M;
  cin >> N >> M;
  // Aiは位置iの値
  // Ciは値iの数
  // Liは[i-1,i]を覆う区間の数
  vi A(N + 1, 0), C(N + 1, 0), L(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    C[A[i]]++;
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < C[i]; j++) {
      if (i - j <= 0)
        break;
      L[i - j]++;
    }
  }

  int zero = 0;
  for (int i = 1; i <= N; i++)
    if (L[i] == 0)
      zero++;
  for (int i = 1; i <= M; i++) {
    int x, y;
    cin >> x >> y;
    // 変化前
    L[A[x] - (C[A[x]] - 1)]--;
    if (L[A[x] - (C[A[x]] - 1)] == 0)
      zero++;
    C[A[x]]--;
    // 変化後
    A[x] = y;
    C[A[x]]++;
    if (A[x] - (C[A[x]] - 1) >= 1) {
      if (L[A[x] - (C[A[x]] - 1)] == 0)
        zero--;
      L[A[x] - (C[A[x]] - 1)]++;
    }

    cout << zero << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)

using namespace std;
using llng = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
template <class T> using hset = unordered_set<T>;
template <class Key, class T> using hmap = unordered_map<Key, T>;

int main() {
  int N, M;
  cin >> N >> M;
  // Aiは位置iの値
  // Ciは値iの数
  // Liは[i-1,i]を覆う区間の数
  vi A(N + 1, 0), C(N + 1, 0), L(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    C[A[i]]++;
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < C[i]; j++) {
      if (i - j <= 0)
        break;
      L[i - j]++;
    }
  }

  int zero = 0;
  for (int i = 1; i <= N; i++)
    if (L[i] == 0)
      zero++;
  for (int i = 1; i <= M; i++) {
    int x, y;
    cin >> x >> y;
    // 変化前
    if (A[x] - (C[A[x]] - 1) >= 1) {
      L[A[x] - (C[A[x]] - 1)]--;
      if (L[A[x] - (C[A[x]] - 1)] == 0)
        zero++;
    }
    C[A[x]]--;
    // 変化後
    A[x] = y;
    C[A[x]]++;
    if (A[x] - (C[A[x]] - 1) >= 1) {
      if (L[A[x] - (C[A[x]] - 1)] == 0)
        zero--;
      L[A[x] - (C[A[x]] - 1)]++;
    }

    cout << zero << endl;
  }

  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <iomanip>
#include <iostream>
// #include<cstdio>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int Nmax = 200000;
const int Mmax = 200000;
int N, M;
int A[Nmax], X[Mmax], Y[Mmax];

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < M; i++) {
    cin >> X[i] >> Y[i];
    X[i]--;
  }

  vector<int> l(N), n(N);
  int ans = 0;
  for (int i = 0; i < N; i++) {
    l[i] = i + 1;
  }
  for (int i = 0; i < N; i++) {
    l[A[i] - 1]--;
    if (l[A[i] - 1] >= 0) {
      n[l[A[i] - 1]]++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (n[i] == 0)
      ans++;
  }
  // for(int i=0; i<N; i++) cout << l[i] << (i==N-1?'\n':' ');
  // for(int i=0; i<N; i++) cout << n[i] << (i==N-1?'\n':' ');
  // cout << endl;

  for (int i = 0; i < M; i++) {
    int a = A[X[i]];
    n[l[a - 1]]--;
    if (l[a - 1] >= 0 && n[l[a - 1]] == 0)
      ans++;
    l[a - 1]++;
    // for(int j=0; j<N; j++) cout << l[j] << (j==N-1?'\n':' ');
    // for(int j=0; j<N; j++) cout << n[j] << (j==N-1?'\n':' ');
    // cout << endl;

    l[Y[i] - 1]--;
    if (l[Y[i] - 1] >= 0) {
      n[l[Y[i] - 1]]++;
      if (n[l[Y[i] - 1]] == 1)
        ans--;
    }
    A[X[i]] = Y[i];
    // for(int j=0; j<N; j++) cout << l[j] << (j==N-1?'\n':' ');
    // for(int j=0; j<N; j++) cout << n[j] << (j==N-1?'\n':' ');

    cout << ans << endl;
  }

  return 0;
}


Fixed submission:
#include <iomanip>
#include <iostream>
// #include<cstdio>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int Nmax = 200000;
const int Mmax = 200000;
int N, M;
int A[Nmax], X[Mmax], Y[Mmax];

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < M; i++) {
    cin >> X[i] >> Y[i];
    X[i]--;
  }

  vector<int> l(N), n(N);
  int ans = 0;
  for (int i = 0; i < N; i++) {
    l[i] = i + 1;
  }
  for (int i = 0; i < N; i++) {
    l[A[i] - 1]--;
    if (l[A[i] - 1] >= 0) {
      n[l[A[i] - 1]]++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (n[i] == 0)
      ans++;
  }
  // for(int i=0; i<N; i++) cout << l[i] << (i==N-1?'\n':' ');
  // for(int i=0; i<N; i++) cout << n[i] << (i==N-1?'\n':' ');
  // cout << endl;

  for (int i = 0; i < M; i++) {
    int a = A[X[i]];
    if (l[a - 1] >= 0) {
      n[l[a - 1]]--;
      if (n[l[a - 1]] == 0)
        ans++;
    }
    l[a - 1]++;
    // for(int j=0; j<N; j++) cout << l[j] << (j==N-1?'\n':' ');
    // for(int j=0; j<N; j++) cout << n[j] << (j==N-1?'\n':' ');
    // cout << endl;

    l[Y[i] - 1]--;
    if (l[Y[i] - 1] >= 0) {
      n[l[Y[i] - 1]]++;
      if (n[l[Y[i] - 1]] == 1)
        ans--;
    }
    A[X[i]] = Y[i];
    // for(int j=0; j<N; j++) cout << l[j] << (j==N-1?'\n':' ');
    // for(int j=0; j<N; j++) cout << n[j] << (j==N-1?'\n':' ');

    cout << ans << endl;
  }

  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
// by Balloons
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define mpr make_pair
#define debug() puts("okkkkkkkk")
#define rep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)

using namespace std;

typedef long long LL;

const int inf = 1 << 30;
const int maxn = 1e5 + 5;

int n, ques;
int a[maxn], tim[maxn];
int vis[maxn];

int main() {
  scanf("%d%d", &n, &ques);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), ++tim[a[i]];
  for (int i = 1; i <= n; i++)
    for (int j = max(i - tim[i], 0); j < i; j++)
      ++vis[j];
  int ans = 0;
  for (int i = 0; i < n; i++)
    if (!vis[i])
      ++ans;
  while (ques--) {
    int x, to;
    scanf("%d%d", &x, &to);
    int tmp = a[x];
    if (tmp - tim[tmp] >= 0 && vis[tmp - tim[tmp]] == 1)
      ++ans;
    if (tmp - tim[tmp] >= 0)
      --vis[tmp - tim[tmp]];
    //		printf("----%d %d %d\n",ans,tmp,tim[tmp]);
    --tim[tmp];

    int tt = to;
    a[x] = tt;
    if (tt - tim[tt] >= 1 && !vis[tt - tim[tt] - 1])
      --ans;
    ++tim[tt];
    if (tt - tim[tt] >= 0)
      ++vis[tt - tim[tt]];
    printf("%d\n", ans);
  }

  return 0;
}


Fixed submission:
// by Balloons
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define mpr make_pair
#define debug() puts("okkkkkkkk")
#define rep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)

using namespace std;

typedef long long LL;

const int inf = 1 << 30;
const int maxn = 2e5 + 5;

int n, ques;
int a[maxn], tim[maxn];
int vis[maxn];

int main() {
  scanf("%d%d", &n, &ques);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), ++tim[a[i]];
  for (int i = 1; i <= n; i++)
    for (int j = max(i - tim[i], 0); j < i; j++)
      ++vis[j];
  int ans = 0;
  for (int i = 0; i < n; i++)
    if (!vis[i])
      ++ans;
  while (ques--) {
    int x, to;
    scanf("%d%d", &x, &to);
    int tmp = a[x];
    if (tmp - tim[tmp] >= 0 && vis[tmp - tim[tmp]] == 1)
      ++ans;
    if (tmp - tim[tmp] >= 0)
      --vis[tmp - tim[tmp]];
    //		printf("----%d %d %d\n",ans,tmp,tim[tmp]);
    --tim[tmp];

    int tt = to;
    a[x] = tt;
    if (tt - tim[tt] >= 1 && !vis[tt - tim[tt] - 1])
      --ans;
    ++tim[tt];
    if (tt - tim[tt] >= 0)
      ++vis[tt - tim[tt]];
    printf("%d\n", ans);
  }

  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())
#define pii pair<int, int>
#define mp make_pair
#define f first
#define ll long long
#define ld long double
#define s second
#define vec vector<int>

using namespace std;

const int N = (int)1e5 + 10;
const int M = (int)2018 * 2018;
const int K = (int)15;
const int INF = (int)1e9 + 7;
const int mod = (int)998244353;
const ld EPS = (ld)1e-9;
const ll LINF = (ll)1e18;

int n, m;
int a[N], mt[N], cnt[N];
set<int> st;

void del(int x) {
  if (x - cnt[x] >= 0) {
    mt[x - cnt[x]]--;
    if (!mt[x - cnt[x]])
      st.erase(x - cnt[x]);
  }
  cnt[x]--;
}

void add(int x) {
  cnt[x]++;
  if (x - cnt[x] >= 0) {
    if (mt[x - cnt[x]] == 0)
      st.insert(x - cnt[x]);
    mt[x - cnt[x]]++;
  }
}

int main() {
#ifdef sony
  freopen("input.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  srand(time(0));
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    add(a[i]);
  }
  while (m--) {
    int x, y;
    cin >> x >> y;
    del(a[x]);
    a[x] = y;
    add(a[x]);
    cout << n - sz(st) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())
#define pii pair<int, int>
#define mp make_pair
#define f first
#define ll long long
#define ld long double
#define s second
#define vec vector<int>

using namespace std;

const int N = (int)3e5 + 10;
const int M = (int)2018 * 2018;
const int K = (int)15;
const int INF = (int)1e9 + 7;
const int mod = (int)998244353;
const ld EPS = (ld)1e-9;
const ll LINF = (ll)1e18;

int n, m;
int a[N], mt[N], cnt[N];
set<int> st;

void del(int x) {
  if (x - cnt[x] >= 0) {
    mt[x - cnt[x]]--;
    if (!mt[x - cnt[x]])
      st.erase(x - cnt[x]);
  }
  cnt[x]--;
}

void add(int x) {
  cnt[x]++;
  if (x - cnt[x] >= 0) {
    if (mt[x - cnt[x]] == 0)
      st.insert(x - cnt[x]);
    mt[x - cnt[x]]++;
  }
}

int main() {
#ifdef sony
  freopen("input.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  srand(time(0));
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    add(a[i]);
  }
  while (m--) {
    int x, y;
    cin >> x >> y;
    del(a[x]);
    a[x] = y;
    add(a[x]);
    cout << n - sz(st) << endl;
  }
  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, x) for (int(i) = 0; (i) < (x); (i)++)
using namespace std;

const int maxn = 1e5 + 5;
int res, n, m, sum[maxn], ok[maxn], a[maxn];

int main() {
  scanf("%d%d", &n, &m);
  rep(i, n) {
    int x;
    scanf("%d", &x);
    a[i] = x;
    sum[x]++;
    if (x - sum[x] + 1 >= 1) {
      ok[x - sum[x] + 1]++;
      if (ok[x - sum[x] + 1] == 1)
        res++;
    }
  }
  rep(i, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    int p = a[x] - sum[a[x]] + 1;
    if (p >= 1) {
      if (ok[p] == 1)
        res--;
      ok[p]--;
    }

    sum[a[x]]--;
    a[x] = y;
    sum[y]++;
    p = y - sum[y] + 1;
    if (p >= 1) {
      if (ok[p] == 0)
        res++;
      ok[p]++;
    }
    printf("%d\n", n - res);
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, x) for (int(i) = 0; (i) < (x); (i)++)
using namespace std;

const int maxn = 3 * 1e5 + 5;
int res, n, m, sum[maxn], ok[maxn], a[maxn];

int main() {
  scanf("%d%d", &n, &m);
  rep(i, n) {
    int x;
    scanf("%d", &x);
    a[i] = x;
    sum[x]++;
    if (x - sum[x] + 1 >= 1) {
      ok[x - sum[x] + 1]++;
      if (ok[x - sum[x] + 1] == 1)
        res++;
    }
  }
  rep(i, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    int p = a[x] - sum[a[x]] + 1;
    if (p >= 1) {
      if (ok[p] == 1)
        res--;
      ok[p]--;
    }

    sum[a[x]]--;
    a[x] = y;
    sum[y]++;
    p = y - sum[y] + 1;
    if (p >= 1) {
      if (ok[p] == 0)
        res++;
      ok[p]++;
    }
    printf("%d\n", n - res);
  }
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define cmin(a, b) (a > b ? a = b : a)
#define cmax(a, b) (a < b ? a = b : a)
#define lop(i, s, t) for (int i = s; i < (t); ++i)
#define rep(i, s, t) for (int i = s; i <= (t); ++i)
#define dec(i, s, t) for (int i = s; i >= (t); --i)
#define fore(i, v)                                                             \
  for (int i = g[v], d = es[i].d; i; i = es[i].nxt, d = es[i].d)
using namespace std;

typedef long long ll;

const int N = 55;

/*
int mul(int a,int b){ return 1ll*a*b%P; }
int add(int a,int b){ a+=b; return a>=P?a-P:a; }
int sub(int a,int b){ a-=b; return a<0?a+P:a; }
*/

int n, m, a[N], c[N], cov[N + N], ans;

int main(int argc, char *argv[]) {
#ifdef CURIOUSCAT
  freopen("dat.in", "r", stdin);
  // freopen("my.out","w",stdout);
#endif
  scanf("%d%d", &n, &m);
  rep(i, 1, n) scanf("%d", a + i), ++c[a[i]];
  rep(i, 1, n) rep(j, 1, c[i])++ cov[n + i - j + 1];
  rep(i, 1, n) if (!cov[n + i])++ ans;
  rep(i, 1, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (a[x] - c[a[x]] + 1 >= 1 && --cov[n + a[x] - c[a[x]] + 1] == 0)
      ++ans;
    --c[a[x]];
    a[x] = y;
    ++c[a[x]];
    if (a[x] - c[a[x]] + 1 >= 1 && ++cov[n + a[x] - c[a[x]] + 1] == 1)
      --ans;
    printf("%d\n", ans);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define cmin(a, b) (a > b ? a = b : a)
#define cmax(a, b) (a < b ? a = b : a)
#define lop(i, s, t) for (int i = s; i < (t); ++i)
#define rep(i, s, t) for (int i = s; i <= (t); ++i)
#define dec(i, s, t) for (int i = s; i >= (t); --i)
#define fore(i, v)                                                             \
  for (int i = g[v], d = es[i].d; i; i = es[i].nxt, d = es[i].d)
using namespace std;

typedef long long ll;

const int N = 4e5 + 50;

/*
int mul(int a,int b){ return 1ll*a*b%P; }
int add(int a,int b){ a+=b; return a>=P?a-P:a; }
int sub(int a,int b){ a-=b; return a<0?a+P:a; }
*/

int n, m, a[N], c[N], cov[N + N], ans;

int main(int argc, char *argv[]) {
#ifdef CURIOUSCAT
  freopen("dat.in", "r", stdin);
  // freopen("my.out","w",stdout);
#endif
  scanf("%d%d", &n, &m);
  rep(i, 1, n) scanf("%d", a + i), ++c[a[i]];
  rep(i, 1, n) rep(j, 1, c[i])++ cov[n + i - j + 1];
  rep(i, 1, n) if (!cov[n + i])++ ans;
  rep(i, 1, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (a[x] - c[a[x]] + 1 >= 1 && --cov[n + a[x] - c[a[x]] + 1] == 0)
      ++ans;
    --c[a[x]];
    a[x] = y;
    ++c[a[x]];
    if (a[x] - c[a[x]] + 1 >= 1 && ++cov[n + a[x] - c[a[x]] + 1] == 1)
      --ans;
    printf("%d\n", ans);
  }
  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAX = (1e5) + 1;

int n, m, cover, a[MAX], curr[2 * MAX + 10], f[2 * MAX + 10];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    curr[i] = (MAX - 1) + i + 1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    --curr[a[i]];
    if (curr[a[i]] > MAX - 1)
      cover += !f[curr[a[i]]];
    ++f[curr[a[i]]];
  }
  for (int x, y; m > 0; m--) {
    cin >> x >> y;
    --f[curr[a[x]]];
    if (curr[a[x]] > MAX - 1)
      cover -= !f[curr[a[x]]];
    ++curr[a[x]];

    a[x] = y;
    --curr[a[x]];
    if (curr[a[x]] > MAX - 1)
      cover += !f[curr[a[x]]];
    ++f[curr[a[x]]];

    cout << n - cover << '\n';
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAX = (2e5) + 1;

int n, m, cover, a[MAX], curr[2 * MAX + 10], f[2 * MAX + 10];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    curr[i] = (MAX - 1) + i + 1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    --curr[a[i]];
    if (curr[a[i]] > MAX - 1)
      cover += !f[curr[a[i]]];
    ++f[curr[a[i]]];
  }
  for (int x, y; m > 0; m--) {
    cin >> x >> y;
    --f[curr[a[x]]];
    if (curr[a[x]] > MAX - 1)
      cover -= !f[curr[a[x]]];
    ++curr[a[x]];

    a[x] = y;
    --curr[a[x]];
    if (curr[a[x]] > MAX - 1)
      cover += !f[curr[a[x]]];
    ++f[curr[a[x]]];

    cout << n - cover << '\n';
  }
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
template <class T> bool chmin(T &a, const T &b) {
  return a > b ? (a = b, true) : false;
}
template <class T> bool chmax(T &a, const T &b) {
  return a < b ? (a = b, true) : false;
}

int nextInt() {
  int x;
  scanf("%d", &x);
  return x;
}

const int MAX_N = 100000 + 10;
int A[MAX_N];
int cnt[MAX_N];
int cover[MAX_N];

void add(int p, int &zero) {
  if (p >= 1) {
    if (cover[p] == 0)
      zero--;
    cover[p]++;
  }
}
void sub(int p, int &zero) {
  if (p >= 1) {
    cover[p]--;
    if (cover[p] == 0)
      zero++;
  }
}
int main2() {
  memset(cnt, 0, sizeof(cnt));
  memset(cover, 0, sizeof(cover));
  int N = nextInt();
  int M = nextInt();
  int zero = N;
  REP(i, N) {
    int a = nextInt();
    A[i] = a;
    ++cnt[a];
    add(a - cnt[a] + 1, zero);
  }
  REP(mi, M) {
    int X = nextInt() - 1;
    int Y = nextInt();
    int x = A[X];
    A[X] = Y;
    sub(x - cnt[x] + 1, zero);
    --cnt[x];
    ++cnt[Y];
    add(Y - cnt[Y] + 1, zero);
    printf("%d\n", zero);
  }
  return 0;
}

int main() {
  for (; !cin.eof(); cin >> ws)
    main2();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
template <class T> bool chmin(T &a, const T &b) {
  return a > b ? (a = b, true) : false;
}
template <class T> bool chmax(T &a, const T &b) {
  return a < b ? (a = b, true) : false;
}

int nextInt() {
  int x;
  scanf("%d", &x);
  return x;
}

const int MAX_N = 200000 + 10;
int A[MAX_N];
int cnt[MAX_N];
int cover[MAX_N];

void add(int p, int &zero) {
  if (p >= 1) {
    if (cover[p] == 0)
      zero--;
    cover[p]++;
  }
}
void sub(int p, int &zero) {
  if (p >= 1) {
    cover[p]--;
    if (cover[p] == 0)
      zero++;
  }
}
int main2() {
  memset(cnt, 0, sizeof(cnt));
  memset(cover, 0, sizeof(cover));
  int N = nextInt();
  int M = nextInt();
  int zero = N;
  REP(i, N) {
    int a = nextInt();
    A[i] = a;
    ++cnt[a];
    add(a - cnt[a] + 1, zero);
  }
  REP(mi, M) {
    int X = nextInt() - 1;
    int Y = nextInt();
    int x = A[X];
    A[X] = Y;
    sub(x - cnt[x] + 1, zero);
    --cnt[x];
    ++cnt[Y];
    add(Y - cnt[Y] + 1, zero);
    printf("%d\n", zero);
  }
  return 0;
}

int main() {
  for (; !cin.eof(); cin >> ws)
    main2();
  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define M 100005
#define N 1005
#define ll long long
#define oo 1000000000
#define mod 5557
using namespace std;
int A[M], B[M], ans, n, m, dp[M], cnt[M], sum[M], t;
void check(int &a, int b) {
  if (a == -1 || a > b)
    a = b;
}
int main() {
  int i, j, a, b;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    scanf("%d", &A[i]);
    cnt[A[i]]++;
  }
  for (i = 1; i <= n; i++) {
    for (j = min(cnt[i], i) - 1; j >= 0; j--) {
      if (!sum[i - j])
        ++t;
      sum[i - j]++;
    }
  }
  for (i = 1; i <= m; i++) {
    scanf("%d %d", &a, &b);
    cnt[A[a]]--;
    if (A[a] - cnt[A[a]] > 0 && !(--sum[A[a] - cnt[A[a]]]))
      --t;
    A[a] = b;
    if (A[a] - cnt[A[a]] > 0 && !sum[A[a] - cnt[A[a]]]++)
      ++t;
    ++cnt[A[a]];
    printf("%d\n", n - t);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define M 200005
#define N 1005
#define ll long long
#define oo 1000000000
#define mod 5557
using namespace std;
int A[M], B[M], ans, n, m, dp[M], cnt[M], sum[M], t;
void check(int &a, int b) {
  if (a == -1 || a > b)
    a = b;
}
int main() {
  int i, j, a, b;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    scanf("%d", &A[i]);
    cnt[A[i]]++;
  }
  for (i = 1; i <= n; i++) {
    for (j = min(cnt[i], i) - 1; j >= 0; j--) {
      if (!sum[i - j])
        ++t;
      sum[i - j]++;
    }
  }
  for (i = 1; i <= m; i++) {
    scanf("%d %d", &a, &b);
    cnt[A[a]]--;
    if (A[a] - cnt[A[a]] > 0 && !(--sum[A[a] - cnt[A[a]]]))
      --t;
    A[a] = b;
    if (A[a] - cnt[A[a]] > 0 && !sum[A[a] - cnt[A[a]]]++)
      ++t;
    ++cnt[A[a]];
    printf("%d\n", n - t);
  }
  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  vector<int> cnt(n + 1, 0);
  set<int> s;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
    s.insert(a[i]);
  }
  vector<int> seg_cnt(n, 0);
  for (auto a : s) {
    int left = a - cnt[a];
    int right = a;
    if (left < 0)
      seg_cnt[0]++;
    else
      seg_cnt[left]++;
    seg_cnt[right]--;
  }
  for (int i = 0; i < n - 1; i++)
    seg_cnt[i + 1] += seg_cnt[i];
  int not_covered = 0;
  for (int i = 0; i < n; i++)
    not_covered += seg_cnt[i] == 0;
  while (m--) {
    int x, y;
    cin >> x >> y;
    x--;
    int prev = a[x];
    a[x] = y;
    if (prev - cnt[prev] >= 0) {
      seg_cnt[prev - cnt[prev]]--;
      if (seg_cnt[prev - cnt[prev]] == 0)
        not_covered++;
    }
    cnt[prev]--;
    if (y - cnt[y] - 1 >= 0) {
      seg_cnt[y - cnt[y] - 1]++;
      if (seg_cnt[y - cnt[y] - 1] == 1)
        not_covered--;
    }
    cnt[y]++;
    cout << not_covered << endl;
  }
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  vector<int> cnt(n + 1, 0);
  set<int> s;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
    s.insert(a[i]);
  }
  vector<int> seg_cnt(n + 1, 0);
  for (auto a : s) {
    int left = a - cnt[a];
    int right = a;
    if (left < 0)
      seg_cnt[0]++;
    else
      seg_cnt[left]++;
    seg_cnt[right]--;
  }
  for (int i = 0; i < n - 1; i++)
    seg_cnt[i + 1] += seg_cnt[i];
  int not_covered = 0;
  for (int i = 0; i < n; i++)
    not_covered += seg_cnt[i] == 0;
  while (m--) {
    int x, y;
    cin >> x >> y;
    x--;
    int prev = a[x];
    a[x] = y;
    if (prev - cnt[prev] >= 0) {
      seg_cnt[prev - cnt[prev]]--;
      if (seg_cnt[prev - cnt[prev]] == 0)
        not_covered++;
    }
    cnt[prev]--;
    if (y - cnt[y] - 1 >= 0) {
      seg_cnt[y - cnt[y] - 1]++;
      if (seg_cnt[y - cnt[y] - 1] == 1)
        not_covered--;
    }
    cnt[y]++;
    cout << not_covered << endl;
  }
  return 0;
}