Under construction

Still being built. Things may change

Counting Consecutive Ascending Subarrays

Interview

Time limit1sMemory limit1024 MB

Summary
Given a permutation A of length N, count the pairs (i, j) with i at most j where A_i through A_j are strictly increasing.
Level

Easy2 of 10

Topics
Array, Implementation
Solved
No attempts yet

Problem

You are given a sequence A1,⋯ ,ANA_1, \cdots, A_N of length NN. Every number in the sequence is a distinct integer from 11 to NN. Count the pairs of integers (i,j)(i, j) that satisfy all of the following conditions.

  • 1≤i≤j≤N1 \le i \le j \le N.
  • From the ii-th number to the jj-th number of AA, the sequence is in ascending order. That is, for every integer kk with i≤k<ji \le k < j, Ak<Ak+1A_k < A_{k+1}.

Input

The first line contains the length NN of the sequence. (1≤N≤200 000)(1 \le N \le 200\,000)

The next line contains the elements A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N separated by spaces. (1≤Ai≤N(1 \le A_i \le N; A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N are distinct integers.))

Output

Print the number of pairs (i,j)(i, j) that satisfy the conditions.

Examples1

  1. Example 1

    Input
    5
    1 2 5 4 3
    
    Expected output
    8