CF1744F.MEX vs MED

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a permutation p1,p2,…,pnp_1, p_2, \ldots, p_n of length nn of numbers 0,…,n−10, \ldots, n - 1. Count the number of subsegments 1≤l≤r≤n1 \leq l \leq r \leq n of this permutation such that mex(pl,pl+1,…,pr)>med(pl,pl+1,…,pr)mex(p_l, p_{l+1}, \ldots, p_r) \gt med(p_l, p_{l+1}, \ldots, p_r).

mexmex of SS is the smallest non-negative integer that does not occur in SS. For example:

  • mex(0,1,2,3)=4mex({0, 1, 2, 3}) = 4
  • mex(0,4,1,3)=2mex({0, 4, 1, 3}) = 2
  • mex(5,4,0,1,2)=3mex({5, 4, 0, 1, 2}) = 3

medmed of the set SS is the median of the set, i.e. the element that, after sorting the elements in non-decreasing order, will be at position number ⌊∣S∣+12⌋\left \lfloor{ \frac{|S| + 1}{2} } \right \rfloor (array elements are numbered starting from 11 and here ⌊v⌋\left \lfloor{v} \right \rfloor denotes rounding vv down.). For example:

  • med(0,1,2,3)=1med({0, 1, 2, 3}) = 1
  • med(0,4,1,3)=1med({0, 4, 1, 3}) = 1
  • med(5,4,0,1,2)=2med({5, 4, 0, 1, 2}) = 2

A sequence of nn numbers is called a permutation if it contains all the numbers from 00 to n−1n - 1 exactly once.

给你一个长度为 nn 的排列 p1,p2,…,pnp_1, p_2, \ldots, p_n,其中包含数字 0,…,n−10, \ldots, n - 1 各恰好一次。请统计满足以下条件的子段(连续子数组)1≤l≤r≤n1 \leq l \leq r \leq n 的个数:

mex(pl,pl+1,…,pr)>med(pl,pl+1,…,pr).mex(p_l, p_{l+1}, \ldots, p_r) \gt med(p_l, p_{l+1}, \ldots, p_r).

集合 SS 的 mexmex 指不在 SS 中出现的最小非负整数。例如:

  • mex(0,1,2,3)=4mex({0, 1, 2, 3}) = 4
  • mex(0,4,1,3)=2mex({0, 4, 1, 3}) = 2
  • mex(5,4,0,1,2)=3mex({5, 4, 0, 1, 2}) = 3

集合 SS 的 medmed 指其中位数,即:将 SS 中所有元素按非降序排序后,位于第 ⌊∣S∣+12⌋\left \lfloor{ \frac{|S| + 1}{2} } \right \rfloor 个位置上的元素(数组下标从 11 开始计数;此处 ⌊v⌋\left \lfloor{v} \right \rfloor 表示对 vv 向下取整)。例如:

  • med(0,1,2,3)=1med({0, 1, 2, 3}) = 1
  • med(0,4,1,3)=1med({0, 4, 1, 3}) = 1
  • med(5,4,0,1,2)=2med({5, 4, 0, 1, 2}) = 2

若一个由 nn 个数组成的序列恰好包含 00 到 n−1n - 1 中的每个数各一次,则称其为一个排列。

输入格式

The first line of the input contains a single integer tt (1≤t≤104(1 \leq t \leq 10^4), the number of test cases.

The descriptions of the test cases follow.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5), the length of the permutation pp.

The second line of each test case contains exactly nn integers: p1,p2,…,pnp_1, p_2, \ldots, p_n (0≤pi≤n−10 \leq p_i \leq n - 1), elements of permutation pp.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4),表示测试用例的数量。

随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5),表示排列 pp 的长度。

每个测试用例的第二行包含恰好 nn 个整数:p1,p2,…,pnp_1, p_2, \ldots, p_n(0≤pi≤n−10 \leq p_i \leq n - 1),即排列 pp 的元素。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case print the answer in a single line: the number of subsegments 1≤l≤r≤n1 \leq l \leq r \leq n of this permutation such that mex(pl,pl+1,…,pr)>med(pl,pl+1,…,pr)mex(p_l, p_{l+1}, \ldots, p_r) \gt med(p_l, p_{l+1}, \ldots, p_r).

对于每个测试用例,在一行中输出答案:满足 mex(pl,pl+1,…,pr)>med(pl,pl+1,…,pr)\mathrm{mex}(p_l, p_{l+1}, \ldots, p_r) \gt \mathrm{med}(p_l, p_{l+1}, \ldots, p_r) 的子段 1≤l≤r≤n1 \leq l \leq r \leq n 的个数。

输入输出样例

  • 输入#1

    8
    1
    0
    2
    1 0
    3
    1 0 2
    4
    0 2 1 3
    5
    3 1 0 2 4
    6
    2 0 4 1 3 5
    8
    3 7 2 6 0 1 5 4
    4
    2 0 1 3

    输出#1

    1
    2
    4
    4
    8
    8
    15
    6

说明/提示

The first test case contains exactly one subsegment and mex(0)=1>med(0)=0mex({0}) = 1 \gt med({0}) = 0 on it.

In the third test case, on the following subsegments: [1,0][1, 0], [0][0], [1,0,2][1, 0, 2] and [0,2][0, 2], mexmex is greater than medmed.

In the fourth test case, on the following subsegments: [0,2][0, 2], [0][0], [0,2,1][0, 2, 1] and [0,2,1,3][0, 2, 1, 3], mexmex greater than medmed.

第一个测试用例恰好包含一个子段,且在其上满足 mex(0)=1>med(0)=0mex({0}) = 1 \gt med({0}) = 0。

在第三个测试用例中,在以下子段上:[1,0][1, 0]、[0][0]、[1,0,2][1, 0, 2] 和 [0,2][0, 2],有 mex>medmex > med。

在第四个测试用例中,在以下子段上:[0,2][0, 2]、[0][0]、[0,2,1][0, 2, 1] 和 [0,2,1,3][0, 2, 1, 3],有 mex>medmex > med。

输入解题思路,AI测评打分。不知道怎么写?

首页