Northwest Wind
Time limit1sMemory limit256 MB
Count pairs of islands where one can sail to the other moving only east or south (both coordinates monotone between the two points).
- Level
Medium5 of 10
- Topics
- Sorting, Prefix sum, Binary search, Array
- Solved
- No attempts yet
Problem
A strong northwest wind is blowing. This means you can sail in any direction between east and south (including due east and due south), but you can never sail toward the north or the west.
There is a sea dotted with several small islands. Each island is a point in the coordinate plane, where increasing points north and increasing points east.
A pair of islands counts if you can sail from one of them to the other using the northwest wind. Write a program that counts the number of island pairs you can travel between with the northwest wind.
Input
The first line contains the number of test cases .
The first line of each test case contains the number of islands (). Each of the next lines contains the coordinates and of one island, separated by a space (). No two islands share the same coordinates.
Output
For each test case, print the number of island pairs you can travel between with the northwest wind, one per line.