alexjercan/bugnet huggingface dataset:
Problem p02273 (C++) — buggy submission, verdict: Runtime Error

#include <math.h>
#include <stdio.h>

struct dot {
  double x;
  double y;
};

void koch(struct dot d1, struct dot d2, int n) {
  if (n == 0) {
    return;
  }

  struct dot s;
  s.x = d1.x * 2 / 3 + d2.x * 1 / 3;
  s.y = d1.y * 2 / 3 + d2.y * 1 / 3;

  struct dot t;
  t.x = d1.x * 1 / 3 + d2.x * 2 / 3;
  t.y = d1.y * 1 / 3 + d2.y * 2 / 3;

  struct dot u;
  u.x = ((d2.x - d1.x) - sqrt(3) * (d2.y - d1.y)) / 6 + d1.x * 2 / 3 +
        d2.x * 1 / 3;
  u.y = (sqrt(3) * (d2.x - d1.x) + (d2.y - d1.y)) / 6 + d1.y * 2 / 3 +
        d2.y * 1 / 3;

  koch(d1, s, n - 1);
  printf("%.8f %.8f\\n", s.x, s.y);
  koch(s, u, n - 1);
  printf("%.8f %.8f\\n", u.x, u.y);
  koch(u, t, n - 1);
  printf("%.8f %.8f\\n", t.x, t.y);
  koch(t, d2, n - 1);
}

int main(void) {
  int N;
  scanf("%d", &N);

  struct dot p1;
  p1.x = 0.0;
  p1.y = 0.0;
  struct dot p2;
  p2.x = 100.0;
  p2.y = 0.0;

  printf("%.8f %.8f\\n", p1.x, p1.y);
  koch(p1, p2, N);
  printf("%.8f %.8f\\n", p2.x, p2.y);

  return 0;
}


Error: WA

Fixed submission:

#include <math.h>
#include <stdio.h>

struct dot {
  double x;
  double y;
};

void koch(struct dot d1, struct dot d2, int n) {
  if (n == 0) {
    return;
  }

  struct dot s;
  s.x = d1.x * 2 / 3 + d2.x * 1 / 3;
  s.y = d1.y * 2 / 3 + d2.y * 1 / 3;

  struct dot t;
  t.x = d1.x * 1 / 3 + d2.x * 2 / 3;
  t.y = d1.y * 1 / 3 + d2.y * 2 / 3;

  struct dot u;
  u.x = ((d2.x - d1.x) - sqrt(3) * (d2.y - d1.y)) / 6 + d1.x * 2 / 3 +
        d2.x * 1 / 3;
  u.y = (sqrt(3) * (d2.x - d1.x) + (d2.y - d1.y)) / 6 + d2.y * 2 / 3 +
        d2.y * 1 / 3;

  koch(d1, s, n - 1);
  printf("%f %f\\n", s.x, s.y);
  koch(s, u, n - 1);
  printf("%f %f\\n", u.x, u.y);
  koch(u, t, n - 1);
  printf("%f %f\\n", t.x, t.y);
  koch(t, d2, n - 1);
}

