[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. 【串线篇】spring boot自定义starter

    starter: 一.这个场景需要使用到的依赖是什么? 二.如何编写自动配置 启动器只用来做依赖导入:(启动器模块是一个空 JAR 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库) ...

  2. Azure IoT 技术研究系列4

    上两篇博文中,我们介绍了将设备注册到Azure IoT Hub,设备到云.云到设备之间的通信: Azure IoT 技术研究系列2-设备注册到Azure IoT Hub Azure IoT 技术研究系 ...

  3. window.open()的实际应用

    Window open() 方法 定义和用法 open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口. 语法 window.open(URL,name,specs,replace) 参数 ...

  4. ubuntu16.04 安装mysql

    安装mysql 1.sudo apt-get install mysql-server 2.sudo apt install mysql-client 3.sudo apt install libmy ...

  5. layui数据表格,当数据过长出现三个...的时候,点击会弹出一个框全部显示,如何去掉这个框

    最笨的方法就是通过css把那个框隐藏掉 .layui-table-tips-main{display:none} .layui-table-tips-c{display:none}

  6. 对postman的研究

    1.Postman可用作macOS,Windows和Linux操作系统的本机应用程序. 2.最常用的方法是GET,POST,PUT和DELETE.方法的名称是不言自明的.例如,GET使您可以从服务器检 ...

  7. ASP.NET超大文件上传与下载

    总结一下大文件分片上传和断点续传的问题.因为文件过大(比如1G以上),必须要考虑上传过程网络中断的情况.http的网络请求中本身就已经具备了分片上传功能,当传输的文件比较大时,http协议自动会将文件 ...

  8. sh_02_第2个Python程序

    sh_02_第2个Python程序 print("hello")

  9. layer 弹出层不能居中

    $("#btnAdd").button("loading"); parent.layer.open({ title: '添加菜单', type: 2, maxm ...

  10. [BZOJ3781]:小B的询问(离线莫队算法)

    题目传送门 题目描述 小B有一个序列,包含$N$个$1~K$之间的整数.他一共有$M$个询问,每个询问给定一个区间$[L...R]$,求$\sum \limits_{i=1}^{K}c(i)^2$的值 ...