Drone Photo
Time limit2sMemory limit2048 MB
Count the axis-aligned rectangles of contestants where the two youngest corners form one pair of opposite sides and the two oldest form the other pair.
- Level
Hard8 of 10
- Topics
- Matrix, Sorting, Segment tree
- Solved
- No attempts yet
Problem
Today, as every year at SWERC, the contestants have gathered outside the venue to take a drone photo. Jennifer, the social media manager for the event, has arranged them into an square. She knows that the contestant at the intersection of row and column is years old. She also notices that no two contestants have the same age, and every age is between and .
Jennifer wants some contestants to hold a banner with the ICPC logo, parallel to the ground, so that it is clearly visible from the air. She follows these steps to take the perfect SWERC drone photo.
- First, she selects four contestants standing at the vertices of an axis-aligned rectangle.
- Then the two younger contestants hold one pole, and the two older contestants hold the other.
- Finally, she unfolds the banner and uses the poles to support its two ends. This works only if the two poles are parallel and do not cross (see the pictures below).

Jennifer would like to try every possible arrangement, but she worries this will make the contestants late for the competition. How many different ways are there to choose the four contestants holding the poles so that the photo is perfect? Two choices are different if at least one contestant is in one but not the other.
Input
The first line contains an integer (). The next lines describe the ages. The -th line contains the integers (). It is guaranteed that whenever or .
Output
Print the number of ways for Jennifer to choose the four contestants holding the poles.