H-Shaped Figures

시간 제한2초메모리 제한1024 MB

요약
고정된 선분 PQ가 P와 Q를 각각 내부에 포함하도록 주어진 선분 두 개를 골라 H자 모양을 만드는 순서쌍의 수를 센다.
난이도

어려움10점 중 8점

유형
기하, 정렬, 그리디, 구현
정답자
아직 제출이 없습니다

문제

After a huge success of the last year's "K-Shaped Figures" problem, we've come up with an innovative "H-Shaped Figures" problem for this year. And we have some plans for the next 24 years.

Let's say that three segments PQPQ, aa, and bb on a plane form an H-shaped figure if:

  • point PP lies strictly inside segment aa, and segments PQPQ and aa are not collinear;
  • point QQ lies strictly inside segment bb, and segments PQPQ and bb are not collinear;
  • segments aa and bb do not have common points.
Valid H-shaped figuresInvalid H-shaped figures

You are given the coordinates of points PP and QQ, along with nn candidate segments for aa and bb. Note that some of the given segments may coincide, but they should still be treated as different segments.

Find the number of possible ways to choose one of the given nn segments as aa and another one as bb to form an H-shaped figure along with the given segment PQPQ.

입력

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1051 \le t \le 10^5). The description of the test cases follows.

The first line of each test case contains four integers x_P,y_P,x_Q,y_Qx\_P, y\_P, x\_Q, y\_Q, denoting the coordinates of points PP and QQ (−109≤x_P,y_P,x_Q,y_Q≤109-10^9 \le x\_P, y\_P, x\_Q, y\_Q \le 10^9). Points PP and QQ do not coincide.

The second line contains a single integer nn, denoting the number of candidate segments (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5).

The ii-th of the following nn lines contains four integers x_i,1,y_i,1,x_i,2,y_i,2x\_{i,1}, y\_{i,1}, x\_{i,2}, y\_{i,2}, denoting the coordinates of the endpoints of the ii-th segment (−109≤x_i,1,y_i,1,x_i,2,y_i,2≤109-10^9 \le x\_{i,1}, y\_{i,1}, x\_{i,2}, y\_{i,2} \le 10^9). All segments have positive lengths.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

출력

For each test case, print the number of ways to form an H-shaped figure using the given segment PQPQ and two of the candidate segments.

예제1

  1. 예제 1

    입력
    1
    0 0 4 0
    8
    0 0 2 1
    -1 -1 2 2
    3 3 5 -3
    0 2 6 -1
    2 -2 5 1
    -1 1 3 -3
    -1 0 2 0
    -1 -1 2 2
    
    예상 출력
    6