3552.Two Permutations

Time Limit: 1s Memory Limit: 256MB

Rubik is very keen on number permutations.

A permutation a with length n is a sequence, consisting of n different numbers from 1 to n. Element number i (1 \le i \le n) of this permutation will be denoted as ai.

Furik decided to make a present to Rubik and came up with a new problem on permutations. Furik tells Rubik two number permutations: permutation a with length n and permutation b with length m. Rubik must give an answer to the problem: how many distinct integers d exist, such that sequence c (c1=a1+d,c2=a2+d,...,cn=an+d) of length n is a subsequence of b.

Sequence a is a subsequence of sequence b, if there are such indices i1,i2,...,in (1 \le i1 \lt i2 \lt ... \lt in \le m), that a1=bi1, a2=bi2, ..., an=bin, where n is the length of sequence a, and m is the length of sequence b.

You are given permutations a and b, help Rubik solve the given problem.

Input Format(From the terminal/stdin)

The first line contains two integers n and m (1 \le n \le m \le 200000) - the sizes of the given permutations. The second line contains n distinct integers - permutation a, the third line contains m distinct integers - permutation b. Numbers on the lines are separated by spaces.

Output Format(To the terminal/stdout)

On a single line print the answer to the problem.

Sample Input 1

Copy
1 1
1
1
 · \n
 \n
 \n

Sample Output 1

Copy
1
 \n

Sample Input 2

Copy
1 2
1
2 1
 · \n
 \n
 · \n

Sample Output 2

Copy
2
 \n

Sample Input 3

Copy
3 3
2 3 1
1 2 3
 · \n
 · · \n
 · · \n

Sample Output 3

Copy
0
 \n

Submit

请先 登录

© 2025 FAQs