int main(void) {
  int N;
  scanf("%d", &N);

  struct dot p1;
  p1.x = 0.0;
  p1.y = 0.0;
  struct dot p2;
  p2.x = 100.0;
  p2.y = 0.0;

  printf("%f %f\\n", p1.x, p1.y);
  koch(p1, p2, N - 1);
  printf("%f %f\\n", p2.x, p2.y);

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

#include <iomanip>
#include <iostream>
#include <math.h>
using namespace std;
struct Point {
  double x;
  double y;
  Point(double _x, double _y) : x(_x), y(_y) {}
  Point operator+(const Point &a) const { return Point(x + a.x, y + a.y); }
  Point operator-(const Point &a) const { return Point(x - a.x, y - a.y); }
  Point operator*(const int r) const { return Point(x * r, y * r); }
  Point operator/(const int r) const { return Point(x / r, y / r); }
  void print() const {
    cout << fixed << std::setprecision(6) << x << " " << y << std::endl;
  }
};
Point computeTriangleTop(const Point s, const Point t) {
  Point a = t - s;
  const double pi = 3.14159265359879;
  const double cos60 = cos(pi / 3);
  const double sin60 = sin(pi / 3);
  return s + Point(cos60 * a.x - sin60 * a.y, sin60 * a.x + cos60 * a.y);
}
void KochCurve(int level, int n, Point p1, Point p2) {
  if (level == n) {
    p2.print();
    return;
  }
  Point s = (p1 * 2 + p2) / 3;
  Point t = (p1 + p2 * 2) / 3;
  Point u = computeTriangleTop(s, t);
  KochCurve(level + 1, n, p1, s);
  KochCurve(level + 1, n, s, u);
  KochCurve(level + 1, n, u, t);
  KochCurve(level + 1, n, t, p2);
}

int main() {
  int n;
  cin >> n;
  Point P1(0, 0), P2(100, 0);
  P1.print();
  ::KochCurve(0, n, P1, P2);
}

Error: WA

Fixed submission:

#include <iomanip>
#include <iostream>
#include <math.h>
using namespace std;
struct Point {
  double x;
  double y;
  Point(double _x, double _y) : x(_x), y(_y) {}
  Point operator+(const Point &a) const { return Point(x + a.x, y + a.y); }
  Point operator-(const Point &a) const { return Point(x - a.x, y - a.y); }
  Point operator*(const int r) const { return Point(x * r, y * r); }
  Point operator/(const int r) const { return Point(x / r, y / r); }
  void print() const {
    cout << fixed << std::setprecision(6) << x << " " << y << std::endl;
  }
};
Point computeTriangleTop(const Point s, const Point t) {
  Point a = t - s;
  const double pi = 3.14159265359879;
  const double cos60 = cos(pi / 3);
  const double sin60 = sin(pi / 3);
  return s + Point(cos60 * a.x - sin60 * a.y, sin60 * a.x + cos60 * a.y);
}
void KochCurve(int level, int n, Point p1, Point p2) {
  Point s = (p1 * 2 + p2) / 3;
  Point t = (p1 + p2 * 2) / 3;
  Point u = computeTriangleTop(s, t);
  if (level + 1 == n) {
    s.print();
    u.print();
    t.print();
    p2.print();
    return;
  }
  KochCurve(level + 1, n, p1, s);
  KochCurve(level + 1, n, s, u);
  KochCurve(level + 1, n, u, t);
  KochCurve(level + 1, n, t, p2);
}

int main() {
  int n;
  cin >> n;
  Point P1(0, 0), P2(100, 0);
  P1.print();
  ::KochCurve(0, n, P1, P2);
}
Problem p02273 (C++) — buggy submission, verdict: Runtime Error

#include <math.h>
#include <stdio.h>

struct Point {
  double x, y;
};

void koch(int n, Point a, Point b) {
  if (n == 0)
    return;
  Point a1, b1, ab;
  double th = M_PI * 60.0 / 180.0;

  a1.x = (2.0 * a.x + 1.0 * b.x) / 3.0;
  a1.y = (2.0 * a.y + 1.0 * b.y) / 3.0;

  b1.x = (1.0 * a.x + b.x * 2.0) / 3.0;
  b1.y = (1.0 * a.y + b.y * 2.0) / 3.0;

  ab.x = (b1.x - a1.x) * cos(th) - (b1.y - a1.y) * sin(th) + a1.x;
  ab.y = (b1.x - a1.x) * sin(th) + (b1.y - a1.y) * cos(th) + a1.y;

  koch(n - 1, a, a1);
  printf("%.8f %.8f\\n", a1.x, a1.y);
  koch(n - 1, a1, ab);
  printf("%.8f %.8f\\n", ab.x, ab.y);
  koch(n - 1, ab, b1);
  printf("%.8f %.8f\\n", b1.x, b1.y);
  koch(n - 1, b1, b);
}

int main() {
  Point a, b;
  int n;

  scanf("%d", &n);

  a.x = 0;
  a.y = 0;
  b.x = 100;
  b.y = 0;

  printf("%.8f %.8f\\n", a.x, a.y);
  koch(n, a, b);
  printf("%.8f %.8f\\n", b.x, b.y);

  return 0;
}


Error: -11

Fixed submission:

#include <math.h>
#include <stdio.h>

struct Point {
  double x, y;
};

void koch(int n, Point a, Point b) {
  Point a1, b1, ab;
  double th = M_PI * 60.0 / 180.0;

  a1.x = (2.0 * a.x + 1.0 * b.x) / 3.0;
  a1.y = (2.0 * a.y + 1.0 * b.y) / 3.0;

  b1.x = (1.0 * a.x + b.x * 2.0) / 3.0;
  b1.y = (1.0 * a.y + b.y * 2.0) / 3.0;

  ab.x = (b1.x - a1.x) * cos(th) - (b1.y - a1.y) * sin(th) + a1.x;
  ab.y = (b1.x - a1.x) * sin(th) - (b1.y - a1.y) * cos(th) + a1.y;

  koch(n - 1, a, a1);
  printf("%.8f %.8f\\n", a1.x, a1.y);
  koch(n - 1, a1, ab);
  printf("%.8f %.8f\\n", ab.x, ab.y);
  koch(n - 1, ab, b1);
  printf("%.8f %.8f\\n", b1.x, b1.y);
  koch(n - 1, b1, b);
}

int main() {
  Point a, b;
  int n;

  scanf("%d", &n);

  a.x = 0;
  a.y = 0;
  b.x = 100;
  b.y = 0;

  printf("%.8f %.8f\\n", a.x, a.y);
  koch(n, a, b);
  printf("%.8f %.8f\\n", b.x, b.y);

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

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

const int MAX_N = 200000;
int BIT[MAX_N + 1];
int A[MAX_N], unzip[MAX_N];
int N;
map<int, int> zip;

int sum(int i) {
  i++;
  int s = 0;
  while (i > 0) {
    s += BIT[i];
    i -= i & -i;
  }
  return s;
}

void add(int i, int x) {
  i++;
  while (i <= N) {
    BIT[i] += x;
    i += i & -i;
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    unzip[i] = A[i];
  }

  sort(unzip, unzip + N);
  for (int i = 0; i < N; i++) {
    zip[unzip[i]] = i;
  }

  long long ans = 0;
  for (int j = 0; j < N; j++) {
    ans += j - sum(zip[A[j]]);
    add(zip[A[j]], 1);
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

const int MAX_N = 200000;
int BIT[MAX_N + 1];
int A[MAX_N];
int N;

int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += BIT[i];
    i -= i & -i;
  }
  return s;
}

void add(int i, int x) {
  while (i <= N) {
    BIT[i] += x;
    i += i & -i;
  }
}

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i)
    scanf("%d", A + i);

  long long ans = 0;
  for (int j = 0; j < N; j++) {
    ans += j - sum(A[j]);
    add(A[j], 1);
  }
  cout << ans << endl;

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

#include <climits>
#include <iostream>
int s[500001], l[250001], r[250001];
static long mg(int left, int m, int right) {
  int n1 = m - left, n2 = right - m, i = 0, j = 0;
  long c = 0;
  for (int k = 0; k < n1; ++k) {
    l[k] = s[left + k];
  }
  for (int k = 0; k < n2; ++k) {
    r[k] = s[m + k];
  }
  l[n1] = INT_MAX;
  r[n2] = INT_MAX;
  for (int k = left; k < right; ++k) {
    if (l[i] <= r[j]) {
      s[k] = l[i++];
    } else {
      s[k] = r[j++];
      c += n1 - i;
    }
  }
  return c;
}
static long msort(int left, int right) {
  long c = 0;
  if (left + 1 < right) {
    int m = left + (right - left) / 2;
    c += msort(left, m);
    c += msort(m, right);
    c += mg(left, m, right);
  }
  return c;
}
int main() {
  int n;
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::cin >> s[i];
  }
  long c = msort(0, n);
  std::cout << c << std::endl;
}


