SEGVIS - Horizontally Visible Segments

no tags 

There is a number of disjoint vertical line segments in the plane. We say that two segments are horizontally visible if they can be connected by a horizontal line segment that does not have any common points with other vertical segments. Three different vertical segments are said to form a triangle of segments if each two of them are horizontally visible. How many triangles can be found in a given set of vertical segments?


Write a program that:

  • reads the description of a set of vertical segments,
  • computes the number of triangles in this set,
  • writes the result.


The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 20. The data sets follow.

The first line of each data set contains exactly one integer n, 1 <= n < = 8000, equal to the number of vertical line segments.

Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces: y'i, y''i, xi(that is the y-coordinate of the beginning of a segment, y-coordinate of its end and its x-coordinate, respectively). The coordinates satisfy: 0 < = y'i< y''i <= 8000, 0 < = xi <= 8000. The segments are disjoint.


The output should consist of exactly d lines, one line for each data set. Line i should contain exactly one integer equal to the number of triangles in the i-th data set.


Sample input:
0 4 4 
0 3 1 
3 4 2 
0 2 2 
0 2 3 

Sample output:

Added by:adrian
Time limit:3.25s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Resource:ACM Central European Programming Contest, Warsaw 2001