问题 H: Insert 1, Insert 2, Insert 3, ...

内存限制:512 MB 时间限制:2 S 标准输入输出
题目类型:传统 评测方式:Special Judge 上传者:
提交:2 通过:2

题目描述

链接:https://ac.nowcoder.com/acm/contest/57362/H
来源:牛客网
A sequence a1,a2,…,ana_1, a_2, \ldots, a_na1,a2,,an consisting of positive integers is said to be generatable when one can obtain a by repeating the following operation on an empty sequence:
  • Choose a positive integer kkk, and insert 111, insert 222, ..., insert kkk respectively into some position in the sequence while preserving their relative order. More specifically, let ixi_xix be the index of newly inserted xxx in the sequence after the operation, then i1<i2<⋯<iki_1 < i_2 < \cdots < i_ki1<i2<<ik.
For instance, a=(1,1,2,2,1,1,3,4,2,3)a = (1,1,2,2,1,1,3,4,2,3)a=(1,1,2,2,1,1,3,4,2,3) is generatable. Here is one way to generate it:

()→(1,2)→(1,2,1,2,3)→(1,1,2,2,1,3,4,2,3)→(1,1,2,2,1,1,3,4,2,3)() \to (\textbf{1},\textbf{2}) \to (1,2,\textbf{1},\textbf{2},\textbf{3}) \to (1,\textbf{1},2,\textbf{2},1,\textbf{3},\textbf{4},2,3) \to (1,1,2,2,\textbf{1},1,3,4,2,3)()(1,2)(1,2,1,2,3)(1,1,2,2,1,3,4,2,3)(1,1,2,2,1,1,3,4,2,3).

You are given a sequence A1,A2,…,ANA_1, A_2, \ldots, A_NA1,A2,,AN consisting of positive integers. Find the number of pairs of integers (L,R)(L, R)(L,R) such that:

  •   1≤L≤R≤N1 \le L \le R \le N1LRN and the contiguous subsequence AL,…,ARA_L,\ldots,A_RAL,,AR is generatable.

输入格式

The first line contains an integer NNN (1≤N≤1061 \le N \le 10^61N106).
The second line contains NNN integers A1,A2,…,ANA_1,A_2,\ldots,A_NA1,A2,,AN (1≤Ai≤N1 \le A_{i} \le N1AiN).

输出格式

Output the number of pairs of integers (L,R)(L,R)(L,R) satisfying the conditions.
样例输入1:
6
1 2 1 2 1 3
样例输出1:
11

输入样例 复制

6
1 1 2 2 3 3

输出样例 复制

8

数据范围与提示

In the first sample, the 888 pairs are: (1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(2,2),(2,3)(1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(2,2),(2,3)(1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(2,2),(2,3).