#P9697. [GDCPC 2023] Canvas

    ID: 8923 Type: RemoteJudge 3000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>2023广东Special JudgeO2优化XCPC

[GDCPC 2023] Canvas

题目描述

There is a sequence of length nn. At the beginning, all elements in the sequence equal to 00. There are also mm operations, where the ii-th operation will change the value of the lil_i-th element in the sequence to xix_i, and also change the value of the rir_i-th element in the sequence to yiy_i. Each operation must be performed exactly once.

Find the optimal order to perform the operations, so that after all operations, the sum of all elements in the sequence is maximized.

输入格式

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers nn and mm (2n,m5×1052 \leq n, m \leq 5 \times 10^5) indicating the length of the sequence and the number of operations.

For the following mm lines, the ii-th line contains four integers lil_i, xix_i, rir_i and yiy_i (1li<rin1 \leq l_i<r_i \leq n, 1xi,yi21 \leq x_i,y_i \leq 2) indicating the ii-th operation.

It's guaranteed that neither the sum of nn nor the sum of mm of all test cases will exceed 5×1055 \times 10^5.

输出格式

For each test case, first output one line containing one integer, indicating the maximum sum of all elements in the sequence after all operations. Then output another line containing mm integers a1,a2,,ama_1, a_2, \cdots, a_m separated by a space, indicating the optimal order to perform the operations, where aia_i is the index of the ii-th operation to be performed. Each integer from 11 to mm (both inclusive) must appear exactly once. If there are multiple valid answers, you can output any of them.

2
4 4
1 1 2 2
3 2 4 1
1 2 3 2
2 1 4 1
4 2
3 2 4 1
1 2 3 1
7
4 1 3 2
5
2 1

提示

For the first sample test case, after performing operations 4,1,3,24, 1, 3, 2 in order, the sequence becomes {2,2,2,1}\{2, 2, 2, 1\}. The sum of all elements is 77.

For the second sample test case, after performing operations 2,12, 1 in order, the sequence becomes {2,0,2,1}\{2, 0, 2, 1\}. The sum of all elements is 55.