CF 1023D Array Restoration - 线段树
题解
非常容易想到的线段树, 还可以用并查集来。 还有一位大神用了$O(n)$ 就过了Orz
要判断是否能染色出输入给出的序列,必须满足两个条件:
1、 序列中必须存在一个$q$
2、 两个相同的数$x$的中间不存在比 $ x$ 小的数
首先判断输入的数列中是否存在$q$, 若不存在$q$ 且没有 $a_i = 0$, 表示序列中一定没有$q$, 直接输出NO
若存在某个$a_i = 0$ , 将任意一个染成$q$即可
然后我们再查询两个相同的数$x$ 中是否存在比$x$ 小的数,用线段树来维护区间最小即可实现
接着把两个$x$中间的序列染色, 用MinOK来记录,表示区间内$a_i = 0$,可以染色的最小值。 (线段树区间修改
最后把$a_i = 0$ 进行染色(利用线段树点查询
CF现场就想到的算法,然而没有特判存在$q$,pushdown还少打了唔, CF百分百掉分,我要变成pupil了,太惨啦QAQ
代码
#include<cstring>
#include<algorithm>
#include<cstdio>
#define lson nd<<1
#define rson nd<<1|1
#define rd read()
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define per(i,a,b) for(int i = (a); i >= (b); --i)
using namespace std; const int N = 3e5, inf = ~0U >> ; int MIN[N << ], a[N], L[N], R[N], lazy[N << ], q, n, pos;
int Mok[N << ]; int read() {
int X = , p = ; char c = getchar();
for(; c > '' || c < ''; c = getchar()) if( c == '-') p = -;
for(; c >= '' && c <= ''; c = getchar()) X = X * + c - '';
return X * p;
} void update(int nd) {
MIN[nd] = min(MIN[lson], MIN[rson]);
} void pushdown(int nd) {
if(lazy[nd]) {
Mok[lson] = lazy[nd];
Mok[rson] = lazy[nd];
lazy[lson] = lazy[rson] = lazy[nd];
lazy[nd] = ;
}
} void build(int l, int r, int nd) {
if(l == r) {
MIN[nd] = a[l] == ? inf : a[l];
return;
}
int mid = (l + r) >> ;
build(l, mid, lson);
build(mid + , r, rson);
update(nd);
} int query(int Li, int Ri, int l, int r, int nd) {//查询区间最小
if(Li <= l && r <= Ri) return MIN[nd];
int mid = (l + r) >> , re = inf;
if(Li <= mid) re = min(re, query(Li, Ri, l, mid, lson));
if(mid < Ri) re = min(re, query(Li, Ri, mid + , r, rson));
return re;
} int query_pt(int p, int l, int r, int nd) {//查询可以染上的值
if(l == r) return Mok[nd];
int mid = (l + r) >> ;
pushdown(nd);
if(p <= mid) return query_pt(p, l, mid, lson);
else return query_pt(p, mid + , r, rson);
} void change(int Li, int Ri, int c, int l, int r, int nd) {
if(Li <= l && r <= Ri) {
lazy[nd] = c;
Mok[nd] = c;
return;
}
int mid = (l + r) >> ;
pushdown(nd);
if(Li <= mid) change(Li, Ri, c, l, mid, lson);
if(mid < Ri) change(Li, Ri, c, mid + , r, rson);
update(nd);
} int main()
{
n = rd; q = rd;
for(int i = ; i <= n; ++i) {
a[i] = rd;
if(!a[i]) pos = i;
if(!L[a[i]]) L[a[i]] = i;
R[a[i]] = i;
}
if(!L[q] && !pos) return printf("NO\n"), ;//无a[i]=0也无q
build(, n, );
for(int i = ; i <= q; ++i) {//必须按颜色从小到大覆盖
if(!L[i]) continue;
int minv = query(L[i], R[i], , n, );
if(minv < i) return printf("NO\n"), ;
change(L[i], R[i], i, , n, );
}
for(int i = ; i <= n; ++i) if(!a[i]) {
a[i] = query_pt(i, , n, );
if(i == pos && !L[q]) a[i] = q; // 必须存在q
else if(!a[i]) a[i] = ;
}
printf("YES\n");
for(int i = ; i <= n; ++i) printf("%d ", a[i]);
putchar('\n');
}
CF 1023D Array Restoration - 线段树的更多相关文章
- Codeforces #504(div1+div2) 1023D Array Restoration(线段树)
题目大意:给你一个数组,数组是经过q次区间覆盖后的结果,第i次覆盖是把区间内的值赋值为i,其中有若干个地方数值未知(就是0),让你判断这个数组是否可以经过覆盖后得到的,如果可以,输出任意一种可行数组. ...
- CF1114F Please, another Queries on Array?(线段树,数论,欧拉函数,状态压缩)
这题我在考场上也是想出了正解的……但是没调出来. 题目链接:CF原网 题目大意:给一个长度为 $n$ 的序列 $a$,$q$ 个操作:区间乘 $x$,求区间乘积的欧拉函数模 $10^9+7$ 的值. ...
- Codedforces 1076G Array Game 线段树
题意 现在cf上看题意真nm麻烦,有道网页翻译和谷歌翻译鬼畜的一匹 两个人在玩一个游戏. 有一个有\(n\)个数序列\(B\),一开始有一个棋子在\(B\)的第一个位置. 双方轮流操作,第一次操作前将 ...
- codeforces 482B. Interesting Array【线段树区间更新】
题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val. 就是区间l---r上的与的值为val,最后问你原来的数 ...
- CF 787D Legacy(线段树思想构图+最短路)
D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~
...
- Codeforces E. Interesting Array(线段树)
题目描述: D. Interesting Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard ...
- B. Interesting Array(线段树)
B. Interesting Array time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF 19D Points 【线段树+平衡树】
在平面上进行三种操作: 1.add x y:在平面上添加一个点(x,y) 2.remove x y:将平面上的点(x,y)删除 3.find x y:在平面上寻找一个点,使这个点的横坐标大于x,纵坐标 ...
随机推荐
- python对象转字典
1.基础实现 class TestDict: name = "wyb" age = " def __init__(self): self.gender = 'male' ...
- django-后台sms管理系统的css框架
地址:https://adminlte.io/ 下载代码.使用index.html的页面及相关文件 通过下在线检查adminlte.io的后台的各种模块元素,仿写.
- 网络命令ping/netstat/ipconfig/arp/tracert/nbstat
1.1 Ping命令的使用 ping检测网络故障步骤: ping 127.0.0.1 ping环绕指针检测是否在计算上anzhaung了TCP/IP协议及配置是否正确 ping本机IP这个命令被被送到 ...
- 文件的编辑命令-echo/cat
touch test.yaml echo "line1 line2" >> test.yaml cat test.yaml line1 line2 # 创建test.y ...
- Caused by: java.lang.IllegalStateException: Expected raw type form of org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$Match
spring 4.0.2,mybatis 3.2.6,aspectjweaver 1.8.10 使用的时候,报错: Caused by: java.lang.IllegalStateException ...
- python 安装mysqldb组件
只能源码安装 源码下载: http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/ http://sourcefor ...
- ubuntu查询可用安装包
当使用apt-get install packages时,如果不知道安装包的具体名称,可以使用关键字进行搜索,使用:apt-cache search keywords
- 控制html元素的隐藏问题
控制元素隐藏的方式,有display:none.visibility:hidden以及不透明度设置. 一.display:none 被隐藏的元素,在页面中不占位,空出的位置会被相邻的元素占用. < ...
- TPCC-MySQL的安装与使用
Tpcc-mysql 是 percona 基于 tpcc 衍生出来的产品,专用于 mysql 基准测试,其源码放在 bazaar 上( Bazaar 是一个分布式的版本控制系统,采用 GPL 许可协议 ...
- ArcGIS模型构建器案例学习笔记-字段处理模型集
ArcGIS模型构建器案例学习笔记-字段处理模型集 联系方式:谢老师,135-4855-4328,xiexiaokui@qq.com 由四个子模型组成 子模型1:判断字段是否存在 方法:python工 ...