Hiring Workers
InterviewTime limit2sMemory limit1024 MB
Count contiguous groups of workers with equal numbers of task 1 and task 2 workers whose efficiency sums differ by at most K.
- Level
Medium5 of 10
- Topics
- Prefix sum, Hash map, Binary search
- Solved
- No attempts yet
Problem
Youngsam is playing a simulation game about running a factory. To keep the factory running smoothly, he wants to hire workers.
workers are standing in a line, and he wants to hire a contiguous group of them. The factory has two kinds of work. The -th worker can perform only one assigned task , which is either 1 or 2. Each worker also has its own efficiency .
To keep the factory efficient, Youngsam wants to hire workers that satisfy both of the following conditions:
- The number of workers performing each task must be the same.
- The difference between the sums of efficiencies of the two tasks must be at most .
Count the number of ways to choose workers so that he can hire them under these conditions.
Input
The first line contains the number of test cases .
The first line of each test case contains and , separated by a space.
The second line contains the task types in order, separated by spaces.
The third line contains the efficiencies in order, separated by spaces.
Output
For each test case, print the answer on its own line, in the order of the test cases.
Constraints
- All given numbers are integers.
- The sum of over all test cases is at most .
- ()
- ()