#P10210. [CTS2024] 水镜

[CTS2024] 水镜

Problem Description

Hint: At the end of the statement, we provide a brief and formal description.

City A is a rainy city with many mountain streams and springs. Because people love water, they built a large fountain in the center of the city.

In the fountain pool, there is a row of nn stone pillars, numbered from left to right as 1,2,,n1, 2, \cdots , n. The height of the ii-th pillar is hih_i. The pool contains water, and the water level LL is a positive real number. The ii-th pillar produces a reflection with height hi=2Lhih'_i = 2L - h_i. If the pillar is above the water surface, the reflection is below the water surface; if the pillar is below the water surface, the reflection is above the water surface; if the top of the pillar is exactly on the water surface, then the reflection is also exactly on the water surface.

It is said that spring spirits live in the water. On every full moon night, they dance on the pillars, following these rules:

  • A spring spirit can only stay on the top of a pillar, or on the top of the pillar’s reflection. That is, if the spirit is on pillar uu, its height rur_u can only be huh_u or huh'_u.
  • Each move, the spring spirit can only go to the adjacent pillar on the right (or the adjacent reflection).
  • During the movement, the spirit’s height must be strictly increasing.

A spring spirit chooses a pillar (or its reflection) as the starting point, makes several moves, and then stops. Such a process is called a dance.

City A has a long rainy season. Because rainfall is irregular, the fountain’s water level may change many times, and the possible dance paths change as well. As a traveler from afar, you want to know how many dances can be made possible. Specifically, you need to count how many pairs (u,v)(u, v) (1u<vn1 \le u < v \le n) satisfy that there exists a water level LL such that, in a single dance, the spirit can start from the uu-th pillar (or its reflection) and reach the vv-th pillar (or its reflection).

Formal: Given a positive integer sequence h1,h2,,hnh_1, h_2,\cdots , h_n of length nn, find the number of pairs (u,v)(u, v) that satisfy all of the following conditions:

  • 1u<vn1 \le u < v \le n, and u,vu, v are integers.
  • There exists a positive real number LL and a sequence ru,ru+1,,rvr_u, r_{u+1},\cdots , r_v of length (vu+1)(v - u + 1) such that all of the following hold:
  • uiv\forall u \le i \le v, let hi=2Lhih'_i = 2L - h_i. Then ri{hi,hi}r_i \in \{h_i,h'_i\}. In particular, when hi=hih_i = h'_i, we have ri=hir_i = h_i.
  • ui<v,ri<ri+1\forall u \le i < v, r_i < r_{i+1}.

Input Format

The first line contains a positive integer nn, representing the number of stone pillars.

The second line contains nn positive integers h1,h2,,hnh_1, h_2, \cdots , h_n, representing the heights of the pillars.

Output Format

Output one line containing one integer, representing the number of pairs (u,v)(u, v) that satisfy the statement.

4
1 3 2 4

6

Hint

Explanation for Sample 1

All (42)=6\binom{4}{2}=6 pairs (u,v)(u, v) are feasible.
For u=1,v=4u = 1, v = 4, you can choose L=2.5L = 2.5, then the sequence hh' is {4,2,3,1}\{4, 2, 3, 1\}. Taking the sequence rr as {1,2,3,4}\{1, 2, 3, 4\} satisfies all conditions.

Constraints

For all testdata, 2n5×1052\leq n\leq 5\times 10 ^ 5, 1hi10121\leq h_i\leq 10 ^ {12}.

Subtask ID nn\leq Points
11 1010 77
22 100100 1010
33 40004000 1818
44 10510 ^ 5 3030
55 5×1055\times 10^ 5 3535

Translated by ChatGPT 5