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

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

const int DIM = 1e1 + 5;

vector<int> lst;
int arr[DIM], par[DIM];

int main(void) {

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    lst.push_back(arr[i]);
  }

  sort(lst.begin(), lst.end());

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    arr[i] =
        (int)(lower_bound(lst.begin(), lst.end(), arr[i]) - lst.begin() + 1);

    if (arr[i] % 2 == 0 && i % 2 == 1)
      ans++;
  }

  cout << ans << endl;
  return 0;
}

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

const int DIM = 1e5 + 5;

vector<int> lst;
int arr[DIM], par[DIM];

int main(void) {

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    lst.push_back(arr[i]);
  }

  sort(lst.begin(), lst.end());

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    arr[i] =
        (int)(lower_bound(lst.begin(), lst.end(), arr[i]) - lst.begin() + 1);

    if (arr[i] % 2 == 0 && i % 2 == 1)
      ans++;
  }

  cout << ans << endl;
  return 0;
}
Problem p04021 (C++)

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

using namespace std;

long long a[100001];
long long b[100001];
int u[100001];

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++) {
    u[b[i]] = i % 2;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += abs((i % 2) - u[a[i]]);
  }
  cout << ans / 2;
  return 0;
}

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

using namespace std;

long long a[100001];
long long b[100001];
map<int, int> u;

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++) {
    u[b[i]] = i % 2;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += abs((i % 2) - u[a[i]]);
  }
  cout << ans / 2;
  return 0;
}
Problem p04021 (C++)

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

using namespace std;

int a[100001];
int b[100001];
int u[100001];

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++)
    u[b[i]] = i % 2;
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans += abs(i % 2 - u[a[i]]);
  cout << ans / 2;
  return 0;
}

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

using namespace std;

int a[100001];
int b[100001];
map<int, int> u;

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++)
    u[b[i]] = i % 2;
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans += abs(i % 2 - u[a[i]]);
  cout << ans / 2;
  return 0;
}
Problem p04021 (C++)

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

using namespace std;

int n, k;
int a[100001];
int b[100001];
int kol;
int pos[100001];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++) {
    pos[b[i]] = i % 2;
  }
  for (int i = 1; i <= n; i++) {
    if (i % 2 != pos[a[i]]) {
      kol++;
    }
  }
  cout << kol / 2;
  return 0;
}

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

using namespace std;

int n, k;
int a[100001];
int b[100001];
int kol;
map<int, int> pos;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++) {
    pos[b[i]] = i % 2;
  }
  for (int i = 1; i <= n; i++) {
    if (i % 2 != pos[a[i]]) {
      kol++;
    }
  }
  cout << kol / 2;
  return 0;
}
Problem p04021 (C++)

Buggy submission (status: Runtime Error):
// Do you knOW what it feels like?

// To be TorTured by your own MinD?

// I don't wanna feel the PAIN.

// I BeG you to KILL me, pleASE...

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

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("Os")

#define F first
#define S second
#define pb push_back
#define SZ(x) (ll)(x.size())
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;

const ll maxn = 1e5 + 10, mod = 1e9 + 7, inf = 1e18;

ll n, a[maxn], b[maxn], wer[maxn];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  cin >> n;
  ll tom = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++)
    wer[a[i]] = i;
  for (int i = 0; i < n; i++)
    tom += (wer[b[i]] + i) % 2;
  cout << tom / 2;

  return 0;
}


Fixed submission:
// Do you knOW what it feels like?

// To be TorTured by your own MinD?

// I don't wanna feel the PAIN.

// I BeG you to KILL me, pleASE...

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

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("Os")

#define F first
#define S second
#define pb push_back
#define SZ(x) (ll)(x.size())
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;

const ll maxn = 1e5 + 10, mod = 1e9 + 7, inf = 1e18;

ll n, a[maxn], b[maxn];
map<ll, ll> wer;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  cin >> n;
  ll tom = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++)
    wer[a[i]] = i;
  for (int i = 0; i < n; i++)
    tom += (wer[b[i]] + i) % 2;
  cout << tom / 2;

  return 0;
}
Problem p04021 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define int long long
int mod = 1e9 + 7;

signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> b = a;
  sort(b.begin(), b.end());
  vector<int> c(1e9 + 1);
  for (int i = 0; i < n; i++) {
    c[b[i]] = i;
  }
  vector<int> ans(n);
  for (int i = 0; i < n; i++)
    ans[i] = c[a[i]];
  int x = 0;
  for (int i = 0; i < n; i += 2) {
    if (ans[i] % 2 == 1)
      x++;
  }
  cout << x << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define int long long
int mod = 1e9 + 7;

signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> b = a;
  sort(b.begin(), b.end());
  map<int, int> c;
  for (int i = 0; i < n; i++) {
    c[b[i]] = i;
  }
  vector<int> ans(n);
  for (int i = 0; i < n; i++)
    ans[i] = c[a[i]];
  int x = 0;
  for (int i = 0; i < n; i += 2) {
    if (ans[i] % 2 == 1)
      x++;
  }
  cout << x << endl;
}
Problem p04021 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define INF (INT_MAX / 3)
#define PSB push_back
#define PN 1000000007
typedef long long LL;
// #define int long long

// booooooooooooost
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;	//多倍長整数(mp::cpp_int)

int inv_mod(int a, int m) {
  int b, x, u, q, tmp;

  b = m;
  x = 1;
  u = 0;
  while (b > 0) {
    q = a / b;
    tmp = u;
    u = x - q * u;
    x = tmp;
    tmp = b;
    b = a - q * b;
    a = tmp;
  }

  return (x < 0) ? m + x : x;
}

