MST and Rectangles
Time limit8sMemory limit1024 MB
Given an N by N matrix updated by Q rectangle additions (mirrored across the diagonal), find the MST cost of the complete graph with edge weights A[i][j].
- Level
Hard8 of 10
- Topics
- Minimum spanning tree, Prefix sum, Union-find
- Solved
- No attempts yet
Problem
You have a 2D array of size . Each cell initially contains 0.
You are also given queries of the form , which means you should perform the following operation on :
- For each pair of integers where , , and , add to the values of cells and .
Consider an undirected graph with vertices. For each , there is an edge connecting vertices and with cost . Calculate the cost of the minimum spanning tree of .
Input
The first line contains two integers and .
Each of the next lines contains five integers , describing one query.
Output
Print the cost of the minimum spanning tree of .
Constraints
- Time limit: 8000 ms. Memory limit: 1024 MB.