355 传统题

[ABC485G] 相似相邻子段 (Similar Adjacent Subarrays)

时间限制
2000ms
内存限制
1024MiB
登顶 / 尝试
1/ 1
难度
8
    上传者 标签AtCoderABC 485省选-后缀数组调和级数字符串

[ABC485G] 相似相邻子段 (Similar Adjacent Subarrays)

Score : 625625 points

Problem Statement

You are given a sequence of positive integers A=(A1,A2,…,AN)A=(A_1,A_2,\dots,A_N) of length NN.

Find the number of triples of integers (a,b,c)(a,b,c) that satisfy all of the following conditions.

  • 0≤a<b<c≤N0 \leq a \lt b \lt c \leq N

  • b−a=c−bb-a=c-b

  • There is exactly one integer ii between 11 and b−ab-a, inclusive, such that Aa+i≠Ab+iA_{a+i} \neq A_{b+i}.

Constraints

  • 2≤N≤2×1052 \leq N \leq 2 \times 10^5

  • 1≤Ai≤1091 \leq A_i \leq 10^9

  • All input values are integers.

Input

The input is given from Standard Input in the following format:

$N$
$A_1$ $A_2$ $\ldots$ $A_N$

Output

Print the answer in one line.

Sample Input 1

7
1 2 1 3 1 2 1

Sample Output 1

10

The triples (a,b,c)(a,b,c) that satisfy the conditions are the following ten: $(0,1,2),(0,2,4),(1,2,3),(1,3,5),(2,3,4),(2,4,6),(3,4,5),(3,5,7),(4,5,6),(5,6,7)$.

Sample Input 2

2
1000000000 1000000000

Sample Output 2

0

Sample Input 3

6
1 2 3 1 2 4

Sample Output 3

6