signed main() {

  int N;
  vector<int> A, SA;
  cin >> N;
  REP(i, N) {
    int tmp;
    cin >> tmp;
    A.PSB(tmp);
  }
  SA = A;
  sort(ALL(SA));

  int ans = 0;
  REP(i, N) {
    REP(j, N) {
      if (SA[i] == A[j])
        ans += abs(i - j) % 2;
    }
  }
  cout << ans / 2 << endl;

  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define INF (INT_MAX / 3)
#define PSB push_back
#define PN 1000000007
typedef long long LL;
// #define int long long

// booooooooooooost
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;	//多倍長整数(mp::cpp_int)

int inv_mod(int a, int m) {
  int b, x, u, q, tmp;

  b = m;
  x = 1;
  u = 0;
  while (b > 0) {
    q = a / b;
    tmp = u;
    u = x - q * u;
    x = tmp;
    tmp = b;
    b = a - q * b;
    a = tmp;
  }

  return (x < 0) ? m + x : x;
}

signed main() {

  int N;
  vector<int> A, SA;
  cin >> N;
  REP(i, N) {
    int tmp;
    cin >> tmp;
    A.PSB(tmp);
  }
  SA = A;
  sort(ALL(SA));

  int ans = 0;
  REP(i, N) {
    int l = 0, r = N, m;
    while (l <= r) {
      m = (l + r) / 2;
      if (SA[m] == A[i]) {
        ans += abs(m - i) % 2;
        break;
      } else if (SA[m] < A[i])
        l = m + 1;
      else
        r = m - 1;
    }
  }
  cout << ans / 2 << endl;

  return 0;
}
Problem p04021 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<bool> f(n + 1);
  rep(i, n) {
    cin >> a[i];
    f[a[i]] = i % 2;
  }
  sort(all(a));

  ll ans = 0;
  rep(i, n) {
    if (f[a[i]] && i % 2 == 0)
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<bool> f(1e9 + 100);
  rep(i, n) {
    cin >> a[i];
    f[a[i]] = i % 2;
  }
  sort(all(a));

  ll ans = 0;
  rep(i, n) {
    if (f[a[i]] && i % 2 == 0)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p04021 (C++)

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

#define int long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reps(i, n) for (int(i) = 1; (i) <= (n); (i)++)
typedef vector<int> vint;
typedef pair<int, int> pint;
const double PI = 3.141592653589793238462643383279;
const int DY[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int DX[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template <typename T, typename U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T, typename U> inline bool chmin(T &a, const U &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
struct Init {
  Init() { cout << fixed << setprecision(20); };
} init;

int N;
int A[110000];
signed main() {
  cin >> N;
  rep(i, N) { cin >> A[i]; }
  vint a, b;
  for (int i = 0; i < N; i += 2) {
    a.push_back(A[i]);
  }
  sort(a.begin(), a.end());
  sort(A, A + N);
  for (int i = 0; i < N; i += 2) {
    b.push_back(A[i]);
  }
  b.push_back(LLONG_MAX);
  int j = 0, n = 0;
  rep(i, N) {
    while (a[i] > b[j]) {
      j++;
    }
    if (a[i] == b[j]) {
      n++, j++;
    }
  }
  cout << a.size() - n << endl;
}

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

#define int long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reps(i, n) for (int(i) = 1; (i) <= (n); (i)++)
typedef vector<int> vint;
typedef pair<int, int> pint;
const double PI = 3.141592653589793238462643383279;
const int DY[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int DX[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template <typename T, typename U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T, typename U> inline bool chmin(T &a, const U &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
struct Init {
  Init() { cout << fixed << setprecision(20); };
} init;

int N;
int A[110000];
signed main() {
  cin >> N;
  rep(i, N) { cin >> A[i]; }
  vint a, b;
  for (int i = 0; i < N; i += 2) {
    a.push_back(A[i]);
  }
  sort(a.begin(), a.end());
  sort(A, A + N);
  for (int i = 0; i < N; i += 2) {
    b.push_back(A[i]);
  }
  b.push_back(LLONG_MAX);
  int j = 0, n = 0;
  rep(i, a.size()) {
    while (a[i] > b[j]) {
      j++;
    }
    if (a[i] == b[j]) {
      n++, j++;
    }
  }
  cout << a.size() - n << endl;
}
Problem p04021 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_cxx;

int main() {
  int n;
  cin >> n;
  int arr[n];
  int ind = 0;
  while (ind < n)
    cin >> arr[ind++];
  int op1 = 0;
  vector<int> arr2;
  for (int i = 0; i < n; ++i)
    arr2.push_back(arr[i]);
  sort(arr2.begin(), arr2.end());
  for (int i = 0; i < n; ++i) {
    auto bs = find(arr2.begin(), arr2.end(), arr[i]);

    int x = (bs - arr2.begin());
    if (i % 2 != x % 2)
      op1++;
  }
  cout << op1 / 2 << "\n";
}

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

int main() {
  int n;
  cin >> n;
  int arr[n];
  int ind = 0;
  while (ind < n)
    cin >> arr[ind++];
  int op1 = 0;
  vector<int> arr2;
  for (int i = 0; i < n; ++i)
    arr2.push_back(arr[i]);
  sort(arr2.begin(), arr2.end());
  for (int i = 0; i < n; ++i) {
    auto bs = lower_bound(arr2.begin(), arr2.end(), arr[i]);

    int x = (bs - arr2.begin());
    if (i % 2 != x % 2)
      op1++;
  }
  cout << op1 / 2 << "\n";
}
Problem p04021 (C++)

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

int main() {
  int n;
  cin >> n;
  int a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b, b + n);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int j = find(a, a + n, b[i]) - a;
    int d = abs(i - j);
    if (d % 2 != 0) {
      ++ans;
    }
  }
  cout << ans / 2 << endl;
}

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

int main() {
  int n;
  cin >> n;
  int a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b, b + n);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int j = lower_bound(b, b + n, a[i]) - b;
    int d = abs(i - j);
    if (d % 2 != 0) {
      ++ans;
    }
  }
  cout << ans / 2 << endl;
}
Problem p04021 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define all(a) (a).begin(), (a).end()

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

using ll = long long;
using namespace std;

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
};

ll N;
ll A[100010];
std::vector<ll> crds;
std::map<ll, ll> mp;
bool fl[100010];

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

  rep(i, N) {
    scanf("%lld", A + i);

    crds.emplace_back(A[i]);
  }

  std::sort(all(crds));

  rep(i, N) mp[crds[i]] = i;

  rep(i, N) fl[mp[A[i]]] = i & 1;

  ll cnt = 0;
  rep(i, N) cnt += (fl[crds[i]] != (i & 1));

  printf("%lld\n", cnt / 2);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define all(a) (a).begin(), (a).end()

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

using ll = long long;
using namespace std;

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
};

ll N;
ll A[100010];
std::vector<ll> crds;
std::map<ll, ll> mp;
bool fl[100010];

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

  rep(i, N) {
    scanf("%lld", A + i);

    crds.emplace_back(A[i]);
  }

  std::sort(all(crds));

  rep(i, N) mp[crds[i]] = i;

  rep(i, N) fl[mp[A[i]]] = i & 1;

  ll cnt = 0;
  rep(i, N) cnt += (fl[mp[crds[i]]] != (i & 1));

  printf("%lld\n", cnt / 2);

  return 0;
}
Problem p04021 (C++)

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

using namespace std;

int const N = 1e5 + 41;

int n, a[N], ans, b[N];

int getPos(int v) {
  int l = -1;
  int r = n;
  while (r - l > 1) {
    int m = (l + r);
    if (b[m] <= v) {
      l = m;
    } else {
      r = m;
    }
  }
  return l;
}

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

  sort(b, b + n);
  for (int i = 0; i < n; ++i) {
    if ((i % 2) != getPos(a[i]) % 2) {
      ++ans;
    }
  }
  ans /= 2;
  printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

int const N = 1e5 + 41;

int n, a[N], ans, b[N];

int getPos(int v) {
  int l = -1;
  int r = n;
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (b[m] <= v) {
      l = m;
    } else {
      r = m;
    }
  }
  return l;
}

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

  sort(b, b + n);
  for (int i = 0; i < n; ++i) {
    if ((i % 2) != getPos(a[i]) % 2) {
      ++ans;
    }
  }
  ans /= 2;
  printf("%d\n", ans);

  return 0;
}
Problem p04021 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define ll long long
#define INF 2147483647
using namespace std;
using Graph = vector<vector<int>>;

