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 ...
随机推荐
- 3.mybatis实战教程(mybatis in action)之三:实现数据的增删改查
转自:https://blog.csdn.net/tangruyi1992/article/details/52583910 前面已经讲到用接口的方式编程.这种方式,要注意的一个地方就是.在User. ...
- python之model模块和包的介绍
一,模块的概念:常见场景:一个模块就是一个包含了一组功能的Python文件,比如spam.py,模块名为spam,可以通过import spam使用 在Python中,模块的使用方式都是一样的,但其实 ...
- java 观察者模式 与spring配置
一.Observer模式的意图: 在对象的内部状态发生变化时,自动通知外部对象进行响应. 二.Observer模式的构成: ·被观察者:内部状态有可能被改变,而且又需要通知外部的对象 ·观察者:需要对 ...
- ntohs, ntohl, htons,htonl的比较和详解【转】
ntohs =net to host short int 16位 htons=host to net short int 16位 ntohs =net to host long int 32位 hto ...
- Python Env
简介: 记录 CentOS 6.x Python 环境的安装步骤. 一.安装依赖包 shell > yum -y install epel-release shell > yum -y i ...
- jQuery:总体掌握
链式编程....方法多,属性无法得到对象进行链式.vs10自动完成.书籍锋利的jQuery vsdoc有智能提示开发时候用,开发完之后,换成min压缩版的. 经验:打开网站文件夹.可以把vs网站上的解 ...
- MongoDB 数据库命令
数据库命令 连接成功后,默认使用test数据库 查看当前数据库名称 db 查看所有数据库名称,列出所有在物理上存在的数据库 show dbs 切换数据库,如果数据库不存在也并不创建,直到插入数据或创建 ...
- linux 配置Tomcat开机启动
一台安装有tomcat的linux服务器 方法/步骤 1 请自行下载安装配置tomcat的服务器环境 本经验仅仅介绍如何配置tomcat的开机自动启动 2 切换到tomcat/bin目录下 用vi ...
- 使用python识别验证码
公司的登录注册等操作有验证码,测试环境可以让开发屏蔽掉验证码,但是如果到线上的话就要想办法识别验证码或必过验证码了. 识别验证码主要分为三部分,一.对验证码进行二值化.二.将二值化后的图片分割.三.进 ...
- 奇偶数判断2(if else+switch语句)
public class 奇偶数判断2 { public static void main(String [] agrs){ float s = 17f; //定义浮点型数据s float h = s ...