#P4319. 变化的道路

    ID: 3181 远端评测题 1000~7000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树O2优化生成树Link-Cut Tree,LCT可持久化

变化的道路

Description

Xiao w and Xiao c are in Country H. In recent years, as Country H has developed, its roads have been constantly changing.

According to Country H’s road law, every road has a value ww, meaning that if Xiao w and Xiao c pass through this road, their LL value will decrease by ww. However, if Xiao w and Xiao c have already passed this road before, their LL value will not decrease.

Country H has NN cities. Initially, Country H has N1N-1 roads, and these N1N-1 roads form a tree.

Xiao w and Xiao c will start from city 11 and visit all cities of Country H. They will travel for 32766 days in total. For each day, they want the LL value to still be positive after finishing the tour. Find the minimum initial LL they need at departure for each day.

All edges in Country H are undirected. No road connects a city to itself.

Input Format

Line 1: an integer NN.

Lines 2 to NN: each line contains three positive integers u,v,wu, v, w, meaning there is a road of value ww between cities uu and vv.

Line N+1N+1: an integer MM, meaning Country H has MM roads that are changing.

Lines N+2N+2 to N+M+1N+M+1: each line contains 55 integers u,v,w,l,ru, v, w, l, r, meaning there is a road of value ww from city uu to city vv, and this road exists from day ll to day rr.

Output Format

Output 3276632766 lines. The ii-th line is the minimum LL required on day ii.

4
1 3 3
3 4 4
2 4 5
3
1 2 1 1 2
2 3 8 2 3
3 4 2 1 1
7
9
13
由于版面原因,仅显示三行,接下来32763行都是13

Hint

On day 1, choose $1 \xrightarrow{1} 2 \xrightarrow{0} 1 \xrightarrow{3} 3 \xrightarrow{2} 4$. The LL value decreases by 66 in total, so the minimum LL is 77.

On day 2, choose $1 \xrightarrow{1} 2 \xrightarrow{0} 1 \xrightarrow{3} 3 \xrightarrow{4} 4$. The LL value decreases by 88 in total, so the minimum LL is 99.

From day 3 onward, choose $1 \xrightarrow{3} 3 \xrightarrow{4} 4 \xrightarrow{5} 2$. The LL value decreases by 1212 in total, so the minimum LL is 1313.

Subtask 1: 15 points, N=100N = 100, rm = 233.

Subtask 2: 15 points, N=1000N = 1000, rm = 2333.

Subtask 3: 20 points, N=49998N = 49998, rm = 32766, l=rl = r.

Subtask 4: 20 points, N=49999N = 49999, rm = 32766, r=rmr = rm.

Subtask 5: 30 points, N=50000N = 50000, rm = 32766.

For subtask 3, M=rmM = rm; for other subtasks, M=3×rmM = 3 \times rm.

Constraints for all testdata: 1N500001 \leq N \leq 50000, 1lrrm327661 \leq l \leq r \leq rm \leq 32766, 1w1091 \leq w \leq 10^9.

Translated by ChatGPT 5