P28464 [洛谷] [COCI2009-2010#1] GENIJALAC
提高-
题目描述

Problem Description

Translated from COCI 2009.10 T5 "GENIJALAC"

Mirko has invented a machine that shuffles. The machine accepts a tape with N columns and infinite rows as input and output. These N columns are numbered from 1 to N.

Initially, only the first row of the tape is written with numbers, and all the rows below are blank. In the first row of the tape, each column contains an integer, with the integer i written in the i-th column. Additionally, Mirko provides a shuffling permutation, which is a permutation of length N, denoted as \( s_1, s_2, \ldots, s_N \).

There is a row pointer that initially points to the first row. Each time the machine runs, it places the number in the i-th column of the current row (the row pointed to by the pointer) into the \( s_i \)-th column of the next row. After all N numbers are placed, the pointer moves down one row.

Mirko runs the machine infinitely many times. Now, Mirko cuts off the first C columns and the last D columns of the tape, and we call this the new tape.

Your task is to determine how many rows from the A-th to the B-th row of the new tape are identical to the first row of the new tape.

Input Format

The first line contains five integers \( N, A, B, C, D \).
The second line contains N integers \( s_1, s_2, \ldots, s_N \).

Output Format

Output a single integer, which is the number of rows from the A-th to the B-th row of the new tape that are identical to the first row of the new tape.

Sample Input and Output

Sample Input #1

4 1 5 0 1
1 3 4 2

Sample Output #1

2

Sample Input #2

7 3 8 1 2
2 3 1 6 4 7 5

Sample Output #2

0

Sample Input #3

6 2 11 3 0
6 3 5 4 2 1

Sample Output #3

1

Notes/Hints

Sample Explanation 1

+-----+
|1 2 3|4 <--
|1 3 4|2
|1 4 2|3
|1 2 3|4 <--
|1 3 4|2
+-----+
 1 4 2 3
 1 2 3 4

Sample Explanation 2

1 2 3 4 5 6 7
2 3 1 6 4 7 5
 +-------+
3|1 2 7 6|5 4
1|2 3 5 7|4 6
2|3 1 4 5|6 7
3|1 2 6 4|7 5
1|2 3 7 6|5 4
2|3 1 5 7|4 6
 +-------+
3 1 2 4 5 6 7
1 2 3 6 4 7 5

Sample Explanation 3

1 2 3 4 5 6
     +-----+
6 3 5|4 2 1|
1 5 2|4 3 6|
6 2 3|4 5 1|
1 3 5|4 2 6|
6 5 2|4 3 1|
1 2 3|4 5 6| <--
6 3 5|4 2 1|
1 5 2|4 3 6|
6 2 3|4 5 1|
1 3 5|4 2 6|
     +-----+

Data Range and Hints

For 40% of the data, \( A, B, C, D, N \leq 2000 \).
For all data, \( 1 \leq N \leq 5 \times 10^5, 1 \leq A, B \leq 10^{12}, 0 \leq C, D \leq N, C + D \leq N \).

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
4 1 5 0 1 1 3 4 2
样例输出
2