CodeTON Round 6 (Div. 1 + Div. 2, Rated, Prizes!)C - Colorful Table

C. Colorful Table

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two integers n� and k�. You are also given an array of integers a1,a2,…,an�1,�2,…,�� of size n�. It is known that for all 1≤i≤n1≤�≤�, 1≤ai≤k1≤��≤�.

Define a two-dimensional array b� of size n×n�×� as follows: bi,j=min(ai,aj)��,�=min(��,��). Represent array b� as a square, where the upper left cell is b1,1�1,1, rows are numbered from top to bottom from 11 to n�, and columns are numbered from left to right from 11 to n�. Let the color of a cell be the number written in it (for a cell with coordinates (i,j)(�,�), this is bi,j��,�).

For each color from 11 to k�, find the smallest rectangle in the array 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� (1≤t≤1041≤�≤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� and k� (1≤n,k≤1051≤�,�≤105) — the size of array a� and the number of colors.

The second line of each test case contains n� integers a1,a2,…,an�1,�2,…,�� (1≤ai≤k1≤��≤�) — the array a�.

It is guaranteed that the sum of the values of n� and k� over all test cases does not exceed 105105.

Output

For each test case, output k� numbers: the sums of width and height of the smallest rectangle containing all cells of a color, for each color from 11 to k�.

Example

input

Copy

 

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

Copy

4 
4 2 
0 6 6 2 0 
8 6 
10 6 2 

Note

In the first test case, the entire array b� consists of color 11, so the smallest rectangle for color 11 has a size of 2×22×2, and the sum of its sides is 44.

In the second test case, the array b� looks like this:

11
12

One of the corner cells has color 22, and the other three cells have color 11. Therefore, the smallest rectangle for color 11 has a size of 2×22×2, and for color 22 it is 1×11×1.

In the last test case, the array b� looks like this:

11111
12221
12321
12221
11111

#include <bits/stdc++.h>

using namespace std;
using ll=long long;
ll LEFT_l[1000010], RIGHT_l[1000010],LEFT_r[1000010], RIGHT_r[1000010];
void findRectangles(ll n, ll k, vector<ll> &a)
{
    for(int i=0; i<max(k,n)+100; i++)
    {
        LEFT_l[i]=1000000;
//        MIN[i]=1000000;
        RIGHT_r[i]=-1;
//        MAX[i]=-1;
    }
    ll MAX=-1,MIN=1000000;
    for (ll i = 0; i < n; i++)
    {
        ll x=a[i];
        LEFT_l[x] = min(LEFT_l[x], i);
        RIGHT_r[x] = max(RIGHT_r[x], i);
    }
    vector<ll>ans;
    ans.clear();
    for(ll i=k; i>0; i--)
    {
        if(LEFT_l[i]==1000000&&RIGHT_r[i]==-1)
        {
            ans.push_back(0);
        }
        else
        {

            MAX=max(MAX,RIGHT_r[i]);
            MIN=min(MIN,LEFT_l[i]);
            ans.push_back(2*(MAX-MIN+1));
        }
    }

    for(int i=0; i<max(n,k)+100; i++)
    {
        LEFT_l[i]=1000000;
//        MIN[i]=1000000;
        RIGHT_r[i]=-1;
//        MAX[i]=-1;
    }

    for (ll i = k-1; i >=0; i--)
    {
        cout<<ans[i]<<' ';
    }
    cout << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    ll t;
    cin >> t;
    while (t--)
    {
        ll n, k;
        cin >> n >> k;

        vector<ll> a(n);
        for (ll i = 0; i < n; i++)
        {
            cin >> a[i];
        }

        findRectangles(n, k, a);
    }

    return 0;
}

清空数组没有清空完全

 for(int i=0; i<max(k,n)+100; i++)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值