#P8500. [NOI2022] 冒泡排序

[NOI2022] 冒泡排序

Background

Recently, Little Z has become very interested in bubble sort.

Below is the pseudocode of bubble sort:

Input: a sequence a[1...n] of length n
Output: the result of sorting a in nondecreasing order
for i = 1 to n do:
    for j = 1 to n - 1 do
        if (a[j] > a[j + 1])
            swap the values of a[j] and a[j + 1]

The number of swaps in bubble sort is defined as the number of swaps performed during sorting, that is, the number of times line 6 of the bubble sort pseudocode above is executed. He wants to find a sequence with as few swaps as possible.

Background

Problem Description

All sequences studied by Little Z consist of nonnegative integers. The length of the sequence is nn, and it must satisfy mm additional constraints. The ii-th constraint is:

Among the numbers whose indices lie in [Li,Ri][L_i, R_i], i.e., aLi,aLi+1,…,aRia_{L_i}, a_{L_{i+1}},\dots,a_{R_i}, the minimum value is exactly Vi\boldsymbol{V_i}.

He knows that bubble sort often times out. Therefore, he wants to know: among all sequences that satisfy the additional constraints, what is the minimum possible number of swaps performed by bubble sort?

Input Format

This problem contains multiple test cases.

The first line of input contains a positive integer TT.

For each test case, the first line contains two positive integers n,mn, m. The data guarantee that 1≤n,m≤1061 \leq n, m \leq 10^6.

In the next mm lines, each line contains three nonnegative integers Li,Ri,ViL_i, R_i, V_i, representing one additional constraint. The data guarantee that 1≤Li≤Ri≤n1 \leq L_i \leq R_i \leq n, and 0≤Vi≤1090 \leq V_i \leq 10^9.

Output Format

Output TT lines in total, each line containing one integer.

For each test case, if there exists a sequence satisfying all mm additional constraints, output the minimum number of bubble sort swaps among all such sequences. If no sequence satisfies all constraints, output −1-1.

1
3 2
1 1 2022
2 3 39

1

Hint

[Sample Explanation #1]

The constraints for this test case are a1=2022a_1 = 2022 and min⁡{a2,a3}=39\min\{a_2, a_3\} = 39.

If a2=39a_2 = 39 and 39≤a3<202239 \leq a_3 < 2022, then bubble sort performs swaps only in the first pass. In this pass, it swaps a1,a2a_1, a_2 and a2,a3a_2, a_3, so the total number of swaps is 22.

If a2=39a_2 = 39 and a3≥2022a_3 \geq 2022, then bubble sort performs swaps only in the first pass. In this pass, it swaps only a1,a2a_1, a_2, so the total number of swaps is 11.

If a3=39a_3 = 39 and 39<a2<202239 < a_2 < 2022, then in the first pass bubble sort swaps a1,a2a_1, a_2 and a2,a3a_2, a_3, and in the second pass it swaps a1,a2a_1, a_2. The total number of swaps is 33.

If a3=39a_3 = 39 and a2≥2022a_2 \geq 2022, then in the first pass bubble sort swaps a2,a3a_2, a_3, and in the second pass it swaps a1,a2a_1, a_2. The total number of swaps is 22.

Therefore, the minimum number of swaps is 11.


[Sample #2]

See bubble/bubble2.in and bubble/bubble2.ans in the attachment.


[Sample #3]

See bubble/bubble3.in and bubble/bubble3.ans in the attachment.

This sample satisfies the conditions of test points 8∼108 \sim 10.


[Sample #4]

See bubble/bubble4.in and bubble/bubble4.ans in the attachment.

This sample satisfies the conditions of test points 13∼1413 \sim 14.


[Sample #5]

See bubble/bubble5.in and bubble/bubble5.ans in the attachment.

This sample satisfies the conditions of test points 15∼1615 \sim 16.


[Sample #6]

See bubble/bubble6.in and bubble/bubble6.ans in the attachment.

This sample satisfies the conditions of test points 23∼2523 \sim 25.


[Constraints]

This problem has 2525 test points. All test points satisfy: 1≤T≤10001 \leq T \leq 1000, 1≤∑n,∑m≤1061 \leq \sum n, \sum m \leq 10^6, 1≤Li≤Ri≤n1 \leq L_i \leq R_i \leq n, 0≤Vi≤1090 \leq V_i \leq 10^9.

Here, ∑n\sum n and ∑m\sum m denote the total sum of nn and the total sum of mm over all test points. The meanings of ∑n2,∑m2,∑n3,∑m3\sum n^2, \sum m^2, \sum n^3, \sum m^3 are similar.

Test Points Constraints Special Property
1∼41 \sim 4 n,m≤7n,m \leq 7, and at most 22 test cases do not satisfy n,m≤5n, m \leq 5
5∼75 \sim 7 n,m≤17n,m \leq 17, and at most 33 test cases do not satisfy n,m≤9n, m \leq 9 A
8∼108 \sim 10 n,m≤100n,m \leq 100, ∑n3,∑m3≤4×107\sum n^3,\sum m^3 \leq 4 \times 10^7
11∼1211 \sim 12 n,m≤2000n,m \leq 2000, ∑n2,∑m2≤4×107\sum n^2,\sum m^2 \leq 4 \times 10^7
13∼1413 \sim 14 B
15∼1615 \sim 16 C
17∼1817 \sim 18
1919 ∑n,∑m≤106\sum n,\sum m \leq 10^6 A
2020 B
21∼2221 \sim 22 C
23∼2523 \sim 25

Special property A: For 1≤i≤m1 \leq i \leq m, 0≤Vi≤10 \leq V_i \leq 1.
Special property B: For 1≤i≤m1 \leq i \leq m, Li=RiL_i = R_i.
Special property C: The mm intervals [Li,Ri][L_i, R_i] given in the input are pairwise disjoint.


[Hint]

For some test points, the input size is large. We recommend using a faster input method.

Translated by ChatGPT 5