int main() {
  int n, ans = 0;
  cin >> n;
  int a[n], moto[n], zaatu[n];
  rep(i, n) {
    cin >> a[i];
    moto[i] = a[i];
  }
  sort(a, a + n);
  map<int, int> mp;
  rep(i, n) { mp[a[i]] = i; }
  rep(i, n) { zaatu[i] = mp[moto[i]]; }
  rep(i, n) {
    int x = zaatu[i];
    if (x % 2 != i % 2) {
      ans++;
    }
  }
  cout << ans / 2;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define ll long long
#define INF 2147483647
using namespace std;
using Graph = vector<vector<int>>;

int main() {
  int n, ans = 0;
  cin >> n;
  int a[n], moto[n], zaatu[n];
  rep(i, n) {
    cin >> a[i];
    moto[i] = a[i];
  }
  sort(a, a + n);
  map<int, int> mp;
  rep(i, n) { mp[a[i]] = i; }
  rep(i, n) { zaatu[i] = mp[moto[i]]; }
  rep(i, n) {
    int x = zaatu[i];
    if (x % 2 != i % 2) {
      ans++;
    }
  }
  cout << ans / 2;
}
Problem p04021 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define AS(arr) (sizeof(arr) / sizeof(arr[0]))
#define ALL(c) (c).begin(), (c).end()
#define SIZE(a) int((a).size())
#define EACH(it, c)                                                            \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
typedef vector<int> VI;
typedef vector<string> VS;
typedef long long LL;
int a[100005];
int b[100005];

int main(void) {
  int N;
  cin >> N;
  long long ret = 0;
  REP(i, N) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(&b[0], &b[N]);
  REP(i, N) {
    REP(j, N) {
      if (a[i] == b[j]) {
        if ((abs(i - j) % 2) == 1)
          ret++;
        break;
      }
    }
  }
  ret /= 2;
  cout << ret << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define AS(arr) (sizeof(arr) / sizeof(arr[0]))
#define ALL(c) (c).begin(), (c).end()
#define SIZE(a) int((a).size())
#define EACH(it, c)                                                            \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
typedef vector<int> VI;
typedef vector<string> VS;
typedef long long LL;
int a[100005];
int b[100005];

int main(void) {
  int N;
  cin >> N;
  long long ret = 0;
  REP(i, N) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(&b[0], &b[N]);
  REP(i, N) {
    auto it = lower_bound(&b[0], &b[N], a[i]);
    if (abs(it - &a[i]) % 2 == 1)
      ret++;
  }
  ret /= 2;
  cout << ret << endl;
}
Problem p04021 (C++)

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

using namespace std;

typedef long long int ll;
typedef pair<int, int> ii;

const int N = 1e5 + 5;

int n, a[N], b[N], c[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    c[a[i]] = i;
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++)
    if ((i - c[b[i]]) % 2)
      ans++;
  cout << ans / 2 << endl;
}

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

using namespace std;

typedef long long int ll;
typedef pair<int, int> ii;

const int N = 1e5 + 5;

int n, a[N], b[N];
map<int, int> c;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    c[a[i]] = i;
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++)
    if ((i - c[b[i]]) % 2)
      ans++;
  cout << ans / 2 << endl;
}
Problem p04021 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define MP make_pair
#define F first
#define PB push_back
#define S second
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 50;
const int mod = 1e9 + 7;
const int inf = (1 << 30);
int p[maxn], a[maxn], b[maxn], c[maxn];

