Problem p03483 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

/* Binary Indexed Tree(0-index) */

struct BIT {
  typedef int Type;
  int bit_size;
  vector<Type> data;

  BIT(int n) {
    bit_size = n;
    data.assign(n + 1, 0);
  }

  // add x to kth value
  void add(int k, Type x) {
    k += 1;

    while (k <= bit_size) {
      data[k] += x;
      k += k & (-k);
    }
  }

  // sum of [1,k]
  Type query(int k) {
    k += 1;

    Type rec = 0;
    while (k > 0) {
      rec += data[k];
      k -= k & (-k);
    }
    return rec;
  }
};

int main() {
  char s[SIZE], sL[SIZE] = {}, sR[SIZE] = {};
  int sLsize = 0, sRsize = 0;
  int n;
  int al[26] = {};
  ll ans = 0;

  scanf("%s", s);
  n = strlen(s);

  for (int i = 0; i < n; i++) {
    al[s[i] - 'a']++;
  }

  int odd = 0;
  for (int i = 0; i < 26; i++) {
    odd += al[i] % 2;
  }

  if (odd >= 2) {
    puts("-1");
    return 0;
  }

  int half = n / 2;
  int rest = half;
  int center = n % 2;
  int al2[26];
  for (int i = 0; i < 26; i++)
    al2[i] = al[i] / 2;

  for (int i = 0; i < n; i++) {
    if (al2[s[i] - 'a'] == 0) {
      if (al[s[i] - 'a'] % 2) {
        ans += rest;
        center = 0;
      } else {
        ans += rest + center;
        sR[half - (++sRsize)] = s[i];
      }
    } else {
      rest--;
      al2[s[i] - 'a']--;
      sL[sLsize++] = s[i];
    }
  }

  queue<int> que[26];
  BIT bit(half + 1);

  for (int i = 0; i < half; i++)
    bit.add(i, 1);

  for (int i = 0; i < half; i++) {
    que[sR[i] - 'a'].push(i);
  }

  for (int i = 0; i < half; i++) {
    int p = que[sL[i] - 'a'].front();
    que[sL[i] - 'a'].pop();

    bit.add(p, -1);
    ans += bit.query(p);
  }

  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

/* Binary Indexed Tree(0-index) */

struct BIT {
  typedef int Type;
  int bit_size;
  vector<Type> data;

  BIT(int n) {
    bit_size = n;
    data.assign(n + 1, 0);
  }

  // add x to kth value
  void add(int k, Type x) {
    k += 1;

    while (k <= bit_size) {
      data[k] += x;
      k += k & (-k);
    }
  }

  // sum of [1,k]
  Type query(int k) {
    k += 1;

    Type rec = 0;
    while (k > 0) {
      rec += data[k];
      k -= k & (-k);
    }
    return rec;
  }
};

int main() {
  char s[SIZE], sL[SIZE] = {}, sR[SIZE] = {};
  int sLsize = 0, sRsize = 0;
  int n;
  int al[26] = {};
  ll ans = 0;

  scanf("%s", s);
  n = strlen(s);

  for (int i = 0; i < n; i++) {
    al[s[i] - 'a']++;
  }

  int odd = 0;
  for (int i = 0; i < 26; i++) {
    odd += al[i] % 2;
  }

  if (odd >= 2) {
    puts("-1");
    return 0;
  }

  int half = n / 2;
  int rest = half;
  int center = n % 2;
  int al2[26];
  for (int i = 0; i < 26; i++)
    al2[i] = al[i] / 2;

  for (int i = 0; i < n; i++) {
    if (al2[s[i] - 'a'] <= 0) {
      if (al[s[i] - 'a'] % 2 && center) {
        ans += rest;
        center = 0;
      } else {
        ans += rest + center;
        sR[half - (++sRsize)] = s[i];
      }
    } else {
      rest--;
      al2[s[i] - 'a']--;
      sL[sLsize++] = s[i];
    }
  }

  queue<int> que[26];
  BIT bit(half + 1);

  for (int i = 0; i < half; i++)
    bit.add(i, 1);

  for (int i = 0; i < half; i++) {
    que[sR[i] - 'a'].push(i);
  }

  for (int i = 0; i < half; i++) {
    int p = que[sL[i] - 'a'].front();
    que[sL[i] - 'a'].pop();

    bit.add(p, -1);
    ans += bit.query(p);
  }

  printf("%lld\n", ans);

  return 0;
}