#abc248h. Ex - Beautiful Subsequences

Ex - Beautiful Subsequences

Score : 600600 points

问题描述

你给定一个 (1,,N)(1,\ldots,N) 的排列 P=(P1,,PN)P=(P_1,\ldots,P_N),以及一个整数 KK

找出满足以下所有条件的整数对 (L,R)(L, R) 的数量:

  • 1LRN1 \leq L \leq R \leq N

  • $\mathrm{max}(P_L,\ldots,P_R) - \mathrm{min}(P_L,\ldots,P_R) \leq R - L + K$

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

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

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

  • 1LRN1 \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

  • 1N1.4×1051 \leq N \leq 1.4\times 10^5
  • PP is a permutation of (1,,N)(1,\ldots,N).
  • 0K30 \leq K \leq 3
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN KK

P1P_1 P2P_2 \ldots PNP_N

Output

Print the answer.

Sample Input 1

4 1
1 4 2 3

Sample Output 1

9

The following nine pairs (L,R)(L, R) satisfy the conditions.

  • (1,1)(1,1)
  • (1,3)(1,3)
  • (1,4)(1,4)
  • (2,2)(2,2)
  • (2,3)(2,3)
  • (2,4)(2,4)
  • (3,3)(3,3)
  • (3,4)(3,4)
  • (4,4)(4,4)

For (L,R)=(1,2)(L,R) = (1,2), we have $\mathrm{max}(A_1,A_2) -\mathrm{min}(A_1,A_2) = 4-1 = 3$ and RL+K=21+1=2R-L+K=2-1+1 = 2, not satisfying the condition.

Sample Input 2

2 0
2 1

Sample Output 2

3

Sample Input 3

10 3
3 7 10 1 9 5 4 8 6 2

Sample Output 3

37

update @ 2024/3/10 10:38:16