#P7149. [USACO20DEC] Rectangular Pasture S

[USACO20DEC] Rectangular Pasture S

Description

Farmer John’s largest pasture can be viewed as a huge two-dimensional grid made of square cells (imagine a huge chessboard). Now, there are NN cows occupying some of the cells (1N25001 \le N \le 2500).

Farmer John wants to build a fence that encloses a rectangular region; this rectangle must have its four sides parallel to the xx-axis and the yy-axis, and it must contain at least one cell. Please help him compute the number of different subsets of cows that he can enclose in such a region. Note that the empty set should be counted as one of the answers.

Input Format

The first line of input contains an integer NN. The next NN lines each contain two space-separated integers, indicating the coordinates (x,y)(x,y) of the cell occupied by a cow. All xx coordinates are distinct, and all yy coordinates are distinct. All values of xx and yy are in the range 01090 \ldots 10^9.

Output Format

Output the number of subsets of cows that FJ can enclose. It can be proven that this number fits in a 64-bit signed integer type (for example, long long in C/C++).

4
0 2
1 0
2 3
3 5
13

Hint

There are 242^4 subsets in total. FJ cannot build a fence that encloses only cows 11, 22, and 44, or only cows 22 and 44, or only cows 11 and 44, so the answer is 243=163=132^4 - 3 = 16 - 3 = 13.

  • Testdata 2-3 satisfy N20N \le 20.
  • Testdata 4-6 satisfy N100N \le 100.
  • Testdata 7-12 satisfy N500N \le 500.
  • Testdata 13-20 have no additional constraints.

Problem by: Benjamin Qi.

Translated by ChatGPT 5