knightnemo commited on
Commit
1a97858
·
verified ·
1 Parent(s): 4c3d73e

Upload code_segments/segment_73.txt with huggingface_hub

Browse files
Files changed (1) hide show
  1. code_segments/segment_73.txt +43 -0
code_segments/segment_73.txt ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Turtle just received $n$ segments and a sequence $a_1, a_2, \ldots, a_n$. The $i$-th segment is $[l_i, r_i]$.
2
+
3
+ Turtle will create an undirected graph $G$. If segment $i$ and segment $j$ intersect, then Turtle will add an undirected edge between $i$ and $j$ with a weight of $|a_i - a_j|$, for every $i \ne j$.
4
+
5
+ Turtle wants you to calculate the sum of the weights of the edges of the minimum spanning tree of the graph $G$, or report that the graph $G$ has no spanning tree.
6
+
7
+ We say two segments $[l_1, r_1]$ and $[l_2, r_2]$ intersect if and only if $\max(l_1, l_2) \le \min(r_1, r_2)$.
8
+
9
+ Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). The description of the test cases follows.
10
+
11
+ The first line of each test case contains a single integer $n$ ($2 \le n \le 5 \cdot 10^5$) — the number of segments.
12
+
13
+ The $i$-th of the following $n$ lines contains three integers $l_i, r_i, a_i$ ($1 \le l_i \le r_i \le 10^9, 1 \le a_i \le 10^9$) — the $i$-th segment and the $i$-th element of the sequence.
14
+
15
+ It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
16
+
17
+ For each test case, output a single integer — the sum of the weights of the edges of the minimum spanning tree of the graph $G$. If the graph $G$ has no spanning tree, output $-1$.
18
+
19
+ In the first test case, the graph $G$ is as follows:
20
+
21
+ ![](CDN_BASE_URL/7b0a32f8f1d54a5dbbaffee1efd8ece1)
22
+
23
+ One of the minimum spanning trees of $G$ is as follows:
24
+
25
+ ![](CDN_BASE_URL/7a035af2d5227648781d0c24b81c076e)
26
+
27
+ The sum of the weights of the edges of the minimum spanning tree is $9$.
28
+
29
+ In the second test case, the graph $G$ is as follows:
30
+
31
+ ![](CDN_BASE_URL/62be880a08fa85071b491d6badd5f58e)
32
+
33
+ $G$ is already a tree, and the sum of the weights of the tree is $13$.
34
+
35
+ In the third test case, the graph $G$ is as follows:
36
+
37
+ ![](CDN_BASE_URL/b667aa771d019e3a8a3a4c4372200f03)
38
+
39
+ In the fourth test case, the graph $G$ is as follows:
40
+
41
+ ![](CDN_BASE_URL/45bc2d052677dbf032ff37fd1d723d83)
42
+
43
+ It's easy to see that $G$ is no