#P16319. [ICPC 2023 Jinan R] 铁路环游

    ID: 18255 远端评测题 3000ms 128MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>动态规划 DP并查集2023动态规划优化ICPC济南

[ICPC 2023 Jinan R] 铁路环游

Problem Description

$\textbf{Please note that this problem has an unusual memory limit.}$

"Railway Tour" is a German-style board game themed around railways. In the game, players play train cards and build railways on the map. The score is determined by the total length of railways built and whether the player can connect faraway cities. The cities that need to be connected are determined by the drawn ticket cards.

:::align{center}

A photo taken by BoardGameGeek user @garyjames :::

Consider a one-dimensional version of the game. There are (n+1)(n + 1) cities in a line, numbered from 00 to nn from left to right. For each 1in1 \le i \le n, you may place a railway between city (i1)(i - 1) and city ii to connect them.

There are mm ticket cards that reward the player for connecting cities. The ii-th card can be described by three integers lil_i, rir_i, and viv_i, meaning that if city lil_i and city rir_i can be connected by railways (that is, for all li<jril_i < j \le r_i, there is a railway between city (j1)(j - 1) and city jj), you will gain viv_i points.

For each 1kn1 \le k \le n, compute the maximum score when you place exactly kk railways. If you do not get any reward, your score is 00.

Input Format

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

The first line contains two integers nn and mm (1n,m1041 \le n, m \le 10^4), representing the maximum number of railways you may place and the number of ticket cards for rewards.

In the next mm lines, the ii-th line contains three integers lil_i, rir_i, and viv_i (0li<rin0 \le l_i < r_i \le n, 1vi1091 \le v_i \le 10^9), meaning that if city lil_i and city rir_i can be connected by railways, you will gain viv_i points.

It is guaranteed that the sum of all nn and the sum of all mm over all test cases are both at most 10410^4.

Output Format

For each test case, output one line with nn integers separated by single spaces, where the ii-th integer represents the maximum score when you place exactly ii railways.

Please do not output extra spaces at the end of the line, otherwise your answer may be judged as wrong.

2
4 3
0 2 3
3 4 2
0 3 1
3 1
1 3 100
2 3 5 6
0 100 100

Hint

Let (i1,i)(i - 1, i) denote a railway between city (i1)(i - 1) and city ii. For the first sample test case:

  • If you place 11 railway, you can place (3,4)(3, 4) and then get the second reward. The answer is 22.
  • If you place 22 railways, you can place (0,1)(0, 1) and (1,2)(1, 2) and then get the first reward. The answer is 33.
  • If you place 33 railways, you can place (0,1)(0, 1), (1,2)(1, 2), and (3,4)(3, 4) and then get the first and second rewards. The answer is 3+2=53 + 2 = 5.
  • If you place all 44 railways, you can get all rewards. The answer is 3+2+1=63 + 2 + 1 = 6.

Translated by ChatGPT 5