alexjercan/bugnet huggingface dataset:
Problem p02274 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int bit[200000], s, a[200000], b[200000], n;
void add(int k, int x) {
  while (k <= s) {
    bit[k] += x;
    k += k & -k;
  }
}
int sum(int k) {
  int res = 0;
  while (k > 0) {
    res += bit[k];
    k -= k & -k;
  }
  return res;
}
void compress() {
  sort(b, b + n);
  int t = unique(b, b + n) - b;
  rep(i, n) a[i] = lower_bound(b, b + t, a[i]) - b + 1;
  s = t + 1;
}
int main() {
  scanf("%d", &n);
  long long ans = 0;
  rep(i, n) {
    scanf("%d", &a[i]);
    b[i] = a[i];
  }
  compress();
  rep(i, n) {
    ans += i - sum(a[i]);
    add(a[i], 1);
  }
  printf("%lld\\n", ans);
}

Error: MLE

Fixed submission:

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

int bit[1000000002];
void add(int k, int x) {
  while (k <= 1000000001) {
    bit[k] += x;
    k += k & -k;
  }
}
int sum(int k) {
  int res = 0;
  while (k > 0) {
    res += bit[k];
    k -= k & -k;
  }
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  long long ans = 0;
  rep(i, n) {
    int a;
    scanf("%d", &a);
    a++;
    ans += i - sum(a);
    add(a, 1);
  }
  printf("%lld\\n", ans);
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

const long MAX = 200000;
#define SENTINEL 2000000000;

int L[MAX / 2 + 2], R[MAX / 2 + 2];

long long merge(int A[], int n, int left, int mid, int right) {
  int i, j, k;
  long long cnt = 0;
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; ++i)
    L[i] = A[left + i];
  for (int i = 0; i < n2; ++i)
    R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  i = j = 0;
  for (int k = left; k < right; ++k) {

    if (L[i] <= R[j]) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
      cnt += n1 - i; //=mid+j-k-1
    }
  }
  return cnt;
}

long long mergesort(int A[], int n, int left, int right) {
  int mid;
  long long v1, v2, v3;
  if (left + 1 < right) {
    mid = (left + right) / 2;
    v1 = mergesort(A, n, left, mid);
    v2 = mergesort(A, n, mid, right);
    v3 = merge(A, n, left, mid, right);
    return v1 + v2 + v3;
  } else
    return 0;
};

int main() {
  int A[MAX], n, i;

  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> A[i];
  }

  long long ans = mergesort(A, n, 0, n);
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define SENTINEL 200000000;

int L[100002], R[100002];

long long merge(int A[], int n, int left, int mid, int right) {
  int i, j, k;
  long long cnt = 0;
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; ++i) {
    L[i] = A[left + i];
  }
  for (int i = 0; i < n; ++i) {
    R[i] = A[mid + i];
  }
  L[n1] = R[n2] = SENTINEL;
  i = j = 0;
  for (int k = left; k < right; ++k) {
    if (L[i] <= R[j]) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
      cnt += n1 - i; //=mid+j-k-1
    }
  }
  return cnt;
}

long long mergesort(int A[], int n, int left, int right) {
  int mid;
  long long v1, v2, v3;
  if (left + 1 < right) {
    mid = (left + right) / 2;
    v1 = mergesort(A, n, left, mid);
    v2 = mergesort(A, n, mid, right);
    v3 = merge(A, n, left, mid, right);
    return v1 + v2 + v3;
  } else
    return 0;
};

int main() {
  int A[200000], n, i;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
  }
  long long ans = mergesort(A, n, 0, n);
  cout << ans << endl;

  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

#define MAX 2000000000

long long Merge(std::vector<int> &List, int Left, int Mid, int Right);
long long MergeSort(std::vector<int> &List, int Left, int Right);

