cswork / OA打开练习工作台

DatabricksOA #24

Subarray Counting

估计中等可在线判题

考点(AI 标注)原地数组字符串

You are given numbers and a pattern containing only -1, 0, and 1. For a contiguous segment of elements, compare each pair of adjacent values: record 1 if the next value is larger, 0 if it is equal, and -1 if it is smaller. Count the contiguous segments whose change sequence exactly equals pattern.

Standard input/output and this site's supplementary ranges follow this problem's description.

输入格式

The first line contains n m (2≤n≤200000, 1≤m≤200000), the second line contains n integers (−10⁹..10⁹), and the third line contains m values, each −1, 0, or 1. The upper bound on m is a site supplement. A matching segment contains m+1 elements of numbers.

输出格式

Output the number of matching contiguous subarrays, as one integer.

样例 1

输入:

8 3
4 1 3 4 4 5 5 1
1 0 -1

输出:

1

样例 2

输入:

4 3
1 2 2 1
1 0 -1

输出:

1

样例 3

输入:

2 1
3 3
0

输出:

1