Farmer John's N (1≤N≤5⋅105) cows are standing in a line, with cow 1
at the front of the line and cow N at the back of the line. FJ's cows also come in many different species. He denotes each species with an integer from 1 to N. The i'th cow from the front of the line is of species ai (1≤ai ≤N).
FJ is taking his cows to a checkup at a local bovine hospital. However, the bovine veterinarian is very picky and wants to perform a checkup on the i'th cow in the line, only if it is species bi (1≤bi≤N).
FJ is lazy and does not want to completely reorder his cows. He will perform the following operation exactly once.
Select two integers l and r such that 1≤l≤r≤N. Reverse the order of the cows that are between the l-th cow and the r-th cow in the line, inclusive.
FJ wants to measure how effective this approach is. Find the sum of the number of cows that are checked by the veterinarian over all N(N+1)/2 possible operations.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains an integer N.
The second line contains a1,a2,…,aN.
The third line contains b1,b2,…,bN.
OUTPUT FORMAT (print output to the terminal / stdout):
Output one line with the sum of the number of cows that are checked by the veterinarian over all possible operations.
SAMPLE INPUT:
3
1 3 2
3 2 1
SAMPLE OUTPUT:
3
If FJ chooses (l=1,r=1), (l=2,r=2), or (l=3,r=3) then no cows will be checked. Note that those operations do not modify the location of the cows.
The following operations result in one cow being checked.
- l=1,r=2: FJ reverses the order of the first and second cows so the species of each cow in the new lineup will be [3,1,2]. The first cow will be checked.
- l=2,r=3: FJ reverses the order of the second and third cows so the species of each cow in the new lineup will be [1,2,3]. The second cow will be checked.
- l=1,r=3: FJ reverses the order of the first, second, and third cows so the species of each cow in the new lineup will be [2,3,1]. The third cow will be checked.
The total number of cows checked over all six operations is 0+0+0+1+1+1=3.
SAMPLE INPUT:
3
1 2 3
1 2 3
SAMPLE OUTPUT:
12
There are three possible operations that cause 3 cows to be checked: (l=1,r=1), (l=2,r=2), and (l=3,r=3). The remaining operations each result in 1 cow being checked. The total number of cows checked over all six operations is 3+3+3+1+1+1=12.
SAMPLE INPUT:
7
1 3 2 2 1 3 2
3 2 2 1 2 3 1
SAMPLE OUTPUT:
60
SCORING:
Input 4: N≤100
Input 5: N≤5000
Inputs 6-9:ai,bi are all generated uniformly at random in the range [1,N]
Inputs 10-15:ai,bi are all generated uniformly at random in the range [1,2]
Inputs 16-23: No additional constraints.
Problem credits: Chongtian Ma, Haokai Ma, and Alex Liang
扫码领取USACO试题答案+详细解析
咨询一对一备赛规划