Problem p03483 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// define
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)), UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a + mod - b) % mod
#define MUL(a, b) a = (((a) % mod) * ((b) % mod)) % mod
#define rollcall cout << "I'm Sucu." << endl;
#define repi(i, m, n) for (int i = m; i < n; i++)
#define drep(i, n, m) for (int i = n; i >= m; i--)
#define rep(i, n) repi(i, 0, n)
#define rrep(i, n) repi(i, 1, n + 1)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x, y) make_pair(x, y)
#define dmt(x, y, z) make_tuple(x, y, z)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define fi first
#define se second
// debug
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename T, typename U, typename V>
ostream &operator<<(ostream &os, const tuple<T, U, V> &t) {
  os << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin())
      os << " ";
    os << *it;
  }
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &mp) {
  for (auto x : mp)
    os << "(" << x.first << "," << x.second << ")" << endl;
  return os;
}
template <typename T, int SIZE> int array_length(const T (&)[SIZE]) {
  return SIZE;
}
template <typename T, int N>
void PRINTF(const T (&a)[N], int s = N, int t = -1, bool f = true) {
  if (t == -1) {
    rep(i, s) {
      if (i)
        cout << " ";
      cout << a[i];
    }
  } else
    repi(i, s, t) {
      if (i != s)
        cout << " ";
      cout << a[i];
    }
  if (f)
    cout << "\n";
}
template <typename T, int N1, int N2>
void PRINTF(const T (&a)[N1][N2], int h = N1, int w = N2) {
  rep(i, h) {
    rep(j, w) { cout << a[i][j] << " \n"[j == w - 1]; }
  }
}
string substr(const string &str, int S1, int S2 = -1) {
  if (S2 == -1)
    return str.substr(S1);
  return str.substr(S1, S2 - S1);
}
// typedef
typedef complex<double> Point;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<P, int> Pi;
typedef tuple<int, int, int> TT;
typedef pair<int, TT> PT;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
// int sum = accumulate(a, a+n, 0LL);
// int mx = max_element(a, a+n);
// int mn = min_element(a, a+n);
// int cnt = count(a, a+n, k);

// 0に対応できるよう内部でindexを1ずらしてる
class BIT {
public:
  int n = 1;
  vector<int> bit;
  void init(int N) { //[0, N)のBIT作成
    while (n < N + 1)
      n *= 2;
    bit.resize(n + 1);
    fill(all(bit), 0);
  }
  void add(int i, int x) {
    i++;
    while (i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }
  int get(int i) {
    int s = 0;
    i++;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  int lower_bound(int k) {
    int lb = -1, ub = n;
    while (ub - lb > 1) {
      if (get((lb + ub) / 2) >= k)
        ub = (lb + ub) / 2;
      else
        lb = (lb + ub) / 2;
    }
    return ub;
  }
};

int solve(int n, vector<int> v) {
  BIT bit;
  bit.init(n);
  int ans = 0;
  rep(i, n) {
    int res = bit.get(v[i]);
    ans += i - res;
    bit.add(v[i], 1);
  }
  return ans;
}

int a[50], b[50];
signed main() {
  string str, T;
  cin >> str;
  int n = str.size(), cnt = 0;
  rep(i, n) a[str[i] - 'a']++;
  rep(i, 26) cnt += (a[i] % 2 == 1);
  if (cnt >= 2) {
    cout << -1 << endl;
    return 0;
  }

  int t1 = 0, t2 = (n % 2 == 1);
  vector<int> vec;
  string A = "", B = "", C = "";
  rep(i, n) {
    int c = str[i] - 'a';
    b[c]++;
    if (a[c] % 2 == 1 and b[c] == a[c] / 2 + 1) {
      vec.pb(n / 2);
      B.pb(c + 'a');
      continue;
    }
    if (b[c] <= a[c] / 2) {
      A.pb(c + 'a');
      vec.pb(t1);
      t1++;
    } else {
      C.pb(c + 'a');
      vec.pb(n / 2 + t2);
      t2++;
    }
  }
  T = A + B + C;
  int ans = solve(n, vec);

  vector<int> d[50], vec2;
  rep(i, n / 2) d[T[i] - 'a'].pb(n - i - 1);
  int l = (n + 1) / 2, r = n;
  repi(i, l, r) {
    int tmp = d[T[i] - 'a'].back();
    d[T[i] - 'a'].pop_back();
    vec2.pb(tmp);
  }

  // cout << T << endl;
  // cout << vec2 << endl;
  ans += solve(r - l, vec2);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// define
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)), UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a + mod - b) % mod
#define MUL(a, b) a = (((a) % mod) * ((b) % mod)) % mod
#define rollcall cout << "I'm Sucu." << endl;
#define repi(i, m, n) for (int i = m; i < n; i++)
#define drep(i, n, m) for (int i = n; i >= m; i--)
#define rep(i, n) repi(i, 0, n)
#define rrep(i, n) repi(i, 1, n + 1)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x, y) make_pair(x, y)
#define dmt(x, y, z) make_tuple(x, y, z)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define fi first
#define se second
// debug
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename T, typename U, typename V>
ostream &operator<<(ostream &os, const tuple<T, U, V> &t) {
  os << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin())
      os << " ";
    os << *it;
  }
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &mp) {
  for (auto x : mp)
    os << "(" << x.first << "," << x.second << ")" << endl;
  return os;
}
template <typename T, int SIZE> int array_length(const T (&)[SIZE]) {
  return SIZE;
}
template <typename T, int N>
void PRINTF(const T (&a)[N], int s = N, int t = -1, bool f = true) {
  if (t == -1) {
    rep(i, s) {
      if (i)
        cout << " ";
      cout << a[i];
    }
  } else
    repi(i, s, t) {
      if (i != s)
        cout << " ";
      cout << a[i];
    }
  if (f)
    cout << "\n";
}
template <typename T, int N1, int N2>
void PRINTF(const T (&a)[N1][N2], int h = N1, int w = N2) {
  rep(i, h) {
    rep(j, w) { cout << a[i][j] << " \n"[j == w - 1]; }
  }
}
string substr(const string &str, int S1, int S2 = -1) {
  if (S2 == -1)
    return str.substr(S1);
  return str.substr(S1, S2 - S1);
}
// typedef
typedef complex<double> Point;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<P, int> Pi;
typedef tuple<int, int, int> TT;
typedef pair<int, TT> PT;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
// int sum = accumulate(a, a+n, 0LL);
// int mx = max_element(a, a+n);
// int mn = min_element(a, a+n);
// int cnt = count(a, a+n, k);

