ArenaRating

solved.ac Grand Arena Party × NEXON (onsite, rated)

For the subarray to be sorted starting from the ii-th integer to the jj-th integer, for all kk such that ik<ji \le k < j, the condition Ak<Ak+1A_k < A_{k+1} should be satisfied. This condition can be checked by simply checking all adjacent elements. That is, if we set BkB_k as True or False based on whether Ak<Ak+1A_k < A_{k+1} is true, for the subarray to be ascending starting from the ii-th integer to the jj-th integer, Bi,Bi+1,,Bj1B_i, B_{i+1}, \cdots, B_{j-1} must all be true.

There are multiple ways to count such (i,j)(i, j) pairs.

The answer is the sum of k(k+1)2\frac{k(k+1)}{2} for each continuous block of length kk in BB. All calculations can be performed in O(N)\mathcal O (N) time.