codeforces 1023 D Array Restoration 【线段树】

D. Array Restoration

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Initially there was an array aa consisting of nn integers. Positions in it are numbered from 11 to nn.

Exactly qq queries were performed on the array. During the ii-th query some segment (li,ri)(li,ri) (1≤li≤ri≤n)(1≤li≤ri≤n) was selected and values of elements on positions from lili to riri inclusive got changed to ii. The order of the queries couldn't be changed and all qq queries were applied. It is also known that every position from 11 to nn got covered by at least one segment.

We could have offered you the problem about checking if some given array (consisting of nn integers with values from 11 to qq) can be obtained by the aforementioned queries. However, we decided that it will come too easy for you.

So the enhancement we introduced to it is the following. Some set of positions (possibly empty) in this array is selected and values of elements on these positions are set to 00.

Your task is to check if this array can be obtained by the aforementioned queries. Also if it can be obtained then restore this array.

If there are multiple possible arrays then print any of them.

Input

The first line contains two integers nn and qq (1≤n,q≤2⋅1051≤n,q≤2⋅105) — the number of elements of the array and the number of queries perfomed on it.

The second line contains nn integer numbers a1,a2,…,ana1,a2,…,an (0≤ai≤q0≤ai≤q) — the resulting array. If element at some position jj is equal to 00then the value of element at this position can be any integer from 11 to qq.

Output

Print "YES" if the array aa can be obtained by performing qq queries. Segments (li,ri)(li,ri) (1≤li≤ri≤n)(1≤li≤ri≤n) are chosen separately for each query. Every position from 11 to nn should be covered by at least one segment.

Otherwise print "NO".

If some array can be obtained then print nn integers on the second line — the ii-th number should be equal to the ii-th element of the resulting array and should have value from 11 to qq. This array should be obtainable by performing exactly qq queries.

If there are multiple possible arrays then print any of them.

Examples

input

Copy

4 3
1 0 2 3

output

Copy

YES
1 2 2 3

input

Copy

3 10
10 10 10

output

Copy

YES
10 10 10 

input

Copy

5 6
6 5 6 2 2

output

Copy

NO

input

Copy

3 5
0 0 0

output

Copy

YES
5 4 2

Note

In the first example you can also replace 00 with 11 but not with 33.

In the second example it doesn't really matter what segments to choose until query 1010 when the segment is (1,3)(1,3).

The third example showcases the fact that the order of queries can't be changed, you can't firstly set (1,3)(1,3) to 66 and after that change (2,2)(2,2) to 55. The segment of 55 should be applied before segment of 66.

There is a lot of correct resulting arrays for the fourth example.

#include<bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 7;
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
int a[MAX << 2];
int cnt[MAX];
void pushup(int rt){
    a[rt] = a[rt << 1] + a[rt << 1 | 1];
}
int lz[MAX << 2], x[MAX];
void pushdown(int rt, int l, int r){
    if(lz[rt]){
        int mid = (l + r) >> 1;
        lz[rt << 1] = lz[rt << 1 | 1] = 1;
        a[rt << 1] = mid - l + 1;
        a[rt << 1 | 1] = r - mid;
    }
}
void update(int rt, int l, int r, int x, int y){
    if(x <= l && r <= y){
        a[rt] = r - l + 1;
        lz[rt] = 1;
        return;
    }
    pushdown(rt, l, r);
    int mid = (l + r) >> 1;
    if(x <= mid) update(lson, x, y);
    if(mid < y) update(rson, x, y);
    pushup(rt);
}
int query(int rt, int l, int r, int x, int y){
    if(x <= l && r <= y) return a[rt];
    pushdown(rt, l, r);
    int mid = (l + r) >> 1;
    int ans = 0;
    if(x <= mid) ans += query(lson, x, y);
    if(mid < y) ans += query(rson, x, y);
    return ans;
}
vector <int> v[MAX];
int main(){
    int n, m;
    memset(a, 0, sizeof a);
    scanf("%d%d", &n, &m);
    int pos = -1, flag = 0;
    for(int i = 0; i < n; i++){
        scanf("%d", &x[i]);
        if(x[i] == 0 && pos == -1) pos = i;
        if(x[i] == m) flag = m;
    }
    if(!flag && pos != -1) x[pos] = m;
    for(int i = 1; i < n; i++) if(x[i] == 0) x[i] = x[i - 1];
    for(int i = n - 2; i >= 0; i--) if(x[i] == 0) x[i] = x[i + 1];
    for(int i = 0; i < n; i++){
        cnt[x[i]]++;
        if(v[x[i]].size() == 0) v[x[i]].push_back(i + 1);
    }
    for(int i = n - 1; i >= 0; i--)
        if(v[x[i]].size() == 1) v[x[i]].push_back(i + 1);
    if(v[m].size() == 0){
        puts("NO");
        return 0;
    }
    flag = 0;

    for(int i = m; i >= 1; i--){
        if(v[i].size() == 0) continue;
        if(query(1, 1, n, v[i][0], v[i][1]) + cnt[i] != v[i][1] - v[i][0] + 1){
            flag = 1;
            break;
        }
        else update(1, 1, n, v[i][0], v[i][1]);
    }
    if(flag) puts("NO");
    else{
        puts("YES");
        for(int i = 0; i < n; i++) printf("%d ", x[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Head_Hard/article/details/81813311
今日推荐