Language: C++

#include <bits/stdc++.h>

#define S_LEN_MAX 200000

#ifdef DEBUG
#define PRINT(x)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#x<<" = "<<(x)<<endl;
#define PRINTA(a,first,last)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#a<<"["<<(first)<<", "<<(last)<<")"<<endl;\
    for (int i=(first);i<(last);++i){cout<<#a<<"["<<i<<"] = "<<(a)[i]<<endl;}
#else
#define PRINT(x)
#define PRINTA(a,first,last)
#endif

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define MOD 1000000007

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

template <class T, class U> void amax(T& x, U y) {if (x < y) x = y;}
template <class T, class U> void amin(T& x, U y) {if (x > y) x = y;}

class binary_indexed_tree {
    private:
        int size;
        long long *a;
    public:
        binary_indexed_tree(int n);
        void add(int i, long long x);
        long long sum(int i);
};

binary_indexed_tree::binary_indexed_tree(int n)
{
    size = n;
    a = new long long[n]();
}

void binary_indexed_tree::add(int i, long long x)
{
    for (int j = i; j < size; j |= j + 1) {
        a[j] += x;
    }
}

long long binary_indexed_tree::sum(int i)
{
    long long result = 0;
    for (int j = i; j >= 0; j = (j & (j + 1)) - 1) {
        result += a[j];
    }
    return result;
}

template <class Iterator>
unsigned long long count_inversions(Iterator first, Iterator last, int max) {
    unsigned long long result = 0;
    binary_indexed_tree bit(max);
    for (Iterator it = last - 1; it >= first; --it) {
        result += bit.sum(*it-1);
        bit.add(*it, 1);
    }
    return result;
}


int solve() {
    string S;
    cin >> S;
    PRINT(S);
    int n = S.size();
    unsigned long long result = 0;
    int *a = new int[n];
    REP(i, n) a[i] = S[i] - 'a';
    int *count = new int[26]();
    REP(i, n) {
        count[a[i]]++;
    }
    int center = -1;
    REP(i, 26) {
        if (count[i] % 2 == 1) {
            if (center == -1) {
                center = i;
            } else {
                return -1;
            }
        }
    }

    int *b = new int[n];
    int *c = new int[n / 2];
    int count_[26];
    vector<int> v[26];

    //PRINTA(count, 0, 26);

    if (center != -1) {
        count_[center] = 0;
        REP(i, n) {
            if (a[i] == center) {
                if (count_[center] == count[center] / 2) {
                    b[i] = n / 2;
                }
                count_[center]++;
            }
        }
    }

    PRINT(center);
/*
    int pt_right = n - 1;
    REP(i, 26) count_[i] = 0;
    RREP(i, n) {
        PRINT(i);
        PRINT(a[i]);
        PRINT(count[a[i]]);
        PRINT(count_[a[i]]);
        if (count_[a[i]] < count[a[i]] / 2) {
            b[i] = pt_right;
            PRINT(b[i]);
            v[a[i]].push_back(n - pt_right);
            pt_right--;
            count_[a[i]]++;
        }
        PRINT("---");
    }

    int pt_left = 0;
    REP(i, 26) count_[i] = 0;
    REP(i, n) {
        if (count_[a[i]] < count[a[i]] / 2) {
            b[i] = pt_left;
            PRINT(i);
            PRINT(b[i]);
            c[pt_left] = v[a[i]][count_[a[i]]];
            pt_left++;
            count_[a[i]]++;
        }
    }

/*
    result = count_inversions(b, b + n, S_LEN_MAX) + count_inversions(c, c + (n / 2), S_LEN_MAX);
    return result;
}

int main(void)
{
    cout << solve();
    return 0;
}


Compiler output:
a.cc:121:1: error: unterminated comment
  121 | /*
      | ^
a.cc: In function 'int solve()':
a.cc:120:19: error: expected '}' at end of input
  120 |     PRINT(center);
      |                   ^
a.cc:78:13: note: to match this '{'
   78 | int solve() {
      |             ^
a.cc:120:19: warning: control reaches end of non-void function [-Wreturn-type]
  120 |     PRINT(center);
      |                   ^