#P9152. 待黑白分明

    ID: 9971 远端评测题 3000ms 64MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>洛谷原创O2优化分块洛谷月赛笛卡尔树

待黑白分明

Background

.

Problem Description

The city where Shiro lives can be viewed as nn consecutive points on a number line. The height of point ii is pip_i, and it is guaranteed that pp is a permutation of {1,2,,n}\{1,2,\ldots,n\}.

In the year 3202, technology is highly advanced, and wormhole train technology has been developed. There are nn types of trains. The ii-th type of train passes through all positions whose height is at least ii. Each train line is bidirectional, meaning you can take the train from left to right or from right to left.

Shiro wants to travel around the city. She defines a set of positions SS to be valid if and only if, after sorting the positions in SS by height, each pair of adjacent positions in this order can be traveled between directly by taking one type of train without stopping in between.

She will ask you qq queries. In each query, you are given l,rl,r, and you need to tell Shiro the number of valid sets TT such that the heights of all positions are within [l,r][l,r], modulo 998244353998244353.

Input Format

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

The second line contains nn positive integers, representing p1,2,,np_{1,2,\ldots,n}.

The next qq lines each contain two positive integers li,ril_i, r_i, representing the height interval of the ii-th query.

Output Format

Output qq lines. Each line contains one non-negative integer, representing the answer.

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

5
12
6

Hint

Sample Explanation

Explanation for the first query:

The valid height sets are: {3},{4},{5},{3,5},{4,5}\{3\},\{4\},\{5\},\{3,5\},\{4,5\}.


Constraints

For 100%100\% of the testdata, 1n,q2×1051\le n,q\le 2\times {10}^5, pp is a permutation, and 1lirin1\le l_i \le r_i \le n.

Subtask nn\le qq\le Special Property Score
1 1515 10001000 1010
2 10001000 1515
3 A 55
4 B 3030
5 5×1045\times{10}^4 2020
6

Special Property A: pi=ip_i=i.

Special Property B: pp is chosen uniformly at random from all permutations of length nn.

Translated by ChatGPT 5