#include namespace ac { using namespace std; } #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define all(r) r.begin(),r.end() using namespace std; using namespace ac; typedef int64_t ll; typedef vector vll; namespace ac { // FenwickTree繝サBit struct BinaryIndexedTree { int n; vector b; // [1, m_n] BinaryIndexedTree() {} void init(int a_n) { n = a_n; b.resize(n + 1, 0); } int64_t sum(int i) { int64_t s = 0; while (0 < i) { s += b[i]; i -= i & -i; } return s; } void add(int i, int64_t x) { while (i <= n) { b[i] += x; i += i & -i; } } }; struct Inversion { int n; ac::BinaryIndexedTree bit; Inversion(int a_n) { init(a_n); } void init(int a_n) { n = a_n; bit.init(n); } vector getGreaterThan(vector &a_v) { int length = a_v.size(); vector ord(length); for (int i = length - 1; i >= 0; i--) { ord[i] = bit.sum(a_v[i]); bit.add(a_v[i] + 1, 1); } return ord; } }; } struct Solution { void solve(std::istream& in, std::ostream& out) { string S; in >> S; Inversion i(S.size()); vll a(S.size()); rep(i, S.size()) { a[i] = S[i] - 'A'; } vll res = i.getGreaterThan(a); ll ans = accumulate(all(res), 0); out << ans << '\n'; } }; void solve(std::istream& in, std::ostream& out) { out << std::setprecision(12); Solution solution; solution.solve(in, out); } #include #include int main() { ios_base::sync_with_stdio(0); cin.tie(0); istream& in = cin; ostream& out = cout; solve(in, out); return 0; }