2018.07.25 hdu5306Gorgeous Sequence(线段树)
传送门
线段树基本操作。
要求维护区间取min" role="presentation" style="position: relative;">minmin(给定一个数x" role="presentation" style="position: relative;">xx,让整个区间中的数都变成min(x,a[i])" role="presentation" style="position: relative;">min(x,a[i])min(x,a[i]))
然后是查询区间最大与区间和。
第一眼看到以为是max" role="presentation" style="position: relative;">maxmax和min" role="presentation" style="position: relative;">minmin直接剪枝,结果写了一发发现挂了,然后找到了一篇叫做segment" role="presentation" style="position: relative;">segmentsegment tree" role="presentation" style="position: relative;">treetree beats" role="presentation" style="position: relative;">beatsbeats的论文,%%%%%%%%%%%jiry2" role="presentation" style="position: relative;">jiry2jiry2,然后就学会了这种操作。
其实也就是维护区间最大和区间次大然后分类讨论。
如果当前的x" role="presentation" style="position: relative;">xx已经不小于区间最大值了直接返回,因为无法更新区间中任意一个节点。
如果当前的x" role="presentation" style="position: relative;">xx比区间次大值要大的话,说明只有区间最大值会被更新。这样我们还要维护一个区间中最大值的出现次数,方便更新。
如果x" role="presentation" style="position: relative;">xx比区间次大值还要小?递归更新吧。
论文中着重讲述了复杂度的证明。
代码如下:
#include<bits/stdc++.h>
#define ll long long
#define N 1000005
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
using namespace std;
inline ll read(){
ll ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
inline void write(ll x){
if(x>9)write(x/10);
putchar((x%10)^48);
}
int T_T,n,m;
struct Node{int l,r;ll cx,sum,mx,mxx;}T[N<<2];
inline ll max(ll a,ll b){return a>b?a:b;}
inline void pushup(int p){
T[p].sum=T[lc].sum+T[rc].sum;
T[p].mx=max(T[lc].mx,T[rc].mx);
T[p].cx=(T[p].mx==T[lc].mx?T[lc].cx:0)+(T[p].mx==T[rc].mx?T[rc].cx:0);
T[p].mxx=max(T[lc].mx==T[p].mx?T[lc].mxx:T[lc].mx,T[rc].mx==T[p].mx?T[rc].mxx:T[rc].mx);
}
inline void pushnow(int p,ll v){if(T[p].mx<=v)return;T[p].sum+=T[p].cx*(v-T[p].mx),T[p].mx=v;}
inline void pushdown(int p){pushnow(lc,T[p].mx),pushnow(rc,T[p].mx);}
inline void build(int p,int l,int r){
T[p].l=l,T[p].r=r;
if(l==r){T[p].cx=1,T[p].mxx=-1,T[p].mx=T[p].sum=read();return;}
build(lc,l,mid),build(rc,mid+1,r),pushup(p);
}
inline void update(int p,int ql,int qr,ll v){
if(ql>T[p].r||qr<T[p].l||T[p].mx<=v)return;
if(ql<=T[p].l&&T[p].r<=qr&&T[p].mxx<v){pushnow(p,v);return;}
pushdown(p);
if(qr<=mid)update(lc,ql,qr,v);
else if(ql>mid)update(rc,ql,qr,v);
else update(lc,ql,mid,v),update(rc,mid+1,qr,v);
pushup(p);
}
inline ll query_max(int p,int ql,int qr){
if(ql>T[p].r||qr<T[p].l)return 0;
if(ql<=T[p].l&&T[p].r<=qr)return T[p].mx;
pushdown(p);
if(qr<=mid)return query_max(lc,ql,qr);
if(ql>mid)return query_max(rc,ql,qr);
return max(query_max(lc,ql,mid),query_max(rc,mid+1,qr));
}
inline ll query_sum(int p,int ql,int qr){
if(ql>T[p].r||qr<T[p].l)return 0;
if(ql<=T[p].l&&T[p].r<=qr)return T[p].sum;
pushdown(p);
if(qr<=mid)return query_sum(lc,ql,qr);
if(ql>mid)return query_sum(rc,ql,qr);
return query_sum(lc,ql,mid)+query_sum(rc,mid+1,qr);
}
int main(){
T_T=read();
while(T_T--){
n=read(),m=read();
build(1,1,n);
while(m--){
int op=read(),l=read(),r=read();
switch(op){
case 0:{ll v=read();update(1,l,r,v);break;}
case 1:{write(query_max(1,l,r)),puts("");break;}
default:{write(query_sum(1,l,r)),puts("");break;}
}
}
}
return 0;
}
2018.07.25 hdu5306Gorgeous Sequence(线段树)的更多相关文章
- 2018.07.22 codeforces750E(线段树维护状态转移)
传送门 给出一个数字字串,给出若干个询问,询问在字串的一段区间保证出现2017" role="presentation" style="position: re ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- 2018.07.25 bzoj3878: [Ahoi2014&Jsoi2014]奇怪的计算器(线段树)
传送门 线段树综合. 让我想起一道叫做siano" role="presentation" style="position: relative;"&g ...
- Wow! Such Sequence!(线段树4893)
Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- hdu4893Wow! Such Sequence! (线段树)
Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger f ...
- HDU 6047 Maximum Sequence(线段树)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...
- Codeforces 438D The Child and Sequence - 线段树
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...
- hdu 5828 Rikka with Sequence 线段树
Rikka with Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...
随机推荐
- Spring MVC @InitBinder 数据绑定 & 数据格式化 & 数据校验
1 数据绑定 2 数据格式化 修改绑定的字段等等操作 日期 - 接收表单日期字符串格式内容.,在实体类加入@DateTimeFormat 数值 原理: DefautFormattingConversi ...
- Delphi 停靠技术的应用3(两个窗体停靠成PageControl样式, 分页停靠)
Delphi 停靠技术的应用3(两个窗体停靠成PageControl样式, 分页停靠) 因为TPageControl组件重载了TWinControl组件的DoAddDockClient和DoRemov ...
- Numpy 基础知识
1.使用ipython --pylab 自动加载 Numpy.Scipy.Matplotlib模块. 创建数组a = arange(10) b = arange(10,dtype='f')c = ar ...
- conductor FAQ
在一段时间后(如1小时,1天等),您如何安排将任务放入队列中? 轮询任务后,更新任务的状态IN_PROGRESS并将其callbackAfterSeconds设置为所需的时间.任务将保留在队列中,直到 ...
- zg 作业一
作业一: 将xxjjT01增加窗体及按钮(增.修.删) 表单(form1.html) 设定按钮作业,授权 1.新增:XXJJT01+CREATE 2.修改:XXJJT01+UPDATE 3.删除:XX ...
- maven package install deploy
1.maven package:打包到本项目,一般是在项目target目录下. 如果a项目依赖于b项目,打包b项目时,只会打包到b项目下target下,编译a项目时就会报错,因为找不到所依赖的b项目, ...
- in 和 exist 区别 (转)
select * from Awhere id in(select id from B) 以上查询使用了in语句,in()只执行一次,它查出B表中的所有id字段并缓存起来.之后,检查A表的id是否与B ...
- 用Python提取XML里的内容,存到Excel中
最近做一个项目是解析XML文件,提取其中的chatid和lt.timestamp等信息,存到excel里. 1.解析xml,提取数据 使用python自带的xml.dom中的minidom(也可以用l ...
- hdoj1257(DP-LIS/贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1257 方法1--贪心: 定义一个数组f[30005],由于题目没给数据量大小,故为了保险,开到最大(高 ...
- eclipse搭建struts2环境及所遇到的问题
最近几天一直在搭建struts2框架,本身struts2框架的搭建是非常简单的,但不知道为什么最近就是总是报错,报了一大串的错 首先就是每次在类的根路径下创建struts.xml时,就报错,也不知道为 ...