Problem p03483 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <set>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

typedef double LD;
typedef long long LL;
typedef pair<int, int> PII;
#define MP make_pair
#define PB push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define REP(i, t) FOR(i, 0, t)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())

const LL MOD = 1000000007;
const int INF = 1e9;
const LL LINF = 1LL * INF * INF;
const int MAXN = 200007;
const LD EPS = 1e-7;

string S;
int n, T[MAXN], L[MAXN];
vector<int> P[30];

void upd(int pos) {
  for (int i = pos; i < n; i |= i + 1)
    ++T[i];
}

int query(int pos) {
  int s = 0;
  for (int i = pos; i >= 0; i = (i & (i + 1)) - 1)
    s += T[i];
  return s;
}

int main() {
  ios_base::sync_with_stdio(0);
  // freopen("In.txt", "r", stdin);
  LL ans = 0;
  cin >> S;
  n = SZ(S);
  REP(i, n)
  P[S[i] - 'a'].push_back(i);
  int cntodd = 0, podd;
  bool odd = (n & 1);
  REP(i, 26) {
    if (SZ(P[i]) & 1) {
      ++cntodd;
      podd = i;
    }
  }
  if (cntodd > 1) {
    cout << -1;
    return 0;
  }
  int sk = 0;
  REP(i, n) {
    if (P[S[i] - 'a'][L[S[i] - 'a']] != i ||
        L[S[i] - 'a'] >= SZ(P[S[i] - 'a']) / 2) {
      if (i == P[podd][SZ(P[podd]) / 2])
        sk = 2;
      continue;
    }
    int l = i - query(i),
        r = P[S[i] - 'a'][SZ(P[S[i] - 'a']) - 1 - L[S[i] - 'a']];
    r -= query(r);
    ans += n - 1 - l - r + sk;
    upd(P[S[i] - 'a'][SZ(P[S[i] - 'a']) - 1 - L[S[i] - 'a']]);
    ++L[S[i] - 'a'];
  }
  cout << ans;
  // cout << endl; system("pause");
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <set>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

typedef double LD;
typedef long long LL;
typedef pair<int, int> PII;
#define MP make_pair
#define PB push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define REP(i, t) FOR(i, 0, t)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())

const LL MOD = 1000000007;
const int INF = 1e9;
const LL LINF = 1LL * INF * INF;
const int MAXN = 200007;
const LD EPS = 1e-7;

string S;
int n, T[MAXN], L[MAXN];
vector<int> P[30];

void upd(int pos) {
  for (int i = pos; i < n; i |= i + 1)
    ++T[i];
}

int query(int pos) {
  int s = 0;
  for (int i = pos; i >= 0; i = (i & (i + 1)) - 1)
    s += T[i];
  return s;
}

int main() {
  ios_base::sync_with_stdio(0);
  // freopen("In.txt", "r", stdin);
  LL ans = 0;
  cin >> S;
  n = SZ(S);
  REP(i, n)
  P[S[i] - 'a'].push_back(i);
  int cntodd = 0, podd;
  bool odd = (n & 1);
  REP(i, 26) {
    if (SZ(P[i]) & 1) {
      ++cntodd;
      podd = i;
    }
  }
  if (cntodd > 1) {
    cout << -1;
    return 0;
  }
  int sk = 0;
  REP(i, n) {
    if (P[S[i] - 'a'][L[S[i] - 'a']] != i ||
        L[S[i] - 'a'] >= SZ(P[S[i] - 'a']) / 2) {
      if (odd && i == P[podd][SZ(P[podd]) / 2])
        sk = 2;
      continue;
    }
    int l = i - query(i),
        r = P[S[i] - 'a'][SZ(P[S[i] - 'a']) - 1 - L[S[i] - 'a']];
    r -= query(r);
    ans += n - 1 - l - r + sk;
    upd(P[S[i] - 'a'][SZ(P[S[i] - 'a']) - 1 - L[S[i] - 'a']]);
    ++L[S[i] - 'a'];
  }
  cout << ans;
  // cout << endl; system("pause");
  return 0;
}