TA. Colorful Table
You are given two integers n n n and k k k. You are also given an array of integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an of size n n n. It is known that for all 1 ≤ i ≤ n 1 \leq i \leq n 1≤i≤n, 1 ≤ a i ≤ k 1 \leq a_i \leq k 1≤ai≤k.
Define a two-dimensional array b b b of size n × n n \times n n×n as follows: b i , j = min ( a i , a j ) b_{i, j} = \min(a_i, a_j) bi,j=min(ai,aj). Represent array b b b as a square, where the upper left cell is b 1 , 1 b_{1, 1} b1,1, rows are numbered from top to bottom from 1 1 1 to n n n, and columns are numbered from left to right from 1 1 1 to n n n. Let the color of a cell be the number written in it (for a cell with coordinates ( i , j ) (i, j) (i,j), this is b i , j b_{i, j} bi,j).
For each color from 1 1 1 to k k k, find the smallest rectangle in the array b b b containing all cells of this color. Output the sum of width and height of this rectangle.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1≤t≤104) — the number of test cases. Then follows the description of the test cases.
The first line of each test case contains two integers n n n and k k k ( 1 ≤ n , k ≤ 1 0 5 1 \leq n, k \leq 10^5 1≤n,k≤105) — the size of array a a a and the number of colors.
The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 1 ≤ a i ≤ k 1 \leq a_i \leq k 1≤ai≤k) — the array a a a.
It is guaranteed that the sum of the values of n n n and k k k over all test cases does not exceed 1 0 5 10^5 105.
Output
For each test case, output k k k numbers: the sums of width and height of the smallest rectangle containing all cells of a color, for each color from 1 1 1 to k k k.
Example
Input
5
2 1
1 1
2 2
1 2
3 5
3 2 4
4 2
1 2 1 2
5 3
1 2 3 2 1
Output
4
4 2
0 6 6 2 0
8 6
10 6 2
Note
In the first test case, the entire array b b b consists of color 1 1 1, so the smallest rectangle for color 1 1 1 has a size of 2 × 2 2 \times 2 2×2, and the sum of its sides is 4 4 4.
In the second test case, the array b b b looks like this:
1 | 1 |
---|---|
1 | 2 |
One of the corner cells has color 2 2 2, and the other three cells have color 1 1 1. Therefore, the smallest rectangle for color 1 1 1 has a size of 2 × 2 2 \times 2 2×2, and for color 2 2 2 it is 1 × 1 1 \times 1 1×1.
In the last test case, the array b b b looks like this:
1 | 1 | 1 | 1 | 1 |
---|---|---|---|---|
1 | 2 | 2 | 2 | 1 |
1 | 2 | 3 | 2 | 1 |
1 | 2 | 2 | 2 | 1 |
1 | 1 | 1 | 1 | 1 |
code
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5+10,INF=0x3f3f3f3f,mod=1e9+7;
typedef pair<int,int> PII;
int T=1;
void solve(){
int n,k;
int a;
cin >> n >> k;
vector<int> mx(k+1, -1), mn(k+1, n);
for (int i = 0; i < n; i++) {
cin >> a;
mn[a] = min(mn[a], i);
mx[a] = max(mx[a], i);
}
int mn2 = mn[k], mx2 = mx[k];
for (int i = k - 1; i > 0; i--) {
mn2 = min(mn2, mn[i]);
mx2 = max(mx2, mx[i]);
if (mn[i] < n) mn[i] = mn2;
if (mx[i] > -1) mx[i] = mx2;
}
for (int i = 1; i <= k; i++) {
int t=max((int)0,(int)mx[i]-mn[i]+1)*2;
cout<<t<<" ";
}
cout << endl;
}
signed main(){
cin>>T;
while(T--){
solve();
}
return 0;
}