Sultan's Land
InterviewTime limit1sMemory limit128 MB
Given P pillars on an N by N grid, count how many sets of four pillars form the corners of an axis-aligned rectangle.
- Level
Medium4 of 10
- Topics
- Array, Hash map, Combinatorics, Geometry
- Solved
- No attempts yet
Problem
Sultan Al-Bandar, the ruler of the Old West Sumatra Sultanate, has decided to give his land to his only son. Although the Sultan believes his son would never misuse the land, he still has doubts about his son's ability to rule. So he decided to give his son a puzzle as a test.
On his land the Sultan drew an grid (imaginary, of course) in which every pair of adjacent grid intersections is the same distance apart. He then placed some pillars on the land, each pillar standing on exactly one intersection, and summoned his son.
"My dear son, if you were to choose four of these pillars so that each one serves as a corner of a single region, how many different selections could you make?" asked the Sultan. His son answered with a broad smile. Remembering that his son works for the Sultanate's Advanced Combinatorial Ministry, the Sultan quickly added: "Ah — and every side of the region must be parallel to the grid lines I drew."
The Sultan had not actually worked out an answer himself; he asked on a whim after seeing that smile. Help him by writing a program that counts how many such selections are possible.
Input
The input contains several test cases. Each case begins with two integers (), the grid size, and (), the number of pillars. Rows and columns are numbered from to . Each of the next lines contains two integers and (), the row and column of a pillar.
The input is terminated by a line containing two zeros, which must not be processed.
Output
For each test case, output a single line containing the number of ways to choose four pillars that form the corners of a region whose sides are parallel to the grid lines (that is, an axis-aligned rectangle).