luogu#P10356. [PA 2024] Splatanie ciągów
[PA 2024] Splatanie ciągów
Background
PA 2024 3A
Problem Description
Define the stability of an array as the length of the longest contiguous subarray that is strictly increasing or strictly decreasing. For example, the stability of the array is , because it has a contiguous increasing subarray , and there is no longer strictly monotone contiguous subarray.
Define as a new array formed by merging and in some way (that is, the new array can be split into two non-overlapping subsequences, which are exactly and ). For example, if and , then can be , , or , but it cannot be or .
Let denote the minimum possible stability among all arrays in .
You are given an array of length and an array of length . Let be a non-empty contiguous subarray of , and be a non-empty contiguous subarray of . For every integer in the range , compute how many pairs satisfy . Output the answer modulo .
Input Format
The first line contains two integers and , the lengths of arrays and .
The second line contains integers , describing array .
The third line contains integers , describing array .
It is guaranteed that all integers in arrays and are pairwise distinct. In other words, is always a permutation of the integers from to .
Output Format
Output one line with integers. The -th integer is the number of pairs such that , modulo .
5 3
1 2 5 7 4
8 3 6
0 84 6 0 0 0 0 0
Hint
For the full ranges, . For example, the stability of is , and it is impossible to make it smaller.
Consider the ranges and . We have , for example . It can be proven that for , no merging method can achieve stability smaller than .
Consider the ranges and . We have . These two ranges have only two ways to interleave: or , and both have stability .
In the sample, the stability of every subarray pair is at most , so for the answer is .
Translated by ChatGPT 5