#P6655. [YsOI2020] 制高

    ID: 7431 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP树状数组可持久化线段树

[YsOI2020] 制高

Background

Ysuperman especially likes playing strategy games.

Problem Description

The game map is a rooted tree with nn nodes. The root node is 11. Except for node 11, every other node has a unique parent.

Each node has a height. The height of node ii is hih_i. We say that a node vv is a "high ground" if and only if vv is the root, or its parent node uu is a "high ground" and hvhuh_v \ge h_u.

However, Ysuperman does not know the exact parent of each node. He only knows the possible interval of its parent index. For node ii, the possible parent index range is [li,ri][l_i, r_i], and it is guaranteed that 1liri<i1 \le l_i \le r_i < i.

Now, Ysuperman wants to know, for all possible cases, what the total sum of the number of "high ground" nodes is.

Since the result can be very large, you only need to output the value modulo 998244353998244353.

Input Format

The input has n+1n + 1 lines.

The first line contains a positive integer nn.

The next line contains nn non-negative integers. The ii-th integer describes hih_i.

The next n1n - 1 lines each contain two positive integers, describing l2,r2,,ln,rnl_2, r_2, \cdots, l_n, r_n.

It is guaranteed that 1liri<i1 \le l_i \le r_i < i.

Output Format

One line containing one integer, which is the answer.

3
1 3 2
1 1
1 2

5

10
1 1 1 0 5 2 11 12 17 7
1 1
1 2
2 2
1 3
1 1
1 4
1 2
6 7
1 5

4044

50
1 0 0 6 2 5 0 2 16 15 14 8 20 22 23 21 7 24 27 17 1 13 39 40 31 38 40 16 25 48 2 0 15 7 0 47 58 11 22 54 11 78 30 32 31 35 44 56 59 85
1 1
2 2
1 2
2 3
3 3
1 6
2 6
3 5
5 9
3 4
1 4
3 12
1 12
5 7
5 13
1 10
7 9
4 11
12 12
16 17
3 9
8 15
15 16
1 19
9 10
10 12
8 10
4 10
6 13
10 13
11 30
11 21
2 30
13 23
4 24
32 34
8 29
4 22
2 26
29 33
28 38
18 31
19 36
15 32
8 14
15 32
4 33
30 45
8 25

904672069

Hint

Explanation for Sample 1:

There are two cases. Case 1: the parent of 22 is 11, and the parent of 33 is 11. Then 1,2,31, 2, 3 are all "high ground". Case 2: the parent of 22 is 11, and the parent of 33 is 22. Since h2>h3h_2 > h_3, 33 is not "high ground". Then 1,21, 2 are "high ground".

So the sum of the numbers of "high ground" nodes over all cases is 55.

Test Point ID\text{Test Point ID} nn i=2n(rili+1)\prod_{i=2}^n(r_i-l_i+1) Special Property\text{Special Property}
121 \sim 2 =10=10 106\le 10^6 None\text{None}
33 =105=10^5
44 \ hihi+1h_i \le h_{i+1}
55 hi>hi+1h_i > h_{i+1}
6126 \sim 12 =103=10^3 None\text{None}
132013 \sim 20 =105=10^5

The testdata guarantees that hih_i is within the maximum range representable by int, and 1liri<i1 \le l_i \le r_i < i.

The problem is not hard.

Translated by ChatGPT 5