Error: TLE

Fixed submission:

#include <iostream>
int main() {
  int n, a[200001], b[200002];
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::cin >> a[i];
    b[i] = 0;
  }
  b[n] = 0;
  int c = 0;
  for (int i = n - 1; i >= 0; --i) {
    int x, y;
    x = y = a[i];
    while (x > 0) {
      c += b[x];
      x &= x - 1;
    }
    while (y <= n) {
      b[y] += 1;
      y += y & -y;
    }
  }
  std::cout << c << std::endl;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
struct Pair {
  Pair(const int &v = 0, const int &i = 0) : val(v), idx(i){};
  bool operator==(const Pair &other) const { return val == other.val; }
  bool operator<(const Pair &other) const { return val < other.val; }
  bool operator>(const Pair &other) const { return val > other.val; }
  bool operator<=(const Pair &other) const { return val <= other.val; }
  bool operator>=(const Pair &other) const { return val >= other.val; }
  int val, idx;
};
class BIT {
public:
  BIT(const int &n) : vector(n + 1){};
  int get_num(const int &idx) const {
    if (idx == 0) {
      return 0;
    } else {
      return vector.at(idx) + get_num(idx - last_bit(idx));
    }
  }
  void set_num(const int &idx) {
    if (idx < vector.size()) {
      ++vector.at(idx);
      set_num(idx + last_bit(idx));
    }
  }

private:
  int last_bit(const int &n) const { return (-n) & n; }
  std::vector<int> vector;
};
int main() {
  int n;
  std::cin >> n;
  BIT bit(n);
  std::vector<Pair> vector(n);
  for (auto i = 1; i <= n; ++i) {
    int a;
    std::cin >> a;
    vector.at(i - 1) = Pair(a, i);
  }
  sort(vector.begin(), vector.end());
  long long int sum = 0;
  for (auto i = n - 1; i >= 0; --i) {
    sum += bit.get_num(vector.at(i).idx);
    bit.set_num(vector.at(i).idx);
  }
  std::cout << sum << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

class BIT {
public:
  BIT(int n = 1) {
    int size = 1;
    while (n != 0) {
      size = size << 1;
      n = n >> 1;
    }
    vector = std::vector<int>(size + 1, 0);
  };
  int get_n(int n) {
    int result = 0;
    while (n != 0) {
      result += vector.at(n);
      n = left_n(n);
    }
    return result;
  }
  void set_n(int n, const int &v = 1) {
    while (n < vector.size()) {
      vector.at(n) += v;
      n = above_n(n);
    }
  }

private:
  int left_n(const int &n) { return n - (n & (-n)); }
  int above_n(const int &n) { return n + (n & (-n)); }
  std::vector<int> vector;
};

int main() {
  int n;
  std::cin >> n;
  int sum = 0;
  BIT bit(n);
  for (auto i = 0; i < n; ++i) {
    int a;
    std::cin >> a;
    bit.set_n(n - a + 1);
    sum += bit.get_n(n - a);
  }
  std::cout << sum << std::endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int A[200100];
int W[200100];
int n;
int a;

long long int merge_and_count(int l, int r) {
  if (l + 1 >= r) {
    return 0;
  }
  if (l + 2 == r) {
    if (A[l] <= A[l + 1]) {
      return 0;
    }
    swap(A[l], A[l + 1]);
    return 1;
  }

  int m = (l + r) / 2;
  long long int cl = merge_and_count(l, m);
  long long int cr = merge_and_count(m, r);
  long long int c = 0;
  int x = l, y = m;
  int k = l;
  while (x < m && y < r) {
    if (A[x] <= A[y]) {
      W[k] = A[x];
      k = k + 1;
      x = x + 1;
    } else {
      W[k] = A[y];
      c = c + m - x;
      k = k + 1;
      y = y + 1;
    }
  }

  while (x < m) {
    W[k] = A[x];
    k = k + 1;
    x = x + 1;
  }
  while (y < r) {
    W[k] = A[y];
    k = k + 1;
    y = y + 1;
  }
  assert(k == r);
  copy(W + l, W + r, A + l);
  return cl + cr + c;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    A[i] = a;
  }
  cout << merge_and_count(0, n) << endl;
}

Error: TLE

Fixed submission:

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

int A[200100];
int n;
int a;

int count(int l, int r) {
  if (l + 1 >= r)
    return 0;
  if (l + 2 == r) {
    if (A[l] <= A[l + 1]) {
      return 0;
    }
    return 1;
  }

  int m = (l + r) / 2;
  int cl = count(l, m);
  int cr = count(m, r);
  int c = 0;
  int x = l, y = m;
  for (int x = l; x < m; x++) {
    for (int y = m; y < r; y++) {
      if (A[x] > A[y]) {
        c = c + 1;
      }
    }
  }

  return cl + cr + c;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    A[i] = a;
  }
  cout << count(0, n) << endl;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

#define 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 (i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  i = j = 0;
  for (k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
      cnt += n1 - i;
    }
  }
  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(void) {
  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:

#pragma warning(disable : 4996)
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

vector<int> a;
// int a[200000];
long long ans = 0;

/*????????¨?????¢??°1
void solve(int n) {
        for (int i = n - 2; i >= 0; i--) {
                int *point = lower_bound(a + i + 1, a + n, a[i]);
                ans += point - (a + i + 1);
                int temp = a[i];

                //sort
                for (int *j = a + i; j < a + n; j += 1) {
                        if ((j + 1) < point) {
                                *j = *(j + 1);
                        } else {
                                *j = temp;
                                break;
                        }
                }
        }
}
*/

void solve(int n) {
  for (vector<int>::iterator i = a.end() - 2; i >= a.begin(); i--) {
    vector<int>::iterator point = lower_bound(i + 1, a.end(), *i);
    int data = point - i - 1;
    if (data > 0) {
      ans += data;
      int temp = *i;
      a.erase(i);
      a.insert(point - 1, temp);
    }
  }
}

int main(void) {
  int n, num;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &num);
    a.push_back(num);
  }
  solve(n);
  printf("%ld\\n", ans);
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#define INF 1e15 + 1
typedef long long llong;

