Subway Ride
Time limit8sMemory limit1024 MB
Given N axis-parallel subway segments, find the sum of minimum transfer counts d(i,j) times i times j over all pairs, modulo 1000000007.
- Level
Medium7 of 10
- Topics
- Graph, BFS, Segment tree, Sorting
- Solved
- No attempts yet
Problem
Jiho is an expert on the subway. Eunho rides the subway often and keeps asking Jiho how to get from one subway line to another. Jiho always gives the route that requires the most transfers, which wears Eunho out.
Fed up with Jiho's answers, Eunho wants to stop asking him. For every pair of subway lines, Eunho will find the minimum number of transfers needed to get from one line to the other.
There are subway lines in total. Each line is a segment parallel to the x-axis or the y-axis. If the segments of two subway lines share a point, you can move from one line to the other with one transfer. Two lines that are both parallel to the x-axis, or both parallel to the y-axis, never allow a transfer between them. The length of every line is at least .
Help Eunho compute, for every pair of lines, the minimum number of transfers needed to go from one line to the other.
Let be the minimum number of transfers needed to go from line to line . Find the sum of over all ordered pairs of integers with , modulo . If it is impossible to transfer from line to line , then .
Input
The first line contains the number of subway lines .
Each of the next lines contains four integers , , , , separated by spaces, that describe line . Line is the segment connecting and . The input guarantees that or , and that the length of each segment is at least .
Output
Print the answer to the problem on the first line.
Constraints
- For every subway line,