시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
5 초 512 MB 95 40 28 37.333%

## 문제

You are given a weighted undirected tree on n vertices and a list of q updates. Each update changes the weight of one edge. The task is to output the diameter of the tree after each update.

(The distance between two vertices is the sum of the weights on the unique simple path that connects them. The diameter is the largest of all those distances.)

## 입력

The first line contains three space-separated integers n, q and w (2 ≤ n ≤ 100, 000, 1 ≤ q ≤ 100, 000, 1 ≤ w ≤ 20, 000, 000, 000, 000) – the number of vertices in the tree, the number of updates and the limit on the weights of edges. The vertices are numbered 1 through n.

Next, n − 1 lines describing the initial tree follow. The i-th of these lines contains three space-separated integers ai, bi, ci (1 ≤ ai, bi ≤ n, 0 ≤ ci < w) meaning that initially, there is an edge between vertices ai and bi with weight ci. It is guaranteed that these n − 1 lines describe a tree.

Finally, q lines describing queries follow. The j-th of these lines contains two space-separated integers dj, ej (0 ≤ dj < n − 1, 0 ≤ ej < w). These two integers are then transformed according to the following scheme:

• d'j = (dj + last) mod (n − 1)
• e'j = (ej + last) mod w

where last is the result of the last query (initially last = 0). Tuple (d'j , e'j) represents a query which takes the d'j + 1-th edge from the input and sets its weight to e'j.

## 출력

Output q lines. For each i, line i should contain the diameter of the tree after the i-th update.

## 예제 입력 1

4 3 2000
1 2 100
2 3 1000
2 4 1000
2 1030
1 1020
1 890


## 예제 출력 1

2030
2080
2050


## 예제 입력 2

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


## 예제 출력 2

6164
7812
8385
6737
6738
7205
6641
7062
6581
5155


## 힌트

The first sample is depicted in the figure below. The left-most picture shows the initial state of the graph. Each following picture depicts the situation after an update. The weight of the updated edge is painted green, and the diameter is red.

The first query changes the weight of the 3rd edge, i.e. {2, 4}, to 1030. The largest distance between any pair of vertices is 2030 – the distance between 3 and 4.

As the answer is 2030, the second query is

d'2 = (1 + 2030) mod 3 = 0

e'2 = (1020 + 2030) mod 2000 = 1050

Hence the weight of the edge {1, 2} is changed to 1050. This causes the pair {1, 4} to be the pair with the greatest distance, namely 2080.

The third query is decoded as

d'3 = (1 + 2080) mod 3 = 2

e'3 = (890 + 2080) mod 2000 = 970

As the weight of the edge {2, 4} decreases to 970, the most distant pair is suddenly {1, 3} with 2050.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.18356266617774963, "perplexity": 1032.2770357460618}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-10/segments/1614178358798.23/warc/CC-MAIN-20210227084805-20210227114805-00559.warc.gz"}