LCC '25 Contest 2 S5 - Happy (Part 2)
View as PDFThis problem is worth 70% of the 100 points allocated to S5
Consider an array of length , filled with positive integers from the range
to
, inclusive (the array does not necessarily need to contain every number, however).
An array with maximum element is considered happy under the following conditions:
- All of its elements lie in the range
to
- Each number from the range
to
occur at least once in the array
- The first occurence of the positive integer
lies before the first occurence of the positive integer
.
Brian hands you over a happy array, and wants you to find the number of happy arrays of the same length that are lexographically less than or equal to the happy array modulo . However, he thinks this current task is too easy for you. Instead, he wants you to find this result for
queries, where each query
increases the element
in the original happy array by
. If the modified happy array is no longer happy, then output
-1 for that query. After each query, the change in element resets if
, otherwise it will persist.
Input Specification
The first line of input will contain two space-separated integers , representing the number of elements in the array and the number of queries, respectively.
The second line of input will contain space-separated integers
, representing the elements in the original happy array. It is guaranteed that this array satisifies the happy condition.
The next lines of input will contain two integers
each, representing the query to increase element
by 1 and whether to persist the query or not (
means persist,
means discard). It is guaranteed that if a query were to persist, then the array is still happy.
Output Specification
Output lines, each containing one integer, representing the number of happy arrays lexographically less than or equal to the new array. Since the answer may be very large, output modulo
.
Constraints
For all subtasks,
| Marks Allocated | Bounds on |
Bounds on |
Bounds on |
|---|---|---|---|
Sample Input 1
4 2
1 1 2 2
4 0
3 0
Sample Output 1
5
-1
Sample Explanation 1
There are two queries, neither of which persist. The first query changes the array to 1 1 2 3, which is still happy. There are 5 happy arrays that are lexographically less than or equal to this array, those being 1 1 1 1, 1 1 1 2, 1 1 2 1, 1 1 2 2, 1 1 2 3. Keep in mind that 1 1 1 3 does NOT satisfy all conditions to be a happy array.
The second query changes the array to 1 1 3 2, which is not a happy array.
Sample Input 2
5 3
1 1 2 3 2
2 1
3 0
4 0
Sample Output 2
33
45
-1
Comments