Random intersection

Time Limit : 40 Second

Memory Limit : 128 MB

Submission: 142

Solved: 37

Description
There are N segments on the plane.
You are to find out how many pairs of segments intersect with each other.
A pair of segments intersect with each other if and only if they have at least one common point.
Input
the first line contains a single integer T -- the number of the testcases
then T testcases are followed

each testcase contains an integer N( N <= 100000), the number of segments
then N lines are followed, each line contains 4 floating point numbers:

x1 y1 x2 y2

which means the coordinates of the two ends of a segment

for more details, see the sample input
(the input data is generated randomly, and the probability of a pair of segments intersecting is about 0.001)
Output
for each testcase, print 1 line with the number of the pairs of segments intersect with each other.
sample input
3
3
0.0000 0.0000 1.0000 2.0000
0.0000 1.0000 1.0000 0.0000
0.0000 2.0000 1.0000 1.0000
4
0.0000 0.0000 0.0000 -1.0000
0.0000 0.0000 1.0000 0.0000
0.0000 0.0000 0.0000 1.0000
0.0000 0.0000 -1.0000 0.0000
2
0.0000 0.0000 1.0000 1.0000
0.0000 0.0000 2.0000 2.0000
sample output
2
6
1
hint
source
Hust Monthly 10.06.13/Li ZHANG
© 2015 HUST ACMICPC TEAM. All Right Reserved.