#539. 「Nowcoder多校 2019 Day1」Equivalent Prefixes

「Nowcoder多校 2019 Day1」Equivalent Prefixes

题目描述

Two arrays u and v each with m distinct elements are called equivalent if and only if RMQ(u,l,r)=RMQ(v,l,r) \mathrm{RMQ}(u, l, r) = \mathrm{RMQ}(v, l, r) for all 1lrm 1 \leq l \leq r \leq m where RMQ(w,l,r) \mathrm{RMQ}(w, l, r) denotes the index of the minimum element among wl,wl+1,,wr.w_l, w_{l + 1}, \dots, w_{r}​. Since the array contains distinct elements, the definition of minimum is unambiguous.

Bobo has two arrays a and b each with n distinct elements. Find the maximum number pn p \leq n where {a1,a2,,ap}\{a_1, a_2, \dots, a_p\} and {b1,b2,,bp} \{b_1, b_2, \dots, b_p\} are equivalent.

输入格式

The input consists of several test cases and is terminated by end-of-file. The first line of each test case contains an integer n. The second line contains n integers a1,a2,,ana_1, a_2, \dots, a_n. The third line contains n integers b1,b2,,bnb_1, b_2, \dots, b_n.

  • 1n1051 \leq n \leq 10^5
  • 1ai,bin1 \leq a_i, b_i \leq n
  • {a1,a2,,an}\{a_1, a_2, \dots, a_n\} are distinct.
  • {b1,b2,,bn}\{b_1, b_2, \dots, b_n\} are distinct.
  • The sum of n does not exceed 5×1055 \times 10^5.

输出格式

For each test case, print an integer which denotes the result.

样例

样例输入

2
1 2
2 1
3
2 1 3
3 1 2
5
3 1 5 2 4
5 2 4 3 1

样例输出

1
3
4