## Editorial for An Animal Contest 5 P5 - Counting Rectangles

**only**when stuck, and

**not to copy-paste code from it**. Please be respectful to the problem author and editorialist.

**Submitting an official solution before solving the problem yourself is a bannable offence.**

Author:

#### Subtask 1

For each pair of horizontal lines, we can find the number of pairs of vertical lines that intersect both horizontal lines by looping through all vertical lines in order of their x-axis.

**Time Complexity:**

#### Subtask 2

For full marks, we need a faster way to query the number of rectangles. One possible solution is described here. For each horizontal line , find any vertical lines that intersect and insert them into a BIT. Then, run line sweep descending the y-axis. When we reach the endpoint of a vertical line that intersected , remove it from the BIT. When we reach another horizontal line , query the union of the two horizontal lines to get the number of vertical lines that intersect both and . To exclude rectangles with an area of , we maintain a frequency array and another BIT to keep track of the number of pairs of overlapping vertical lines on each x-coordinate. A well implemented solution should pass comfortably under the time limit.

**Time Complexity:**

## Comments