The Great Wall (Small)
Time limit5sMemory limit512 MB
Count the interval attacks that breach a wall which rises to each successful attack's strength, judging attacks on the same day against the unchanged wall.
- Level
Medium5 of 10
- Topics
- Simulation, Intervals, Sorting
- Solved
- No attempts yet
Problem
You are studying the history of the Great Wall of China, built to hold off military incursions from the north. In this problem the Great Wall runs along a straight line, from infinity in the east to minus infinity in the west. That is a lot of distance to cover, so the Wall was not built all at once. The builders worked reactively instead: whenever a stretch of the border was attacked successfully, the Wall on that stretch was raised to the height that would have stopped an identical attack.
The northern border was attacked often by nomadic tribes. Each tribe attacks some interval with a strength . To repel the attack, the Wall must be at least high everywhere on the defended interval. If a single point inside the interval is lower than , the attack breaches the Wall there and succeeds. Only the interior of the interval counts. Two intervals that meet at one endpoint do not overlap.
A successful attack does no damage to the Wall. After the attack, every attacked part of the Wall that was lower than is raised to height , so the Wall grows in the minimal way that would have stopped the attack. If two or more attacks happen on the same day, the Wall is raised only after all of them resolve, and it is raised in the minimal way that would stop all of them.
Nomadic tribes do not stay in one place. They move east or west and attack the Wall periodically. In this problem each tribe moves at a constant speed and attacks at constant intervals, and the strength of its attacks changes by a constant amount after each attack. The strength can shrink from attrition or grow from experience.
In 250 BC the Wall did not exist yet and its height was zero everywhere. Given the full description of every nomadic tribe that attacked the Wall, determine how many attacks succeeded.
Input
The first line contains the number of test cases . Each test case begins with a line containing the number of tribes that attacked the Wall. lines follow. The th of them describes one tribe with eight space separated integers , , , , , , , .
- : the day of the tribe's first attack, where 1 January 250 BC is day 0
- : the number of attacks from this tribe
- , : the westmost and eastmost points of the Wall attacked on the first attack
- : the strength of the first attack
- : the number of days between two consecutive attacks by this tribe
- : the distance the tribe travels east between two consecutive attacks. A negative value means it travels west.
- : the change in strength between two consecutive attacks
Limits
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from 1 and is the number of attacks that succeed.
Explanation
In the first test case of the first example, tribe 1 attacks three times: on day 0 it hits the interval with strength 10, on day 2 it hits with strength 8, and on day 4 it hits with strength 6. All three succeed. Tribe 2 attacks three times with strength 8 each time. On day 10 it hits and succeeds (at position 2.5, for example, the Wall is still zero). On day 17 it hits and fails, because the Wall on is already 8 high and covers . On day 24 it hits and succeeds, because the Wall there was 6 high.
In the second test case the attacks of three tribes interleave.
- Day 0: tribe 2 attacks with strength 7 and succeeds.
- Day 1: tribe 1 attacks with strength 10 and tribe 2 attacks with strength 9. Both succeed, because the attacks are simultaneous and the Wall built after the first tribe's attack is not there in time to stop the second tribe.
- Day 2: tribe 2 attacks with strength 11 and succeeds. The Wall there was 10 high.
- Day 3: tribe 1 attacks with strength 10 and succeeds. On the same day tribe 3 attacks with strength 1 and fails, because the Wall there is 10 and 11 high.
- Day 4: tribe 3 attacks with strength 1 and succeeds. There was no Wall between 5 and 8.
- Day 5: tribe 3 attacks with strength 1 and fails, because a Wall of height 10 is there.