long long Merge(std::vector<int> &List, int Left, int Mid, int Right) {
  int n1 = Mid - Left;
  int n2 = Right - Mid;
  long long Count = 0;

  std::vector<int> LList;
  LList.clear();
  for (int i = 0; i < n1; i++) {
    LList.push_back(List[Left + i]);
  }

  std::vector<int> RList;
  RList.clear();
  for (int i = 0; i < n2; i++) {
    RList.push_back(List[Mid + i]);
  }
  LList.push_back(MAX);
  RList.push_back(MAX);

  int j = 0;
  int k = 0;
  for (int l = Left; l < Right; l++) {
    if (LList[j] <= RList[k]) {
      List[l] = LList[j];
      j++;
    } else {
      List[l] = RList[k];
      k++;
      Count = Count + Mid + k - l - 1;
    }
  }
  return Count;
}

long long MergeSort(std::vector<int> &List, int Left, int Right) {
  long long Count = 0;
  if (Left + 1 < Right) {
    int Mid = (Left + Right) / 2;
    Count += MergeSort(List, Left, Mid);
    Count += MergeSort(List, Mid, Right);
    Count += Merge(List, Left, Mid, Right);
  }
  return Count;
}

int main(void) {
  int Num, Temp;
  vector<int> List;
  List.clear();

  std::cin >> Num;

  for (int i = 0; i < Num; i++) {
    std::cin >> Temp;
    List.push_back(Temp);
  }
  std::cout << MergeSort(List, 0, Num) << std::endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  int Num, Count, Temp;
  vector<int> List;
  List.clear();

  std::cin >> Num;

  for (int i = 0; i < Num; i++) {
    std::cin >> Temp;
    List.push_back(Temp);
  }

  Count = 0;
  for (int i = Num - 1; i > 0; i--) {
    int j = i;
    while (j >= 0) {
      if (List[i] < List[j])
        Count++;
      j--;
    }
  }

  std::cout << Count << std::endl;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <stdio.h>
using namespace std;

int long cnt = 0;

void merge(int a[], int left, int mid, int right);
void mergeSort(int a[], int left, int right);

int main(void) {
  int n;
  cin >> n;
  int *a = new int[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  mergeSort(a, 0, n - 1);

  cout << cnt << endl;

  delete[] a;
  return 0;
}

void merge(int a[], int left, int mid, int right) {
  int n1 = mid - left + 1;
  int n2 = right - (mid + 1) + 1;
  int *l = new int[n1 + 1];
  int *r = new int[n2 + 1];
  for (int i = 0; i <= n1; i++)
    l[i] = a[left + i];
  for (int i = 0; i <= n2; i++)
    r[i] = a[(mid + 1) + i];
  l[n1] = 1000000000;
  r[n2] = 1000000000;

  int i = 0, j = 0;
  for (int k = left; k <= right; k++) {
    if (l[i] <= r[j]) {
      a[k] = l[i];
      i++;
      cnt += j;
    } else {
      a[k] = r[j];
      j++;
    }
  }
  delete[] l, r;
}

void mergeSort(int a[], int left, int right) {
  if (left < right) {
    int mid = (left + right) / 2;
    mergeSort(a, left, mid);
    mergeSort(a, mid + 1, right);
    merge(a, left, mid, right);
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <stdio.h>
using namespace std;

int main(void) {
  int n;
  cin >> n;

  int *a = new int[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[i] > a[j])
        cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

template <class V, int ME> class BIT {
public:
  V bit[1 << ME];
  V total(int e) {
    V s = 0;
    e++;
    while (e)
      s += bit[e - 1], e -= e & -e;
    return s;
  }
  void update(int e, V val) {
    e++;
    while (e <= 1 << ME)
      bit[e - 1] += val, e += e & -e;
  }
};

int main() {
  int N, tmp;
  cin >> N;
  vector<int> inputs(N), rank(N);
  vector<pii> x(N);
  REP(i, N) {
    cin >> x[i].first;
    x[i].second = i;
  }
  sort(x.begin(), x.end());
  REP(i, N) { rank[x[i].second] = i + 1; }

  REP(i, N) { inputs[--rank[i]] = i; }

  BIT<int, 19> bit;
  ll res = 0;
  REP(i, N) {
    bit.update(inputs[i], 1);
    // cout << bit.total(N) - bit.total(inputs[i]) << endl;
    res += (bit.total(N) - bit.total(inputs[i]));
  }
  cout << res << endl;
  return 0;
}


Error: 0

Fixed submission:

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

template <class V, int ME> class BIT {
public:
  V bit[1 << ME];
  V total(int e) {
    V s = 0;
    e++;
    while (e)
      s += bit[e - 1], e -= e & -e;
    return s;
  }
  void update(int e, V val) {
    e++;
    while (e <= 1 << ME)
      bit[e - 1] += val, e += e & -e;
  }
};

int main() {
  int N, tmp;
  cin >> N;
  vector<int> inputs(N), rank(N);
  vector<pii> x(N);
  REP(i, N) {
    cin >> x[i].first;
    x[i].second = i;
  }
  sort(x.begin(), x.end());
  REP(i, N) { rank[x[i].second] = i + 1; }

  REP(i, N) { inputs[--rank[i]] = i; }

  BIT<int, 17> bit;
  ll res = 0;
  REP(i, N) {
    bit.update(inputs[i], 1);
    // cout << bit.total(N) - bit.total(inputs[i]) << endl;
    res += (bit.total(N) - bit.total(inputs[i]));
  }
  cout << res << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error


#include <cstdio>
#include <iostream>

using namespace std;

typedef long long LL;

const int maxn = 2e5 + 5;
const int sentinel = 0x3f3f3f3f;

int l[maxn / 2 + 2], r[maxn / 2 + 2], a[maxn], n;

LL merge(int left, int mid, int right) {
  int i, j, k;
  LL cnt = 0;
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++) {
    l[i] = a[left + i];
  }
  for (int i = 0; i < n2; i++) {
    r[i] = a[mid + i];
  }
  l[n1] = r[n2] = sentinel;
  i = j = 0;
  for (int k = left; k < right; k++) {
    if (l[i] <= r[j]) {
      a[k] = l[i++];
    } else {
      a[k] = r[j++];
      cnt += n1 - i;
    }
  }
  return cnt;
}

LL mergesort(int left, int right) {
  int mid;
  LL v1, v2, v3;
  if (left + 1 < right) {
    mid = (left + right) >> 1;
    v1 = mergesort(left, mid);
    v2 = mergesort(mid, right);
    v3 = merge(left, mid, right);
    return v1 + v2 + v3;
  } else {
    return 0;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  LL ans = mergesort(0, n);
  cout << ans << endl;
  return 0;
}
/*
5
3 5 2 1 4
*/


Error: 0

Fixed submission:


#include <cstdio>
#include <iostream>

using namespace std;

typedef long long LL;

const int maxn = 2e4 + 5;
const int sentinel = 0x3f3f3f3f;

int l[maxn / 2 + 2], r[maxn / 2 + 2], a[maxn], n;

LL merge(int left, int mid, int right) {
  int i, j, k;
  LL cnt = 0;
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++) {
    l[i] = a[left + i];
  }
  for (int i = 0; i < n2; i++) {
    r[i] = a[mid + i];
  }
  l[n1] = r[n2] = sentinel;
  i = j = 0;
  for (int k = left; k < right; k++) {
    if (l[i] <= r[j]) {
      a[k] = l[i++];
    } else {
      a[k] = r[j++];
      cnt += n1 - i;
    }
  }
  return cnt;
}

LL mergesort(int left, int right) {
  int mid;
  LL v1, v2, v3;
  if (left + 1 < right) {
    mid = (left + right) >> 1;
    v1 = mergesort(left, mid);
    v2 = mergesort(mid, right);
    v3 = merge(left, mid, right);
    return v1 + v2 + v3;
  } else {
    return 0;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  LL ans = mergesort(0, n);
  cout << ans << endl;
  return 0;
}
/*
5
3 5 2 1 4
*/
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;
const int SENTINEL = 2000000000;

long long merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int *L = new int[n1 + 1];
  int *R = new int[n2 + 1];
  for (int i = 0; i < n1; ++i)
    L[i] = A[left + i];
  for (int i = 0; i < n2; ++i)
    R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  int i = 0, j = 0;
  long long cnt = 0;
  for (int k = left; k < right; k++) {

    if (L[i] <= R[j])
      A[k] = L[i++];
    else {
      cnt += n1 - i;
      A[k] = R[j++];
    }
  }
  delete L;
  delete R;
  return cnt;
}
long long mergeSort(int A[], int n, int left, int right) {
  long long v1, v2, v3;
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    v1 = mergeSort(A, n, left, mid);
    v2 = mergeSort(A, n, mid, right);
    v3 = merge(A, n, left, mid, right);
    return v1 + v2 + v3;
  } else
    return 0;
}
int main() {
  int n;
  cin >> n;
  int *A = new int[n];
  for (int i = 0; i < n; ++i)
    cin >> A[i];

  long long cnt = mergeSort(A, n, 0, n);

  cout << cnt << endl;
  delete A;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {

  int n;
  cin >> n;
  int *A = new int[n];
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  int cnt = 0;
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j)
      if (A[j] < A[i])
        cnt++;
  }
  cout << cnt << endl;
  delete A;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
#define INF (1000000001)
int N;
int A[(1 << 21) + 1];
int T[(1 << 21) + 1];
ll res = 0;
void marge(int l, int r) {
  if (r - l == 2) {
    if (A[l] > A[r - 1]) {
      res++;
      swap(A[l], A[r - 1]);
    }
  } else {
    int m = (l + r) / 2;
    marge(l, m);
    marge(m, r);
    for (int i = l, j = m, k = l; k < r; k++) {
      if ((j < r && A[i] > A[j]) || i == m) {
        res += (m - i);
        T[k] = A[j++];
      } else {
        T[k] = A[i++];
      }
    }
    for (int i = l; i < r; i++)
      A[i] = T[i];
  }
  /*
  cout <<"( " <<  l << " , "<< r << " ) " << endl;
  for(int i=l;i<r;i++) cout << A[i] << " ";
  cout << endl;
  cout << "res = "<< res << endl;
  */
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  if (N == 1)
    cout << 0 << endl;
  else {
    int n = 1;
    while (n < N)
      n *= 2;
    for (int i = N; i < n; i++)
      A[i] = INF + i;
    N = n;
    marge(0, N);
    cout << res << endl;
  }
}

Error: 0

Fixed submission:

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

typedef long long ll;
#define INF (1000000001)
int N;
int A[(1 << 21) + 1];
int T[(1 << 21) + 1];
ll res = 0;
void marge(int l, int r) {
  if (r - l == 2) {
    if (A[l] > A[r - 1]) {
      res++;
      swap(A[l], A[r - 1]);
    }
  } else {
    int m = (l + r) / 2;
    marge(l, m);
    marge(m, r);
    for (int i = l, j = m, k = l; k < r; k++) {
      if ((j < r && A[i] > A[j]) || i == m) {
        res += (m - i);
        T[k] = A[j++];
      } else {
        T[k] = A[i++];
      }
    }
    for (int i = l; i < r; i++)
      A[i] = T[i];
  }
  /*
  cout <<"( " <<  l << " , "<< r << " ) " << endl;
  for(int i=l;i<r;i++) cout << A[i] << " ";
  cout << endl;
  cout << "res = "<< res << endl;
  */
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int n = 1;
  while (n < N)
    n *= 2;
  for (int i = N; i < n; i++)
    A[i] = INF + i;
  N = n;
  marge(0, N);
  cout << res << endl;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

const int MAX_N = 200000;

template <typename T> class BIT {
private:
  vector<T> bit; //[1, n]
public:
  BIT() { bit = vector<T>(MAX_N + 1, 0); }
  T sum(int i) {
    assert(1 <= i and i <= MAX_N and "invalid argument");
    T s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }

  void add(int i, int x) {
    assert(1 <= i and i <= MAX_N and "invalid argument");
    while (i <= MAX_N) {
      bit[i] += x;
      // bit[i] = max(bit[i], x);
      i += i & -i;
    }
  }
};

template <typename T> class compressCoordinate {
public:
  vector<T> a, c; // 圧縮した数列、ソートした数列
  compressCoordinate(const vector<int> &arg) {
    a = vector<T>(arg.size());
    c = arg;
    compress(c);
    rep(i, arg.size()) { a[i] = lb(c, arg[i]); }
  }
  void compress(vector<T> &v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
  }
  int lb(const vector<T> &v, T num) {
    return lower_bound(all(v), num) - v.begin();
  }
  int map(T a) { return lb(c, a); }
};

int main() {
  BIT<int> bit;
  int n;
  cin >> n;

  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  compressCoordinate<int> com(a);

  long long cnt = 0;
  rep(i, n) {
    int t = com.map(a[i]) + 1;

    cnt += bit.sum(MAX_N) - bit.sum(t);
    bit.add(t, 1);
  }
  cout << cnt << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

const int MAX_N = 200000;

template <typename T> class BIT {
private:
  vector<T> bit; //[1, n]
public:
  BIT() { bit = vector<T>(MAX_N + 1, 0); }
  T sum(int i) {
    assert(1 <= i and i <= MAX_N and "invalid argument");
    T s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }

  void add(int i, int x) {
    assert(1 <= i and i <= MAX_N and "invalid argument");
    while (i <= MAX_N) {
      bit[i] += x;
      // bit[i] = max(bit[i], x);
      i += i & -i;
    }
  }
};

int main() {
  BIT<int> bit;
  int n;
  cin >> n;

  long long cnt = 0;
  rep(i, n) {
    int a;
    cin >> a;
    a++;
    cnt += bit.sum(MAX_N) - bit.sum(a);
    bit.add(a, 1);
  }
  cout << cnt << endl;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <vector>
using namespace std;

int n;
void counting_sort(vector<int> &A, vector<int> &B, int k) {
  int C[k + 1];
  // 0 initialize
  for (int i = 0; i <= k; i++) {
    C[i] = 0;
  }
  // counting A_j
  for (int j = 1; j <= n; j++) {
    C[A[j]]++;
  }
  // counting less than A_j
  for (int i = 1; i <= k; i++) {
    C[i] = C[i] + C[i - 1];
  }
  // sorting
  for (int j = n; j >= 1; j--) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }
}

int max(vector<int> &A) {
  int max = A[1];
  for (int i = 1; i <= n; i++) {
    if (A[i] > max)
      max = A[i];
  }
  return max;
}

int main() {
  scanf("%d", &n);
  vector<int> A(n + 1);
  vector<int> B(n + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%d ", &A[i]);
  }

  counting_sort(A, B, max(A));
  for (int i = 1; i < n; i++) {
    printf("%d ", B[i]);
  }
  printf("%d\\n", B[n]);
}

Error: 0

Fixed submission:

#include <cstdio>

int n;
void counting_sort(int A[], int B[], int k) {
  int C[k + 1];
  // 0 initialize
  for (int i = 0; i <= k; i++) {
    C[i] = 0;
  }
  // counting A_j
  for (int j = 1; j <= n; j++) {
    C[A[j]]++;
  }
  // counting less than A_j
  for (int i = 1; i <= k; i++) {
    C[i] = C[i] + C[i - 1];
  }
  // sorting
  for (int j = n; j >= 1; j--) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }
}

int max(int A[]) {
  int max = A[1];
  for (int i = 1; i <= n; i++) {
    if (A[i] > max)
      max = A[i];
  }
  return max;
}

int main() {
  scanf("%d", &n);
  int A[n + 1];
  int B[n + 1];
  for (int i = 1; i <= n; i++) {
    scanf("%d ", &A[i]);
  }

  counting_sort(A, B, max(A));
  for (int i = 1; i < n; i++) {
    printf("%d ", B[i]);
  }
  printf("%d\\n", B[n]);
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>
using namespace std;

int a[2000005], b[2000005], c[2000005];
int max = 10001;

void cs(int n) {
  memset(c, 0, sizeof(c));
  for (int i = 0; i < n; i++) {
    c[a[i]]++;
  }
  for (int i = 1; i < max; i++)
    c[i] += c[i - 1];
  for (int i = n - 1; i >= 0; i--) {
    b[c[a[i]]] = a[i];
    c[a[i]]--;
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  cs(n);
  for (int i = 1; i <= n; i++) {
    if (i == 1)
      printf("%d", b[i]);
    else
      printf(" %d", b[i]);
  }
  printf("\\n");
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <string.h>
short a[100005];
short b[100005];
short c[2000001];
int main() {
  int n;
  memset(a, 0, sizeof(a));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%hd", &c[i]);
    a[c[i]]++;
  }
  for (int i = 1; i <= 100001; i++) {
    a[i] = a[i] + a[i - 1];
  }
  for (int i = n; i > 0; i--) {
    b[a[c[i]]] = c[i];
    a[c[i]]--;
  }
  printf("%d", b[1]);
  for (int i = 2; i <= n; i++) {
    printf(" %hd", b[i]);
  }
  printf("\\n");
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

const int MAX = 10010;

void countingSort(std::vector<int> &A, std::vector<int> &sorted) {
  int count[MAX];
  int n = A.size();
  memset(count, 0, sizeof(count));
  for (int i = 0; i < n; i++)
    count[A[i]]++;
  for (int i = 0; i < MAX; i++)
    count[i + 1] += count[i];
  for (int i = n - 1; i >= 0; i--)
    sorted[--count[A[i]]] = A[i];
}

int main(void) {
  int n;
  std::cin >> n;
  std::vector<int> A(n);
  for (int i = 0; i < n; i++)
    std::cin >> A[i];
  std::vector<int> sorted(n);
  countingSort(A, sorted);
  for (int i = 0; i < n; i++)
    std::cout << (i ? " " : "") << sorted[i];
  std::cout << std::endl;

  return 0;
}


Error: -11

Fixed submission:

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

const int MAX = 10010;

void countingSort(std::vector<int> &A, std::vector<int> &sorted) {
  int count[MAX];
  int n = A.size();
  memset(count, 0, sizeof(count));
  for (int i = 0; i < n; i++)
    count[A[i]]++;
  for (int i = 0; i < MAX; i++)
    count[i + 1] += count[i];
  for (int i = n - 1; i >= 0; i++)
    sorted[--count[A[i]]] = A[i];
}

int main(void) {
  int n;
  std::cin >> n;
  std::vector<int> A(n);
  for (int i = 0; i < n; i++)
    std::cin >> A[i];
  std::vector<int> sorted(n);
  countingSort(A, sorted);
  for (int i = 0; i < n; i++)
    std::cout << (i ? " " : "") << sorted[i];
  std::cout << std::endl;

  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define N 100000
using namespace std;
int n;
int A[2000010], B[2000010];

void Csort() {
  int cnt[N] = {};
  for (int i = 0; i < n; i++)
    cnt[A[i]]++;
  for (int i = 1; i < N; i++)
    cnt[i] += cnt[i - 1];
  for (int i = 0; i < n; i++)
    B[cnt[A[i]]--] = A[i];
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  Csort();
  for (int i = 1; i <= n; i++) {
    if (i > 1)
      printf(" ");
    printf("%d", B[i]);
  }
  cout << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define N 1010
using namespace std;
int n;
int A[2000010], B[2000010];

void Csort() {
  int cnt[1010] = {};
  for (int i = 0; i < n; i++)
    cnt[A[i]]++;
  for (int i = 1; i < N; i++)
    cnt[i] += cnt[i - 1];
  for (int i = 0; i < n; i++)
    B[cnt[A[i]]--] = A[i];
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  Csort();
  for (int i = 1; i <= n; i++) {
    if (i > 1)
      printf(" ");
    printf("%d", B[i]);
  }
  cout << endl;
  return 0;
}