#P8087. 『JROI-5』Interval

『JROI-5』Interval

Background

Little C likes data structures with interval operations, because such problems always have an easy-to-write O(n2)\mathcal{O}\left(n^2\right) partial score.

Problem Description

This problem has a large input size. It is recommended to use a fast input method. You may refer to the contest bulletin board.

Little C has a sequence aa of length nn, where the ii-th term is aia_i.

aa is a permutation of 1n1 \sim n (that is, each of 1n1 \sim n appears exactly once in aa).

Define Mexl,r\operatorname{Mex}_{l,r} as the smallest positive integer that does not appear in {al,al+1,,ar1,ar}\{a_l,a_{l+1},\cdots,a_{r-1},a_r\}.

For example, Mex{2,3}=1\operatorname{Mex}\{2,3\}=1, and Mex{1,2,3}=4\operatorname{Mex}\{1,2,3\}=4.

Little C also has an array ff of length nn.

An interval [l,r]\left[l,r\right] is defined to be valid if and only if

frl+1<Mexl,rf_{r-l+1}< \operatorname{Mex}_{l,r}

Little C wants you to tell him the length of the shortest valid interval. In particular, if no interval is valid, output 0.

Input Format

The first line contains a positive integer nn.

The second line contains nn positive integers a1,a2,,ana_1,a_2,\cdots,a_n.

The third line contains nn positive integers f1,f2,,fnf_1,f_2,\cdots,f_n.

Output Format

Output one integer in a single line, indicating the length of the shortest valid interval.

5
2 3 1 5 4
2 2 3 4 5
3
5
2 3 1 5 4
1 2 2 4 5
1
5
1 3 4 2 5
6 7 8 9 10
0
见附件
见附件

Hint

[Sample Explanation]

For #1, it is easy to see that [1,3]\left[1,3\right] is the shortest valid interval.

For #2, it is easy to see that [3,3]\left[3,3\right] is the shortest valid interval.

For #3, it is easy to see that there is no valid interval.


For 10%10\% of the testdata, 1n1001\leq n\leq 100.

For 20%20\% of the testdata, 1n10001\leq n\leq 1000.

For another 10%10\% of the testdata, ff is non-increasing, i.e. f1f2fnf_1\geq f_2\geq\cdots\geq f_n, and 1n1061\leq n\leq 10^6.

For 100%100\% of the testdata, 1n4×1061\leq n\leq 4\times 10^6 and 1fi1091\leq f_i\leq 10^9.

Translated by ChatGPT 5