#P6854. Tram

Tram

Description

You are about to debut.

Your coach and classmates have contacted a photographer for you, and you come to take promotional photos by Des Voeux Road.

There are nn buildings on the street, arranged in a line from left to right, numbered from 11 to nn. The height of building ii is aia_i.

A photo can be described by an ordered pair (l,r)(l,r), where 1lrn1\le l\le r\le n. This photo contains all buildings with indices in [l,r][l,r].

The photographer thinks a photo is beautiful if and only if it satisfies both conditions below:

  • For any i<j<ki<j<k, if buildings of heights ii and kk both appear in the photo, then buildings of height jj also appear in the photo.
  • For any ii, buildings of height ii either do not appear in the photo, or appear exactly ii times in the photo.

The photographer asks you: how many different beautiful photos can be taken in total?

Two photos (l1,r1)(l_1,r_1) and (l2,r2)(l_2,r_2) are different if and only if l1l2l_1\ne l_2 or r1r2r_1\ne r_2.

Input Format

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

The next line contains nn positive integers. The ii-th integer is the height of building ii.

Output Format

Output one line containing one integer, representing the number of different beautiful photos that can be taken.

10
2 2 1 1 2 2 3 1 3 3 
8

Hint

This problem uses bundled tests. You can get the score for a subtask only if you pass all test points in that subtask.

  • Subtask 1 (10 points): n200n\le 200.
  • Subtask 2 (5 points): n1000n\le 1000.
  • Subtask 3 (10 points): n6000n\le 6000.
  • Subtask 4 (20 points): n3×104n\le 3\times 10^4.
  • Subtask 5 (30 points): n105n\le 10^5.
  • Subtask 6 (25 points): n106n\le 10^6.

For all testdata: 1n,ai1061\le n,a_i\le 10^6.

Note that the answer may exceed the range of a 3232-bit signed integer.

The input size is large, so please use a fast input method.

Translated by ChatGPT 5