#P8600. [蓝桥杯 2013 省 B] 连号区间数

    ID: 7660 远端评测题 750ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2013线段树分治前缀和蓝桥杯省赛

[蓝桥杯 2013 省 B] 连号区间数

Problem Description

Xiaoming has been thinking about a strange but interesting problem these days:

In a certain permutation of 11 to NN, how many consecutive-number intervals are there? The definition of a consecutive-number interval is:

If all elements in the interval [L,R][L, R] (that is, the LL-th to the RR-th elements of the permutation), after being sorted in increasing order, form a "consecutive" sequence of length RL+1R - L + 1, then this interval is called a consecutive-number interval.

The definition of a "consecutive" sequence is:

For a sequence AA of length mm, let its ii-th element be AiA_i. If i[2,m]\forall i \in [2,m], Ai=Ai1+1A_i = A_{i-1} + 1, then the sequence is considered "consecutive".

When NN is very small, Xiaoming can quickly compute the answer. But when NN becomes large, the problem is not so simple. Now Xiaoming needs your help.

Input Format

The first line contains a positive integer NN (1N50000)(1 \le N \le 50000), indicating the size of the permutation.

The second line contains NN distinct numbers PiP_i (1PiN)(1 \le P_i \le N), representing a permutation of these NN numbers.

Output Format

Output an integer, representing the number of different consecutive-number intervals.

4
3 2 4 1
7
5
3 4 2 5 1
9

Hint

In the first sample, there are 77 consecutive-number intervals: [1,1][1,1], [1,2][1,2], [1,3][1,3], [1,4][1,4], [2,2][2,2], [3,3][3,3], [4,4][4,4].

In the second sample, there are 99 consecutive-number intervals: [1,1][1,1], [1,2][1,2], [1,3][1,3], [1,4][1,4], [1,5][1,5], [2,2][2,2], [3,3][3,3], [4,4][4,4], [5,5][5,5].

Original time limit: 5 seconds, 64 MB. Lanqiao Cup 2013, the 4th Provincial Contest.

Translated by ChatGPT 5