Under construction

Still being built. Things may change

Communication Jamming

Time limit2sMemory limit256 MB

Summary
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 (i,0)(i, 0). 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 MkM_k hubs and N+Mk−1N + M_k - 1 lines. The hubs of system k are numbered 1 to MkM_k, and hub j is treated as the point (Xkj,Ykj)(X_{kj}, Y_{kj}). 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 Y1jY_{1j} of each hub j in system 1 is greater than 0. The y coordinate Y2jY_{2j} 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.

Figure 1: Example network 1Figure 2: Example network 2

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 (A≥0A \ge 0, B≤0B \le 0). 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 AqA_q, which means that every hub with a y coordinate greater than AqA_q 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 BqB_q (Bq≤0B_q \le 0) such that all villages can still communicate after destroying every hub with a y coordinate greater than AqA_q and every hub with a y coordinate less than BqB_q.

Input

The first line contains four integers N, M1, M2, Q separated by spaces.

The next M1+(N+M1−1)M_1 + (N + M_1 - 1) lines describe system 1.

  • The first M1M_1 lines give, for the i-th line, two integers X1iX_{1i} and Y1iY_{1i}.
  • The next N+M1−1N + M_1 - 1 lines give, for the i-th line, three integers T1iT_{1i}, C1iC_{1i}, D1iD_{1i} describing line i, where T1iT_{1i} is 1 or 2.
    • If T1iT_{1i} is 1, line i connects village C1iC_{1i} and hub D1iD_{1i} (1≤C1i≤N1 \le C_{1i} \le N, 1≤D1i≤M11 \le D_{1i} \le M_1).
    • If T1iT_{1i} is 2, line i connects hub C1iC_{1i} and hub D1iD_{1i} (1≤C1i,D1i≤M11 \le C_{1i}, D_{1i} \le M_1, C1i≠D1iC_{1i} \ne D_{1i}).

The information for system 2 follows in the same format, with M2+(N+M2−1)M_2 + (N + M_2 - 1) lines.

The last Q lines give, for the i-th line, one integer AiA_i.

Output

Print Q lines. The i-th line contains the answer BiB_i to query i. If the answer is 0, print 0, not -0.

Constraints

  • 1≤N,M1,M2≤1000001 \le N, M_1, M_2 \le 100000
  • −1000000000≤X1i≤1000000000-1000000000 \le X_{1i} \le 1000000000 (1≤i≤M11 \le i \le M_1).
  • −1000000000≤X2i≤1000000000-1000000000 \le X_{2i} \le 1000000000 (1≤i≤M21 \le i \le M_2).
  • 1≤Y1i≤10000000001 \le Y_{1i} \le 1000000000 (1≤i≤M11 \le i \le M_1).
  • −1000000000≤Y2i≤−1-1000000000 \le Y_{2i} \le -1 (1≤i≤M21 \le i \le M_2).
  • For 1≤i,j≤M11 \le i, j \le M_1 with i≠ji \ne j, X1i≠X1jX_{1i} \ne X_{1j} or Y1i≠Y1jY_{1i} \ne Y_{1j}.
  • For 1≤i,j≤M21 \le i, j \le M_2 with i≠ji \ne j, X2i≠X2jX_{2i} \ne X_{2j} or Y2i≠Y2jY_{2i} \ne Y_{2j}.
  • 1≤Q≤1000001 \le Q \le 100000
  • 0≤Ai≤10000000000 \le A_i \le 1000000000 (1≤i≤Q1 \le i \le Q).
  • 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.

Examples2

  1. Example 1

    Input
    4 3 3 1
    1 1
    3 2
    2 3
    1 1 1
    1 2 1
    1 3 2
    1 4 2
    2 1 3
    2 2 3
    3 -1
    2 -2
    1 -3
    1 1 3
    1 2 2
    1 3 1
    1 4 1
    2 1 2
    2 2 3
    2
    
    Expected output
    -2
    
  2. Example 2

    Input
    6 4 5 4
    2 1
    4 1
    3 3
    5 2
    1 1 1
    1 2 1
    1 3 2
    1 4 2
    2 2 4
    1 5 4
    1 6 4
    2 1 3
    2 4 3
    3 -3
    5 -1
    2 -2
    2 -1
    4 -2
    1 2 4
    1 3 4
    1 1 4
    2 1 3
    1 5 2
    1 6 2
    1 4 5
    2 2 5
    1 3 1
    2 5 1
    3
    1
    2
    0
    
    Expected output
    0
    -2
    -1
    -3