Problem p03483 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
#define fu(a, b, c) for (int a = b; a <= (int)c; ++a)
#define fd(a, b, c) for (int a = b; a >= (int)c; --a)
#define cu(a, b, c) for (int a = b; a < (int)c; ++a)
#define cd(a, b, c) for (int a = (b)-1; a >= (int)c; --a)
#define gcd(a, b) __gcd(a, b)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define pf push_front
#define uint unsigned int
#define MOD 100000007
#define MAXN 200005
#define MAXH 4
#define MAXM 100
#define INF 1LL << 61
#define ll long long
#define ull unsigned long long
#define RNG rng mt19937(time(NULL))
#define udist(a, b) uniform_int_distribution<uint32_t> distribution(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define gmax(a, b) a = max(a, b)
#define gmin(a, b) a = min(a, b)
#define whatis(x) cerr << #x << " is " << x << endl;
#define mat vector<vector<T>>
#define T long long
#define fast_io()                                                              \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define cld complex<long double>
using namespace std;
const int N = 100000;
long long t[N + 5];

void inc(int x, long long val) {
  for (++x; x <= N; x += x & -x) {
    t[x] += val;
  }
}

long long query(int x) {
  ll res = 0;
  for (++x; x > 0; x &= x - 1) {
    res += t[x];
  }
  return res;
}

bool b[N + 5];

int main() {
  fast_io();
  string s;
  cin >> s;
  int n = s.length();
  vector<int> p[26];
  cu(i, 0, n) { p[s[i] - 'a'].pb(i); }
  int odd = 0;
  cu(i, 0, 26) odd += p[i].size() & 1;
  if (odd > 1) {
    cout << -1 << '\n';
    return 0;
  }
  int st[26] = {};

  ll ans = 0;

  int cur_start = 0;
  bool after = false;
  cd(i, n, 0) {
    if (!b[i]) {
      int cur = s[i] - 'a';
      int l_ind = p[cur][st[cur]];
      if (st[cur] >= p[cur].size() / 2) {
        after = true;
        continue;
      }
      int greater = cur_start - query(l_ind);
      ans += l_ind - (cur_start - greater);
      if (after)
        ++ans;
      b[l_ind] = true;
      inc(l_ind, 1);
      ++cur_start;
      ++st[cur];
    }
  }
  cout << ans << '\n';
}


Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
#define fu(a, b, c) for (int a = b; a <= (int)c; ++a)
#define fd(a, b, c) for (int a = b; a >= (int)c; --a)
#define cu(a, b, c) for (int a = b; a < (int)c; ++a)
#define cd(a, b, c) for (int a = (b)-1; a >= (int)c; --a)
#define gcd(a, b) __gcd(a, b)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define pf push_front
#define uint unsigned int
#define MOD 100000007
#define MAXN 200005
#define MAXH 4
#define MAXM 100
#define INF 1LL << 61
#define ll long long
#define ull unsigned long long
#define RNG rng mt19937(time(NULL))
#define udist(a, b) uniform_int_distribution<uint32_t> distribution(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define gmax(a, b) a = max(a, b)
#define gmin(a, b) a = min(a, b)
#define whatis(x) cerr << #x << " is " << x << endl;
#define mat vector<vector<T>>
#define T long long
#define fast_io()                                                              \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define cld complex<long double>
using namespace std;
const int N = 200000;
long long t[N + 5];

void inc(int x, long long val) {
  for (++x; x <= N; x += x & -x) {
    t[x] += val;
  }
}

long long query(int x) {
  ll res = 0;
  for (++x; x > 0; x &= x - 1) {
    res += t[x];
  }
  return res;
}

bool b[N + 5];

int main() {
  fast_io();
  string s;
  cin >> s;
  int n = s.length();
  vector<int> p[26];
  cu(i, 0, n) { p[s[i] - 'a'].pb(i); }
  int odd = 0;
  cu(i, 0, 26) odd += p[i].size() & 1;
  if (odd > 1) {
    cout << -1 << '\n';
    return 0;
  }
  int st[26] = {};

  ll ans = 0;

  int cur_start = 0;
  bool after = false;
  cd(i, n, 0) {
    if (!b[i]) {
      int cur = s[i] - 'a';
      int l_ind = p[cur][st[cur]];
      if (st[cur] >= p[cur].size() / 2) {
        after = true;
        continue;
      }
      int greater = cur_start - query(l_ind);
      ans += l_ind - (cur_start - greater);
      if (after)
        ++ans;
      b[l_ind] = true;
      inc(l_ind, 1);
      ++cur_start;
      ++st[cur];
    }
  }
  cout << ans << '\n';
}