Fast Bridges
Time limit2sMemory limit1024 MB
Given a k by k grid plus n bridges that cut travel time, find the sum of shortest distances over all cell pairs modulo 998244353.
- Level
Hard10 of 10
- Topics
- Shortest path, Sorting, Math, Combinatorics
- Solved
- No attempts yet
Problem
Consider a square city of size . There is exactly one house in each cell.
People can walk from any cell to a neighbouring cell (sharing a side) in unit of time.
The government decided to build fast bridges to make the city better. Each fast bridge connects two cells and with and . People can travel from one end of a bridge to the other in units of time.
To analyze how much faster the city became, compute the sum of shortest distances between all pairs of cells. Since the sum can be large, output it modulo .
Input
The first line contains two integers and (, ), the number of bridges and the size of the city.
Each of the next lines contains four integers , , , (, , ). All tuples are different.
Output
Print a single integer: the sum of shortest distances between all pairs of cells, modulo .
Hint
In the first input, the shortest distance between every pair of cells is , so the sum is .