int main() {
  ios_base::sync_with_stdio(false);
  int n, tmp1 = 0, tmp2 = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    if (i % 2)
      cin >> a[tmp1++];
    else
      cin >> b[tmp2++];
  }
  sort(a, a + tmp1);
  sort(b, b + tmp2);
  for (int i = 0; i < n; i++) {
    if (i % 2)
      c[i] = a[i / 2];
    else
      c[i] = b[i / 2];
  }
  sort(c, c + n);
  for (int i = 0; i < n; i++)
    p[c[i]] = i;
  ll ans = 0;
  for (int i = 0; i < tmp1; i++)
    if (p[a[i]] % 2 == 0)
      ans++;
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define MP make_pair
#define F first
#define PB push_back
#define S second
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 50;
const int mod = 1e9 + 7;
const int inf = (1 << 30);
int a[maxn], b[maxn], c[maxn];
map<int, int> p;

int main() {
  ios_base::sync_with_stdio(false);
  int n, tmp1 = 0, tmp2 = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    if (i % 2)
      cin >> a[tmp1++];
    else
      cin >> b[tmp2++];
  }
  sort(a, a + tmp1);
  sort(b, b + tmp2);
  for (int i = 0; i < n; i++) {
    if (i % 2)
      c[i] = a[i / 2];
    else
      c[i] = b[i / 2];
  }
  sort(c, c + n);
  for (int i = 0; i < n; i++)
    p[c[i]] = i;
  ll ans = 0;
  for (int i = 0; i < tmp1; i++)
    if (p[a[i]] % 2 == 0)
      ans++;
  cout << ans << endl;
}
Problem p04022 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
typedef long long LL;
const LL mod = 1e9 + 7;
// head
const int maxn = 1e5 + 5;
int tot, n, p[maxn], ans, res;
bool vis[maxn], f[maxn];
LL a[maxn], b[maxn];
map<LL, int> Map;
map<LL, bool> pr;

int main() {
  cin >> n;
  rep(i, 2, 1e5) {
    if (!vis[i])
      p[++tot] = i, pr[i] = 1;
    for (int j = 1; j <= tot && p[j] * i <= 1e5; j++) {
      vis[i * p[j]] = 1;
      if (i % p[j] == 0)
        break;
    }
  }
  rep(i, 1, n) {
    cin >> a[i];
    LL x = a[i];
    a[i] = b[i] = 1;
    rep(j, 1, tot) if (x % p[j] == 0) {
      int cnt = 0;
      while (x % p[j] == 0)
        cnt++, x /= p[j];
      cnt = cnt % 3;
      if (cnt == 1)
        a[i] *= p[j], b[i] *= p[j] * p[j];
      if (cnt == 2)
        a[i] *= p[j] * p[j], b[i] *= p[j];
    }
    if (x > 1) {
      LL s = sqrt(x);
      if (s * s == x)
        a[i] *= x, b[i] *= s, Map[a[i]]++;
      else if (x <= 1e5 && pr[x])
        a[i] *= x, b[i] *= x * x, Map[a[i]]++;
      else
        f[i] = 1, ans++;
    } else
      Map[a[i]]++;
  }
  ans += (Map[1] > 0) ? 1 : 0;
  rep(i, 1, n) {
    if (f[i])
      continue;
    if (a[i] == 1)
      continue;
    if (Map[a[i]] > Map[b[i]])
      ans++;
    if (Map[a[i]] == Map[b[i]])
      res++;
  }
  cout << ans + res / 2 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
typedef long long LL;
const LL mod = 1e9 + 7;
// head
const int maxn = 1e5 + 5;
int tot, n, p[maxn], ans, res;
bool vis[maxn], f[maxn];
LL a[maxn], b[maxn];
map<LL, int> Map;
map<LL, bool> pr;

int main() {
  cin >> n;
  rep(i, 2, 1e5) {
    if (!vis[i])
      p[++tot] = i, pr[i] = 1;
    for (int j = 1; j <= tot && p[j] * i <= 1e5; j++) {
      vis[i * p[j]] = 1;
      if (i % p[j] == 0)
        break;
    }
  }
  rep(i, 1, n) {
    cin >> a[i];
    LL x = a[i];
    a[i] = b[i] = 1;
    rep(j, 1, 300) if (x % p[j] == 0) {
      int cnt = 0;
      while (x % p[j] == 0)
        cnt++, x /= p[j];
      cnt = cnt % 3;
      if (cnt == 1)
        a[i] *= p[j], b[i] *= p[j] * p[j];
      if (cnt == 2)
        a[i] *= p[j] * p[j], b[i] *= p[j];
    }
    if (x > 1) {
      LL s = sqrt(x);
      if (s * s == x)
        a[i] *= x, b[i] *= s, Map[a[i]]++;
      else if (x <= 1e5 && pr[x])
        a[i] *= x, b[i] *= x * x, Map[a[i]]++;
      else
        f[i] = 1, ans++;
    } else
      Map[a[i]]++;
  }
  ans += (Map[1] > 0) ? 1 : 0;
  rep(i, 1, n) {
    if (f[i])
      continue;
    if (a[i] == 1)
      continue;
    if (Map[a[i]] > Map[b[i]])
      ans++;
    if (Map[a[i]] == Map[b[i]])
      res++;
  }
  cout << ans + res / 2 << endl;
}
Problem p04022 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
typedef vector<ll> vct;
int n, cnt, sz = 0;
ll a[100007], b[100007], x, pr[3000];
map<ll, int> USS;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    for (ll j = 2; j * j * j <= x; j++)
      while (x % (pr[j] * pr[j] * pr[j]) == 0)
        x /= pr[j] * pr[j] * pr[j];
    USS[x]++;
    a[i] = x;
    ll y = 1;
    for (ll j = 2; j * j * j <= x; j++) {
      if (x % j == 0) {
        y *= j;
        if (x % (j * j) != 0)
          y *= j;
        while (x % j == 0)
          x /= j;
      }
    }
    ll x1 = sqrt(x);
    if (x1 * x1 == x)
      y *= x1;
    else
      y *= x * x;
    b[i] = y;
  }
  if (USS[1]) {
    cnt++;
    USS[1] = 0;
  }
  for (int i = 0; i < n; i++) {
    cnt += max(USS[a[i]], USS[b[i]]);
    USS[a[i]] = USS[b[i]] = 0;
  }
  cout << cnt;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
