#P9775. [HUSTFC 2023] 广义线段树

[HUSTFC 2023] 广义线段树

Problem Description

For any sequence aa of length nn, we can build a generalized segment tree based on aa. A generalized segment tree is a weighted binary tree with (2n1)(2n-1) nodes. For each node with index pp, it has two attributes LpL_p and RpR_p, meaning that the interval it maintains is [Lp,Rp][L_p, R_p], and its weight is Mp=i=LpRpaiM_p =\prod_{i=L_p}^{R_p}a_i. In addition, the generalized segment tree also satisfies the following properties:

  • All nodes with index p[n,2n1]p\in [n,2n-1] are leaf nodes, and Lp=Rp=p+1nL_p = R_p = p + 1 - n.
  • All nodes with index p[1,n1]p\in [1,n-1] are non-leaf nodes. Each of them must have a left child XpX_p and a right child YpY_p, and p<Xp<Ypp < X_p < Y_p. The interval maintained by node pp is the union of the intervals maintained by its two children, and it is guaranteed to be continuous. Formally, RXp=LYp1R_{X_p}=L_{Y_p}-1, and Lp=LXpL_p=L_{X_p}, Rp=RYpR_p=R_{Y_p}.

For example, the following is a generalized segment tree built from n=4n=4 and the sequence a={1,2,3,4}a=\{1, 2, 3, 4\} (the integer pair (p,Mp)(p, M_p) inside each node denotes its index and weight, respectively). You can see that the shape of a generalized segment tree is not unique.

1

For this generalized segment tree:

  • [L7,R7]=[4,4][L_7, R_7] = [4, 4], so M7=a4M_7 = a_4
  • [L6,R6]=[3,3][L_6, R_6] = [3, 3], so M6=a3M_6 = a_3
  • [L5,R5]=[2,2][L_5, R_5] = [2, 2], so M5=a2M_5 = a_2
  • [L4,R4]=[1,1][L_4, R_4] = [1, 1], so M4=a1M_4 = a_1
  • [L3,R3]=[L4,R5]=[1,2][L_3, R_3] = [L_4, R_5] = [1, 2], so M3=a1×a2M_3 = a_1 \times a_2
  • [L2,R2]=[L3,R6]=[1,3][L_2, R_2] = [L_3, R_6] = [1, 3], so M2=a1×a2×a3M_2 = a_1 \times a_2 \times a_3
  • [L1,R1]=[L2,R7]=[1,4][L_1, R_1] = [L_2, R_7] = [1, 4], so M1=a1×a2×a3×a4M_1 = a_1 \times a_2 \times a_3 \times a_4

You are given two sequences aa and bb of length nn, and the shape of a generalized segment tree TT with (2n1)(2n-1) nodes (that is, the indices of the left and right child for each node). Then you need to perform nn operations. In the ii-th operation, change aia_i into ai×bia_i\times b_i.

After each operation, you need to build a generalized segment tree with the same shape as TT based on the modified sequence aa, and compute the sum of weights of all nodes, i.e. i=12n1Mi\sum_{i=1}^{2n-1}M_i. Since the result can be very large, you only need to output it modulo 998244353998\,244\,353.

Input Format

The first line contains an integer n (1n5105)n\ (1\le n\le 5\cdot 10^5), representing the length of sequences aa and bb.

The second line contains nn integers, where the ii-th integer is defined as ai (1ai<998244353)a_i\ (1 \le a_i < 998\,244\,353).

The third line contains nn integers, where the ii-th integer is defined as bi (1bi<998244353)b_i\ (1 \le b_i < 998\,244\,353).

The next n1n-1 lines describe the tree. The ii-th of these lines contains two integers Xi,Yi (i<Xi<Yi2n1)X_i, Y_i\ (i<X_i<Y_i\le 2n-1), representing the indices of the left and right child of node ii, respectively. It is guaranteed that the given generalized segment tree shape satisfies the requirements in the statement.

Output Format

Output one line with nn integers separated by spaces, where the ii-th integer is the answer after the ii-th modification modulo 998244353998\,244\,353.

4
1 2 3 4
2 3 2 3
2 7
3 6
4 5

75 207 390 974 

Hint

In the sample, the shape of the generalized segment tree is the same as the example in the statement.

After the first modification, a1a_1 becomes a1×b1=1×2=2a_1 \times b_1 = 1 \times 2 = 2, so the new a={2,2,3,4}a = \{2, 2, 3, 4\}. We can compute:

  • M7=a4=4M_7 = a_4 = 4
  • M6=a3=3M_6 = a_3 = 3
  • M5=a2=2M_5 = a_2 = 2
  • M4=a1=2M_4 = a_1 = 2
  • M3=a1×a2=2×2=4M_3 = a_1 \times a_2 = 2 \times 2 = 4
  • $M_2 = a_1 \times a_2 \times a_3 = 2 \times 2 \times 3 = 12$
  • $M_1 = a_1 \times a_2 \times a_3 \times a_4 = 2 \times 2 \times 3 \times 4 = 48$

So the sum of weights is M1+M2++M7=75M_1 + M_2 + \ldots + M_7 = 75.

After the second modification, a2a_2 becomes a2×b2=2×3=6a_2 \times b_2 = 2 \times 3 = 6. The remaining operations are similar to the first one, so they are not repeated here.

Translated by ChatGPT 5