#P9697. [GDCPC2023] Canvas

[GDCPC2023] 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.

题目大意

【题目描述】

有一个长度为 nn 的序列,一开始序列中的所有元素均为 00。另外还有 mm 个操作,其中第 ii 个操作会将序列中第 lil_i 个元素的值改为 xix_i,以及将序列中第 rir_i 个元素的值改为 yiy_i。每个操作必须恰好执行一次。

求执行操作的最优顺序,使得所有操作执行完成后,序列中所有元素之和最大。

【输入格式】

有多组测试数据。第一行输入一个整数 TT 表示测试数据组数。对于每组测试数据:

第一行输入两个整数 nnmm2n,m5×1052 \leq n, m \leq 5 \times 10^5)表示序列的长度和操作的个数。

对于接下来 mm 行,第 ii 行输入四个整数 lil_ixix_irir_iyiy_i1li<rin1 \leq l_i<r_i \leq n1xi,yi21 \leq x_i,y_i \leq 2)表示第 ii 个操作。

保证所有数据 nn 之和与 mm 之和均不超过 5×1055 \times 10^5

【输出格式】

每组数据首先输出一行一个整数,表示执行操作后,所有元素的最大和。接下来输出一行 mm 个由单个空格分隔的整数 a1,a2,,ama_1, a_2, \cdots, a_m 表示执行操作的最优顺序,其中 aia_i 表示第 ii 次执行的操作的编号。从 11mm 的每个整数(含两端)必须恰好出现一次。若有多种合法答案,您可以输出任意一种。

【样例解释】

对于第一组样例数据,按 4,1,3,24, 1, 3, 2 的顺序执行操作后,序列变为 {2,2,2,1}\{2, 2, 2, 1\},元素之和为 77

对于第二组样例数据,按 2,12, 1 的顺序执行操作后,序列变为 {2,0,2,1}\{2, 0, 2, 1\},元素之和为 55

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.