typedef vector<ll> vct;
int n, cnt, sz = 0;
ll a[100007], b[100007], x, pr[3000];
map<ll, int> USS;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    for (ll j = 2; j * j * j <= x; j++)
      while (x % (j * j * j) == 0)
        x /= j * j * j;
    USS[x]++;
    a[i] = x;
    ll y = 1;
    for (ll j = 2; j * j * j <= x; j++) {
      if (x % j == 0) {
        y *= j;
        if (x % (j * j) != 0)
          y *= j;
        while (x % j == 0)
          x /= j;
      }
    }
    ll x1 = sqrt(x);
    if (x1 * x1 == x)
      y *= x1;
    else
      y *= x * x;
    b[i] = y;
  }
  if (USS[1]) {
    cnt++;
    USS[1] = 0;
  }
  for (int i = 0; i < n; i++) {
    cnt += max(USS[a[i]], USS[b[i]]);
    USS[a[i]] = USS[b[i]] = 0;
  }
  cout << cnt;
}
Problem p04022 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fo(i, a, b) for (int i = (a); i < (b); i++)
#define MOD 1000000007
#define MP make_pair
#define PB push_back
typedef long long ll;

int N, ans;
ll a[100100];
map<ll, int> num;
map<ll, ll> sq;
vector<ll> p;
vector<pair<ll, ll>> cand;
bool d[100100];

int main() {

  fo(i, 2, 100100) if (!d[i]) {
    p.PB(i);
    for (int j = i + i; j < 100100; j += i)
      d[j] = 1;
    sq[1ll * i * i] = i;
  }

  scanf("%d", &N);

  fo(i, 0, N) {
    scanf("%lld", &a[i]);

    ll m = 1ll, l = a[i];

    for (ll j : p) {
      if (j > a[i])
        break;
      int n = 0;
      while (a[i] % j == 0)
        a[i] /= j, l /= j, n++;
      fo(x, 0, n % 3) l *= j;
      if (n % 3)
        fo(x, 0, 3 - (n % 3)) m *= j;
    }

    num[l]++;
    if (num[l] >= 2)
      continue;

    if (sq.count(a[i])) {
      m *= sq[a[i]];
    } else {
      if (m <= ((ll)1e12) / a[i])
        m *= a[i];
      else
        m = 0;
      if (m <= ((ll)1e12) / a[i])
        m *= a[i];
      else
        m = 0;
    }

    cand.PB(MP(l, m));
  }

  if (num[1ll])
    ans++, num[1ll] = 0;
  for (auto i : cand) {
    if (!num[i.first])
      continue;
    ans += max(num[i.first], num[i.second]);
    num[i.first] = num[i.second] = 0;
  }

  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fo(i, a, b) for (int i = (a); i < (b); i++)
#define MOD 1000000007
#define MP make_pair
#define PB push_back
typedef long long ll;

int N, ans;
ll a[100100];
map<ll, int> num;
map<ll, ll> sq;
vector<ll> p;
vector<pair<ll, ll>> cand;
bool d[100100];

int main() {

  fo(i, 2, 100100) if (!d[i]) {
    p.PB(i);
    for (int j = i + i; j < 100100; j += i)
      d[j] = 1;
    sq[1ll * i * i] = i;
  }

  scanf("%d", &N);

  fo(i, 0, N) {
    scanf("%lld", &a[i]);

    ll m = 1ll, l = a[i];

    for (ll j : p) {
      if (j > 2200 || j > a[i])
        break;
      int n = 0;
      while (a[i] % j == 0)
        a[i] /= j, l /= j, n++;
      fo(x, 0, n % 3) l *= j;
      if (n % 3)
        fo(x, 0, 3 - (n % 3)) m *= j;
    }

    num[l]++;
    if (num[l] >= 2)
      continue;

    if (sq.count(a[i])) {
      m *= sq[a[i]];
    } else {
      if (m <= ((ll)1e12) / a[i])
        m *= a[i];
      else
        m = 0;
      if (m <= ((ll)1e12) / a[i])
        m *= a[i];
      else
        m = 0;
    }

    cand.PB(MP(l, m));
  }

  if (num[1ll])
    ans++, num[1ll] = 0;
  for (auto i : cand) {
    if (!num[i.first])
      continue;
    ans += max(num[i.first], num[i.second]);
    num[i.first] = num[i.second] = 0;
  }

  printf("%d\n", ans);
  return 0;
}
Problem p04022 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define L long long
#define pl pair<L, L>
#define aa first
#define bb second
using namespace std;
int n, w[3000], q, p;
L a[100010];
pl x[100010], y;
map<pl, int> f;
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int i, j, k;
  for (i = 2; i <= 2155; i++) {
    for (j = 2; j * j <= i; j++)
      if (i % j == 0)
        break;
    if (j * j > i)
      w[++q] = i;
  }
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    x[i].aa = x[i].bb = 1;
    for (j = 1; j <= q && w[j] <= a[i]; j++)
      if (a[i] % w[j] == 0) {
        for (k = 0; a[i] % w[j] == 0; k++, a[i] /= w[j])
          ;
        if (k % 3 == 1)
          x[i].aa *= w[j];
        if (k % 3 == 2)
          x[i].bb *= w[j];
      }
    if (a[i] > 1) {
      k = sqrt(a[i]) - 2;
      for (j = k; j <= k + 4; j++)
        if (a[i] == (L)j * j)
          break;
      if (j <= k + 4)
        x[i].bb *= w[j];
      else if (a[i] > 2155 * 2155) {
        p++;
        continue;
      } else
        x[i].aa *= a[i];
    }
    f[x[i]]++;
  }
  for (i = 1; i <= n; i++)
    if (x[i].aa == 1 && x[i].bb == 1) {
      if (f[x[i]])
        p++;
      f[x[i]] = 0;
    } else {
      y.aa = x[i].bb;
      y.bb = x[i].aa;
      p += max(f[x[i]], f[y]);
      f[x[i]] = f[y] = 0;
    }
  printf("%d\n", p);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define L long long
