Problem p03483 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

const int INF = 0x3f3f3f3f, MOD = 1000000007;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
/*-----------------------------------------*/
template <typename Abelian> struct BIT {
  BIT(int sz_, const Abelian UNITY_ = 0) : UNITY(UNITY_) {
    sz = sz_ + 1;
    dat.assign(sz, UNITY);
  }

  void add(int idx, Abelian value) {
    while (idx < sz) {
      dat[idx] += value;
      idx += idx & -idx;
    }
  }

  Abelian sum(int idx) {
    Abelian res = UNITY;
    while (idx > 0) {
      res += dat[idx];
      idx -= idx & -idx;
    }
    return res;
  }

  int lower_bound(Abelian value) {
    if (value <= UNITY)
      return UNITY;
    int res = UNITY, exponent = 1;
    while (exponent < sz)
      exponent <<= 1;
    for (int mask = exponent >> 1; mask > 0; mask >>= 1) {
      if (res + mask < sz && dat[res + mask] < value) {
        value -= dat[res + mask];
        res += mask;
      }
    }
    return res + 1;
  }

private:
  int sz;
  const Abelian UNITY;
  vector<Abelian> dat;
};

template <typename T> long long inversion_number(const vector<T> &seq) {
  int sz = seq.size();
  vector<T> cp = seq;
  sort(ALL(cp));
  cp.erase(unique(ALL(cp)), cp.end());
  BIT<int> bit(cp.size());
  long long res = 0;
  REP(i, sz) {
    int idx = lower_bound(ALL(cp), seq[i]) - cp.begin() + 1;
    res += i - bit.sum(idx);
    bit.add(idx, 1);
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  string s;
  cin >> s;
  int n = s.length();
  vector<int> cnt(26, 0);
  for (char c : s)
    ++cnt[c - 'a'];
  string mae = "", ushiro = "";
  long long ans = 0;
  int migi = 0;
  REP(i, n) {
    if (cnt[s[i] - 'a'] == 1) {
      ans += migi;
      ++migi;
      --cnt[s[i] - 'a'];
    } else if (cnt[s[i] - 'a'] >= 2) {
      mae += s[i];
      ans += migi;
      cnt[s[i] - 'a'] -= 2;
    } else {
      ushiro += s[i];
      ++migi;
    }
  }
  reverse(ALL(mae));
  vector<vector<int>> pos(26);
  REP(i, n / 2) pos[mae[i] - 'a'].emplace_back(i);
  vector<int> idx(26, 0), check(n / 2);
  REP(i, n / 2) {
    int now = ushiro[i] - 'a';
    check[i] = pos[now][idx[now]];
    ++idx[now];
  }
  ans += inversion_number(check);
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

const int INF = 0x3f3f3f3f, MOD = 1000000007;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
/*-----------------------------------------*/
template <typename Abelian> struct BIT {
  BIT(int sz_, const Abelian UNITY_ = 0) : UNITY(UNITY_) {
    sz = sz_ + 1;
    dat.assign(sz, UNITY);
  }

  void add(int idx, Abelian value) {
    while (idx < sz) {
      dat[idx] += value;
      idx += idx & -idx;
    }
  }

  Abelian sum(int idx) {
    Abelian res = UNITY;
    while (idx > 0) {
      res += dat[idx];
      idx -= idx & -idx;
    }
    return res;
  }

  int lower_bound(Abelian value) {
    if (value <= UNITY)
      return UNITY;
    int res = UNITY, exponent = 1;
    while (exponent < sz)
      exponent <<= 1;
    for (int mask = exponent >> 1; mask > 0; mask >>= 1) {
      if (res + mask < sz && dat[res + mask] < value) {
        value -= dat[res + mask];
        res += mask;
      }
    }
    return res + 1;
  }

private:
  int sz;
  const Abelian UNITY;
  vector<Abelian> dat;
};

template <typename T> long long inversion_number(const vector<T> &seq) {
  int sz = seq.size();
  vector<T> cp = seq;
  sort(ALL(cp));
  cp.erase(unique(ALL(cp)), cp.end());
  BIT<int> bit(cp.size());
  long long res = 0;
  REP(i, sz) {
    int idx = lower_bound(ALL(cp), seq[i]) - cp.begin() + 1;
    res += i - bit.sum(idx);
    bit.add(idx, 1);
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  string s;
  cin >> s;
  int n = s.length();
  vector<int> cnt(26, 0);
  for (char c : s)
    ++cnt[c - 'a'];
  int snuke = 0;
  REP(i, 26) {
    if (cnt[i] & 1)
      ++snuke;
  }
  if (snuke >= 2) {
    cout << -1 << '\n';
    return 0;
  }
  string mae = "", ushiro = "";
  long long ans = 0;
  int migi = 0;
  REP(i, n) {
    if (cnt[s[i] - 'a'] == 1) {
      ans += migi;
      ++migi;
      --cnt[s[i] - 'a'];
    } else if (cnt[s[i] - 'a'] >= 2) {
      mae += s[i];
      ans += migi;
      cnt[s[i] - 'a'] -= 2;
    } else {
      ushiro += s[i];
      ++migi;
    }
  }
  reverse(ALL(mae));
  vector<vector<int>> pos(26);
  REP(i, n / 2) pos[mae[i] - 'a'].emplace_back(i);
  vector<int> idx(26, 0), check(n / 2);
  REP(i, n / 2) {
    int now = ushiro[i] - 'a';
    check[i] = pos[now][idx[now]];
    ++idx[now];
  }
  ans += inversion_number(check);
  cout << ans << '\n';
  return 0;
}