Two Arrays

4.2

18 votes
Algorithms, Binary Search, Medium, Prefix sum, Searching, Two pointer, prefix-sum
Problem

Let's define the matching number for two arrays X and Y, each with size n, as the number of pairs of indices (i,j) such that Xi=Yj (1i,jn).

You are given an integer K and two arrays A and B with sizes N and M respectively. Find the number of ways to pick two subarrays with equal size, one from array A and the other from array B, such that the matching number for these two subarrays is K.

Note: a subarray consists of one or more consecutive elements of an array.

Constraints

  • 1N,M2,000

  • 1Ai1,000,000,000 for each valid i

  • 1Bi1,000,000,000 for each valid i

  • 1KNM

Input format

The first line of the input contains three space-separated integers N, M and K.

The second line contains N space-separated integers A1,,AN.

The third line contains M space-separated integers B1,,BM.

Output Format

Print a single line containing one number denoting the number of ways to choose the pair of subarrays.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The following pairs of subarrays have matching number 1: ([1], [1]), ([2], [2]), ([3], [3]), ([1, 2], [1, 2]), ([1, 2], [2, 3]), ([2, 3], [1, 2]), ([2, 3], [2, 3]), ([1, 2, 3], [1, 2, 3]).

Editor Image

?