102487: [AtCoder]ABC248 Ex - Beautiful Subsequences

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $600$ points

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.


Sample Input 1

4 1
1 4 2 3

Sample 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.


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

Input

题意翻译

给定排列 $ P_n $ 和整数 $ k $,求满足如下条件的点对 $ (l, r) $ 数量。 * $ 1 \le l \le r \le n $。 * $ \max_{i = l}^rP_i - \min_{i = l}^rP_i \le r - l + k $。

Output

得分:$600$分

问题描述

给你一个$(1,\ldots,N)$的排列$P=(P_1,\ldots,P_N)$,以及一个整数$K$。

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

  • $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$

限制条件

  • $1 \leq N \leq 1.4\times 10^5$
  • $P$是$(1,\ldots,N)$的一个排列。
  • $0 \leq K \leq 3$
  • 输入中的所有值都是整数。

输入

从标准输入以以下格式获取输入:

$N$ $K$
$P_1$ $P_2$ $\ldots$ $P_N$

输出

打印答案。


样例输入1

4 1
1 4 2 3

样例输出1

9

以下九对整数对$(L, R)$满足条件。

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

对于$(L,R) = (1,2)$,我们有$\mathrm{max}(A_1,A_2) -\mathrm{min}(A_1,A_2) = 4-1 = 3$和$R-L+K=2-1+1 = 2$,不满足条件。


样例输入2

2 0
2 1

样例输出2

3

样例输入3

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

样例输出3

37

加入题单

算法标签: