Codeforces Round #504(Div.2)Problem 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

4 3

1 0 2 3

output

YES

1 2 2 3

input

3 10

10 10 10

output

YES

10 10 10

input

5 6

6 5 6 2 2

output

NO

input

3 5

0 0 0

output

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.

 

【思路】

题意:n个空位,填q次数,每一次选取一段填数,第i次填数填的是i,后填者可以覆盖前面填的数,给q次更改执行的后的结果,并且为了为难你,最后结果的某些位置变成0,这个位置可以任意填1到q的数,问是否可以达成该结果,若可以达成该结果输出任意一种。

显然如果后填者的那一段如果包括了前填者,这必定是不满足题意的,我们要做的就是模拟这个填数的过程,然后填之前区间查询一下这整段区间是否都是空白,若否则直接判断这是不可行的,不然便依照已知的位置单点更新。注意到最后一定是要有q存在的,我们还要特别加上这个q,倘若一开始便没有0且没有q,那也是不行的。详看代码。

 

【代码】

//******************************************************************************
// File Name: D.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com
// Created Time: 2018年08月17日 星期五 23时49分04秒
//******************************************************************************

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;

const int MAXN = 2e5 + 5, INF = 0x3f3f3f3f;

struct segment {
	int l, r, min, max, mark;
};

int n, q;
vector<int> pos[MAXN];
int l[MAXN], r[MAXN], a[MAXN], ans[MAXN];
segment tree[MAXN << 2];

void update(int rt)
{
	tree[rt].min = min(tree[rt << 1].min, tree[rt << 1 | 1].min);
	tree[rt].max = max(tree[rt << 1].max, tree[rt << 1 | 1].max);
}

void push_down(int rt)
{
	if (tree[rt].mark) {
		tree[rt << 1].mark = tree[rt].mark;
		tree[rt << 1 | 1].mark = tree[rt].mark;
		tree[rt << 1].min = tree[rt << 1].max = tree[rt].mark;
		tree[rt << 1 | 1].min = tree[rt << 1 | 1].max = tree[rt].mark;
		tree[rt].mark = 0;
	}
}

void build(int l, int r, int rt)
{
	tree[rt].l = l;
	tree[rt].r = r;
	tree[rt].mark = 0;
	if (l == r) {
		tree[rt].min = tree[rt].max = 0;
		return;
	}
	int mid = (l + r) >> 1;
	build(l, mid, rt << 1);
	build(mid + 1, r, rt << 1 | 1);
	update(rt);
}

void modify(int l, int r, int num, int rt)
{
	if (l <= tree[rt].l && tree[rt].r <= r) {
		tree[rt].min = tree[rt].max = num;
		tree[rt].mark = num;
		return;
	}
	push_down(rt);
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	if (l <= mid) modify(l, r, num, rt << 1);
	if (r >= mid + 1) modify(l, r, num, rt << 1 | 1);
	update(rt);
}

int query_min(int l, int r, int rt)
{
	if (l <= tree[rt].l && tree[rt].r <= r) return tree[rt].min;
	push_down(rt);
	int ans = INF;
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	if (l <= mid) ans = min(ans, query_min(l, r, rt << 1));
	if (r >= mid + 1) ans = min(ans, query_min(l, r, rt << 1 | 1));
	return ans;
}

int query_max(int l, int r, int rt)
{
	if (l <= tree[rt].l && tree[rt].r <= r) return tree[rt].max;
	push_down(rt);
	int ans = 0;
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	if (l <= mid) ans = max(ans, query_max(l, r, rt << 1));
	if (r >= mid + 1) ans = max(ans, query_max(l, r, rt << 1 | 1));
	return ans;
}

int main()
{
	scanf("%d %d", &n, &q);
	int last = 0;
	bool havelast = false;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		if (a[i] == q) havelast = true;
		if (a[i] == 0) last = i;
		pos[a[i]].push_back(i);
	}
	memset(l, 0, sizeof(l));
	for (int i = 1; i <= n; i++) {
		if (l[a[i]] == 0) l[a[i]] = i;
		r[a[i]] = i;
	}
	build(1, n, 1);
	bool flag = true;
	for (int i = 1; i <= q && flag; i++) {
		if (l[i] == 0) continue;
		int mn = query_min(l[i], r[i], 1), mx = query_max(l[i], r[i], 1);
		if (mn != INF && mx != 0 && mn != mx) flag = false;
		for (int j = 0; j < (int)pos[i].size(); j++) modify(pos[i][j], pos[i][j], i, 1);
	}
	if (!flag || (last == 0 && !havelast))
		printf("NO\n");
	else {
		printf("YES\n");
		if (!havelast) a[last] = q;
		for (int i = 1; i <= n; i++) {
			if (a[i] == 0) {
				if (i == 1) {
					for (int j = 2; j <= n; j++)
						if (a[j] != 0)  {
							ans[i] = a[j];
							break;
						}
				}
				else
					ans[i] = ans[i - 1];
			}
			else
				ans[i] = a[i];
		}
		for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值