Language: C++

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cout << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//#define int long long

template <class Abel> struct BIT {
private:
	vector<Abel> node; int n;
    Abel UNITY_SUM = 0; //to be set
public:
	BIT(int n_) {
		n = n_; node.resize(n, UNITY_SUM);
	}
	//0-indexed
	void add(int a, Abel w) {
		for (int i = a; i < n; i |= i + 1)node[i] += w;
	}
	//[0,a)
	Abel sum(int a) {
		Abel ret = UNITY_SUM;
		for (int i = a - 1; i >= 0; i = (i&(i + 1)) - 1)ret += node[i];
		return ret;
	}
	//[a,b)
	Abel sum(int a, int b) {
		return sum(b) - sum(a);
	}
    //debug
    void print() {
        for(int i = 0; i < n; ++i) cout << sum(i, i + 1) << ",";
        cout << endl;
    }
};

void solve() {
	string s; cin >> s;
    int n = (int)s.size();
    vector<int> alp[30];
    rep(i, n) {
        alp[s[i] - 'a'].push_back(i);
    }
    int cnt = 0;
    rep(i, 26) {
        if((int)alp[i].size() % 2 == 1) cnt ++;
    }
    if(cnt >= 2) {
        cout << -1 << endl;
        return;
    }
    vector<int> num(n, 0);
    set<int> se[26];
    vector<int> cot(26, 0);
    int co = 0;
    rep(i, n) {
        int c = s[i] - 'a';
        int s = (int)alp[c].size();
        cot[c] ++;
        if(cot[c] <= s / 2) {
            num[i] = co;
            se[c].insert(co);
            ++co;
        } else if(s % 2 == 1 && (s + 1) / 2 == cot[c]) {
            num[i] = n / 2;
            se[c].insert(n / 2);
        } else {
            num[i] = n - 1 - *(se[c].rbegin());
            se[c].erase(se[c].rbegin());
        }
    }
    //num の転倒数を求めればok
    BIT<int> bnum(n);
    int ans = 0;
    rep(i, n) {
        ans += (i - bnum.sum(num[i]));
        bnum.add(num[i], 1);
    }
    cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init();
	solve();
	//cout << "finish" << endl;
    return 0;
}

Compiler output:
a.cc: In function 'void solve()':
a.cc:113:24: error: no matching function for call to 'std::set<int>::erase(std::set<int>::reverse_iterator)'
  113 |             se[c].erase(se[c].rbegin());
      |             ~~~~~~~~~~~^~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/set:63,
                 from a.cc:15:
/usr/include/c++/14/bits/stl_set.h:656:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::erase(const_iterator) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>; iterator = std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::const_iterator; const_iterator = std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::const_iterator]'
  656 |       erase(const_iterator __position)
      |       ^~~~~
/usr/include/c++/14/bits/stl_set.h:656:28: note:   no known conversion for argument 1 from 'std::set<int>::reverse_iterator' {aka 'std::reverse_iterator<std::_Rb_tree_const_iterator<int> >'} to 'std::set<int>::const_iterator' {aka 'std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::const_iterator'}
  656 |       erase(const_iterator __position)
      |             ~~~~~~~~~~~~~~~^~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:686:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::size_type std::set<_Key, _Compare, _Alloc>::erase(const key_type&) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>; size_type = long unsigned int; key_type = int]'
  686 |       erase(const key_type& __x)
      |       ^~~~~
/usr/include/c++/14/bits/stl_set.h:686:29: note:   no known conversion for argument 1 from 'std::set<int>::reverse_iterator' {aka 'std::reverse_iterator<std::_Rb_tree_const_iterator<int> >'} to 'const std::set<int>::key_type&' {aka 'const int&'}
  686 |       erase(const key_type& __x)
      |             ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_set.h:708:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::erase(const_iterator, const_iterator) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>; iterator = std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::const_iterator; const_iterator = std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::const_iterator]'
  708 |       erase(const_iterator __first, const_iterator __last)
      |       ^~~~~
/usr/include/c++/14/bits/stl_set.h:708:7: note:   candidate expects 2 arguments, 1 provided