Cow Checkups

시간 제한2초메모리 제한2048 MB

요약
가능한 모든 N(N+1)/2개의 부분 배열 뒤집기에 대해, 뒤집은 배열이 b_i와 일치하는 위치 i의 개수를 모두 더한다.
난이도

어려움10점 중 9점

유형
배열, 완전 탐색, 조합론, 누적 합
정답자
아직 제출이 없습니다

문제

Farmer John's NN (1≤N≤5⋅1051 \leq N \leq 5 \cdot 10^5) cows are standing in a line, with cow 11 at the front of the line and cow NN at the back of the line. FJ's cows also come in many different species. He denotes each species with an integer from 11 to NN. The ii'th cow from the front of the line is of species a_ia\_i (1≤a_i≤N1 \leq a\_i \leq 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 ii'th cow in the line, only if it is species b_ib\_i (1≤b_i≤N1 \leq b\_i \leq N).

FJ is lazy and does not want to completely reorder his cows. He will perform the following operation exactly once.

  • Select two integers ll and rr such that 1≤l≤r≤N1 \leq l \le r \leq N. Reverse the order of the cows that are between the ll-th cow and the rr-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)/2N(N+1)/2 possible operations.

입력

The first line contains an integer NN.

The second line contains a_1,a_2,…,a_Na\_1, a\_2, \ldots, a\_N.

The third line contains b_1,b_2,…,b_Nb\_1, b\_2, \ldots, b\_N.

출력

Output one line with the sum of the number of cows that are checked by the veterinarian over all possible operations.

예제3

  1. 예제 1

    입력
    3
    1 3 2
    3 2 1
    
    예상 출력
    3
    
  2. 예제 2

    입력
    3
    1 2 3
    1 2 3
    
    예상 출력
    12
    
  3. 예제 3

    입력
    7
    1 3 2 2 1 3 2
    3 2 2 1 2 3 1
    
    예상 출력
    60