#define pl pair<L, L>
#define aa first
#define bb second
using namespace std;
int n, w[3000], q, p;
L a[100010];
pl x[100010], y;
map<pl, int> f;
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int i, j, k;
  for (i = 2; i <= 2155; i++) {
    for (j = 2; j * j <= i; j++)
      if (i % j == 0)
        break;
    if (j * j > i)
      w[++q] = i;
  }
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    x[i].aa = x[i].bb = 1;
    for (j = 1; j <= q && w[j] <= a[i]; j++)
      if (a[i] % w[j] == 0) {
        for (k = 0; a[i] % w[j] == 0; k++, a[i] /= w[j])
          ;
        if (k % 3 == 1)
          x[i].aa *= w[j];
        if (k % 3 == 2)
          x[i].bb *= w[j];
      }
    if (a[i] > 1) {
      k = sqrt(a[i]) - 2;
      for (j = k; j <= k + 4; j++)
        if (a[i] == (L)j * j)
          break;
      if (j <= k + 4)
        x[i].bb *= j;
      else if (a[i] > 2155 * 2155) {
        p++;
        continue;
      } else
        x[i].aa *= a[i];
    }
    f[x[i]]++;
  }
  for (i = 1; i <= n; i++)
    if (x[i].aa == 1 && x[i].bb == 1) {
      if (f[x[i]])
        p++;
      f[x[i]] = 0;
    } else {
      y.aa = x[i].bb;
      y.bb = x[i].aa;
      p += max(f[x[i]], f[y]);
      f[x[i]] = f[y] = 0;
    }
  printf("%d\n", p);
  return 0;
}
Problem p04022 (C++)

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

int n, tot, ans;
int p[3333];
bool tag[3333];
ll a[111111], b[111111];
map<ll, int> m;

void init() {
  for (int i = 2; i < 3333; i++) {
    if (!tag[i])
      p[tot++] = i;
    for (int j = 0; j < tot && i * p[j] < 3333; j++) {
      tag[i * p[j]] = 1;
      if (i % p[j] == 0)
        break;
    }
  }
}

void pre(int id) {
  ll x = 1, y = 1;
  for (int i = 0; p[i] * p[i] * p[i] <= a[id]; i++) {
    int cnt = 0;
    while (a[id] % p[i] == 0)
      cnt++, a[id] /= p[i];
    cnt %= 3;
    if (cnt == 1)
      x *= p[i], y *= p[i] * p[i];
    else if (cnt == 2)
      x *= p[i] * p[i], y *= p[i];
  }
  if (a[id] != 1) {
    x *= a[id];
    ll sq = (ll)sqrt(a[id]);
    if (sq * sq == a[id])
      y *= sq;
    else
      y *= a[id] * a[id];
  }
  a[id] = x, b[id] = y, m[x]++;
}

int main() {
  init();
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i], pre(i);
  if (m[1])
    ans++, m[1] = 0;
  for (int i = 0; i < n; i++)
    if (m[a[i]])
      ans += max(m[a[i]], m[b[i]]), m[a[i]] = m[b[i]] = 0;
  cout << ans;
  return 0;
}


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

int n, tot, ans;
ll p[3333];
bool tag[3333];
ll a[111111], b[111111];
map<ll, int> m;

void init() {
  for (int i = 2; i < 3333; i++) {
    if (!tag[i])
      p[tot++] = i;
    for (int j = 0; j < tot && i * p[j] < 3333; j++) {
      tag[i * p[j]] = 1;
      if (i % p[j] == 0)
        break;
    }
  }
}

