Non-Decreasing Subsequences
Time limit2sMemory limit512 MB
Given an array of values from 1 to K, answer queries counting non-decreasing subsequences within a subarray, including the empty one, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Divide and conquer, Matrix, Prefix sum
- Solved
- No attempts yet
Problem
Bessie was recently taking a USACO contest and encountered the following problem. Of course, Bessie knows how to solve it. But do you?
Consider a sequence of length consisting solely of integers in the range You are given queries of the form For each query, compute the number of non-decreasing subsequences of mod .
A non-decreasing subsequence of is a collection of indices such that and Make sure to consider the empty subsequence!
Input
The first line contains two space-separated integers and .
The second line contains space-separated integers .
The third line contains a single integer
The next lines each contain two space-separated integers and
Output
For each query you should print the number of non-decreasing subsequences of mod on a new line.
Hint
For the first query, the non-decreasing subsequences are and is not a non-decreasing subsequence because
For the second query, the non-decreasing subsequences are , , , and .