ユニークビジョンプログラミングコンテスト2022(AtCoder Beginner Contest 248)
Ex
Beautiful Subsequences
AtCoder
⌀ 2960
原题
←G →
翻译中…

Problem Statement

翻译中…

You are given a permutation $P=(P_1,\ldots,P_N)$ of $(1,\ldots,N)$, and an integer $K$.

翻译中…

Find the number of pairs of integers $(L, R)$ that satisfy all of the following conditions:

翻译中…
  • $1 \leq L \leq R \leq N$
  • $\mathrm{max}(P_L,\ldots,P_R) - \mathrm{min}(P_L,\ldots,P_R) \leq R - L + K$
翻译中…

Constraints

翻译中…
  • $1 \leq N \leq 1.4\times 10^5$
  • $P$ is a permutation of $(1,\ldots,N)$.
  • $0 \leq K \leq 3$
  • All values in input are integers.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

N K
P_1 P_2 \ldots P_N

翻译中…

Output

翻译中…

Print the answer.

翻译中…

样例

Input #1
4 1
1 4 2 3
Output #1
9
The following nine pairs $(L, R)$ satisfy the conditions. - $(1,1)$ - $(1,3)$ - $(1,4)$ - $(2,2)$ - $(2,3)$ - $(2,4)$ - $(3,3)$ - $(3,4)$ - $(4,4)$ For $(L,R) = (1,2)$, we have $\mathrm{max}(A_1,A_2) -\mathrm{min}(A_1,A_2) = 4-1 = 3$ and $R-L+K=2-1+1 = 2$, not satisfying the condition.
Input #2
2 0
2 1
Output #2
3
Input #3
10 3
3 7 10 1 9 5 4 8 6 2
Output #3
37
题目信息

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。