// 0に対応できるよう内部でindexを1ずらしてる
class BIT {
public:
  int n = 1;
  vector<int> bit;
  void init(int N) { //[0, N)のBIT作成
    while (n < N + 1)
      n *= 2;
    bit.resize(n + 1);
    fill(all(bit), 0);
  }
  void add(int i, int x) {
    i++;
    while (i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }
  int get(int i) {
    int s = 0;
    i++;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  int lower_bound(int k) {
    int lb = -1, ub = n;
    while (ub - lb > 1) {
      if (get((lb + ub) / 2) >= k)
        ub = (lb + ub) / 2;
      else
        lb = (lb + ub) / 2;
    }
    return ub;
  }
};

int solve(int n, vector<int> v) {
  BIT bit;
  bit.init(n);
  int ans = 0;
  rep(i, n) {
    int res = bit.get(v[i]);
    ans += i - res;
    bit.add(v[i], 1);
  }
  return ans;
}

int a[50], b[50];
signed main() {
  string str, T;
  cin >> str;
  int n = str.size(), cnt = 0;
  rep(i, n) a[str[i] - 'a']++;
  rep(i, 26) cnt += (a[i] % 2 == 1);
  if (cnt >= 2) {
    cout << -1 << endl;
    return 0;
  }

  int t1 = 0, t2 = (n % 2 == 1);
  vector<int> vec;
  string A = "", B = "", C = "";
  rep(i, n) {
    int c = str[i] - 'a';
    b[c]++;
    if (a[c] % 2 == 1 and b[c] == a[c] / 2 + 1) {
      vec.pb(n / 2);
      B.pb(c + 'a');
      continue;
    }
    if (b[c] <= a[c] / 2) {
      A.pb(c + 'a');
      vec.pb(t1);
      t1++;
    } else {
      C.pb(c + 'a');
      vec.pb(n / 2 + t2);
      t2++;
    }
  }
  T = A + B + C;
  int ans = solve(n, vec);

  vector<int> d[50], vec2;
  rep(i, n / 2) d[T[i] - 'a'].pb(n - i - 1 - ((n + 1) / 2));
  int l = (n + 1) / 2, r = n;
  repi(i, l, r) {
    int tmp = d[T[i] - 'a'].back();
    d[T[i] - 'a'].pop_back();
    vec2.pb(tmp);
  }

  // cout << T << endl;
  // cout << vec2 << endl;
  ans += solve(r - l, vec2);
  cout << ans << endl;
  return 0;
}