Beppa and SwerChat
Contest: SWERC 2022-2023 - Online Mirror (Unrated, ICPC Rules, Teams Preferred)
Difficulty rating: 1300
Tags: two pointers

Beppa and her circle of geek friends keep up to date on a group chat in the instant messaging app SwerChat$$$^{\text{TM}}$$$.

The group has $$$n$$$ members, excluding Beppa. Each of those members has a unique ID between $$$1$$$ and $$$n$$$. When a user opens a group chat, SwerChat$$$^{\text{TM}}$$$ displays the list of other members of that group, sorted by decreasing times of last seen online (so the member who opened the chat most recently is the first of the list). However, the times of last seen are not displayed.

Today, Beppa has been busy all day: she has only opened the group chat twice, once at 9:00 and once at 22:00. Both times, she wrote down the list of members in the order they appeared at that time. Now she wonders: what is the minimum number of other members that must have been online at least once between 9:00 and 22:00?

Beppa is sure that no two members are ever online at the same time and no members are online when Beppa opens the group chat at 9:00 and 22:00.

Input format:
Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10\,000$$$) — the number of test cases. The descriptions of the $$$t$$$ test cases follow.

The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of members of the group excluding Beppa.

The second line contains $$$n$$$ integers $$$a_1, \, a_2, \, \dots, \, a_n$$$ ($$$1 \le a_i \le n$$$) — the list of IDs of the members, sorted by decreasing times of last seen online at 9:00.

The third line contains $$$n$$$ integers $$$b_1, \, b_2, \, \dots, \, b_n$$$ ($$$1 \le b_i \le n$$$) — the list of IDs of the members, sorted by decreasing times of last seen online at 22:00.

For all $$$1\le i < j\le n$$$, it is guaranteed that $$$a_i \ne a_j$$$ and $$$b_i \ne b_j$$$.

It is also guaranteed that the sum of the values of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output format:
For each test case, print the minimum number of members that must have been online between 9:00 and 22:00.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first test case, members $$$4, 5$$$ must have been online between 9:00 and 22:00.

In the second test case, it is possible that nobody has been online between 9:00 and 22:00.

Example input:
4
5
1 4 2 5 3
4 5 1 2 3
6
1 2 3 4 5 6
1 2 3 4 5 6
8
8 2 4 7 1 6 5 3
5 6 1 4 8 2 7 3
1
1
1

Example output:
2
0
4
0

Editorial:
Instead of finding the minimum number of members that must have been online at least once between 9:00 and 22:00, let us study the complement of that set: What is the maximum number of members that have never been online? We have the following observations:
• Members that have never been online must be a suffix of the sequence $$$b$$$. It cannot happen that member $$$b_i$$$ has been online but $$$b_{i-1}$$$ has not, for $$$2 \leq i \leq n$$$.
• Consider two members $$$x$$$ and $$$y$$$ who have never been online. Their relative order in $$$a$$$ and $$$b$$$ is the same. Indeed, if someone else goes online the relative order of $$$x$$$ and $$$y$$$ in the list does not change.
From the above observations, we deduce that the members who have not been online form a suffix of $$$b$$$ that is also a subsequence of $$$a$$$. Let $$$p:\{1,2,\dots, n\}\to\{1,2,\dots,n\}$$$ be the function such that $$$a_{p(x)} = x$$$; we say that a sequence $$$s$$$ of length $$$m$$$ is a subsequence of $$$a$$$ if $$$p(s_{i-1}) < p(s_i)$$$, for $$$2 \leq i \leq m$$$.
It turns out that any "suffix of $$$b$$$ that is a subsequence of $$$a$$$" can be a valid subset of members who have never been online as the following construction shows. For any such suffix $$$b_t, b_{t+1}, \ldots, b_n$$$, it could be that members $$$b_{t-1}, b_{t-2}, \ldots, b_1$$$ went online in this order between 9:00 and 22:00, leading to a valid list $$$b$$$ of last seen online at 22:00.
Thus, the answer to the problem is the length of the longest suffix of $$$b$$$ that is also a subsequence of $$$a$$$. Let us explain how to find such length quickly.
We first preprocess the sequence $$$a$$$ to find the corresponding function $$$p$$$. Then, we iterate from $$$b_n$$$ to $$$b_1$$$. If, for some $$$i$$$ we discover that $$$p(b_i) < p(b_{i-1})$$$, then we know that $$$b_{i-1}$$$ must have been online, and we denote this position $$$i$$$ as $$$r$$$. In this way, $$$b_r, b_{r+1}, \ldots, b_n$$$ is the sought longest suffix of $$$b$$$. The minimum number of members that must have been online at least once between 9:00 and 22:00 is then $$$r - 1$$$.