[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(线段树)的更多相关文章

  1. Codeforces - 1199D - Welfare State - 单调栈 / 线段树

    https://codeforc.es/contest/1199/problem/D 其实后来想了一下貌似是个线段树的傻逼题. 单调栈是这样思考的,每次单点修改打上一个最终修改的时间戳.每次全体修改就 ...

  2. Buses and People CodeForces 160E 三维偏序+线段树

    Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...

  3. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  4. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  5. [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)

    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...

  6. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  7. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

  8. codeforces 383C Propagating tree 线段树

    http://codeforces.com/problemset/problem/383/C 题目就是说,  给一棵树,将一个节点的值+val, 那么它的子节点都会-val, 子节点的子节点+val. ...

  9. CodeForces 228D. Zigzag(线段树暴力)

    D. Zigzag time limit per test 3 seconds memory limit per test 256 megabytes input standard input out ...

随机推荐

  1. 使用Vscode添加中文汉化插件

    一.首先打开Vscode,找到该软件的扩展,如下: 二.点击扩展按钮之后,会出现如下的界面,有一个扩展搜索输入框,输入chinese之后,会随之产生一些匹配的插件 三.重启一下Vscode,然后就看到 ...

  2. 线段树维护区间前k小

    线段树维护区间前k小 $ solution: $ 觉得超级钢琴太麻烦?在这里线段树提供一条龙服务 . 咳咳,开始讲正题!这道题我们有一个和超级钢琴复杂度一样 $ ~O(~\sum x\times lo ...

  3. win10系统迁移到新的硬盘

    笔记本换个硬盘,但是程序员在开发中需要各种环境配置,不想重装系统,重新系统容易,但是装各种软件和配各种环境就比较麻烦了,所以笔记本加固态硬盘,可以不用重装,直接"系统迁移"到新的硬 ...

  4. DNS域名解析系统

    1.DNS的组成 DNS系统是为解析域名为IP地址而存在的,它是由域名空间.资源记录.名称服务器和解析器组成. 域名空间是包含一个树状结构,用于存储资源记录的空间. 资源记录是与域名相关的数据,如IP ...

  5. Headless Windows

    Google Chrome 79.0.3945.79 (正式版本) (64 位) (cohort: 79_Win_79) .\chrome --headless --user-data-dir=tmp ...

  6. Apache主要配置文件http.conf

    1.基本概念 Define SRVROOT "/Apache24" ServerRoot "${SRVROOT}" #Apache安装的根路径 #Listen ...

  7. R 画散点图

    ggplot(data=df, aes(x=n, y=rt, group=kernel, shape=kernel, colour=kernel)) + geom_point(fill="w ...

  8. Redis-cli相关命令

    > flushdb > SELECT OK []> GET db_number (nil) []> []> flushdb OK []> SELECT OK > ...

  9. Docker在CentOS7中的安装与启动

    Docker是当下很流行的应用容器,在系统快速部署方面有着独特的优势.由于最近在做的一个项目需要用到Docker,所以找了些资料学了学.Docker不仅仅在应用快速部署方面有着独特的优势,而且在资源共 ...

  10. StringUtils.join()

    org.apache.commons.lang.StringUtils; StringUtils.join(null)            = null StringUtils.join([])   ...