Connecting Cables
Time limit2sMemory limit1024 MB
Given N axis-aligned rectangles, find the sum over all pairs of the minimum Manhattan distance between them, modulo 998244353.
- Level
Medium6 of 10
- Topics
- Sorting, Prefix sum
- Solved
- No attempts yet
Problem
RUN was recently asked to connect cables between every pair of the areas of KAIST.
Each area is a region on the 2D plane. Its boundary is a rectangle with two edges parallel to the -axis and two edges parallel to the -axis. Area has its lower left corner at and its upper right corner at . Areas may overlap.
For safety reasons, cables must run along the -axis or the -axis. So the cost of a cable from to is won.
A cable between areas and connects one point from each region.
Find the minimum total cost to build all cables, one for each pair of areas. A single cable cannot serve several pairs, even if its endpoints fit more than one pair.
Since the answer can be large, print it modulo . The answer is always a non-negative integer.
Input
The first line contains an integer . Each of the next lines contains four space-separated integers , , , and , the coordinates of the lower left and upper right corners of the -th area.
Output
Print a single integer: the minimum total cost of all cables, modulo . Note that is prime.
Constraints
- ()
- ()