This page is still under construction.

Parts of this page are still being built. What you see may change.

Cartoons

Time limit2.5sMemory limit256 MB

Summary
Count subarrays in which every sub-subarray contains at least one value that appears exactly once, over a sequence of up to 500,000 values.
Level

Hard8 of 10

Topics
Two pointers, Divide and conquer, Array, Binary search
Solved
No attempts yet

Problem

Sophie's parents made a DVD with episodes of her favourite cartoon. When she wants to watch it, they play her an interval of episodes, that is, a sequence of episodes that are consecutive on the DVD. Unfortunately, they were a little careless when making the DVD: some of the episodes repeat, to Sophie's dislike. An interval of episodes is interesting (to Sophie) if at least one episode in it is different than all other. Moreover, Sophie sometimes misses the episodes at the beginning of the interval (as she wants to play with toys a little more) and sometimes she does not watch the whole interval and misses some episodes at the end. Thus an interval of episodes is very interesting, if each of its subinterval of episodes is interesting.

Sophie's parents wonder, which of the intervals of the episodes on the DVD are very interesting? Help them out--given the full interval of episodes on the DVD compute, how many of its subintervals are very interesting?

Input

First line of the input contains a single natural number nn (1≤n≤500 0001 \le n \le 500\,000) which is the number of episodes on the DVD. The second and last line contains nn natural numbers, the ii-th of them is the episode number aia_i of the ii-th episode (1≤ai≤1091 \le a_i \le 10^9).

Output

You should write a single natural number: the number of very interesting intervals of the sequence of cartoons given in the input.

Notes

In Sample 1, the following sequences are interesting: all length-1 sequences, three sequences of length 2: only (6, 6) is not interesting, all three length-3 sequences, one length-4 sequence--(6, 1, 6, 6). Among them the sequences (1, 6, 6) and (6, 1, 6, 6) are not very interesting.

In Sample 2, only the full sequence (1,2,3,1,2,3)(1, 2, 3, 1, 2, 3) is not very interesting.

Examples2

  1. Example 1

    Input
    5
    1 6 1 6 6
    
    Expected output
    10
    
  2. Example 2

    Input
    6
    1 2 3 1 2 3
    
    Expected output
    20