#P6798. 「StOI-2」简单的树

「StOI-2」简单的树

Description

Given a rooted tree with root 11 and nn nodes. Each node has a weight cic_{i}.

Define the value valval of each node as: the maximum cic_{i} among all nodes in the subtree rooted at this node.

Define the function f(x,y)f(x,y) as the sum of valval over the entire tree after changing cxc_{x} to yy.

Now you need to answer qq queries. Each query gives three values (l,r,a)(l,r,a), and you need to compute i=lrf(a,i)\sum\limits_{i=l}^{r}{f(a,i)} modulo 998,244,353998,244,353.

Input Format

This problem is forced online.

The first line contains three positive integers nn, qq, and optopt, representing the number of nodes in the tree, the number of queries, and a value controlling the forced-online behavior.

The second line contains nn positive integers, representing the weight of each node.

The next n1n - 1 lines each contain two positive integers uiu_{i} and viv_{i}, representing an edge of the tree.

The next qq lines each contain three positive integers l,r,al', r', a'. You need to compute the real l,r,al, r, a and then answer the query.

  • l=((l+opt×lastans)modn)+1l = (( l' + opt \times lastans )\bmod n ) + 1
  • r=((r+opt×lastans)modn)+1r = (( r' + opt \times lastans )\bmod n ) + 1
  • a=((a+opt×lastans)modn)+1a = (( a' + opt \times lastans )\bmod n ) + 1

Here, lastanslastans is the answer to the previous query, initially 00.

If l>rl > r, swap ll and rr.

Output Format

For each query, output the final answer.

5 3 0
5 3 4 2 1
1 2
1 3
2 4
2 5
1 3 5
2 4 1
1 3 4
42
48
52

Hint

Sample Explanation

The real (l,r,a)(l,r,a) are:

  • (2,4,1)(2,4,1)
  • (3,5,2)(3,5,2)
  • (2,4,5)(2,4,5)

Constraints

For 10%10\% of the testdata: 1n,q1001 \leq n,q \leq 100
For 20%20\% of the testdata: 1n,q30001 \leq n,q \leq 3000
For another 20%20\% of the testdata: 1l,r,ci21 \leq l',r',c_{i} \leq 2
For another 20%20\% of the testdata: l=rl' = r'
For the first 80%80\% of the testdata: opt=0opt = 0
For 100%100\% of the testdata: 1n,q5×1051 \leq n,q \leq 5 \times 10^{5}, 1ci,a,l,rn1 \leq c_{i}, a', l', r' \leq n

Translated by ChatGPT 5