Problem p03271 (C++)

Buggy submission (status: Runtime Error):
// Author -- Frame

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

#define lowbit(x) ((x) & (-x))
#define Finline __inline__ __attribute__((always_inline))
#define DEBUG                                                                  \
  fprintf(stderr, "Running on Line %d in Function %s\n", __LINE__, __FUNCTION__)

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int inf = 0x3f3f3f3f, Inf = 0x7fffffff;
const ll INF = 0x7fffffffffffffff;
const double eps = 1e-10;

template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) {
  return (!b) ? a : gcd(b, a % b);
}
template <typename _Tp> Finline _Tp abs(const _Tp &a) {
  return a >= 0 ? a : -a;
}
template <typename _Tp> Finline _Tp max(const _Tp &a, const _Tp &b) {
  return a < b ? b : a;
}
template <typename _Tp> Finline _Tp min(const _Tp &a, const _Tp &b) {
  return a < b ? a : b;
}
template <typename _Tp> Finline void chmax(_Tp &a, const _Tp &b) {
  (a < b) && (a = b);
}
template <typename _Tp> Finline void chmin(_Tp &a, const _Tp &b) {
  (b < a) && (a = b);
}
template <typename _Tp> Finline bool _cmp(const _Tp &a, const _Tp &b) {
  return abs(a - b) <= eps;
}
template <typename _Tp> Finline void read(_Tp &x) {
  register char ch(getchar());
  bool f(false);
  while (ch < 48 || ch > 57)
    f |= ch == 45, ch = getchar();
  x = ch & 15, ch = getchar();
  while (ch >= 48 && ch <= 57)
    x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();
  if (f)
    x = -x;
}
template <typename _Tp, typename... Args>
Finline void read(_Tp &t, Args &...args) {
  read(t);
  read(args...);
}
Finline int read_str(char *s) {
  register char ch(getchar());
  while (ch == ' ' || ch == '\r' || ch == '\n')
    ch = getchar();
  register char *tar = s;
  *tar = ch, ch = getchar();
  while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF)
    *(++tar) = ch, ch = getchar();
  return tar - s + 1;
}

const int N = 100005;
std::vector<int> v, v1, v2;
template <typename _Tp> class BIT {
public:
  _Tp c[N];
  Finline void add(int x, _Tp C) {
    ++x;
    for (; x; x -= lowbit(x))
      c[x] += C;
  }
  Finline _Tp sum(int x) {
    ++x;
    _Tp ans = 0;
    for (; x < N; x += lowbit(x))
      ans += c[x];
    return ans;
  }
};
BIT<int> tr;
ll calc(std::vector<int> v) {
  memset(tr.c, 0, sizeof(tr.c));
  ll ans = 0;
  for (auto it : v) {
    ans += tr.sum(it);
    tr.add(it, 1);
  }
  return ans;
}
int main() {
  int n;
  read(n);
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    read(v[i]);
    if (i & 1)
      v1.push_back(v[i]);
    else
      v2.push_back(v[i]);
    if ((v[i] & 1) == (i & 1)) {
      printf("No\n");
      return 0;
    }
  }
  if ((calc(v1) + calc(v2)) * 3 == calc(v)) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }
  return 0;
}


Fixed submission:
// Author -- Frame

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

#define lowbit(x) ((x) & (-x))
#define Finline __inline__ __attribute__((always_inline))
#define DEBUG                                                                  \
  fprintf(stderr, "Running on Line %d in Function %s\n", __LINE__, __FUNCTION__)

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int inf = 0x3f3f3f3f, Inf = 0x7fffffff;
const ll INF = 0x7fffffffffffffff;
const double eps = 1e-10;

template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) {
  return (!b) ? a : gcd(b, a % b);
}
template <typename _Tp> Finline _Tp abs(const _Tp &a) {
  return a >= 0 ? a : -a;
}
template <typename _Tp> Finline _Tp max(const _Tp &a, const _Tp &b) {
  return a < b ? b : a;
}
template <typename _Tp> Finline _Tp min(const _Tp &a, const _Tp &b) {
  return a < b ? a : b;
}
template <typename _Tp> Finline void chmax(_Tp &a, const _Tp &b) {
  (a < b) && (a = b);
}
template <typename _Tp> Finline void chmin(_Tp &a, const _Tp &b) {
  (b < a) && (a = b);
}
template <typename _Tp> Finline bool _cmp(const _Tp &a, const _Tp &b) {
  return abs(a - b) <= eps;
}
template <typename _Tp> Finline void read(_Tp &x) {
  register char ch(getchar());
  bool f(false);
  while (ch < 48 || ch > 57)
    f |= ch == 45, ch = getchar();
  x = ch & 15, ch = getchar();
  while (ch >= 48 && ch <= 57)
    x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();
  if (f)
    x = -x;
}
template <typename _Tp, typename... Args>
Finline void read(_Tp &t, Args &...args) {
  read(t);
  read(args...);
}
Finline int read_str(char *s) {
  register char ch(getchar());
  while (ch == ' ' || ch == '\r' || ch == '\n')
    ch = getchar();
  register char *tar = s;
  *tar = ch, ch = getchar();
  while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF)
    *(++tar) = ch, ch = getchar();
  return tar - s + 1;
}

const int N = 300005;
std::vector<int> v, v1, v2;
template <typename _Tp> class BIT {
public:
  _Tp c[N];
  Finline void add(int x, _Tp C) {
    ++x;
    for (; x; x -= lowbit(x))
      c[x] += C;
  }
  Finline _Tp sum(int x) {
    ++x;
    _Tp ans = 0;
    for (; x < N; x += lowbit(x))
      ans += c[x];
    return ans;
  }
};
BIT<int> tr;
ll calc(std::vector<int> v) {
  memset(tr.c, 0, sizeof(tr.c));
  ll ans = 0;
  for (auto it : v) {
    ans += tr.sum(it);
    tr.add(it, 1);
  }
  return ans;
}
int main() {
  int n;
  read(n);
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    read(v[i]);
    if (i & 1)
      v1.push_back(v[i]);
    else
      v2.push_back(v[i]);
    if ((v[i] & 1) == (i & 1)) {
      printf("No\n");
      return 0;
    }
  }
  if ((calc(v1) + calc(v2)) * 3 == calc(v)) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }
  return 0;
}