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 [8,6,1,3,5,7,4,2][8,6,1,3,5,7,4,2] is 44, because it has a contiguous increasing subarray [1,3,5,7][1,3,5,7], and there is no longer strictly monotone contiguous subarray.

Define A i BA\ \text{i}\ B as a new array formed by merging AA and BB in some way (that is, the new array can be split into two non-overlapping subsequences, which are exactly AA and BB). For example, if A=[1,2,3]A=[1,2,3] and B=[4,5]B=[4,5], then A i BA\ \text{i}\ B can be [1,4,2,5,3][1,4,2,5,3], [4,5,1,2,3][4,5,1,2,3], or [4,1,5,2,3][4,1,5,2,3], but it cannot be [1,2,3,4,3][1,2,3,4,3] or [1,2,3,5,4][1,2,3,5,4].

Let f(A,B)f(A,B) denote the minimum possible stability among all arrays in A i BA\ \text{i}\ B.

You are given an array AA of length nn and an array BB of length mm. Let AA' be a non-empty contiguous subarray of AA, and BB' be a non-empty contiguous subarray of BB. For every integer xx in the range [1,n+m][1,n+m], compute how many pairs (A,B)(A',B') satisfy f(A,B)=xf(A',B')=x. Output the answer modulo 109+710^9+7.

Input Format

The first line contains two integers nn and mm (1n,m3×105)(1\le n,m\le 3\times 10^5), the lengths of arrays AA and BB.

The second line contains nn integers A1,A2,,AnA_1,A_2,\ldots,A_n (1Ain+m)(1\le A_i\le n+m), describing array AA.

The third line contains mm integers B1,B2,,BmB_1,B_2,\ldots,B_m (1Bin+m)(1\le B_i\le n+m), describing array BB.

It is guaranteed that all integers in arrays AA and BB are pairwise distinct. In other words, A i BA\ \text{i}\ B is always a permutation of the integers from 11 to n+mn+m.

Output Format

Output one line with n+mn+m integers. The ii-th integer is the number of pairs (A,B)(A',B') such that f(A,B)=if(A',B')=i, modulo 109+710^9+7.

5 3
1 2 5 7 4
8 3 6

0 84 6 0 0 0 0 0

Hint

For the full ranges, f([1,2,5,7,4],[8,3,6])=2f([1,2,5,7,4],[8,3,6])=2. For example, the stability of [1,8,2,5,3,7,4,6][1,8,2,5,3,7,4,6] is 22, and it is impossible to make it smaller.

Consider the ranges [1,2,5,7][1,2,5,7] and [3][3]. We have f([1,2,5,7],[3])=3f([1,2,5,7],[3])=3, for example [1,2,5,3,7][1,2,5,3,7]. It can be proven that for ([1,2,5,7],[3])([1,2,5,7],[3]), no merging method can achieve stability smaller than 33.

Consider the ranges [4][4] and [6][6]. We have f([4],[6])=2f([4],[6])=2. These two ranges have only two ways to interleave: [4,6][4,6] or [6,4][6,4], and both have stability 22.

In the sample, the stability of every subarray pair is at most 33, so for x4x\ge 4 the answer is 00.

Translated by ChatGPT 5