Problem p03955 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <set>
#include <vector>
using namespace std;

#define mp make_pair

// 1-origin
template <typename T> struct FenwickTree {
  vector<T> data;
  FenwickTree(int n) : data(n + 1, 0) {}
  // data[i] += x
  void add(int i, T x) {
    while (i <= (int)data.size()) {
      data[i] += x;
      i += i & -i;
    }
  }
  //[1, i)
  T sum(int i) {
    T res = 0;
    while (i > 0) {
      res += data[i];
      i -= i & -i;
    }
    return res;
  }
  //[l, r)
  T sum(int l, int r) { return sum(r) - sum(l); }
};
long long InversionNumber(const vector<int> &a) {
  int n = a.size();
  FenwickTree<long long> ft(n + 10);
  long long res = 0;
  for (int i = 0; i < a.size(); i++) {
    res += i - ft.sum(a[i]);
    ft.add(a[i], 1);
  }
  return res;
}

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<int>> a(3, vector<int>(n));
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < n; j++)
      scanf("%d", &a[i][j]);
  vector<vector<int>> b(n, vector<int>(3));
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < n; j++)
      b[j][i] = a[i][j];
  multiset<pair<pair<int, int>, int>> st;
  for (int i = 0; i < n; i++)
    st.insert(mp(mp(b[i][0], b[i][1]), b[i][2]));
  for (int i = 1; i < 3 * n; i += 3) {
    int a = i, b = i + 1, c = i + 2;
    if (!((st.count(mp(mp(a, b), c)) == 0 && st.count(mp(mp(c, b), a)) == 1) ||
          (st.count(mp(mp(a, b), c)) == 1 && st.count(mp(mp(c, b), a)) == 0))) {
      return !puts("No");
    }
  }
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    c[i] = b[i][1] / 3 + 1;
    if (b[i][0] > b[i][2])
      c[i] *= -1;
  }
  vector<int> target(n);
  for (int i = 0; i < n; i++)
    target[i] = i + 1;
  for (int i = 3; i < n; i += 4) {
    c[i] *= -1, target[i] *= -1;
    c[i - 1] *= -1, target[i - 1] *= -1;
  }
  set<int> evenst;
  for (int i = 0; i < n; i += 2)
    evenst.insert(abs(c[i]));
  for (int i = 0; i < n; i += 2)
    if (evenst.count(abs(target[i])) != 1)
      return !puts("No");
  evenst.clear();
  int flip_even = 0, flip_odd = 0;
  for (int i = 0; i < n; i += 2)
    evenst.insert(c[i]);
  for (int i = 0; i < n; i += 2)
    if (!evenst.count(target[i]))
      flip_even++;
  set<int> oddst;
  for (int i = 1; i < n; i += 2)
    oddst.insert(c[i]);
  for (int i = 1; i < n; i += 2)
    if (!oddst.count(target[i]))
      flip_odd++;
  vector<int> even, odd;
  for (int i = 0; i < n; i++) {
    if (i & 1)
      odd.push_back(abs(c[i]));
    else
      even.push_back(abs(c[i]));
  }
  auto even_rev = InversionNumber(even);
  auto odd_rev = InversionNumber(odd);
  puts((flip_even % 2 == odd_rev % 2 && flip_odd % 2 == even_rev % 2) ? "Yes"
                                                                      : "No");
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <set>
#include <vector>
using namespace std;

#define mp make_pair

// 1-origin
template <typename T> struct FenwickTree {
  vector<T> data;
  FenwickTree(int n) : data(n + 1, 0) {}
  // data[i] += x
  void add(int i, T x) {
    while (i <= (int)data.size()) {
      data[i] += x;
      i += i & -i;
    }
  }
  //[1, i)
  T sum(int i) {
    T res = 0;
    while (i > 0) {
      res += data[i];
      i -= i & -i;
    }
    return res;
  }
  //[l, r)
  T sum(int l, int r) { return sum(r) - sum(l); }
};
long long InversionNumber(const vector<int> &a) {
  FenwickTree<long long> ft(300030);
  long long res = 0;
  for (int i = 0; i < a.size(); i++) {
    res += i - ft.sum(a[i]);
    ft.add(a[i], 1);
  }
  return res;
}

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<int>> a(3, vector<int>(n));
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < n; j++)
      scanf("%d", &a[i][j]);
  vector<vector<int>> b(n, vector<int>(3));
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < n; j++)
      b[j][i] = a[i][j];
  multiset<pair<pair<int, int>, int>> st;
  for (int i = 0; i < n; i++)
    st.insert(mp(mp(b[i][0], b[i][1]), b[i][2]));
  for (int i = 1; i < 3 * n; i += 3) {
    int a = i, b = i + 1, c = i + 2;
    if (!((st.count(mp(mp(a, b), c)) == 0 && st.count(mp(mp(c, b), a)) == 1) ||
          (st.count(mp(mp(a, b), c)) == 1 && st.count(mp(mp(c, b), a)) == 0))) {
      return !puts("No");
    }
  }
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    c[i] = b[i][1] / 3 + 1;
    if (b[i][0] > b[i][2])
      c[i] *= -1;
  }
  vector<int> target(n);
  for (int i = 0; i < n; i++)
    target[i] = i + 1;
  for (int i = 3; i < n; i += 4) {
    c[i] *= -1, target[i] *= -1;
    c[i - 1] *= -1, target[i - 1] *= -1;
  }
  set<int> evenst;
  for (int i = 0; i < n; i += 2)
    evenst.insert(abs(c[i]));
  for (int i = 0; i < n; i += 2)
    if (evenst.count(abs(target[i])) != 1)
      return !puts("No");
  evenst.clear();
  int flip_even = 0, flip_odd = 0;
  for (int i = 0; i < n; i += 2)
    evenst.insert(c[i]);
  for (int i = 0; i < n; i += 2)
    if (!evenst.count(target[i]))
      flip_even++;
  set<int> oddst;
  for (int i = 1; i < n; i += 2)
    oddst.insert(c[i]);
  for (int i = 1; i < n; i += 2)
    if (!oddst.count(target[i]))
      flip_odd++;
  vector<int> even, odd;
  for (int i = 0; i < n; i++) {
    if (i & 1)
      odd.push_back(abs(c[i]));
    else
      even.push_back(abs(c[i]));
  }
  auto even_rev = InversionNumber(even);
  auto odd_rev = InversionNumber(odd);
  puts((flip_even % 2 == odd_rev % 2 && flip_odd % 2 == even_rev % 2) ? "Yes"
                                                                      : "No");
  return 0;
}