#P8889. [入门赛 #7] 狠狠地切割 (Hard Version)

    ID: 9926 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>二分2022O2优化排序双指针 two-pointer语言月赛

[入门赛 #7] 狠狠地切割 (Hard Version)

Background

This problem has exactly the same statement as H1. The only difference is the constraints. In the Language Monthly Contest there is no H2 problem. This problem is only used to increase the distinction in the public contest, and it does not strictly follow the contest syllabus. Please solve it as you see fit.

Problem Description

You are given a sequence a1,,ana _ 1, \cdots, a _ n of length nn, and mm pairwise distinct integers b1,,bmb _ 1, \cdots, b _ m. You need to perform a hard cut on the sequence aa according to these mm numbers.

Specifically, for a number i[1,n]i \in [1, n], if there exists an integer j[1,m]j \in [1, m] such that ai=bja _ i = b _ j, then position ii is called a cut point. For every cut point in the sequence aa, we perform one hard cut at this position. The method is: remove the number at this position, and then split the sequence/segment on its left and right into two parts at this position.

If you are unsure about the definition of hard cut, you can refer to “Sample #1” and “Sample Explanation #1”.

You need to calculate: after performing all possible hard cuts, into how many segments the sequence is cut.

In particular, if after cutting, some part contains no numbers, then this part cannot be called a segment. Similarly, if 11 or nn is a cut point, then there is also no segment between it and the beginning or the end.

If you are unsure about the concept of segments, you can refer to “Sample #2” and “Sample Explanation #2”.

Input Format

The first line contains two integers, representing the length nn of sequence aa and the length mm of sequence bb, respectively.
The second line contains nn integers, where the ii-th integer is aia_i.
The third line contains mm integers, where the ii-th integer is bib_i.

Output Format

Output one integer, representing the number of segments after hard cutting.

6 2
3 4 3 5 2 6
5 4
3
6 3
3 4 3 5 2 6
3 5 6
2

Hint

Sample 1 Explanation

Before the hard cut, the sequence aa is as follows:

343526\begin{matrix} 3 & 4 & 3 & 5 & 2 & 6 \end{matrix}

It is easy to see that the second position and the fourth position are cut points. We use | to replace them, representing the removal:

3326\begin{matrix} 3 & | & 3 & | & 2 & 6 \end{matrix}

We mark the segments briefly:

$$\begin{matrix} \overbrace{3} ^ \text{Segment 1} & | & \overbrace{3} ^ \text{Segment 2} & | & \overbrace{2 \quad 6} ^ \text{Segment 3} \end{matrix}$$

There are 33 segments in total.

Sample 2 Explanation

Below we show the sequence after removal:

42\begin{matrix} | & 4 & | & | & 2 & | \end{matrix}

We mark the segments briefly:

$$\begin{matrix} \overbrace{\vphantom{0}} ^ \text{\color{red}This is not a segment} | & \overbrace{4} ^ \text{Segment 1} & | & \overbrace{\vphantom{0}} ^ \text{\color{red}This is not a segment} & | & \overbrace{2} ^ \text{Segment 2} & | \overbrace{\vphantom{0}} ^ \text{\color{red}This is not a segment}\end{matrix}$$

There are 22 segments in total.

Constraints

  • For 30%30\% of the testdata, it is guaranteed that no element in sequence aa appears in bb. Formally, $\forall i \in [1, n], \forall j \in [1, m], a _ i \neq b _ j$.
  • For 100%100\% of the testdata, it is guaranteed that 1n,m5×1051 \leq n, m \leq 5 \times 10 ^ 5, 1018ai,bi1018- 10 ^ {18} \leq a _ i, b_i \leq 10 ^ {18}, and the elements in sequence bb are pairwise distinct.

Hint

The input size of this problem is large, so it is recommended to use faster input/output methods.

Translated by ChatGPT 5