Score : $600$ points
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$
Input is given from Standard Input in the following format:
$N$ $K$ $P_1$ $P_2$ $\ldots$ $P_N$
Print the answer.
4 1 1 4 2 3
9
The following nine pairs $(L, R)$ satisfy the conditions.
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.
2 0 2 1
3
10 3 3 7 10 1 9 5 4 8 6 2
37