Grand Escape
Time limit3sMemory limit1024 MB
Each resident falls straight down, and the fall slows by one step each time it touches a wall. Find the time each resident needs to reach y=0.
- Level
Medium6 of 10
- Topics
- Segment tree, Sorting, Implementation
- Solved
- No attempts yet
Problem
SRC (Science Research City) is a city on the first quadrant of the two-dimensional plane. The city has walls. Each wall is drawn as a line segment parallel to the -axis. Specifically, the -th wall () is the segment connecting and . Heavy rain that has not stopped raises the risk of flooding, so SRC built shelters along the line .
The city has residents. The -th resident () is at . When SRC floods, each resident moves in the direction toward . The initial speed is .
Every time a resident touches a wall, the speed drops. Touching an endpoint of a segment also counts as touching the wall. If the speed before touching a wall was , the speed after touching is . In other words, the time needed to move 1 unit increases by 1. All -coordinates are distinct. So every resident and every wall has a different -coordinate, and no resident starts on a wall. Multiple -coordinates may be equal.
Input
The first line contains the number of walls .
The next lines each contain , , and , separated by spaces, describing the -th wall. The -th wall is the segment connecting and .
The next line contains the number of residents .
The next lines each contain the position and of the -th resident, separated by a space.
Output
Print lines. On the -th line, print the time it takes for the -th resident to reach .
Constraints
- (note that is possible)
- Every coordinate in the input satisfies .
- All -coordinates are distinct.
- All numbers in the input are integers.
Hint
Some test cases may have answers larger than , so use the long long type.