Colorful Table

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 1in, 1 ≤ a i ≤ k 1 \leq a_i \leq k 1aik.

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 1t104) — 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 1n,k105) — 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 1aik) — 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:

11
12

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:

11111
12221
12321
12221
11111

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值