Cow Checkups

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

요약
c가 0부터 N까지일 때, 구간 (l, r)을 한 번 뒤집어 정확히 c마리가 검진 조건 a[i] = b[i]를 만족하는 구간의 수를 각각 구한다.
난이도

어려움10점 중 8점

유형
배열, 누적 합, 동적 계획법, 수학
정답자
아직 제출이 없습니다

문제

Note: We suggest using a language other than Python to earn full credit on this problem.

Farmer John's NN (1≤N≤75001 \leq N \leq 7500) 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 of 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. For each c=0…Nc=0 \ldots N, help FJ find the number of distinct operations (l,rl,r) that result in exactly cc cows being checked. Two operations (l_1,r_1l\_1,r\_1) and (l_2,r_2l\_2,r\_2) are different if l_1≠l_2l\_1 \neq l\_2 or r_1≠r_2r\_1 \neq r\_2.

입력

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 N+1N+1 lines with the ii-th line containing the number of distinct operations (l,rl,r) that result in i−1i-1 cows being checked.

예제3

  1. 예제 1

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

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

    입력
    7
    1 3 2 2 1 3 2
    3 2 2 1 2 3 1
    
    예상 출력
    0
    6
    14
    6
    2
    0
    0
    0