Problem p03483 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

class BinaryIndexedTree {
  vector<int64_t> container;

public:
  BinaryIndexedTree(size_t size) : container(size + 1) {
    fill(container.begin(), container.end(), 0);
  }
  void add(size_t i, int64_t v) {
    size_t idx = i + 1;
    while (idx < container.size()) {
      container[idx] += v;
      idx += idx & -idx;
    }
  }
  int64_t find(size_t i) {
    size_t idx = i;

    int64_t result = 0;
    while (idx != 0) {
      result = container[idx] + result;
      idx -= idx & -idx;
    }
    return result;
  }
};

int main() {
  string s;
  cin >> s;
  const int n = s.size();

  vector<int> cnt(26);

  rep(i, n) { cnt[s[i] - 'a']++; }

  char center = '.';
  rep(i, 26) {
    if (cnt[i] % 2 != 0) {
      if (center != '.') {
        cout << -1 << endl;
        return 0;
      } else {
        center = i + 'a';
      }
    }
  }

  vector<int> v;

  vector<int> tcnt(26);
  string t, u;
  rep(i, n) {
    int j = s[i] - 'a';
    if (s[i] == center) {
      if (tcnt[j] < cnt[j] / 2) {
        t.push_back(s[i]);
        v.push_back(0);
      } else if (tcnt[j] > cnt[j]) {
        t.push_back(s[i]);
        v.push_back(2);
      } else {
        v.push_back(1);
      }
    } else {
      if (tcnt[j] < cnt[j] / 2) {
        t.push_back(s[i]);
        v.push_back(0);
      } else {
        u.push_back(s[i]);
        v.push_back(2);
      }
    }
    tcnt[j]++;
  }
  reverse(u.begin(), u.end());

  BinaryIndexedTree bit1(3);
  int64_t noi1 = 0;
  rep(i, n) {
    noi1 += bit1.find(3) - bit1.find(v[i] + 1);
    bit1.add(v[i], 1);
  }

  vector<vector<int>> idx(26);
  rrep(i, u.size()) { idx[u[i] - 'a'].push_back(i); }

  vector<int> x;
  rep(i, t.size()) {
    x.push_back(idx[t[i] - 'a'].back());
    idx[t[i] - 'a'].pop_back();
  }

  BinaryIndexedTree bit2(x.size());
  int64_t noi2 = 0;
  rep(i, x.size()) {
    noi2 += bit2.find(x.size()) - bit2.find(x[i] + 1);
    bit2.add(x[i], 1);
  }

  cout << noi1 + noi2 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

class BinaryIndexedTree {
  vector<int64_t> container;

public:
  BinaryIndexedTree(size_t size) : container(size + 1) {
    fill(container.begin(), container.end(), 0);
  }
  void add(size_t i, int64_t v) {
    size_t idx = i + 1;
    while (idx < container.size()) {
      container[idx] += v;
      idx += idx & -idx;
    }
  }
  int64_t find(size_t i) {
    size_t idx = i;

    int64_t result = 0;
    while (idx != 0) {
      result = container[idx] + result;
      idx -= idx & -idx;
    }
    return result;
  }
};

int main() {
  string s;
  cin >> s;
  const int n = s.size();

  vector<int> cnt(26);

  rep(i, n) { cnt[s[i] - 'a']++; }

  char center = '.';
  rep(i, 26) {
    if (cnt[i] % 2 != 0) {
      if (center != '.') {
        cout << -1 << endl;
        return 0;
      } else {
        center = i + 'a';
      }
    }
  }

  vector<int> v;

  vector<int> tcnt(26);
  string t, u;
  rep(i, n) {
    int j = s[i] - 'a';
    if (s[i] == center) {
      if (tcnt[j] < cnt[j] / 2) {
        t.push_back(s[i]);
        v.push_back(0);
      } else if (tcnt[j] > cnt[j] / 2) {
        u.push_back(s[i]);
        v.push_back(2);
      } else {
        v.push_back(1);
      }
    } else {
      if (tcnt[j] < cnt[j] / 2) {
        t.push_back(s[i]);
        v.push_back(0);
      } else {
        u.push_back(s[i]);
        v.push_back(2);
      }
    }
    tcnt[j]++;
  }
  reverse(u.begin(), u.end());

  BinaryIndexedTree bit1(3);
  int64_t noi1 = 0;
  rep(i, n) {
    noi1 += bit1.find(3) - bit1.find(v[i] + 1);
    bit1.add(v[i], 1);
  }

  vector<vector<int>> idx(26);
  rrep(i, u.size()) { idx[u[i] - 'a'].push_back(i); }

  vector<int> x;
  rep(i, t.size()) {
    x.push_back(idx[t[i] - 'a'].back());
    idx[t[i] - 'a'].pop_back();
  }

  BinaryIndexedTree bit2(x.size());
  int64_t noi2 = 0;
  rep(i, x.size()) {
    noi2 += bit2.find(x.size()) - bit2.find(x[i] + 1);
    bit2.add(x[i], 1);
  }

  cout << noi1 + noi2 << endl;

  return 0;
}