[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 ...
随机推荐
- WPF Geometry="M0,0 L1,0 1,0.1, 0,0.1Z" 画方格背景图
此项目源码下载地址:https://github.com/lizhiqiang0204/Tile 方格效果: 前端代码如下: <Window x:Class="WpfApp1.Main ...
- 【串线篇】spring boot使用外置的Servlet容器
嵌入式Servlet容器:应用打成可执行的jar 优点:简单.便携: 缺点:默认不支持JSP.优化定制比较复杂 (使用定制器[ServerProperties/自定义EmbeddedServletCo ...
- 接触oracle的第二天
1.2 Sqlplus 这是一个轻量级的功能强大的客户端, 是 dba 必须掌握的工具. 我们可以配置 sqlplus 的一些行为,两个命令: show. 用来显示配置参数 set. 用来设置配置参数 ...
- twint 安装及使用
分享这个post是自己方便查,还有中文网界对这个东西介绍太少. 更多的就看github项目twint吧. Installation: git+pip3: git clone https://githu ...
- jAVA基础 提高文件复制性能之多线程复制文件
利用IO流中的随机访问文件 RandomAccessFile 和文件通道 FileChanne 复制文件可大大提高文件的读写效率,在此基础上利用多线程复制文件使其性能更优.因线程的个数可根据文件的大小 ...
- Java框架之MybatisSQL注入漏洞
一.SQL注入漏洞基本原理 在常见的web漏洞中,SQL注入漏洞较为常见,危害也较大.攻击者一旦利用系统中存在的SQL注入漏洞来发起攻击,在条件允许的情况下,不仅可以获取整站数据,还可通过进一步的渗透 ...
- Python_016(面向对象之属性和类方法)
一.特性(property,setter,deleter) 1.属性:将一个方法伪装成一个属性,在代码级别上没有本质的提升,但是看起来更合理; class Person: def __init__(s ...
- Layer Cake cf
Layer Cake time limit per test 6 seconds memory limit per test 512 megabytes input standard input ou ...
- vue +ts 的一次踩坑日记
在vue的方法里面写事件的时候比如写一个路由跳转,方法大概如下: goBack1() { console.log(this); this.$router. ...
- ctrl+r 调用bash曾经的历史命令
在bash界面 按ctrl+r 可以调出, bash中曾经的历史命令, 光标会停留在 第一次被匹配的字符上, (即使后面你再输入被匹配的字符, 光标也不移动) 然后, 根据你的需要 来进行任何一次的操 ...