forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1430E.go
45 lines (41 loc) · 777 Bytes
/
1430E.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"bufio"
. "fmt"
"io"
)
// github.com/EndlessCheng/codeforces-go
func CF1430E(_r io.Reader, out io.Writer) {
var n int
var s []byte
Fscan(bufio.NewReader(_r), &n, &s)
tree := make([]int, n+1)
add := func(i int) {
for ; i <= n; i += i & -i {
tree[i]++
}
}
sum := func(i int) (res int) {
for ; i > 0; i &= i - 1 {
res += tree[i]
}
return
}
query := func(l, r int) int { return sum(r) - sum(l-1) }
posS := [26][]int{}
t := make([]byte, n)
for i, b := range s {
b -= 'a'
t[n-1-i] = b
posS[b] = append(posS[b], i)
}
ans := int64(0)
for i, b := range t {
p := posS[b][0]
posS[b] = posS[b][1:]
ans += int64(p + query(p+1, n) - i)
add(p + 1)
}
Fprint(out, ans)
}
//func main() { CF1430E(os.Stdin, os.Stdout) }