QOJ.ac

QOJ

時間限制: 9 s 記憶體限制: 1024 MB 總分: 10

#8410. String Splitting [A]

统计

A subsegment of a sequence $C$ is any non-empty and contiguous subsequence. Specifically, this means that every sequence of length $k$ has $\frac{k(k+1)}{2}$ subsegments, as each subsegment is determined by its start and end.

For a given sequence of integers, its stability is defined as the length of its longest strictly monotonic subsegment. More precisely, the stability of a sequence $[c_1, c_2, \dots, c_k]$ is the largest integer $s$ such that there exists an index $i$ ($1 \le i \le k - s + 1$) where $c_i < c_{i+1} < \dots < c_{i+s-1}$ or $c_i > c_{i+1} > \dots > c_{i+s-1}$. For example, the stability of the sequence $[8, 6, 1, 3, 5, 7, 4, 2]$ is $4$, because it contains the strictly monotonic subsegment $[1, 3, 5, 7]$, and no longer one exists.

A shuffle of two sequences $A$ and $B$ is any sequence of length $|A| + |B|$ that contains a subsequence (not necessarily contiguous) equal to $A$, such that all elements not in this subsequence form the sequence $B$. For example, shuffles of the sequences $[1, 2, 3]$ and $[4, 5]$ include $[1, 4, 2, 5, 3]$, $[4, 5, 1, 2, 3]$, and $[4, 1, 5, 2, 3]$, but not $[1, 2, 3, 4, 3]$ or $[1, 2, 3, 5, 4]$.

Finally, $f(A, B)$, where $A$ and $B$ are sequences of integers, denotes the minimum possible stability of their shuffle.

Given two sequences of integers $A$ and $B$, with lengths $n$ and $m$ respectively, your task is to calculate, for each integer $x$ from $1$ to $n + m$ inclusive, the number of pairs $(A', B')$ such that $A'$ is a subsegment of $A$, $B'$ is a subsegment of $B$, and $f(A', B') = x$. Since these numbers can be very large, you only need to provide their remainders modulo $10^9 + 7$.

You may assume that all elements of sequences $A$ and $B$ are pairwise distinct.

Input

The first line of input contains two integers $n$ and $m$ ($1 \le n, m \le 300\,000$), representing the lengths of sequences $A$ and $B$, respectively.

The second line of input contains a sequence of $n$ integers $A_1, A_2, \dots, A_n$ ($1 \le A_i \le n + m$) — the aforementioned sequence $A$.

The third line of input contains a sequence of $m$ integers $B_1, B_2, \dots, B_m$ ($1 \le B_i \le n + m$) — the aforementioned sequence $B$.

It is guaranteed that all elements from sequences $A$ and $B$ are pairwise distinct. In other words, the concatenation of sequences $A$ and $B$ forms a permutation of numbers from $1$ to $n + m$.

Output

The only line of standard output should contain $n + m$ integers separated by single spaces; the $i$-th of these numbers should be equal to the remainder modulo $10^9 + 7$ of the number of pairs $(A', B')$ such that $A'$ is a subsegment of $A$, $B'$ is a subsegment of $B$, and $f(A', B') = i$.

Examples

Input 1

5 3
1 2 5 7 4
8 3 6

Output 1

0 84 6 0 0 0 0 0

Note

For the subsegments that are the entire sequences, $f([1, 2, 5, 7, 4], [8, 3, 6]) = 2$, and a shuffle with stability equal to $2$ is, for example, the sequence $[1, 8, 2, 5, 3, 7, 4, 6]$.

When we consider the subsegments $[1, 2, 5, 7]$ and $[3]$, we get $f([1, 2, 5, 7], [3]) = 3$, and a shuffle with stability equal to $3$ is, for example, the sequence $[1, 2, 5, 3, 7]$. It can be shown that the pair of sequences $([1, 2, 5, 7], [3])$ cannot be shuffled to obtain a sequence with stability less than $3$.

For the subsegments $[4]$ and $[6]$, $f([4], [6]) = 2$, and good examples are both of their possible shuffles: $[4, 6]$ and $[6, 4]$.

Every pair of subsegments in this example can be shuffled such that the stability of the resulting shuffle is no greater than $3$. Hence, the answers for $x \ge 4$ are equal to $0$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.