#P10144. [WC2024] 水镜

[WC2024] 水镜

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. There is water stored in the pool, and the water level LL is a positive real number. The ii-th pillar will form an image with height hi=2Lhih'_i = 2L - h_i. If the pillar is above the water surface, its image is below the water surface; if the pillar is below the water surface, its image is above the water surface; if the top of the pillar coincides with the water surface, then its image also coincides with the water surface.

It is said that spring spirits live in the water. 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 image. That is, if a spring spirit is on pillar uu, its height rur_u can only be huh_u or huh'_u.
  • Each time, a spring spirit can only move to the adjacent pillar on the right (or the image of that pillar).
  • During movement, the spirit’s height must be strictly increasing.

A spring spirit chooses a pillar (or its image) as the start, 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 accordingly. As a traveler from afar, you want to know how many dances are possible. Specifically, you need to compute 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 one dance, a spring spirit can start from the uu-th pillar (or its image) and reach the vv-th pillar (or its image).

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:

  • 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, then 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, indicating the number of stone pillars.

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

Output Format

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

4
1 3 2 4
6

Hint

Sample 1 Explanation

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\le n\le 5\times 10^5.
  • 1in,1hi1012\forall 1\le i\le n,1\le h_i\le 10^{12}.
Test Point ID nn\le
121\sim 2 1010
343\sim 4 100100
565\sim 6 400400
7117\sim 11 40004000
121312\sim 13 5×1045\times 10^4
141614\sim 16 10510^5
171917\sim 19 2×1052\times 10^5
202520\sim 25 5×1055\times 10^5

Translated by ChatGPT 5