#abc262c. C - Min Max Pair

C - Min Max Pair

Score : 300300 points

问题描述

给定一个长度为 NN 的整数序列 a=(a1,,aN)a = (a_1, \dots, a_N),其中的整数取值范围在 11NN 之间。

找出满足以下所有条件的整数对 (i,j)(i, j) 的数量:

  • 1i<jN1 \leq i < j \leq N
  • min(ai,aj)=i\min(a_i, a_j) = i
  • max(ai,aj)=j\max(a_i, a_j) = j

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a sequence a=(a1,,aN)a = (a_1, \dots, a_N) of length NN consisting of integers between 11 and NN.

Find the number of pairs of integers i,ji, j that satisfy all of the following conditions:

  • 1i<jN1 \leq i \lt j \leq N
  • min(ai,aj)=i\min(a_i, a_j) = i
  • max(ai,aj)=j\max(a_i, a_j) = j

Constraints

  • 2N5×1052 \leq N \leq 5 \times 10^5
  • 1aiN(1iN)1 \leq a_i \leq N \, (1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

a1a_1 \ldots aNa_N

Output

Print the answer.

Sample Input 1

4
1 3 2 4

Sample Output 1

2

(i,j)=(1,4),(2,3)(i, j) = (1, 4), (2, 3) satisfy the conditions.

Sample Input 2

10
5 8 2 2 1 6 7 2 9 10

Sample Output 2

8

update @ 2024/3/10 11:05:56

}