#P16923. [JLCPC 2026] 水晶城堡

    ID: 19241 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度提高 上传者: 标签>莫队吉林O2优化组合数学期望2026省赛/邀请赛

[JLCPC 2026] 水晶城堡

Problem Description

tarjen\mathit{tarjen} is the guardian of the Crystal Castle. In the castle corridor, there is a row of nn magic crystals. The color ID of the ii-th crystal is aia_i. Adjacent crystals with the same color will resonate and form a color segment, which is a maximal contiguous segment of the same color. For example, the color sequence [1,1,2,2,1][1, 1, 2, 2, 1] has 33 color segments: [1,1][1, 1], [2,2][2, 2], and [1][1].

Every day, travelers come and ask qq questions. Each question specifies an interval [l,r][l, r]: if we take out the crystals in this interval and randomly shuffle them (all different color sequences appear with equal probability), what is the expected number of color segments after shuffling?

Output the answer modulo 998244353998244353. That is, if the answer is the reduced fraction xy\dfrac{x}{y}, output x⋅y−1 mod 998244353x \cdot y^{-1} \bmod 998244353. It can be proven that under the constraints of this problem, y−1y^{-1} always exists.

Input Format

The first line contains an integer TT (1≤T≤1051 \le T \le 10^5), which is the number of test cases. Then there are TT blocks, each describing one test case:

  • The first line contains two integers n,qn, q (1≤n,q≤1051 \le n, q \le 10^5), representing the number of crystals and the number of queries.
  • The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n), representing the color ID of each crystal.
  • The next qq lines each contain two integers l,rl, r (1≤l≤r≤n1 \le l \le r \le n), representing the endpoints of the query interval.

It is guaranteed that ∑n≤105\sum n \le 10^5 and ∑q≤105\sum q \le 10^5.

Output Format

For each query in each test case, output one integer per line, representing the expected number of color segments modulo 998244353998244353.

1
4 2
1 1 2 2
1 2
1 4
1
3
1
10 5
3 5 3 3 6 4 8 2 3 5
6 9
1 8
8 10
4 9
7 7
4
748683272
3
665496241
1

Hint

For the first sample:

For the first query, the taken crystal colors are [1,1][1, 1]. There is only one permutation, and the number of color segments is 11.

For the second query, the taken crystal colors are [1,1,2,2][1, 1, 2, 2]. The numbers of color segments for the 66 permutations are 2,4,3,3,4,22, 4, 3, 3, 4, 2, so the expected value is 186=3\dfrac{18}{6} = 3.

Translated by ChatGPT 5