PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000210. Các đoạn con đẹp (Beautiful Subsequences)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Các đoạn con đẹp (Beautiful Subsequences)

Beautiful Subsequences

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a permutation P=(P1,P2,…,PN)P=(P_1,P_2,\ldots,P_N) of the integers 1,2,…,N1,2,\ldots,N and a nonnegative integer KK.

A contiguous subarray [L,R][L,R], 1≤L≤R≤N1\le L\le R\le N, is called beautiful if

$$\max(P_L,\ldots,P_R)-\min(P_L,\ldots,P_R)\le R-L+K.$$

Count the index pairs (L,R)(L,R) satisfying this condition. Different index pairs are counted separately.

Input

  • The first line contains two integers N,KN,K, the permutation length and allowed slack.
  • The second line contains exactly NN integers P1,P2,…,PNP_1,P_2,\ldots,P_N, forming a permutation of 1,2,…,N1,2,\ldots,N.

Output

Print one integer: the number of beautiful index pairs (L,R)(L,R).

Subtasks

  • Subtask 1 (20 points): 1≤N≤91\le N\le 9; 0≤K≤30\le K\le 3; PP is a permutation of 1,…,N1,\ldots,N.
  • Subtask 2 (30 points): 1≤N≤25001\le N\le 2500; 0≤K≤30\le K\le 3; PP is a permutation of 1,…,N1,\ldots,N.
  • Subtask 3 (50 points): 1≤N≤1.4⋅1051\le N\le 1.4\cdot10^5; 0≤K≤30\le K\le 3; PP is a permutation of 1,…,N1,\ldots,N.

Examples

Input

4 1
1 4 2 3

Output

9

Explanation

There are 4⋅5/2=104\cdot5/2=10 nonempty subarrays. The only invalid one is [1,2][1,2]: its maximum is 44, its minimum is 11, so the difference is 33, exceeding R−L+K=2R-L+K=2. All other subarrays satisfy the inequality; for example, [1,3][1,3] has difference 4−1=3≤34-1=3\le3, and [3,4][3,4] has difference 3−2=1≤23-2=1\le2. Hence the answer is 10−1=910-1=9.