MountainCraft
Time limit5sMemory limit2048 MB
After each toggle of a mountain peak, the task is to print the total length of the visible skyline edges (the upper envelope of the triangles) within the viewport [0, w].
- Level
Hard9 of 10
- Topics
- Segment tree, Geometry, Implementation
- Solved
- No attempts yet
Statement
It is a glorious day in MountainCraft, a video game meta-world with an expansive open world full of mountains to explore. Your avatar wakes up on a remote island and stares at the mountains on the horizon.
The horizon is modeled as a Cartesian plane, with the -axis () separating land from sea. Each mountain peak is a point . The mountain's sides have slope and , forming a triangle with that peak at the top.
Your avatar can only see the parts of the mountains that lie in the viewport. The visible edges of the mountains, where they do not overlap other mountains, are rendered in bold. Where mountains overlap, the overlapping parts are not rendered in bold. Edges do not have to intersect for mountains to overlap.

Figure H.1: The first input after all mountains appear.
Due to graphical glitches, mountains can appear and disappear. After each change, output the total length of all bold lines currently visible in the viewport.
Input
The first line contains two integers () and (), where is the number of queries and is the width of the viewport. The viewport stretches from to .
Each of the next lines contains two integers () and (), the peak of some mountain. If is the peak of a visible mountain, that mountain disappears. Otherwise, that mountain appears.
Output
Output lines. For each query, in order, output one number: the total length of the bold lines rendered. Your answer is accepted if its absolute or relative error is within of the judge's answer.
Hint
In the first sample, the first two mountains intersect at the point . The parts of the mountains below that point are not rendered in bold.
In the second sample, the left mountain appears, then disappears, then appears again. The right mountain appears and then disappears.