Ornithology
InterviewTime limit5sMemory limit1024 MB
Count the pairs of bird segments from the first line to the second line that cross, given each start position's desired targets.
- Level
Medium4 of 10
- Topics
- Array, Prefix sum
- Solved
- No attempts yet
Problem
On the outskirts of town, close to a farm, two parallel power lines run with a narrow dirt road between them. The lines are a favorite resting spot for many birds.
On this cool autumn morning, a flock of birds sits on the lines. The first line has consecutive positions for birds, numbered . The second line also has positions numbered the same way.
At the start, some positions on the first line hold birds (a position may hold zero birds), and the second line is empty. Each bird has a desired position on the second line. Birds that start from the same position have different desired positions.
At one moment, all birds fly to their desired positions at once. Each bird flies along the straight segment connecting its start and desired position.
If two segments cross, the two birds may collide. An unordered pair of birds like this is a dangerous pair.
For example, if the bird on position 2 wants to go to position 1 and the bird on position 1 wants to go to position 2, their paths cross.
Pairs of birds that start from the same position or want the same desired position are not dangerous pairs.
Count the dangerous pairs.
Input
The first line contains an integer (), the number of positions on each power line.
Each of the next lines describes the desired positions of the birds on one starting position. The -th line starts with an integer (), the number of birds on position . Then come distinct integers (), the desired positions of those birds.
The sum of all does not exceed .
Output
Print one line containing one integer, the number of dangerous pairs.