#P14008. 「florr IO Round 1」序列游戏

    ID: 13381 远端评测题 500ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>洛谷原创分治期望逆元洛谷月赛双指针 two-pointer

「florr IO Round 1」序列游戏

Description

Please note the time limit for this problem.

For a sequence aa, we define its weight as the sum of the squares of the lengths of all maximal color segments. A color segment is defined as an interval al,al+1,,ara_l, a_{l+1}, \dots, a_r where all elements are equal. A maximal color segment is a color segment that cannot be contained within any longer color segment.

Given a sequence aa of length nn, where each aia_i is an integer chosen uniformly at random from the interval [li,ri][l_i, r_i], compute the expected value of the weight over all possible sequences aa, modulo 998244353998244353.

Input Format

The first line contains a single integer nn, representing the length of the sequence.

The next nn lines each contain two integers lil_i and rir_i, indicating the random range for aia_i.

Output Format

Output a single integer, representing the expected weight over all possible sequences aa.

2
1 1
1 1

4
2
1 2
1 2

3
5
1 3
2 4
5 5
2 5
1 5
776412281

Hint

Sample Explanation

Sample Explanation #1

Obviously, in this case a={1,1}a = \{1, 1\}, so the expected weight is 22=42^2 = 4.

Sample Explanation #2

In this case, the four possible sequences are $a = \{1, 1\}, a = \{1, 2\}, a = \{2, 1\}, a = \{2, 2\}$. For {1,1}\{1, 1\} and {2,2}\{2, 2\}, the weight is 22=42^2 = 4, and for {1,2}\{1, 2\} and {2,1}\{2, 1\}, the weight is 12+12=21^2 + 1^2 = 2. Therefore, the expected weight is 4+4+2+24=3\frac{4+4+2+2}{4} = 3.

Data Range

This problem uses bundled testing.

Subtask Score nn\le rir_i\le Special Properties
1 1010 55 None
2 20002000
3 2020 10510^5 10510^5
4 9×1089\times 10^8
5 1010 10610^6 Data is random
6 3030 2×1062\times 10^6 None

Translated by ChatGPT 4.1