llong count;

void merge(int *A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[n1 + 1], R[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] = INF;

  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      j++;
      count += n1 - i;
    }
  }
}

int mergeSort(int *A, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(A, left, mid);
    mergeSort(A, mid, right);
    merge(A, left, mid, right);
  }
}

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

  count = 0;
  mergeSort(A, 0, n);

  printf("%lld\\n", count);
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#define INF 1 << 30

int count;

void merge(int *A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[n1 + 1], R[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] = INF;
  R[n2] = INF;

  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      for (int l = i; l < n1; l++) {
        if (R[j] < L[l])
          count++;
      }
      A[k] = R[j];
      j++;
    }
  }
}

int mergeSort(int *A, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(A, left, mid);
    mergeSort(A, mid, right);
    merge(A, left, mid, right);
  }
}

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

  count = 0;
  mergeSort(A, 0, n);

  printf("%d\\n", count);
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

const long MAX = 200000;
const long SENTINEL = 2000000000;

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

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

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

int main() {
  long A[MAX], n;
  cin >> n;
  for (long 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 <algorithm>
#include <iostream>
using namespace std;

long bubbleSort(long A[], long n) {
  long cnt = 0;
  for (long i = 0; i < n; i++) {
    for (long j = n - 1; j >= i + 1; j--) {
      if (A[j] < A[j - 1]) {
        swap(A[j], A[j - 1]);
        cnt++;
      }
    }
  }
  return cnt;
}

int main() {
  long n, a[200000];
  cin >> n;
  for (long i = 0; i < n; i++)
    cin >> a[i];
  cout << bubbleSort(a, n) << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;

ll /*A[2000000],*/ cnt = 0;
int A[210000];
int L[110001];
int R[110001];

int merge(int l, int m, int r) {
  int n1 = m - l, n2 = r - m, i, j;
  // ll L[n1 + 1], R[n2 + 1];
  for (i = 0; i < n1; i++)
    L[i] = A[i + l];
  for (i = 0; i < n2; i++)
    R[i] = A[i + m];
  i = j = 0;
  for (int k = 0; k < r - l; k++) {
    if (j == n2 || (i < n1 && L[i] <= R[j]))
      A[k + l] = L[i++];
    else
      A[k + l] = R[j++], cnt += n1 - i;
  }
}

int mergeSort(int l, int r) {
  if (l + 1 < r) {
    int m = (l + r) / 2;
    mergeSort(l, m);
    mergeSort(m, r);
    merge(l, m, r);
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  mergeSort(0, n);
  printf("%lld\\n", cnt);

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
long long ll;

int S = 0;
long long A[20000], cnt = 0;

int merge(int l, int m, int r) {
  long long n1 = m - l, n2 = r - m, i, j;
  int L[n1 + 1], R[n2 + 1];
  for (i = 0; i < n1; i++)
    L[i] = A[i + l];
  for (i = 0; i < n2; i++)
    R[i] = A[i + m];
  i = j = 0;
  for (int k = 0; k < r - l; k++, S++) {
    if (j == n2 || (i < n1 && L[i] <= R[j]))
      A[k + l] = L[i++];
    else
      A[k + l] = R[j++], cnt += n1 - i;
  }
}

int mergeSort(int l, int r) {
  if (l + 1 < r) {
    int m = (l + r) / 2;
    mergeSort(l, m);
    mergeSort(m, r);
    merge(l, m, r);
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  mergeSort(0, n);
  printf("%d\\n", cnt);

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

#include <cstdio>
#include <iostream>

using namespace std;

int INFTY = 1000000001;

long long merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;

  int L[n1];
  int R[n2];

  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] = INFTY;
  R[n2] = INFTY;

  int i = 0;
  int j = 0;

  long long cnt = 0;

  for (int k = left; k < right; ++k) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      ++i;
    } else {
      A[k] = R[j];
      ++j;
      cnt += n1 - i;
    }
  }
  return cnt;
}

long long mergeSort(int A[], int left, int right) {

  if (left + 1 < right) {
    int mid = (left + right) / 2;

    long long v1 = mergeSort(A, left, mid);
    long long v2 = mergeSort(A, mid, right);
    long long v3 = merge(A, left, mid, right);

    return v1 + v2 + v3;
  } else {
    return 0;
  }
}

int main() {

  int n;
  scanf("%d", &n);

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

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

  cout << v << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int main() {

  int n;
  scanf("%d", &n);

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

  int result = 0;
  for (int i = n - 1; i >= 0; --i) {
    for (int j = 0; j < i; ++j) {
      if (A[j] > A[i]) {
        ++result;
      }
    }
  }

  cout << result << endl;

  return 0;
}