#P10057. Line

Line

Problem Description

On a 2D plane, you are given two line segments ABAB and CDCD, which are parallel to the xx-axis and the yy-axis respectively.

You may choose one segment and translate it in any direction parallel to the coordinate axes (up, down, left, or right) by any distance. This is called one operation.

What is the minimum number of operations needed to make the two segments intersect?

Input Format

This problem has multiple test cases.

The first line contains an integer TT, the number of test cases.

For each test case:

  • One line contains eight positive integers xA,yA,xB,yB,xC,yC,xD,yDx_A,y_A,x_B,y_B,x_C,y_C,x_D,y_D, representing the coordinates of A,B,C,DA,B,C,D.

Output Format

For each test case:

  • Output one integer per line, the minimum number of operations.
3
1 1 2 1 1 1 1 2
1 1 2 1 1 2 1 3
2 1 3 1 1 2 1 3
0
1
2

Hint

[Sample 1 Explanation]

  • For the first test case, the two segments already intersect, so no operation is needed.
  • For the second test case, one feasible plan is: translate segment ABAB upward by 11 unit.
  • For the third test case, one feasible plan is: translate segment ABAB upward by 11 unit, then translate segment CDCD rightward by 11 unit.

[Constraints]

Let M=max(xA,yA,xB,yB,xC,yC,xD,yD)M=\max(x_A,y_A,x_B,y_B,x_C,y_C,x_D,y_D).

Test Point ID TT\le MM\le Special Property
11 1010 1010 None
232\sim 3 5050
454\sim 5 10310^3
676\sim 7 10510^5 10910^9 The answer is guaranteed to be at most 11
8108\sim 10 10910^{9} None

For 100%100\% of the testdata, 1T1051\le T\le 10^5, 1M1091\le M\le 10^{9}, xA<xBx_A<x_B, xC=xDx_C=x_D, yA=yBy_A=y_B, yC<yDy_C<y_D.

Translated by ChatGPT 5