浅谈区间最值操作和历史最值问题:https://www.cnblogs.com/AKMer/p/10225100.html

题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=4695

吉司机线段树板子大集合。所有信息都封装在一个结构体里会比开多个数组快14秒。

注意暴力\(dfs\)子树时要\(pushdown\)。

时间复杂度:\(O(nlog^2n)\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll; const int maxn=5e5+5,inf=1e9; int n,m;
int a[maxn]; inline int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} struct segment_tree {
struct tree_node {
ll sum;
int cntA,cntZ,tag,A,B,Y,Z;//A最大值,B严格次大值,Y严格次小值,Z最小值
}tree[maxn<<2]; inline void update(int p) {
tree[p].A=max(tree[p<<1].A,tree[p<<1|1].A);
tree[p].Z=min(tree[p<<1].Z,tree[p<<1|1].Z);
tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
tree[p].cntA=(tree[p<<1].A==tree[p].A)*tree[p<<1].cntA;
tree[p].cntA+=(tree[p<<1|1].A==tree[p].A)*tree[p<<1|1].cntA;
tree[p].cntZ=(tree[p<<1].Z==tree[p].Z)*tree[p<<1].cntZ;
tree[p].cntZ+=(tree[p<<1|1].Z==tree[p].Z)*tree[p<<1|1].cntZ;
tree[p].B=tree[p<<1|1].A!=tree[p].A?tree[p<<1|1].A:tree[p<<1|1].B;
tree[p].B=max(tree[p].B,tree[p<<1].A!=tree[p].A?tree[p<<1].A:tree[p<<1].B);
tree[p].Y=tree[p<<1|1].Z!=tree[p].Z?tree[p<<1|1].Z:tree[p<<1|1].Y;
tree[p].Y=min(tree[p].Y,tree[p<<1].Z!=tree[p].Z?tree[p<<1].Z:tree[p<<1].Y);
} inline void build(int p,int l,int r) {
if(l==r) {
tree[p].cntA=tree[p].cntZ=1;
tree[p].sum=tree[p].A=tree[p].Z=a[l];
tree[p].B=-inf,tree[p].Y=inf;
return;
}
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
update(p);
} inline void add_tag(int p,int l,int r,int v) {
tree[p].A+=v,tree[p].Z+=v,tree[p].tag+=v;
if(tree[p].Y!=inf)tree[p].Y+=v;
if(tree[p].B!=-inf)tree[p].B+=v;
tree[p].sum+=1ll*(r-l+1)*v;
} inline void max_tag(int p,int v) {
tree[p].sum+=1ll*tree[p].cntZ*(v-tree[p].Z),tree[p].Z=v;
if(tree[p].Y==inf)tree[p].A=v;
else tree[p].B=max(tree[p].B,v);
} inline void min_tag(int p,int v) {
tree[p].sum-=1ll*tree[p].cntA*(tree[p].A-v),tree[p].A=v;
if(tree[p].B==-inf)tree[p].Z=v;
else tree[p].Y=min(tree[p].Y,v);
} inline void solveMax(int p,int l,int r,int limit) {
if(limit<=tree[p].Z)return;
if(limit>tree[p].Z&&limit<tree[p].Y) {
max_tag(p,limit);
return;
}
int mid=(l+r)>>1;push_down(p,l,r);
solveMax(p<<1,l,mid,limit);
solveMax(p<<1|1,mid+1,r,limit);
update(p);
} inline void solveMin(int p,int l,int r,int limit) {
if(limit>=tree[p].A)return;
if(limit<tree[p].A&&limit>tree[p].B) {
min_tag(p,limit);
return;
}
int mid=(l+r)>>1;push_down(p,l,r);
solveMin(p<<1,l,mid,limit);
solveMin(p<<1|1,mid+1,r,limit);
update(p);
} inline void push_down(int p,int l,int r) {
int mid=(l+r)>>1;
if(tree[p].tag) {
add_tag(p<<1,l,mid,tree[p].tag);
add_tag(p<<1|1,mid+1,r,tree[p].tag);
tree[p].tag=0;
}
solveMin(p<<1,l,mid,tree[p].A);
solveMin(p<<1|1,mid+1,r,tree[p].A);
solveMax(p<<1,l,mid,tree[p].Z);
solveMax(p<<1|1,mid+1,r,tree[p].Z);
} inline void ADD(int p,int l,int r,int L,int R,int v) {
if(L<=l&&r<=R) {
add_tag(p,l,r,v);
return;
}
int mid=(l+r)>>1;push_down(p,l,r);
if(L<=mid)ADD(p<<1,l,mid,L,R,v);
if(R>mid)ADD(p<<1|1,mid+1,r,L,R,v);
update(p);
} inline void MAX(int p,int l,int r,int L,int R,int v) {
if(tree[p].Z>=v)return;
if(L<=l&&r<=R) {
solveMax(p,l,r,v);
return;
}
int mid=(l+r)>>1;push_down(p,l,r);
if(L<=mid)MAX(p<<1,l,mid,L,R,v);
if(R>mid)MAX(p<<1|1,mid+1,r,L,R,v);
update(p);
} inline void MIN(int p,int l,int r,int L,int R,int v) {
if(tree[p].A<=v)return;
if(L<=l&&r<=R) {
solveMin(p,l,r,v);
return;
}
int mid=(l+r)>>1;push_down(p,l,r);
if(L<=mid)MIN(p<<1,l,mid,L,R,v);
if(R>mid)MIN(p<<1|1,mid+1,r,L,R,v);
update(p);
} inline ll querySum(int p,int l,int r,int L,int R) {
if(L<=l&&r<=R)return tree[p].sum;
int mid=(l+r)>>1;ll res=0;push_down(p,l,r);
if(L<=mid)res=querySum(p<<1,l,mid,L,R);
if(R>mid)res+=querySum(p<<1|1,mid+1,r,L,R);
return res;
} inline int queryMax(int p,int l,int r,int L,int R) {
if(L<=l&&r<=R)return tree[p].A;
int mid=(l+r)>>1,res=-inf;push_down(p,l,r);
if(L<=mid)res=max(res,queryMax(p<<1,l,mid,L,R));
if(R>mid)res=max(res,queryMax(p<<1|1,mid+1,r,L,R));
return res;
} inline int queryMin(int p,int l,int r,int L,int R) {
if(L<=l&&r<=R)return tree[p].Z;
int mid=(l+r)>>1,res=inf;push_down(p,l,r);
if(L<=mid)res=min(res,queryMin(p<<1,l,mid,L,R));
if(R>mid)res=min(res,queryMin(p<<1|1,mid+1,r,L,R));
return res;
}
}T; int main() {
n=read();
for(int i=1;i<=n;i++)
a[i]=read();
T.build(1,1,n);
m=read();
for(int i=1;i<=m;i++) {
int opt=read(),l=read(),r=read(),v=(opt<4?read():0);
if(opt==1)T.ADD(1,1,n,l,r,v);
if(opt==2)T.MAX(1,1,n,l,r,v);
if(opt==3)T.MIN(1,1,n,l,r,v);
if(opt==4)printf("%lld\n",T.querySum(1,1,n,l,r));
if(opt==5)printf("%d\n",T.queryMax(1,1,n,l,r));
if(opt==6)printf("%d\n",T.queryMin(1,1,n,l,r));
}
return 0;
}