void pre(int id) {
  ll x = 1, y = 1;
  for (int i = 0; p[i] * p[i] * p[i] <= a[id]; i++) {
    int cnt = 0;
    while (a[id] % p[i] == 0)
      cnt++, a[id] /= p[i];
    cnt %= 3;
    if (cnt == 1)
      x *= p[i], y *= p[i] * p[i];
    else if (cnt == 2)
      x *= p[i] * p[i], y *= p[i];
  }
  if (a[id] != 1) {
    x *= a[id];
    ll sq = (ll)sqrt(a[id]);
    if (sq * sq == a[id])
      y *= sq;
    else
      y *= a[id] * a[id];
  }
  a[id] = x, b[id] = y, m[x]++;
}

int main() {
  init();
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i], pre(i);
  if (m[1])
    ans++, m[1] = 0;
  for (int i = 0; i < n; i++)
    if (m[a[i]])
      ans += max(m[a[i]], m[b[i]]), m[a[i]] = m[b[i]] = 0;
  cout << ans;
  return 0;
}
Problem p04022 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#define fi first
#define se second
#define ll long long
using namespace std;
ll n, s, ans;
map<ll, ll> Map, D, vis;
int main() {
  scanf("%lld", &n);
  for (ll i = 1; i <= n; i++) {
    scanf("%lld", &s);
    ll nw = 1, de = 1;
    for (ll j = 1, p = 0; j * j * j <= s; j++, p = 0) {
      while (s % j == 0)
        s /= j, p++;
      p %= 3;
      if (p)
        nw *= pow(j, p), de *= pow(j, 3 - p);
    }
    if (((int)sqrt(s)) * ((int)sqrt(s)) == s)
      de *= sqrt(s);
    else
      de *= s * s;
    nw *= s;
    D[de] = nw, D[nw] = de, Map[nw]++;
  }
  for (auto x : Map)
    if (!vis[x.fi] && x.fi != 1) {
      ans += max(x.se, Map[D[x.fi]]);
      vis[x.fi] = vis[D[x.fi]] = 1;
    }
  cout << ans + (Map[1] > 0) << endl;
  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#define fi first
#define se second
#define ll long long
using namespace std;
ll n, s, ans;
map<ll, ll> Map, D, vis;
int main() {
  scanf("%lld", &n);
  for (ll i = 1; i <= n; i++) {
    scanf("%lld", &s);
    ll nw = 1, de = 1;
    for (ll j = 2, p = 0; j * j * j <= s; j++, p = 0) {
      while (s % j == 0)
        s /= j, p++;
      p %= 3;
      if (p)
        nw *= pow(j, p), de *= pow(j, 3 - p);
    }
    if (((int)sqrt(s)) * ((int)sqrt(s)) == s)
      de *= sqrt(s);
    else
      de *= s * s;
    nw *= s;
    D[de] = nw, D[nw] = de, Map[nw]++;
  }
  for (auto x : Map)
    if (!vis[x.fi] && x.fi != 1) {
      ans += max(x.se, Map[D[x.fi]]);
      vis[x.fi] = vis[D[x.fi]] = 1;
    }
  cout << ans + (Map[1] > 0) << endl;
  return 0;
}
Problem p04023 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 100005;
typedef long long ll;
int n, q, top;
ll a[MAXN], op[MAXN], t[MAXN], d[MAXN];
inline int getnext(ll k, int r) {
  int l = 1, mid, ret = -1;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (op[mid] <= k)
      l = mid + 1, ret = mid;
    else
      r = mid - 1;
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= q; ++i) {
    scanf("%lld", a + i);
    while (top && op[top] >= a[i])
      --top;
    op[++top] = a[i];
  }
  ll k;
  t[top] = 1;
  for (int i = top; i; --i) {
    k = op[i];
    while (k) {
      int p = getnext(k, i - 1);
      if (p == -1)
        break;
      t[p] += k / op[p] * t[i];
      k %= op[p];
    }
    d[1] += t[i], d[k + 1] -= t[i];
  }
  for (int i = 1; i <= n; ++i)
    printf("%lld\n", d[i] += d[i - 1]);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 100005;
typedef long long ll;
int n, q, top;
ll a[MAXN], op[MAXN], t[MAXN], d[MAXN];
inline int getnext(ll k, int r) {
  int l = 1, mid, ret = -1;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (op[mid] <= k)
      l = mid + 1, ret = mid;
    else
      r = mid - 1;
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &q);
  op[++top] = n;
  for (int i = 1; i <= q; ++i) {
    scanf("%lld", a + i);
    while (top && op[top] >= a[i])
      --top;
    op[++top] = a[i];
  }
  ll k;
  t[top] = 1;
  for (int i = top; i; --i) {
    k = op[i];
    while (k) {
      int p = getnext(k, i - 1);
      if (p == -1)
        break;
      t[p] += k / op[p] * t[i];
      k %= op[p];
    }
    d[1] += t[i], d[k + 1] -= t[i];
  }
  for (int i = 1; i <= n; ++i)
    printf("%lld\n", d[i] += d[i - 1]);
  return 0;
}
Problem p04023 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#define MAXN 100010
#define LL long long

int n, m;
LL a[MAXN], b[MAXN], ans[MAXN];
LL tag[MAXN];

void input() {
  int _m;
  scanf("%d%d", &n, &_m);
  while (_m--) {
    LL x;
    scanf("%lld", &x);
    while (m && a[m] >= x)
      m--;
    a[++m] = x;
  }
}

void gao(LL x, LL d) {
  if (x <= a[1]) {
    b[x] += d;
    return;
  }
  int l = 1, r = m;
  while (l < r) {
    int mid = (l + r + 1) >> 1;
    if (a[mid] < x)
      l = mid;
    else
      r = mid - 1;
  }
  tag[l] += d * (x / a[l]);
  gao(x % a[l], d);
}

