#P6845. [CEOI 2019] Dynamic Diameter

[CEOI 2019] Dynamic Diameter

Description

There is a tree with nn nodes, and the edges are weighted.

There will be qq modifications. Each modification changes the weight of one edge in the tree. After each modification, you need to output the sum of edge weights on the diameter of the tree.

This problem is strictly online.

Input Format

The first line contains three integers n,q,wn, q, w, representing the number of nodes, the number of queries, and the upper bound of edge weights.

The next n1n - 1 lines each contain three integers ai,bi,cia_i, b_i, c_i, meaning there is an edge between aia_i and bib_i with weight cic_i.

The next qq lines each contain two encrypted integers dj,ejd_j, e_j.

The decryption method is:

  • dj=(dj+last)mod(n1)d_j'=(d_j+\text{last})\bmod(n-1)
  • ej=(ej+last)modwe_j'=(e_j+\text{last})\bmod w

Here, last\text{last} is the answer to the previous query, with initial value 00.

This means: change the weight of the (dj+1)(d_j' + 1)-th edge to eje_j'.

Output Format

Output qq lines. Each line contains one integer. The integer on the ii-th line is the total weight on the diameter after the ii-th modification.

4 3 2000
1 2 100
2 3 1000
2 4 1000
2 1030
1 1020
1 890
2030
2080
2050

10 10 10000
1 9 1241
5 6 1630
10 5 1630
2 6 853
10 1 511
5 3 760
8 3 1076
4 10 1483
7 10 40
8 2051
5 6294
5 4168
7 1861
0 5244
6 5156
3 3001
8 5267
5 3102
8 3623
6164
7812
8385
6737
6738
7205
6641
7062
6581
5155

Hint

Explanation of Sample 1

The decrypted modifications are:

2 1030
0 1050
2 970

The following figure shows how the edge weights in the tree change. The red edge represents the diameter of the tree:

Constraints

For 100%100\% of the testdata, it is guaranteed that 2n1052 \le n \le 10^5, 1q1051 \le q \le 10^5, 1w2×10131 \le w \le 2\times 10^{13}, 1ai,bin1 \le a_i, b_i \le n, 0ci,ej<w0 \le c_i, e_j < w, and 0dj<n10 \le d_j < n - 1.

The detailed subtask constraints and scores are shown in the table below:

Subtask ID Constraints Score
1 n,q100n, q \le 100 and w104w \le 10^4 1111
2 n,q5×103n, q \le 5\times 10^3 and w104w \le 10^4 1313
3 w104w \le 10^4 and all edges are of the form (1,i)(1, i) 77
4 w104w \le 10^4 and all edges are of the form (i,2×i)(i, 2\times i) or (i,2×i+1)(i, 2\times i + 1) 1818
5 It is guaranteed that there exists a diameter that passes through node 11 2424
6 No special constraints 2727

Notes

This problem is translated from Central-European Olympiad in Informatics 2019 Day 1 T2 Dynamic Diameter

Translated by ChatGPT 5