Under construction

Still being built. Things may change

Target Practice II

Time limit2.5sMemory limit1024 MB

Summary
Assign each of the 4N cows on the y-axis to a distinct target vertex so no shot crosses a target interior, minimizing the largest distance between cows.
Level

Hard8 of 10

Topics
Geometry, Binary search, Greedy, Sorting
Solved
No attempts yet

Problem

Note: The large size of integers involved in this problem may require 64-bit integer data types (e.g., "long long" in C/C++).

The Paris Moolympics are coming up, and Farmer John is training his cows in archery. He has set up the following exercise on the 2D coordinate plane.

There are NN (1≤N≤4⋅1041 \leq N \leq 4 \cdot 10^4) axis-aligned rectangular targets and 4N4N cows. Every cow must be assigned to a different target vertex. At moment ii, for 1≤i≤N1 \leq i \leq N:

  1. Target ii appears.
  2. The 4 cows assigned to its vertices shoot at them.
  3. If a cow's shot passes through the interior of the target before it hits the assigned vertex or misses, the cows fail the exercise.
  4. The target disappears to make space for the next one.

Each cow is located on the yy-axis (x=0x = 0). Target ii is a rectangle with lower left corner (X1,y1(i))(X_1, y_1^{(i)}) and upper right corner (x2(i),y2(i))(x_2^{(i)}, y_2^{(i)}). The coordinates satisfy 1≤X1<x2(i)≤1091 \leq X_1 < x_2^{(i)} \leq 10^9 and 1≤y1(i)<y2(i)≤1091 \leq y_1^{(i)} < y_2^{(i)} \leq 10^9. Note that X1X_1 is the same for every target.

Each cow also has a "focus" angle, so it turns to a specific angle when it shoots. A shot travels in a straight line from the cow's position to its assigned vertex. The trajectory of cow ii has slope sis_i (0<∣si∣<1090 < |s_i| < 10^9).

Farmer John wants to examine the cows' technique closely, so he wants to minimize the distance between the two cows that are furthest apart. If he assigns each cow to a target vertex optimally and places the cows on the yy-axis, what is the minimum possible distance between the furthest cows? If the cows will always fail the exercise, output -1 instead.

Input

The first line contains TT (1≤T≤101 \leq T \leq 10), the number of test cases. Each test case is described as follows.

The first line of each test case contains two integers NN and X1X_1, the number of targets and the leftmost xx-coordinate of the targets. Then NN lines follow. The ii-th line contains three integers y1(i)y_1^{(i)}, y2(i)y_2^{(i)}, and x2(i)x_2^{(i)}, the lower yy-coordinate, the upper yy-coordinate, and the right xx-coordinate of target ii.

The last line contains 4N4N integers s1,s2,…,s4Ns_1, s_2, \dots, s_{4N}, where sis_i is the slope of cow ii's shot. The sum of NN across all test cases does not exceed 4⋅1044 \cdot 10^4.

Output

Print the minimum possible distance between the furthest cows, or −1-1 if the cows will always fail the exercise.

Examples1

  1. Example 1

    Input
    3
    2 1
    1 3 6
    4 6 3
    1 -1 2 -2 3 -3 4 -4
    2 1
    1 3 6
    4 6 3
    1 1 2 2 3 3 4 4
    2 1
    1 3 3
    4 6 3
    1 -1 2 -2 3 -3 4 -4
    
    Expected output
    17
    -1
    11