#P6009. [USACO20JAN] Non-Decreasing Subsequences P

    ID: 6765 远端评测题 2000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP2020USACO矩阵运算分治

[USACO20JAN] Non-Decreasing Subsequences P

Problem Description

Bessie recently took part in a USACO contest and encountered the following problem. Of course, Bessie knows how to solve it. Do you?

Consider a sequence of length NN, A1,A2,,ANA_1, A_2, \ldots, A_N, consisting only of integers in the range 1K1 \ldots K (1K201 \leq K \leq 20) (1N5×1041 \leq N \leq 5 \times 10^4). You are given QQ (1Q2×1051 \leq Q \leq 2 \times 10^5) queries of the form [Li,Ri][L_i, R_i] (1LiRiN1 \leq L_i \leq R_i \leq N). For each query, compute the number of non-decreasing subsequences in ALi,ALi+1,,ARiA_{L_i}, A_{L_i+1}, \ldots, A_{R_i} modulo 109+710^9+7.

A non-decreasing subsequence of AL,,ARA_L, \ldots, A_R is a sequence of indices (j1,j2,,jx)(j_1, j_2, \ldots, j_x) such that Lj1<j2<<jxRL \le j_1 < j_2 < \ldots < j_x \le R and Aj1Aj2AjxA_{j_1} \le A_{j_2} \le \ldots \le A_{j_x}. Make sure you also count the empty subsequence.

Input Format

The first line contains two space-separated integers NN and KK.

The second line contains NN space-separated integers A1,A2,,ANA_1, A_2, \ldots, A_N.

The third line contains an integer QQ.

The next QQ lines each contain two space-separated integers LiL_i and RiR_i.

Output Format

For each query [Li,Ri][L_i, R_i], output on a new line the number of non-decreasing subsequences in ALi,ALi+1,,ARiA_{L_i}, A_{L_i+1}, \ldots, A_{R_i} modulo 109+710^9+7.

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

Hint

Sample Explanation

For the first query, the non-decreasing subsequences are ()(), (2)(2), and (3)(3). (2,3)(2,3) is not a non-decreasing subsequence because A2≰A3A_2\not \le A_3.

For the second query, the non-decreasing subsequences are ()(), (4)(4), (5)(5), and (4,5)(4,5).

Subtasks

  • Test points 232 \sim 3 satisfy N1000N \leq 1000.
  • Test points 464 \sim 6 satisfy K5K \leq 5.
  • Test points 797 \sim 9 satisfy Q105Q \leq 10^5.
  • Test points 101210 \sim 12 have no additional constraints.

Translated by ChatGPT 5