Collapse
Time limit6sMemory limit512 MB
For each query (W, P), count the connected components among towns 0 to P and among towns P+1 to N-1, using only the cables that stay within each side on day W+1.
- Level
Medium7 of 10
- Topics
- Graph, Union-find, Segment tree, Divide and conquer
- Solved
- No attempts yet
Problem
There are towns along a deep linear valley in JOI Country. The towns are numbered in order of distance from the sea. Mr. I, the chair of the JOI Country Scientific Committee, maintains bidirectional communication cables between the towns. At the start, there are no cables.
Mr. I has a cable construction plan for days. The plan for day () is given by three integers , , :
- If , they build a cable between towns and . No cable between these two towns exists at the start of that day.
- If , they remove the cable between towns and . That cable exists at the start of that day.
Cliff collapse often happens in JOI Country. If a collapse happens between town and town (), every cable connecting a town numbered at most with a town numbered at least becomes unavailable. When a collapse happens, they choose some towns to install base stations. The base stations must be placed so that every town can reach a base station by following available cables.
Mr. I has questions. Question is given by two integers , . It asks for the minimum number of base stations needed if a collapse happens between town and town at the end of day .
Input
The first line contains , , and . Each of the next lines contains , , . Each of the next lines contains and .
Output
Print lines. The -th line is the answer to the -th question.
Hint
Consider 5 towns. Below, denotes a cable connecting towns and .
- Suppose there are 4 cables , , , and when a collapse happens between towns 1 and 2. Cables and become unavailable, leaving and . The minimum is 3, with base stations at towns 0, 2, and 3.
- Suppose there are 6 cables , , , , , and when a collapse happens between towns 3 and 4. Cables , , and become unavailable, leaving , , and . The minimum is 2, with base stations at towns 0 and 4.