

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 1800 点
問題文
正整数列 S 及び正整数 k,l が以下のいずれかの条件をみたすとき、 S が レベル (k,l) に属すると定義することにします。
- S の要素数が 1 であり、その要素の値が k である。
- あるレベル(k-1,l) に属する数列 T_1,T_2,...,T_m (m ≧ l) が存在して、 T_1,T_2,...,T_m をこの順に連結して得られる数列と S が一致する。
ただし、k=1 のとき二番目の条件は意味を持たない、つまりレベル(1,l)の正整数列は一つ目の条件をみたすもののみであることに注意して下さい。
正整数列 A_1,A_2,...,A_N と正整数 L が与えられます。 以下の条件をみたす部分列 A_i,A_{i+1},...,A_j (1 ≦ i ≦ j ≦ N) の個数を求めてください。
- ある正整数 K が存在して、数列 A_i,A_{i+1},...,A_j がレベル(K,L)に属する。
制約
- 1 ≦ N ≦ 2 \times 10^5
- 2 ≦ L ≦ N
- 1 ≦ A_i ≦ 10^9
入力
入力は以下の形式で標準入力から与えられる。
N L A_1 A_2 ... A_N
出力
条件をみたす部分列の個数を出力せよ。
入力例 1
9 3 2 1 1 1 1 1 1 2 3
出力例 1
22
例えば (1,1,1) と (2) という数列はともにレベル (2,3) に属するので、(2,1,1,1,1,1,1) という数列はレベル (3,3) に属します。
入力例 2
9 2 2 1 1 1 1 1 1 2 3
出力例 2
41
入力例 3
15 3 4 3 2 1 1 1 2 3 2 2 1 1 1 2 2
出力例 3
31
Score : 1800 points
Problem Statement
For a sequence S of positive integers and positive integers k and l, S is said to belong to level (k,l) when one of the following conditions is satisfied:
- The length of S is 1, and its only element is k.
- There exist sequences T_1,T_2,...,T_m (m \geq l) belonging to level (k-1,l) such that the concatenation of T_1,T_2,...,T_m in this order coincides with S.
Note that the second condition has no effect when k=1, that is, a sequence belongs to level (1,l) only if the first condition is satisfied.
Given are a sequence of positive integers A_1,A_2,...,A_N and a positive integer L. Find the number of subsequences A_i,A_{i+1},...,A_j (1 \leq i \leq j \leq N) that satisfy the following condition:
- There exists a positive integer K such that the sequence A_i,A_{i+1},...,A_j belongs to level (K,L).
Constraints
- 1 \leq N \leq 2 \times 10^5
- 2 \leq L \leq N
- 1 \leq A_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N L A_1 A_2 ... A_N
Output
Print the number of subsequences A_i,A_{i+1},...,A_j (1 \leq i \leq j \leq N) that satisfy the condition.
Sample Input 1
9 3 2 1 1 1 1 1 1 2 3
Sample Output 1
22
For example, both of the sequences (1,1,1) and (2) belong to level (2,3), so the sequence (2,1,1,1,1,1,1) belong to level (3,3).
Sample Input 2
9 2 2 1 1 1 1 1 1 2 3
Sample Output 2
41
Sample Input 3
15 3 4 3 2 1 1 1 2 3 2 2 1 1 1 2 2
Sample Output 3
31