LUOGU P4560 [IOI2014]Wall 砖墙 (线段树)
2024-09-06 07:37:00
解题思路
线段树打标记,刚开始想复杂了,维护了四个标记。后来才知道只需要维护一个最大值最小值即可,然后更新的时候分类讨论一下。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int MAXN = 2000005;
inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {f=ch=='-'?0:1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f?x:-x;
}
int n,a[MAXN],high[MAXN<<2],low[MAXN<<2],m;
inline int min(int x,int y){
return x<y?x:y;
}
inline int max(int x,int y){
return x>y?x:y;
}
inline void pushdown(int x,int l,int r){
int mid=(l+r)>>1;
if(l==r) {
if(low[x]!=-1) a[l]=min(a[l],low[x]);
if(high[x]!=-1) a[l]=max(a[l],high[x]);
return ;
}
if(low[x]!=-1) {
if(low[x<<1]!=-1) low[x<<1]=min(low[x<<1],low[x]);
else low[x<<1]=low[x];
if(high[x<<1]>low[x]) high[x<<1]=low[x];
if(low[x<<1|1]!=-1) low[x<<1|1]=min(low[x<<1|1],low[x]);
else low[x<<1|1]=low[x];
if(high[x<<1|1]>low[x]) high[x<<1|1]=low[x];
low[x]=-1;
}
if(high[x]!=-1){
high[x<<1]=max(high[x<<1],high[x]);
if(high[x]>low[x<<1] && low[x<<1]!=-1) low[x<<1]=high[x];
high[x<<1|1]=max(high[x<<1|1],high[x]);
if(high[x]>low[x<<1|1] && low[x<<1|1]!=-1) low[x<<1|1]=high[x];
high[x]=-1;
}
}
void update_h(int x,int l,int r,int L,int R,int k){
if(L<=l && r<=R) {
high[x]=max(high[x],k);
if(low[x]!=-1 && low[x]<k) low[x]=k;
if(l==r) {
if(low[x]!=-1) a[l]=min(a[l],low[x]);
if(high[x]!=-1) a[l]=max(a[l],high[x]);
}
return ;
}
int mid=(l+r)>>1;pushdown(x,l,r);
if(L<=mid) update_h(x<<1,l,mid,L,R,k);
if(mid<R) update_h(x<<1|1,mid+1,r,L,R,k);
}
void update_l(int x,int l,int r,int L,int R,int k){
if(L<=l && r<=R) {
if(low[x]==-1) low[x]=k;
else low[x]=min(low[x],k);
if(high[x]!=-1 && high[x]>k) high[x]=k;
if(l==r){
if(low[x]!=-1) a[l]=min(a[l],low[x]);
if(high[x]!=-1) a[l]=max(a[l],high[x]);
}
return ;
}
int mid=(l+r)>>1;pushdown(x,l,r);
if(L<=mid) update_l(x<<1,l,mid,L,R,k);
if(mid<R) update_l(x<<1|1,mid+1,r,L,R,k);
}
void query(int x,int l,int r){
if(l==r) {pushdown(x,l,r);printf("%d\n",a[l]);return ;}
int mid=(l+r)>>1;pushdown(x,l,r);
query(x<<1,l,mid);query(x<<1|1,mid+1,r);
}
int main(){
memset(low,-1,sizeof(low));
memset(high,-1,sizeof(high));
n=rd(),m=rd();int op,l,r,k;
while(m--){
op=rd(),l=rd(),r=rd(),k=rd();l++;r++;
if(op==1) update_h(1,1,n,l,r,k);
else update_l(1,1,n,l,r,k);
}
query(1,1,n);
return 0;
}
最新文章
- Dynamics CRM 2015-Sign Out选项
- python函数 与 函数式编程
- 装b指南
- CSS 高级技巧汇总
- Git中当add错误的时候怎么办?
- FreeBSD 9.1安装KMS 这是一个伪命题###### ,9....
- YbRapidSolution.MVC项目首页分页没有起作用
- MySQL主从同步报Client requested master to start replication from position
- Laravel 用户验证Auth::attempt fail的问题
- Hibernate逆向代码问题
- VIJOS-P1635 城市连接
- css公共库——简介中超过长度显示省略号
- 理论篇-MySQL知识汇总
- day14 Python集合的补充
- android&;sqlsever
- what&#39;s the python之变量、基本数据类型
- WhereHows前后端配置文件
- Node.js实战(一)之概述
- all hands meeting
- POJ 1509 Glass Beads---最小表示法
热门文章
- laravel ajax提交报错Symfony\Component\HttpKernel\Exception\HttpException
- Collections 工具类常见方法
- delphi 窗体的位置和高宽度-TForm:Letf、Top、Width、Height、ClientWidth、ClientHeight
- 分布式项目中增加品牌前端页面出现Uncaught Error: [$injector:modulerr] bug后的原因以及改正方式
- [7.18NOIP模拟测试5]砍树 题解(数论分块)
- Python100天打卡-Day10
- js中的函数声明和函数表达式的区别
- ElasticSearch Roaring bitmap 和跳表联合查询
- robotframework悬浮菜单定位问题
- ASP.NET Core学习——前言