Cloud Retainer's Game
Time limit2sMemory limit1024 MB
Remove some wooden boards so a pinball bouncing between y=0 and y=H collects as many coins as possible, and report the maximum count.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Segment tree, Sorting
- Solved
- No attempts yet
Problem
Cloud Retainer, the builder of the Dwelling in the clouds above Qingyun Peak, is very interested in mechanics. The Lantern Rite Festival in Liyue is more than a month away, but she has already started designing a gaming event for it.

Cloud Retainer and the Traveler
The game is mainly about releasing pinballs to score as high as possible. It is played on a two-dimensional plane with two horizontal lines and . Between the two lines are tiny wooden boards and coins. Both are single points. The -th wooden board is at , and the -th coin is at .
The player releases a pinball from . Let be the velocity of the ball. If the ball is at , it moves to after seconds. Initially .
When the ball hits a wooden board or one of the two horizontal lines, is negated and stays the same. When the ball hits a coin, the score goes up by 1 and the velocity stays the same.
Before the release, the player may remove any number of wooden boards, including none. Cloud Retainer wants to estimate the difficulty of the game. Find the maximum score the player can get after seconds under the best strategy.
Input
The first line contains an integer , the number of test cases. Each test case has the following format.
The first line contains ().
The second line contains (), the number of wooden boards. The next lines each contain and (, ), the location of the -th wooden board.
The next line contains (), the number of coins. The next lines each contain and (, ), the location of the -th coin.
All points in a test case are distinct. The sums of and of over all test cases do not exceed .
Output
For each test case, print one line with the maximum score the player can get after removing some wooden boards, or none.
Hint
The figure below shows two test cases. Solid diamonds are remaining wooden boards, hollow diamonds are removed wooden boards, and round dots are coins.