BZOJ4695:最假女选手的更多相关文章

  1. BZOJ4695 最假女选手(势能线段树)

    BZOJ题目传送门 终于体会到初步掌握势能分析思想的重要性了. 一开始看题,感觉套路还是很一般啊qwq.直接在线段树上维护最大值和最小值,每次递归更新的时候,如果不能完全覆盖就暴力递归下去.挺好写的欸 ...

  2. 2018.07.27 bzoj4695: 最假女选手(线段树)

    传送门 线段树好题 支持区间加,区间取min" role="presentation" style="position: relative;"> ...

  3. bzoj4695 最假女选手

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4695 [题解] SegmentTree beats!(见jiry_2论文/营员交流) 考虑只 ...

  4. [BZOJ4695]最假女选手:segment tree beats!

    分析 segment tree beats!模板题. 看了gxz的博客突然发现自己写的mxbt和mnbt两个标记没用诶. 代码 #include <bits/stdc++.h> #defi ...

  5. bzoj4695 最假女选手(势能线段树/吉司机线段树)题解

    题意: 已知\(n\)个数字,进行以下操作: \(1.\)给一个区间\([L,R]\) 加上一个数\(x\) \(2.\)把一个区间\([L,R]\) 里小于\(x\) 的数变成\(x\) \(3.\ ...

  6. bzoj 4695 最假女选手 吉利线段树

    最假女选手 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 480  Solved: 118[Submit][Status][Discuss] Desc ...

  7. 【bzoj4695】最假女选手 线段树区间最值操作

    题目描述 给定一个长度为 N 序列,编号从 1 到 N .要求支持下面几种操作:1.给一个区间[L,R] 加上一个数x 2.把一个区间[L,R] 里小于x 的数变成x 3.把一个区间[L,R] 里大于 ...

  8. 【bzoj4695】最假女选手

    zcy的励志故事.jpg 傻逼zcy突然想立一个flag,写一个segment-tree-beats的题娱乐一下 于是他就想起了这道题. 他打算今晚写完 然后光是写他就写的头昏脑涨,还犯了询问写反这种 ...

  9. (WC2016模拟十一)【BZOJ4695】最假女选手

    ps:好久没更博啦……这几天连着有模拟赛,等初赛前后休息的时候来疯狂补坑吧……顺便补一下前面的数论啥的? 题解: mdzz我场上写了个15分暴力长度跟标算差不多... 线段树大法好啊!这题听说很多人做 ...

随机推荐

  1. nginx学习之进程控制篇(三)

    1. 进程 nginx有一个master进程和一个或多个工作进程. master process worker process or master process worker process wor ...

  2. JVM类加载器

    系统中的类加载器 1.BootStrap ClassLoader a.启动ClassLoader b.加载rt.jar 2.Extension ClassLoader a.扩展ClassLoader ...

  3. Django开发模式会加载两次settings文件导致RotatingFileHandlerError

    当使用RotatingFileHandler作为django的日志处理器的时候,会报: Traceback (most recent call last): File "C:\Python2 ...

  4. 17.Django表单验证

    Django提供了3中方式来验证表单 官网文档:https://docs.djangoproject.com/en/1.9/ref/validators 1.表单字段验证器 a.引入:from dja ...

  5. 聊聊数据库~5.SQL运维上篇

    1.6.SQL运维篇 运维这块逆天只能说够用,并不能说擅长,所以这篇就当抛砖之用,欢迎补充和纠错 PS:再说明下CentOS优化策略这部分的内容来源:首先这块逆天不是很擅长,所以主要是参考网上的DBA ...

  6. 编程算法 - 最好牛线(Best Cow Line) 代码(C)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/37909933 最好牛线(Best Cow L ...

  7. Jquery的parent和parents(找到某一特定的祖先元素)用法(转发:https://blog.csdn.net/cui_angel/article/details/7903704)

    <!-- parent是指取得一个包含着所有匹配元素的唯一父元素的元素集合. parents则是取得一个包含着所有匹配元素的祖先元素的元素集合(不包含根元素).可以通过一个可选的表达式进行筛选. ...

  8. zipfile.BadZipFile: File is not a zip file

    zipfile.BadZipFile: File is not a zip file 出现这个问题一般是文件损坏的可能性比较大

  9. ThinkPHP中Widget的两种写法及调用

    Widget扩展一般用于页面组件的扩展,在页面根据需要输出不同的内容,下面介绍一下ThinkPHP中Widget的两种写法及调用 写法一: ArticlWidget.class.php文件: clas ...

  10. php基本语法与函数

    1.标记与注释 <?php 代码 ?> 用/*  */注释一段代码,  用 // 注释一行代码   /**    */文档注释 注意:若php下面只有php代码没有别的代码,那么最好不要加 ...