#P10169. [DTCPC 2024] mex,min,max

    ID: 11522 远端评测题 1500ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树2024颜色段均摊(珂朵莉树 ODT)洛谷月赛单调栈

[DTCPC 2024] mex,min,max

Problem Description

Given a sequence {an}\{a_n\} and kk, find how many subarrays [l,r][l,r] satisfy $\operatorname{mex}\{a_l,a_{l+1},\dots,a_{r-1},a_r\}+\min\{a_l,a_{l+1},\dots,a_{r-1},a_r\}+k\geq \max\{a_l,a_{l+1},\dots,a_{r-1},a_r\}$.

mex\operatorname{mex} is defined as the smallest non-negative integer that does not appear in the set.

Input Format

The first line contains two integers n,kn,k (1n5×105,0kn1\leq n\leq 5\times 10^5,0\leq k\leq n).

The second line contains nn non-negative integers, where the ii-th one is aia_i (0ain0\leq a_i\leq n).

Output Format

One line with one number, representing the number of subarrays that satisfy the condition.

3 0
1 0 2
5

Hint

Translated by ChatGPT 5