#P9584. 「MXOI Round 1」城市

    ID: 10723 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划 DP洛谷原创O2优化树形 DP前缀和洛谷月赛

「MXOI Round 1」城市

Problem Description

Little C is the president of Country F. Although this country exists only in an online game, he is indeed the president of this country.

Country F consists of nn cities. These nn cities are connected by n1n-1 bidirectional roads. It is guaranteed that starting from any city, you can reach any other city through these n1n-1 bidirectional roads.

Of course, using these bidirectional roads costs money. Passing through the ii-th bidirectional road costs cic_i yuan. We call cic_i the cost of the ii-th bidirectional road.

We define cost(x,y)cost(x,y) as the sum of the costs of all bidirectional roads on the simple path from city xx to city yy. In particular, when x=yx=y, cost(x,y)=0cost(x,y)=0.

To promote the development of Country F, Little C built a new city n+1n+1. Now he needs to build one more bidirectional road so that city n+1n+1 can also reach any city through these nn bidirectional roads.

He has a total of qq road-building plans. Each plan provides two parameters ki,wik_i,w_i. For each plan, you need to compute, after building a bidirectional road connecting city kik_i and city n+1n+1 with cost wiw_i, the sum of all cost(i,j)cost(i,j), i.e. $\sum\limits_{i=1}^{n+1} \sum\limits_{j=1}^{n+1} cost(i,j)$.

Since the answer may be very large, you only need to output the result modulo 998244353998244353.

The plans are independent of each other, meaning none of the plans will affect the existing roads, and these plans will not actually be carried out.

Input Format

The first line contains two integers n,qn,q.

The next n1n-1 lines: the ii-th line contains three integers ui,vi,ciu_i,v_i,c_i, indicating that there is a bidirectional road connecting city uiu_i and city viv_i with cost cic_i.

The next qq lines: the ii-th line contains two integers ki,wik_i,w_i, indicating a plan for building a new road.

Output Format

Output qq lines, each containing one integer. The integer on the ii-th line denotes, after building a bidirectional road connecting city kik_i and city n+1n+1 with cost wiw_i, the sum of all cost(i,j)cost(i,j) modulo 998244353998244353, i.e. $\sum\limits_{i=1}^{n+1} \sum\limits_{j=1}^{n+1} cost(i,j) \bmod 998244353$.

4 2
2 1 3
3 2 2
4 2 4
1 2
2 2
100
88
9 5
2 3 6
6 1 4
5 2 10
2 4 1
9 1 9
2 8 3
1 2 3
7 4 8
4 9
7 3
6 1
9 7
2 1

1050
1054
970
1148
896

Hint

Sample Explanation #1

After building a bidirectional road connecting city 11 and city 55 with cost 22, the roads of Country F are as shown in the figure below:

For example, in this case, cost(4,5)=9cost(4,5)=9 and cost(1,3)=5cost(1,3)=5.

It is easy to obtain that $\sum\limits_{i=1}^{n+1} \sum\limits_{j=1}^{n+1} cost(i,j)=100$.

Sample #3

See city/city3.in and city/city3.ans in the additional files.

This sample satisfies the constraints of test point 44.

Sample #4

See city/city4.in and city/city4.ans in the additional files.

This sample satisfies the constraints of test point 1111.

Sample #5

See city/city5.in and city/city5.ans in the additional files.

This sample satisfies the constraints of test point 1414.

Sample #6

See city/city6.in and city/city6.ans in the additional files.

This sample satisfies the constraints of test point 2020.

Constraints

For 100%100\% of the data, 2n2×1052 \le n \le 2\times10^5, 1q2×1051 \le q \le 2\times10^5, 1ui,vi,kin1 \le u_i,v_i,k_i \le n, 1ci,wi1061 \le c_i,w_i \le 10^6. It is guaranteed that starting from any city, you can reach any other city through the original n1n-1 bidirectional roads.

Test Point ID nn \le qq \le Special Property
131\sim3 8080 None
474\sim7 50005000 50005000
8108\sim10 2×1052\times10^5
111311\sim13 2×1052\times10^5 A
141614\sim16 B
172017\sim20 None

Special Property A: It is guaranteed that for all 1i<n1 \le i \lt n, ui=i,vi=i+1u_i=i,v_i=i+1.

Special Property B: It is guaranteed that for all 1iq1 \le i \le q, ki=1k_i=1.

Translated by ChatGPT 5