Language: C++


#define N 100010

//1-indexed! 1-indexed!
vl bit(N + 1);
int n;

ll sum(int i) {
	ll s = 0;
	while (i > 0) {
		s += bit[i];
		i -= i & -i;
	}
	return s;
}

void add(int i, ll x) {
	while (i <= n) {
		bit[i] += x;
		i += i & -i;
	}
}

// 1,2,...,nを並び替えた順列をバブルソートで
// 順番通りにするための交換回数
ll bubble(vi p) {
	int n = p.size();
	ll ans = 0;
	for (int j = 0; j < n; j++) {
		ans += (j - sum(p[j]));
		add(p[j], 1);
	}
	return ans;
}

int main() {
	string s;
	cin >> s;
	n = s.size();
	int i, j;
	bit.resize(n + 1);
	vi num(26);
	for (i = 0; i < n; i++) {
		num[s[i] - 'a']++;
	}
	int ki = -1;
	for (i = 0; i < 26; i++) {
		if (num[i] % 2) {
			if (ki == -1) {
				ki = i;
			}
			else {
				cout << -1 << endl;
				return 0;
			}
		}
	}
	vi p(n, -1);
	vi cnt(26);
	vi place(n);
	for (i = 0; i < n; i++) {
		int alpha = s[i] - 'a';
		cnt[alpha]++;
		if (alpha == ki) {
			if (cnt[alpha] <= num[alpha] / 2) {
				place[i] = 0;
			}
			else if (cnt[alpha] == num[alpha] / 2 + 1) {
				place[i] = 1;
			}
			else {
				place[i] = 2;
			}
		}
		else {
			if (cnt[alpha] <= num[alpha] / 2) {
				place[i] = 0;
			}
			else {
				place[i] = 2;
			}
		}
	}
	j = 0;
	for (i = 0; i < n; i++) {
		if (place[i] == 0) {
			p[j++] = i;
		}
		else if (place[i] == 1) {
			p[n / 2] = i;
		}
	}
	vi right(26);
	for (i = 0; i < n; i++) {
		right[s[i] - 'a'] = i;
	}
	for (i = 0; i < n / 2; i++) {
		int alpha = s[p[i]] - 'a';
		p[n - 1 - i] = right[alpha];
		for (j = right[alpha] - 1; j >= 0; j--) {
			if (s[j] - 'a' == alpha) {
				right[alpha] = j;
				break;
			}
		}
	}
	for (i = 0; i < n; i++) {
		p[i]++;
	}
	cout << bubble(p) << endl;
}

Compiler output:
a.cc:5:1: error: 'vl' does not name a type
    5 | vl bit(N + 1);
      | ^~
a.cc:8:1: error: 'll' does not name a type
    8 | ll sum(int i) {
      | ^~
a.cc:17:17: error: 'll' has not been declared
   17 | void add(int i, ll x) {
      |                 ^~
a.cc: In function 'void add(int, int)':
a.cc:19:17: error: 'bit' was not declared in this scope
   19 |                 bit[i] += x;
      |                 ^~~
a.cc: At global scope:
a.cc:26:1: error: 'll' does not name a type
   26 | ll bubble(vi p) {
      | ^~
a.cc: In function 'int main()':
a.cc:37:9: error: 'string' was not declared in this scope
   37 |         string s;
      |         ^~~~~~
a.cc:38:9: error: 'cin' was not declared in this scope
   38 |         cin >> s;
      |         ^~~
a.cc:38:16: error: 's' was not declared in this scope
   38 |         cin >> s;
      |                ^
a.cc:41:9: error: 'bit' was not declared in this scope
   41 |         bit.resize(n + 1);
      |         ^~~
a.cc:42:9: error: 'vi' was not declared in this scope; did you mean 'i'?
   42 |         vi num(26);
      |         ^~
      |         i
a.cc:44:17: error: 'num' was not declared in this scope; did you mean 'enum'?
   44 |                 num[s[i] - 'a']++;
      |                 ^~~
      |                 enum
a.cc:48:21: error: 'num' was not declared in this scope; did you mean 'enum'?
   48 |                 if (num[i] % 2) {
      |                     ^~~
      |                     enum
a.cc:53:33: error: 'cout' was not declared in this scope
   53 |                                 cout << -1 << endl;
      |                                 ^~~~
a.cc:53:47: error: 'endl' was not declared in this scope
   53 |                                 cout << -1 << endl;
      |                                               ^~~~
a.cc:58:11: error: expected ';' before 'p'
   58 |         vi p(n, -1);
      |           ^~
      |           ;
a.cc:59:11: error: expected ';' before 'cnt'
   59 |         vi cnt(26);
      |           ^~~~
      |           ;
a.cc:60:11: error: expected ';' before 'place'
   60 |         vi place(n);
      |           ^~~~~~
      |           ;
a.cc:63:17: error: 'cnt' was not declared in this scope; did you mean 'int'?
   63 |                 cnt[alpha]++;
      |                 ^~~
      |                 int
a.cc:65:43: error: 'num' was not declared in this scope; did you mean 'enum'?
   65 |                         if (cnt[alpha] <= num[alpha] / 2) {
      |                                           ^~~
      |                                           enum
a.cc:66:33: error: 'place' was not declared in this scope
   66 |                                 place[i] = 0;
      |                                 ^~~~~
a.cc:69:33: error: 'place' was not declared in this scope
   69 |                                 place[i] = 1;
      |                                 ^~~~~
a.cc:72:33: error: 'place' was not declared in this scope
   72 |                                 place[i] = 2;
      |                                 ^~~~~
a.cc:76:43: error: 'num' was not declared in this scope; did you mean 'enum'?
   76 |                         if (cnt[alpha] <= num[alpha] / 2) {
      |                                           ^~~
      |                                           enum
a.cc:77:33: error: 'place' was not declared in this scope
   77 |                                 place[i] = 0;
      |                                 ^~~~~
a.cc:80:33: error: 'place' was not declared in this scope
   80 |                                 place[i] = 2;
      |                                 ^~~~~
a.cc:86:21: error: 'place' was not declared in this scope
   86 |                 if (place[i] == 0) {
      |                     ^~~~~
a.cc:87:25: error: 'p' was not declared in this scope
   87 |                         p[j++] = i;
      |                         ^
a.cc:90:25: error: 'p' was not declared in this scope
   90 |                         p[n / 2] = i;
      |                         ^
a.cc:93:11: error: expected ';' before 'right'
   93 |         vi right(26);
      |           ^~~~~~
      |           ;
a.cc:95:17: error: 'right' was not declared in this scope
   95 |                 right[s[i] - 'a'] = i;
      |                 ^~~~~
a.cc:98:31: error: 'p' was not declared in this scope
   98 |                 int alpha = s[p[i]] - 'a';
      |                               ^
a.cc:99:32: error: 'right' was not declared in this scope
   99 |                 p[n - 1 - i] = right[alpha];
      |                                ^~~~~
a.cc:108:17: error: 'p' was not declared in this scope
  108 |                 p[i]++;
      |                 ^
a.cc:110:9: error: 'cout' was not declared in this scope
  110 |         cout << bubble(p) << endl;
      |         ^~~~
a.cc:110:24: error: 'p' was not declared in this scope
  110 |         cout << bubble(p) << endl;
      |                        ^
a.cc:110:17: error: 'bubble' was not declared in this scope
  110 |         cout << bubble(p) << endl;
      |                 ^~~~~~
a.cc:110:30: error: 'endl' was not declared in this scope
  110 |         cout << bubble(p) << endl;
      |                              ^~~~