E - Max Min
Editorial
/


Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 500 点
問題文
長さ N の数列 A = (A_1, A_2, \dots, A_N) および整数 X, Y があります。 次の条件をすべて満たす整数の組 (L, R) の個数を求めてください。
- 1 \leq L \leq R \leq N
- A_L, A_{L+1}, \dots, A_R の最大値は X であり、最小値は Y である。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 2 \times 10^5
- 1 \leq Y \leq X \leq 2 \times 10^5
- 入力される値はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N X Y A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
4 3 1 1 2 3 1
出力例 1
4
条件を満たすのは (L,R)=(1,3),(1,4),(2,4),(3,4) の 4 通りです。
入力例 2
5 2 1 1 3 2 4 1
出力例 2
0
条件を満たす (L,R) は存在しません。
入力例 3
5 1 1 1 1 1 1 1
出力例 3
15
X=Y である場合もあります。
入力例 4
10 8 1 2 7 1 8 2 8 1 8 2 8
出力例 4
36
Score : 500 points
Problem Statement
We have a number sequence A = (A_1, A_2, \dots, A_N) of length N and integers X and Y. Find the number of pairs of integers (L, R) satisfying all the conditions below.
- 1 \leq L \leq R \leq N
- The maximum value of A_L, A_{L+1}, \dots, A_R is X, and the minimum is Y.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 2 \times 10^5
- 1 \leq Y \leq X \leq 2 \times 10^5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X Y A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
4 3 1 1 2 3 1
Sample Output 1
4
4 pairs satisfy the conditions: (L,R)=(1,3),(1,4),(2,4),(3,4).
Sample Input 2
5 2 1 1 3 2 4 1
Sample Output 2
0
No pair (L,R) satisfies the condition.
Sample Input 3
5 1 1 1 1 1 1 1
Sample Output 3
15
It may hold that X=Y.
Sample Input 4
10 8 1 2 7 1 8 2 8 1 8 2 8
Sample Output 4
36