#P9090. 「SvR-2」G64

    ID: 10116 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP贪心洛谷原创O2优化矩阵加速洛谷月赛

「SvR-2」G64

Problem Description

For two rooted binary trees T1,T2T_1, T_2, define the result of merge(T1,T2)\operatorname{merge}(T_1,T_2) as a binary tree whose root’s left subtree is T1T_1 and right subtree is T2T_2. Obviously, the result of merge(T1,T2)\operatorname{merge}(T_1,T_2) is unique and always exists.

For a rooted binary tree TT, define a function Gx(T)G_x(T). Here, G1(T)G_1(T) means: starting from the root of TT, keep moving to the right child until reaching a node that has no right child; then set that node’s right subtree to be TT. The resulting new tree is G1(T)G_1(T). For x>1x>1, Gx(T)G_x(T) satisfies:

$$G_x(T)=G_1(\operatorname{merge}(G_{x-1}(T),G_{x-1}(T)))$$

Given a rooted binary tree with nn nodes and root 11, let the subtree rooted at node ii be TiT_i. There are qq queries. Each query gives x,ix,i. For each query, find the maximum independent set of Gx(Ti)G_x(T_i).

Input Format

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

Then follow nn lines. Each line contains two integers lsi,rsils_i,rs_i, representing the left child and right child. If it is 00, it means the corresponding child does not exist.

Then follow qq lines. Each line contains two integers x,ix,i, representing one query.

Output Format

Output qq lines. Each line contains one number: the size of the maximum independent set of Gx(Ti)G_x(T_i) modulo 998244353998244353 for this query.

5 3
2 3
0 4
5 0
0 0 
0 0
2 5 
2 1
1 1
5
24
6
5 1
2 3
0 4
5 0
0 0 
0 0
64 1
592424678

Hint

Sample Explanation

For the first sample, the result of G2(T5)G_2(T_5) is shown in the figure below (node indices are ignored):

For the second sample, I have a wonderful explanation, but unfortunately G64G_{64} is too large to write it down here.

Constraints and Notes

This problem enables bundled testdata and O2 optimization.

Subtask Constraints / Special Properties Score
11 n,q,x10n,q,x\le 10 10pts10 \operatorname{pts}
22 x=1x =1 5pts5 \operatorname{pts}
33 x3x\le 3 10pts10 \operatorname{pts}
44 x10x\le 10 15pts15 \operatorname{pts}
55 The size of TiT_i is guaranteed to be 11 10pts10 \operatorname{pts}
66 No special restrictions 50pts50 \operatorname{pts}

For 100%100\% of the testdata: 1x1091\le x\le 10^9, 1n5×1051\le n\le 5\times 10^5, 1q5×1051\le q\le 5\times 10^5.

Translated by ChatGPT 5