[Codeforces 1199D]Welfare State(线段树)
[Codeforces 1199D]Welfare State(线段树)
题面
给出一个长度为n的序列,有q次操作,操作有2种
1.单点修改,把\(a_x\)修改成y
2.区间修改,把序列中值<v的数全部修改成v
问q次操作后的序列
分析
主要考虑如何实现操作2,可以通过有条件的下推标记来实现。线段树的叶子节点存储序列的值,上推的时候维护区间最小值。如果给某个节点下推标记的时候发现该节点对于的区间最小值>v,则不下推(最小值>v,即所有数都>v,不用会产生修改),否则把区间中的最小值和v取max.
单点修改的时候先下推标记,然后跟普通线段树一样修改即可。
代码
#include<iostream>
#include<cstdio>
#define maxn 200000
using namespace std;
int n;
int a[maxn+5];
int q;
struct segment_tree{
struct node{
int l;
int r;
int mark;
int v;
}tree[maxn*4+5];
void push_up(int pos){
tree[pos].v=min(tree[pos<<1].v,tree[pos<<1|1].v);
}
void build(int l,int r,int pos){
tree[pos].l=l;
tree[pos].r=r;
tree[pos].mark=-1;
if(l==r){
tree[pos].v=a[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,pos<<1);
build(mid+1,r,pos<<1|1);
push_up(pos);
}
void push_down(int pos){
if(tree[pos].mark!=-1){
//只有当前值比它大才下推
if(tree[pos].mark>tree[pos<<1].mark||tree[pos<<1].mark==-1) tree[pos<<1].mark=tree[pos].mark;
if(tree[pos].mark>tree[pos<<1].v) tree[pos<<1].v=tree[pos].mark;
if(tree[pos].mark>tree[pos<<1|1].mark||tree[pos<<1|1].mark==-1) tree[pos<<1|1].mark=tree[pos].mark;
if(tree[pos].mark>tree[pos<<1|1].v) tree[pos<<1|1].v=tree[pos].mark;
tree[pos].mark=-1;
}
}
void update_segment(int L,int R,int pt,int pos){
if(L<=tree[pos].l&&R>=tree[pos].r){
if(tree[pos].v<=pt){
tree[pos].v=pt;
tree[pos].mark=pt;
}
return;
}
push_down(pos);
int mid=(tree[pos].l+tree[pos].r)>>1;
if(L<=mid) update_segment(L,R,pt,pos<<1);
if(R>mid) update_segment(L,R,pt,pos<<1|1);
push_up(pos);
}
void update_point(int tpos,int val,int pos){
if(tree[pos].l==tree[pos].r){
tree[pos].v=val;
return;
}
push_down(pos);
int mid=(tree[pos].l+tree[pos].r)>>1;
if(tpos<=mid) update_point(tpos,val,pos<<1);
else update_point(tpos,val,pos<<1|1);
push_up(pos);
}
int query(int tpos,int pos){
if(tree[pos].l==tree[pos].r){
return tree[pos].v;
}
push_down(pos);
int mid=(tree[pos].l+tree[pos].r)>>1;
if(tpos<=mid) return query(tpos,pos<<1);
else return query(tpos,pos<<1|1);
}
}T;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
scanf("%d",&q);
int cmd,x,v;
T.build(1,n,1);
for(int i=1;i<=q;i++){
scanf("%d",&cmd);
if(cmd==1){
scanf("%d %d",&x,&v);
T.update_point(x,v,1);
}else{
scanf("%d",&v);
T.update_segment(1,n,v,1);
}
}
for(int i=1;i<=n;i++){
printf("%d ",T.query(i,1));
}
}
[Codeforces 1199D]Welfare State(线段树)的更多相关文章
- Codeforces - 1199D - Welfare State - 单调栈 / 线段树
https://codeforc.es/contest/1199/problem/D 其实后来想了一下貌似是个线段树的傻逼题. 单调栈是这样思考的,每次单点修改打上一个最终修改的时间戳.每次全体修改就 ...
- Buses and People CodeForces 160E 三维偏序+线段树
Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)
[Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- Codeforces 482B Interesting Array(线段树)
题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...
- codeforces 383C Propagating tree 线段树
http://codeforces.com/problemset/problem/383/C 题目就是说, 给一棵树,将一个节点的值+val, 那么它的子节点都会-val, 子节点的子节点+val. ...
- CodeForces 228D. Zigzag(线段树暴力)
D. Zigzag time limit per test 3 seconds memory limit per test 256 megabytes input standard input out ...
随机推荐
- Django【第5篇】:Django之ORM数据库操作
django之ORM数据库操作 一.ORM介绍 映射关系: 表名 -------------------->类名 字段-------------------->属性 表记录-------- ...
- apache-2.4.x 编译安装方法
apache-2.4.x 编译安装方法 作者:朱 茂海 /分类:Apache 字号:L M S apache-.2与新出的apache-.4安装不同的地方在于,.4版的已经不自带apr库,所以在安装a ...
- Python---进阶---函数式编程---按照权重排序
一. 权重是100 价格占的权重是40%,销量占的权重是17%,评级站的权重是13%,评论占的权重是30% ---------------------------------------------- ...
- [转帖]ssh 远程执行命令
ssh 远程执行命令 https://www.cnblogs.com/youngerger/p/9104144.html SSH 是 Linux 下进行远程连接的基本工具,但是如果仅仅用它来登录那可是 ...
- 《SaltStack技术入门与实践》——执行结果处理
执行结果处理 本章节参考<SaltStack技术入门与实践>,感谢该书作者: 刘继伟.沈灿.赵舜东 Return组件可以理解为SaltStack系统对执行Minion返回后的数据进行存储或 ...
- python-unittest生成报告的几种方式
import unittest suite = unittest.TestSuite() #构造套件 #按测试方法添加 suite.addTest(测试类名('方法名')) suite.addTest ...
- Python自动化运维技术与最佳实现
第一章 系统基础信息模块详解 系统基础信息采集模块最为监控模块的重要组成部分,能够帮助运维人员了解当前系统的健康程度,同时也是衡量业务的服务质量的依据,比如系统资源吃紧,会直接影响业务的质量以及用户的 ...
- CF543C Remembering Strings 状压dp
Code: #include <cstdio> #include <algorithm> #include <cstring> #define setIO(s) f ...
- Redis学习:Redis的安装与配置
Redis是新兴的一种内存数据库技术,在数据高速读写方面有着明显的优势.前几天,Redis3.0正式版本发布,为我们带来了Redis集群功能.这一功能很早就投入了开发,直到现在才真正走进我们的视野.可 ...
- 普通用户sudo权限
需求: 1>创建一个saipu普通用户,不允许使用 rm 和 passwd root 和 sudo su - root 命令,其他命令均允许且 sudo 时不用输入密码 2>创建一个lwd ...