MST and Rectangles

N×N 영행렬에서 Q개의 질의가 두 직사각형 영역에 W를 더해 완전 그래프의 간선 가중치를 만든 뒤, 그 최소 신장 트리의 비용을 출력한다.

어려움8동적 계획법그리디최소 신장 트리구현아직 제출이 없습니다시간 제한8초메모리 제한1024 MB

문제

You have a 2D array A of size N×NN \times N. Each cell initially contains 0.

You are also given Q queries of the form X1 X2 Y1 Y2 W, which means you should perform the following operation on A:

  • For each pair of integers (i, j) where X1 ≤ X2 < Y1 ≤ Y2 and X1 ≤ i ≤ X2 and Y1 ≤ j ≤ Y2 , add W to the value of cell A[i][j] and A[j][i].

Consider an undirected graph G with N vertices. For each 1 ≤ i, j ≤ N, there is an edge connecting vertices i and j with cost A[i][j]. Calculate the cost of Minimum Spanning Tree of G.

입력

The first line contains a single integer N, Q.

The next Q lines contain five integers X1 X2 Y1 Y2 W, describing a query.

출력

Print the cost of Minimum Spanning Tree of G.

제한

  • 1N,Q1051 \le N, Q \le 10^5
  • 1X_1X_2<Y_1Y_2N1 \le X\_1 \le X\_2 < Y\_1 \le Y\_2 \le N
  • W106|W| \le 10^6