Immunization Operation
Memory limit1024 MB
Given each vaccine's pickup and delivery points and a list of east or west moves, count the vaccinations completed during each move.
- Level
Medium6 of 10
- Topics
- Sorting, Simulation, Binary search, Array
- Solved
- No attempts yet
Problem
Making every vaccine available to the entire world population is a complicated problem in many respects. Ñambi is leading the effort to optimize delivery. To reduce access barriers as much as possible, she is designing robots that deliver and apply vaccines directly in patients' homes.
In the current iteration, the robot works on a single street that runs from west to east. The robot accepts a single command, 'move meters'. If is positive, the robot moves meters east. If is negative, the robot moves meters west.
At the start of the day, the robot is loaded with information about all immunizations it must provide that day. Each immunization has a pickup location, where the vaccine is currently kept, and a delivery location, where the patient lives. Each vaccine is custom-made for one patient. A delivery location is never the same as its pickup location. The robot must pick up a vaccine before it delivers it.
The robot picks up a vaccine the first time it passes through that vaccine's pickup location. It delivers a vaccine as soon as it passes through the delivery location, provided the vaccine has already been picked up. Ñambi wants to track how many vaccinations happen after each movement command. A vaccination happens when a vaccine is delivered. A vaccine may be picked up during any earlier command, or during the same command as its delivery, as long as the pickup happens before the delivery.
The figure below shows one possible scenario (Sample Case #1). The smiley face is the robot's starting position, and the long black line is the street. Marks above the line are pickup locations, and marks below are delivery locations. The arrows below show the moves in order from top to bottom, labeled with the number of deliveries completed during each move.

What happens during each move, in order:
- Move 1. The robot picks up vaccines 5 and 1, delivers vaccine 1, and picks up vaccine 3 just as the move finishes. The robot passes the delivery location of vaccine 3 before picking it up, so it cannot deliver it.
- Move 2. The robot passes the delivery locations of vaccines 1 and 4. Vaccine 1 is already delivered and vaccine 4 has not been picked up, so no vaccination finishes.
- Move 3. The robot delivers vaccine 3.
- Move 4. The robot picks up vaccine 2, delivers vaccine 5, and picks up vaccine 4.
Vaccines 2 and 4 are picked up but not delivered. The delivery location of vaccine 2 is never reached, and the delivery location of vaccine 4 is not reached after the vaccine is picked up.
Given the list of immunizations and the list of move commands, the robot executes the commands in order. Compute how many vaccinations are completed after each command.
Input
The first line contains the number of test cases, . Each test case consists of four lines.
The first line of a test case contains two integers and , the number of vaccinations and the number of move commands.
The second line contains integers . The -th vaccine must be picked up exactly meters east of the robot's initial location. Several vaccines can share the same pickup location.
The third line contains integers . The -th vaccine must be delivered exactly meters east of the robot's initial location. Several vaccines can share the same delivery location.
The fourth line contains integers . The absolute value of is the number of meters the robot moves for the -th command. The move is east if is positive and west if it is negative. Vaccinations can happen in an order different from the input numbering, but movement commands happen in the given order.
Output
For each test case, output one line containing Case #x: y1 y2 ... yM, where is the test case number (starting from 1) and is the number of vaccinations completed during the -th movement command.
Limits
- .
- , for all .
- , for all .
- , for all .
- , for all .
- , for all .
Hints
Sample Case #1 is the one explained and illustrated in the problem statement.
In Sample Case #2 and Sample Case #3, a vaccine can be picked up and delivered in the same move only if its pickup place is visited first. A pickup and delivery can also happen exactly as a move ends.
In Sample Case #4, the robot moves 987654321 meters west five times, then 987654321 meters east four times, then 987654323 meters east. The only pickup and delivery both happen during the final move. The commands can be very large, so the robot can end up very far from its starting position, either west or east.