CF1744F.MEX vs MED
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a permutation p1,p2,…,pn of length n of numbers 0,…,n−1 . Count the number of subsegments 1≤l≤r≤n of this permutation such that mex(pl,pl+1,…,pr)>med(pl,pl+1,…,pr) .
mex of S is the smallest non-negative integer that does not occur in S . For example:
- mex(0,1,2,3)=4
- mex(0,4,1,3)=2
- mex(5,4,0,1,2)=3
med of the set S is the median of the set, i.e. the element that, after sorting the elements in non-decreasing order, will be at position number ⌊2∣S∣+1⌋ (array elements are numbered starting from 1 and here ⌊v⌋ denotes rounding v down.). For example:
- med(0,1,2,3)=1
- med(0,4,1,3)=1
- med(5,4,0,1,2)=2
A sequence of n numbers is called a permutation if it contains all the numbers from 0 to n−1 exactly once.
输入格式
The first line of the input contains a single integer t (1≤t≤104 ), the number of test cases.
The descriptions of the test cases follow.
The first line of each test case contains a single integer n ( 1≤n≤2⋅105 ), the length of the permutation p .
The second line of each test case contains exactly n integers: p1,p2,…,pn ( 0≤pi≤n−1 ), elements of permutation p .
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .
输出格式
For each test case print the answer in a single line: the number of subsegments 1≤l≤r≤n of this permutation such that mex(pl,pl+1,…,pr)>med(pl,pl+1,…,pr) .
输入输出样例
输入#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)=0 on it.
In the third test case, on the following subsegments: [1,0] , [0] , [1,0,2] and [0,2] , mex is greater than med .
In the fourth test case, on the following subsegments: [0,2] , [0] , [0,2,1] and [0,2,1,3] , mex greater than med .