← Home
write a go solution for Description:
Let's call an array a of m integers a_1,a_2,ldots,a_m Decinc if a can be made increasing by removing a decreasing subsequence (possibly empty) from it.

- For example, if a=[3,2,4,1,5], we can remove the decreasing subsequence [a_1,a_4] from a and obtain a=[2,4,5], which is increasing.

You are given a permutation p of numbers from 1 to n. Find the number of pairs of integers (l,r) with 1<=l<=r<=n such that p[lldotsr] (the subarray of p from l to r) is a Decinc array.

Input Format:
The first line contains a single integer n (1<=n<=2*10^5)  — the size of p.

The second line contains n integers p_1,p_2,ldots,p_n (1<=p_i<=n, all p_i are distinct)  — elements of the permutation.

Output Format:
Output the number of pairs of integers (l,r) such that p[lldotsr] (the subarray of p from l to r) is a Decinc array. (1<=l<=r<=n)

Note:
In the first sample, all subarrays are Decinc.

In the second sample, all subarrays except p[1ldots6] and p[2ldots6] are Decinc.. Output only the code with no comments, explanation, or additional text.