Limit of MEX For an array 𝐵 B consisting of non-negative integers, let 𝑔 ( 𝐵 , 𝑥 )…
- User
Limit of MEX For an array 𝐵 B consisting of non-negative integers, let 𝑔 ( 𝐵 , 𝑥 ) g(B,x) denote the maximum possible 𝑀 𝐸 𝑋 † MEX † of the array 𝐵 B we can obtain, by appending 𝑥 x integers to 𝐵 B. For example, 𝑔 ( [ 1 , 2 , 4 ] , 1 )
3 g([1,2,4],1)=3 as we can obtain the array [ 1 , 2 , 4 , 0 ] [1,2,4,0] by appending 0 0 which results in 𝑀 𝐸 𝑋
3 MEX=3.
It can be shown that there exists a non-negative integer 𝐶 C such that 𝑔 ( 𝐵 , 𝑥 + 1 ) − 𝑔 ( 𝐵 , 𝑥 )
1 g(B,x+1)−g(B,x)=1 for all 𝑥 ≥ 𝐶 x≥C. We define 𝑓 ( 𝐵 ) f(B) as the minimum such 𝐶 C.
Given an array 𝐴 A of size 𝑁 N, find ∑ 𝐿
1 𝑁 ∑ 𝑅
𝐿 𝑁 𝑓 ( 𝐴 [ 𝐿 , 𝑅 ] ) L=1 ∑ N
R=L ∑ N f(A[L,R]), where 𝐴 [ 𝐿 , 𝑅 ] A[L,R] represents the subarray [ 𝐴 𝐿 , 𝐴 𝐿 + 1 , … , 𝐴 𝑅 ] [A L ,A L+1 ,…,A R ].
† 𝑀 𝐸 𝑋 † MEX is the minimum excluded element, i.e., the minimum non-negative integer that does not appear in the array. For example, the 𝑀 𝐸 𝑋 MEX of [ 2 , 0 , 1 , 4 ] [2,0,1,4] is 3 3 and the 𝑀 𝐸 𝑋 MEX of [ 1 ] [1] is 0 0.
Input Format The first line of input will contain a single integer 𝑇 T, denoting the number of test cases. Each test case consists of multiple lines of input. The first line of each test case contains one integer 𝑁 N - the size of the array 𝐴 A. The second line of each test case contains 𝑁 N integers - 𝐴 1 , 𝐴 2 , … , 𝐴 𝑁 A 1 ,A 2 ,…,A N . Output Format For each test case, output on a new line, the required sum.
Constraints 1 ≤ 𝑇 ≤ 2 ⋅ 1 0 4 1≤T≤2⋅10 4
1 ≤ 𝑁 ≤ 2 ⋅ 1 0 5 1≤N≤2⋅10 5
0 ≤ 𝐴 𝑖 < 𝑁 0≤A i <N The sum of 𝑁 N over all test cases does not exceed 2 ⋅ 1 0 5 2⋅10 5 . Sample 1: Input Output 4 2 1 0 3 1 1 2 6 1 4 0 1 3 0 1 0 1 7 32 0 Explanation: Test case 1 1 : It can be shown that 𝑓 ( [ 0 ] )
0 f([0])=0, 𝑓 ( [ 1 ] )
1 f([1])=1, 𝑓 ( [ 0 , 1 ] )
0 f([0,1])=0. Thus, the sum is 1 1.
Test case 2 2 :
𝑓 ( [ 1 ] )
1 f([1])=1. The count of [ 1 ] [1] in the given array is 2 2. 𝑓 ( [ 2 ] )
2 f([2])=2 𝑓 ( [ 1 , 1 ] )
1 f([1,1])=1 𝑓 ( [ 1 , 2 ] )
1 f([1,2])=1 𝑓 ( [ 1 , 1 , 2 ] )
1 f([1,1,2])=1 because 𝑔 ( [ 1 , 1 , 2 ] , 0 )
0 g([1,1,2],0)=0, 𝑔 ( [ 1 , 1 , 2 ] , 1 )
3 g([1,1,2],1)=3, 𝑔 ( [ 1 , 1 , 2 ] , 2 )
4 g([1,1,2],2)=4, 𝑔 ( [ 1 , 1 , 2 ] , 3 )
5 g([1,1,2],3)=5, and it can be verified that for all 𝑥 ≥ 1 x≥1, 𝑔 ( [ 1 , 1 , 2 ] , 𝑥 + 1 ) − 𝑔 ( [ 1 , 1 , 2 ] , 𝑥 )
1 g([1,1,2],x+1)−g([1,1,2],x)=1 holds. Thus, the required sum is 1 ⋅ 2 + 2 + 1 + 1 + 1
7 1⋅2+2+1+1+1=7.
write C++ program
- Assistant
- User
Your Output 3 0 26 1
required output:
1 7 32 0
- Assistant