#P11134. 【MX-X5-T6】「GFOI Round 1」Abiogenesis

【MX-X5-T6】「GFOI Round 1」Abiogenesis

Background

Original link: https://oier.team/problems/X5G.


Abiogenesis - Juggernaut.

This problem is adapted from Codeforces 1981 E. Turtle and Intersected Segments.

Problem Description

You are given nn segments [li,ri][l_i, r_i]. The ii-th segment has a pair of weights ai,bia_i, b_i.

There is an undirected graph GG that initially has nn vertices and 00 edges. For every pair of positive integers i,ji, j such that 1i<jn1 \le i < j \le n, if segments ii and jj intersect, then add an edge in GG with endpoints ii and jj, and edge weight ai+aj+bibja_i + a_j + |b_i - b_j|.

Find the sum of edge weights of a minimum spanning tree of GG, or report that GG is disconnected.

Two segments [l1,r1][l_1, r_1] and [l2,r2][l_2, r_2] are considered intersecting if max(l1,l2)min(r1,r2)\max(l_1, l_2) \le \min(r_1, r_2).

Input Format

This problem has multiple test cases.

The first line contains a positive integer TT, the number of test cases.

For each test case:

The first line contains a positive integer nn, the number of segments.

Each of the next nn lines, the ii-th line contains four positive integers li,ri,ai,bil_i, r_i, a_i, b_i.

Output Format

For each test case, output one integer per line, the sum of edge weights of a minimum spanning tree of GG. In particular, if GG is disconnected, output 1-1.

4
5
1 7 1 3
2 4 2 6
3 5 3 5
6 7 2 9
3 4 1 4
5
2 7 3 3
1 3 5 6
4 5 3 5
6 7 1 9
1 1 5 4
4
1 4 1 3
1 2 2 1
3 4 3 5
1 4 4 4
3
1 3 1 1
2 3 1 3
4 5 1 8
22
41
17
-1

Hint

Sample Explanation

For the first test case, the graph GG looks like this:

One possible minimum spanning tree of GG looks like this:

Constraints

This problem uses bundled testdata and has subtask dependencies enabled.

Subtask ID nn \le n\sum n \le Special Property Dependencies Score
11 100100 10510^5 None None 1111
22 10510^5 AC 55
33 A 22 1414
44 B None
55 C 22
66 D None
77 None 1,2,3,4,5,61, 2, 3, 4, 5, 6 2828
  • Special Property A: i[1,n],li=1\forall i \in [1, n], l_i = 1.
  • Special Property B: $\forall i \in [1, n - 1], l_i \le l_{i + 1}, r_i \le r_{i + 1}$.
  • Special Property C: i[1,n],bi=1\forall i \in [1, n], b_i = 1.
  • Special Property D: i[1,n],ai=bi\forall i \in [1, n], a_i = b_i.

For all testdata, it holds that 1T1041 \le T \le 10^4, 1n,n1051 \le n, \sum n \le 10^5, 1li,ri,ai,bi1081 \le l_i, r_i, a_i, b_i \le 10^8, and liril_i \le r_i.

Translated by ChatGPT 5