| 2024湖南省赛 |
|---|
| Finished |
A beautiful sequence is defined as a nonempty sequence that does not contain duplicate numbers, and the difference between the maximum and minimum values of the sequence is $$$k-1$$$, where $$$k$$$ is the length of the sequence. For example, $$$[1]$$$ and $$$[6, 4, 3, 5]$$$ are beautiful sequences, while $$$[2, 3, 2]$$$ and $$$[6, 9, 5, 8]$$$ are not.
There are two permutations, both of which contain $$$n$$$ distinct numbers from $$$1$$$ to $$$n$$$. As a sequence enthusiast, Alice wants to know how many beautiful sequences appear as subsequences in both permutations at the same time.
A sequence $$$s$$$ is a subsequence of a sequence $$$t$$$ if $$$s$$$ can be obtained from $$$t$$$ by deletion of several (possibly, zero or all) elements.
The first line contains an integer $$$ n $$$ $$$(1 \leq n \leq 100000)$$$ — the length of these two permutations.
The second line contains $$$n$$$ distinct integers, representing the first permutation.
The third line contains $$$n$$$ distinct integers, representing the second permutation.
An integer representing the number of the beautiful sequences appear as subsequences in both permutations at the same time.
42 4 1 34 2 3 1
7
In the test case, the beautiful sequences that meet the condition are $$$[1]$$$, $$$[2]$$$, $$$[3]$$$, $$$[4]$$$, $$$[2,1]$$$, $$$[2,3]$$$, $$$[4,3]$$$.
| Name |
|---|


