Sprinklers
Time limit2sMemory limit512 MB
Given a permutation of N sprinklers, count axis-aligned integer rectangles whose every point lies both northeast of some sprinkler and southwest of another, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Combinatorics, Divide and conquer, Segment tree, Math
- Solved
- No attempts yet
Problem
Farmer John's field is a square. Its southwest corner is at and its northeast corner is at . John wants to plant sweet corn in part of it.
At some integer coordinates there are double-headed sprinklers, and each one sprays both water and fertilizer. A sprinkler at waters the part of the field north and east of itself, and fertilizes the part south and west of itself. Formally, it waters every real coordinate with and , and it fertilizes every real coordinate with and .
John picks an axis-aligned rectangle inside the field whose four corners all have integer coordinates, and plants sweet corn in it. For the corn to grow, every point of the rectangle must be both watered and fertilized. The rectangle must also have area greater than , or there is no room for corn.
Help John count the rectangles in which he could grow sweet corn. This number can be large, so report it modulo .
Input
The first line contains one integer , the size of the field ().
Each of the next lines contains two space-separated integers and (), meaning that a sprinkler sits at .
Every column holds exactly one sprinkler and every row holds exactly one sprinkler. That is, no two sprinklers share an -coordinate, and no two sprinklers share a -coordinate.
Output
Print one line with the number of rectangles of area greater than that are fully watered and fully fertilized, modulo .