F - Range Connect MST Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500500

問題文

N+QN + Q 頂点のグラフがあり、頂点には 1,2,,N+Q1, 2, \ldots, N + Q の番号がついています。グラフにははじめ辺がありません。

このグラフに対して i=1,2,,Qi = 1, 2, \ldots, Q の順に以下の操作を行います。

  • LijRiL_i \leq j \leq R_i を満たす各整数 jj について頂点 N+iN + i と頂点 jj の間にコスト CiC_i の無向辺を追加する

すべての操作を終えた後グラフは連結であるか判定し、連結である場合はこのグラフの最小全域木のコストを求めてください。

ただし、最小全域木とはコストが最小の全域木のことを指し、全域木のコストとは全域木に使われた辺のコストの和のことを指します。

制約

  • 1N,Q2×1051 \leq N, Q \leq 2 \times 10^5
  • 1LiRiN1 \leq L_i \leq R_i \leq N
  • 1Ci1091 \leq C_i \leq 10^9
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

NN QQ
L1L_1 R1R_1 C1C_1
L2L_2 R2R_2 C2C_2
\vdots
LQL_Q RQR_Q CQC_Q

出力

グラフが連結である場合は最小全域木のコストを出力せよ。そうでない場合は 1-1 を出力せよ。


入力例 1Copy

Copy
4 3
1 2 2
1 3 4
2 4 5

出力例 1Copy

Copy
22

以下の辺からなる全域木が最小全域木のひとつとなります。

  • 頂点 1155 を結ぶコスト 22 の辺
  • 頂点 2255 を結ぶコスト 22 の辺
  • 頂点 1166 を結ぶコスト 44 の辺
  • 頂点 3366 を結ぶコスト 44 の辺
  • 頂点 3377 を結ぶコスト 55 の辺
  • 頂点 4477 を結ぶコスト 55 の辺

2+2+4+4+5+5=222 + 2 + 4 + 4 + 5 + 5 = 22 であるため、2222 を出力します。


入力例 2Copy

Copy
6 2
1 2 10
4 6 10

出力例 2Copy

Copy
-1

グラフは非連結です。


入力例 3Copy

Copy
200000 4
1 200000 1000000000
1 200000 998244353
1 200000 999999999
1 200000 999999999

出力例 3Copy

Copy
199651870599998

Score : 500500 points

Problem Statement

There is a graph with N+QN + Q vertices, numbered 1,2,,N+Q1, 2, \ldots, N + Q. Initially, the graph has no edges.

For this graph, perform the following operation for i=1,2,,Qi = 1, 2, \ldots, Q in order:

  • For each integer jj satisfying LijRiL_i \leq j \leq R_i, add an undirected edge with cost CiC_i between vertices N+iN + i and jj.

Determine if the graph is connected after all operations are completed. If it is connected, find the cost of a minimum spanning tree of the graph.

A minimum spanning tree is a spanning tree with the smallest possible cost, and the cost of a spanning tree is the sum of the costs of the edges used in the spanning tree.

Constraints

  • 1N,Q2×1051 \leq N, Q \leq 2 \times 10^5
  • 1LiRiN1 \leq L_i \leq R_i \leq N
  • 1Ci1091 \leq C_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN QQ
L1L_1 R1R_1 C1C_1
L2L_2 R2R_2 C2C_2
\vdots
LQL_Q RQR_Q CQC_Q

Output

If the graph is connected, print the cost of a minimum spanning tree. Otherwise, print 1-1.


Sample Input 1Copy

Copy
4 3
1 2 2
1 3 4
2 4 5

Sample Output 1Copy

Copy
22

The following edges form a minimum spanning tree:

  • An edge with cost 22 connecting vertices 11 and 55
  • An edge with cost 22 connecting vertices 22 and 55
  • An edge with cost 44 connecting vertices 11 and 66
  • An edge with cost 44 connecting vertices 33 and 66
  • An edge with cost 55 connecting vertices 33 and 77
  • An edge with cost 55 connecting vertices 44 and 77

Since 2+2+4+4+5+5=222 + 2 + 4 + 4 + 5 + 5 = 22, print 2222.


Sample Input 2Copy

Copy
6 2
1 2 10
4 6 10

Sample Output 2Copy

Copy
-1

The graph is disconnected.


Sample Input 3Copy

Copy
200000 4
1 200000 1000000000
1 200000 998244353
1 200000 999999999
1 200000 999999999

Sample Output 3Copy

Copy
199651870599998


2025-04-18 (Fri)
13:27:00 +00:00