Communication Jamming
Time limit2sMemory limit256 MB
Given two planar networks linking villages on the x-axis to hubs above and below, find for each A the largest B that keeps all villages connected after cutting hubs.
- Level
Hard8 of 10
- Topics
- Union-find, Graph, Sorting, Binary search
- Solved
- No attempts yet
Problem
JOI Country is on a plane. There are N villages, numbered 1 to N. Village i is treated as the point . JOI Country is now building communication lines that connect the villages. To prepare for failures, the lines will be built as two systems, called system 1 and system 2.
System k has hubs and lines. The hubs of system k are numbered 1 to , and hub j is treated as the point . Each line of system k connects a village to a hub of system k, or connects two hubs of system k. Each line is treated as the segment between its two endpoints. No two lines share a point except at their endpoints. The y coordinate of each hub j in system 1 is greater than 0. The y coordinate of each hub j in system 2 is less than 0.
Two locations can communicate if they are connected through lines, directly or indirectly. In other words, they can communicate if one can travel along the lines from one to the other. Using only the lines of system 1, or using only the lines of system 2, any two villages and hubs can communicate.
The figure below shows an example of the network. Gray circles are hubs of system 1, black circles are hubs of system 2, and white circles are villages.
While planning, we want to know how much attack the network can survive and still keep communication. An external attack is described by two integers A and B (, ). The attack destroys every hub with a y coordinate greater than A and every hub with a y coordinate less than B. When a hub is destroyed, communication through that hub is no longer possible.
The information about the villages and each system is given. Then Q queries are given. Each query q is one integer , which means that every hub with a y coordinate greater than is destroyed. For each query, find whether all villages can still communicate when additional hubs with y coordinates below some value are also destroyed. That is, find the largest integer () such that all villages can still communicate after destroying every hub with a y coordinate greater than and every hub with a y coordinate less than .
Input
The first line contains four integers N, M1, M2, Q separated by spaces.
The next lines describe system 1.
- The first lines give, for the i-th line, two integers and .
- The next lines give, for the i-th line, three integers , , describing line i, where is 1 or 2.
- If is 1, line i connects village and hub (, ).
- If is 2, line i connects hub and hub (, ).
The information for system 2 follows in the same format, with lines.
The last Q lines give, for the i-th line, one integer .
Output
Print Q lines. The i-th line contains the answer to query i. If the answer is 0, print 0, not -0.
Constraints
- ().
- ().
- ().
- ().
- For with , or .
- For with , or .
- ().
- No two lines share a point except at their endpoints.
- Using only the lines of system 1, or only the lines of system 2, any two villages and hubs can communicate.

