#P11669. Reversal Operation Cow Checkup

    ID: 13758 Type: Default 1000ms 256MiB

Reversal Operation Cow Checkup

Reversal Operation Cow Checkup

Farmer John has N cows lined up in a row (1 ≤ N ≤ 7500). Each cow has a breed, represented by an integer in the range 1..N. The breed of the i-th cow is given by ai. Farmer John needs to take his cows for a checkup at the local hospital. However, the picky veterinarian will only examine the i-th cow if its breed is bi (another integer between 1 and N).

Farmer John, being rather lazy, will perform exactly one reversal operation: he selects two integers l and r (1 ≤ l ≤ r ≤ N) and reverses the order of the cows in positions l through r (inclusive).

After the reversal, the position i (1 ≤ i ≤ N) will be examined if the cow at that position has the correct breed for that position. In other words, if the resulting breed at position i equals bi, then cow i is checked.

Let the original number of positions matching (i.e. where ai = bi) be M. When a segment from l to r is reversed, any cow outside that segment remains unchanged while for positions inside the segment the cow originally at position i moves to position l+r-i. Therefore, after reversal the total number of cows examined is:

$$\text{Final matches} = M + \sum_{i=l}^{r} \Bigl(\mathbf{1}_{\{a_{l+r-i}=b_i\}} - \mathbf{1}_{\{a_i=b_i\}}\Bigr). $$

Your task is: for each c from 0 to N, count the number of distinct reversal operations \((l,r)\) that yield exactly c cows examined. Two operations \((l_1,r_1)\) and \((l_2,r_2)\) are considered different if \(l_1\neq l_2\) or \(r_1\neq r_2\).

Note: We recommend using a language other than Python to obtain full score on this problem.

inputFormat

The first line contains an integer N. The second line contains N space‐separated integers representing array a. The third line contains N space‐separated integers representing array b.

outputFormat

Output N+1 lines. The i-th line (0-indexed) should contain the number of reversal operations that result in exactly i cows being examined (i.e. the final number of positions where the cow’s breed matches the required breed).

sample

3
1 2 3
1 2 3
0

3 0 3

</p>