int main() {
  input();
  tag[m] = 1;
  for (int i = m; i >= 1; i--)
    gao(a[i], tag[i]);
  LL s = 0;
  for (int i = n; i >= 1; i--)
    ans[i] = s += b[i];
  for (int i = 1; i <= n; i++)
    printf("%lld\n", ans[i]);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#define MAXN 100010
#define LL long long

int n, m;
LL a[MAXN], b[MAXN], ans[MAXN];
LL tag[MAXN];

void input() {
  int _m;
  scanf("%d%d", &n, &_m);
  a[++m] = n;
  while (_m--) {
    LL x;
    scanf("%lld", &x);
    while (m && a[m] >= x)
      m--;
    a[++m] = x;
  }
}

void gao(LL x, LL d) {
  if (x <= a[1]) {
    b[x] += d;
    return;
  }
  int l = 1, r = m;
  while (l < r) {
    int mid = (l + r + 1) >> 1;
    if (a[mid] < x)
      l = mid;
    else
      r = mid - 1;
  }
  tag[l] += d * (x / a[l]);
  gao(x % a[l], d);
}

int main() {
  input();
  tag[m] = 1;
  for (int i = m; i >= 1; i--)
    gao(a[i], tag[i]);
  LL s = 0;
  for (int i = n; i >= 1; i--)
    ans[i] = s += b[i];
  for (int i = 1; i <= n; i++)
    printf("%lld\n", ans[i]);
  return 0;
}
Problem p04023 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define N 200005
using namespace std;
int n, q, m;
long long Q, V[N], ans[N], a[N];
void CL(long long x, long long v) {
  int P;
  P = lower_bound(a + 1, a + m + 1, x) - a;
  if (x <= a[1]) {
    ans[1] += v;
    ans[x + 1] -= v;
    return;
  }
  V[P] += v * (x / a[P]);
  CL(x % a[P], v);
}
int main() {
  scanf("%d%d", &n, &q);
  m++;
  a[m] = n;
  for (int i = 1; i <= q; i++) {
    scanf("%lld", &Q);
    while (Q <= a[m])
      m--;
    a[++m] = Q;
  }
  V[m] = 1;
  for (int i = m; i > 1; i--) {
    V[i - 1] += V[i] * (a[i] / a[i - 1]);
    CL(a[i] % a[i - 1], V[i]);
  }
  ans[0] = 0;
  ans[1] += V[1];
  ans[a[1] + 1] -= V[1];
  for (int i = 1; i <= n; i++) {
    ans[i] += ans[i - 1];
    printf("%lld\n", ans[i]);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define N 200005
using namespace std;
int n, q, m;
long long Q, V[N], ans[N], a[N];
void CL(long long x, long long v) {
  int P;
  P = lower_bound(a + 1, a + m + 1, x) - a - 1;
  if (x <= a[1]) {
    ans[1] += v;
    ans[x + 1] -= v;
    return;
  }
  V[P] += v * (x / a[P]);
  CL(x % a[P], v);
}
int main() {
  scanf("%d%d", &n, &q);
  m++;
  a[m] = n;
  for (int i = 1; i <= q; i++) {
    scanf("%lld", &Q);
    while (Q <= a[m])
      m--;
    a[++m] = Q;
  }
  V[m] = 1;
  for (int i = m; i > 1; i--) {
    V[i - 1] += V[i] * (a[i] / a[i - 1]);
    CL(a[i] % a[i - 1], V[i]);
  }
  ans[0] = 0;
  ans[1] += V[1];
  ans[a[1] + 1] -= V[1];
  for (int i = 1; i <= n; i++) {
    ans[i] += ans[i - 1];
    printf("%lld\n", ans[i]);
  }
  return 0;
}
Problem p04023 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define F(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define D(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define LL long long
LL rd() {
  LL f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  LL x = 0;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}

const int N = 100005;

int n, m;
LL a[N], t[N], b[N];

void p(LL l, LL cnt) {
  if (l <= a[1])
    return b[l] += cnt, void();
  int y = lower_bound(a + 1, a + m + 1, l) - a;
  t[y - 1] += cnt * (l / a[y - 1]);
  p(l % a[y - 1], cnt);
}

int main() {
  freopen("003e.in", "r", stdin);

  a[++m] = n = rd();
  F(i, 1, rd()) {
    LL x = rd();
    while (m > 0 && x <= a[m])
      a[m--] = 0;
    a[++m] = x;
  }

  t[m] = 1;
  D(i, m, 1)
  p(a[i], t[i]);
  D(i, n, 1) b[i] += b[i + 1];
  F(i, 1, n) printf("%lld\n", b[i]);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define D(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define LL long long
LL rd() {
  LL f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  LL x = 0;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}

const int N = 100005;

int n, m;
LL a[N], t[N], b[N];

void p(LL l, LL cnt) {
  if (l <= a[1])
    return b[l] += cnt, void();
  int y = lower_bound(a + 1, a + m + 1, l) - a;
  t[y - 1] += cnt * (l / a[y - 1]);
  p(l % a[y - 1], cnt);
}

int main() {
  a[++m] = n = rd();
  F(i, 1, rd()) {
    LL x = rd();
    while (m > 0 && x <= a[m])
      a[m--] = 0;
    a[++m] = x;
  }

  t[m] = 1;
  D(i, m, 1)
  p(a[i], t[i]);
  D(i, n, 1) b[i] += b[i + 1];
  F(i, 1, n) printf("%lld\n", b[i]);

  return 0;
}