#P10879. 「KDOI-07」对树链剖分的爱

    ID: 11670 远端评测题 1500ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP数学图论2024洛谷原创O2优化动态规划优化组合数学容斥原理期望

「KDOI-07」对树链剖分的爱

Background

The person downstairs is right, but in NOI 2024 D2T2, sszcdjr used a clever method to defeat my brute-force heavy-light decomposition.

The person upstairs is right, but heavy-light decomposition got me to 1010\sqrt{}, so I made this problem to show my love for heavy-light decomposition.

Problem Description

You are given a rooted tree with nn nodes, rooted at 11. For each node 2in2\leq i\leq n, its parent fif_i is chosen uniformly at random from [li,ri][l_i,r_i]. Each edge of the tree has an edge weight, initially 00.

Now there are mm operations. The ii-th operation adds wiw_i to the weights of all edges on the path from (ui,vi)(u_i,v_i). After all mm operations, for all i=2ni=2\sim n, compute the expected value of the weight on edge (i,fi)(i,f_i), modulo 998244353998244353.

Input Format

The first line contains one positive integer nn.

In the next n1n-1 lines, the ii-th line contains two positive integers li+1,ri+1l_{i+1},r_{i+1}.

The next line contains one positive integer mm.

In the next mm lines, the ii-th line contains three positive integers ui,vi,wiu_i,v_i,w_i.

Output Format

Output one line with n1n-1 positive integers. The ii-th integer denotes the expected edge weight of edge (i+1,fi+1)(i+1,f_{i+1}), modulo 998244353998244353.

3
1 1
1 2
1
1 3 2
1 2
5
1 1
1 2
3 3
2 4
9
2 5 497855355
1 5 840823564
3 1 295265328
2 3 457999227
4 4 235621825
2 1 86836399
5 2 800390742
5 3 869167938
2 4 269250165
405260353 409046983 606499796 13504540

Hint

Sample Explanation 1

There are 22 possible cases for the parents of all nodes:

  • f2=1,f3=1f_2=1,f_3=1. In this case, the edge weights on (f2,2),(f3,3)(f_2,2),(f_3,3) are 0,20,2, respectively.

  • f2=1,f3=2f_2=1,f_3=2. In this case, the edge weights on (f2,2),(f3,3)(f_2,2),(f_3,3) are 2,22,2, respectively.

So the expected edge weight of (f2,2)(f_2,2) is 0+22=1\dfrac{0+2}{2}=1, and the expected edge weight of (f3,3)(f_3,3) is 2+22=2\dfrac{2+2}{2}=2.


Constraints

This problem uses bundled testdata.

Subtask\text{Subtask} nn\leq mm\leq Score
11 1010 2020
22 5050
33 500500
44 50005000 11
55 50005000

For all testdata, it is guaranteed that 1n,m50001\leq n,m\leq5000, 1liri<i1\leq l_i\leq r_i<i, 1ui,vin1\leq u_i,v_i\leq n, and 1wi1091\leq w_i\leq 10^9.

Translated by ChatGPT 5