import
java.util.*;
import
java.util.HashMap;
import
java.util.Map;
public
class
Main {
static
void
update(
int
[] bit,
int
x,
int
val,
int
n)
{
while
(x <= n) {
bit[x] += val;
x += x & -x;
}
}
static
int
query(
int
[] bit,
int
x)
{
int
sum =
0
;
while
(x >
0
) {
sum += bit[x];
x -= x & -x;
}
return
sum;
}
static
long
getDisorderLevel(String[] sentence,
String[] normal,
int
n)
{
int
MAX = n +
1
;
Map<String, Integer> m =
new
HashMap<>();
int
[] bit =
new
int
[MAX];
int
[] a =
new
int
[MAX];
for
(
int
i =
0
; i < n; i++) {
m.put(normal[i], i +
1
);
}
for
(
int
i =
0
; i < n; i++) {
a[i] = m.get(sentence[i]);
}
long
disorderLevel =
0
;
for
(
int
i = n -
1
; i >=
0
; i--) {
disorderLevel += query(bit, a[i] -
1
);
update(bit, a[i],
1
, n);
}
return
disorderLevel;
}
public
static
void
main(String[] args)
{
String[] sentence1
= { "red", "apple", "is", "the" };
String[] normal1 = { "the", "apple", "is", "red" };
int
n1 = sentence1.length;
System.out.println(
getDisorderLevel(sentence1, normal1, n1));
String[] sentence2 = { "sun", "the", "shining",
"is", "bright", "today" };
String[] normal2 = { "the", "sun", "is",
"shining", "bright", "today" };
int
n2 = sentence2.length;
System.out.println(
getDisorderLevel(sentence2, normal2, n2));
}
}