HDU - 5306 Gorgeous Sequence 线段树 + 均摊分析
Code:
#include<algorithm>
#include<cstdio>
#include<cstring>
#define ll long long
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 2000000
#define lson (now<<1)
#define rson ((now<<1)|1)
using namespace std;
int n,q;
ll arr[maxn],mx[maxn<<2],se[maxn<<2],cnt[maxn<<2],sum[maxn<<2];
void pushup(int l,int r,int now) {
int mid=(l+r)>>1;
int ls=lson,rs=(r>mid)?rson:0;
sum[now]=sum[ls]+sum[rs];
if(mx[ls]>mx[rs]) mx[now]=mx[ls],cnt[now]=cnt[ls],se[now]=max(se[ls],mx[rs]);
if(mx[rs]>mx[ls]) mx[now]=mx[rs],cnt[now]=cnt[rs],se[now]=max(se[rs],mx[ls]);
if(mx[ls]==mx[rs]) mx[now]=mx[ls],cnt[now]=cnt[ls]+cnt[rs],se[now]=max(se[ls],se[rs]);
}
void mark(int now,ll v) {
if(v<mx[now]) sum[now]-=(mx[now]-v)*cnt[now], mx[now]=v;
}
void pushdown(int l,int r,int now) {
int mid=(l+r)>>1;
mark(lson,mx[now]);
if(r>mid) mark(rson,mx[now]);
}
void build(int l,int r,int now) {
if(l==r) {
mx[now]=arr[l],se[now]=-1,sum[now]=arr[l],cnt[now]=1;
return;
}
int mid=(l+r)>>1;
build(l,mid,lson);
if(r>mid) build(mid+1,r,rson);
pushup(l,r,now);
}
void change(int l,int r,int now,int L,int R,ll v) {
if(mx[now]<=v) return;
if(l>=L&&r<=R&&se[now]<v) {
mark(now,v);
return;
}
pushdown(l,r,now);
int mid=(l+r)>>1;
if(L<=mid) change(l,mid,lson,L,R,v);
if(R>mid) change(mid+1,r,rson,L,R,v);
pushup(l,r,now);
}
ll qmax(int l,int r,int now,int L,int R) {
if(l>=L&&r<=R) return mx[now];
pushdown(l,r,now);
int mid=(l+r)>>1;
ll re=-1;
if(L<=mid) re=max(re,qmax(l,mid,lson,L,R));
if(R>mid) re=max(re,qmax(mid+1,r,rson,L,R));
return re;
}
ll qsum(int l,int r,int now,int L,int R) {
if(l>=L&&r<=R) return sum[now];
pushdown(l,r,now);
int mid=(l+r)>>1;
ll re=0;
if(L<=mid) re+=qsum(l,mid,lson,L,R);
if(R>mid) re+=qsum(mid+1,r,rson,L,R);
return re;
}
void solve() {
scanf("%d%d",&n,&q);
for(int i=1;i<=n;++i) scanf("%lld",&arr[i]);
build(1,n,1);
while(q--) {
int opt,x,y;
ll t;
scanf("%d%d%d",&opt,&x,&y);
if(opt==0) scanf("%lld",&t),change(1,n,1,x,y,t);
if(opt==1) printf("%lld\n",qmax(1,n,1,x,y));
if(opt==2) printf("%lld\n",qsum(1,n,1,x,y));
}
}
int main() {
// setIO("input");
int T;
scanf("%d",&T);
mx[0]=se[0]=-1;
while(T--) solve();
return 0;
}
HDU - 5306 Gorgeous Sequence 线段树 + 均摊分析的更多相关文章
- HDU 5306 Gorgeous Sequence[线段树区间最值操作]
Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 【loj6029】「雅礼集训 2017 Day1」市场 线段树+均摊分析
题目描述 给出一个长度为 $n$ 的序列,支持 $m$ 次操作,操作有四种:区间加.区间下取整除.区间求最小值.区间求和. $n\le 100000$ ,每次加的数在 $[-10^4,10^4]$ 之 ...
- 【uoj#228】基础数据结构练习题 线段树+均摊分析
题目描述 给出一个长度为 $n$ 的序列,支持 $m$ 次操作,操作有三种:区间加.区间开根.区间求和. $n,m,a_i\le 100000$ . 题解 线段树+均摊分析 对于原来的两个数 $a$ ...
- cf250D. The Child and Sequence(线段树 均摊复杂度)
题意 题目链接 单点修改,区间mod,区间和 Sol 如果x > mod ,那么 x % mod < x / 2 证明: 即得易见平凡, 仿照上例显然, 留作习题答案略, 读者自证不难. ...
- HDOJ 5306 Gorgeous Sequence 线段树
http://www.shuizilong.com/house/archives/hdu-5306-gorgeous-sequence/ Gorgeous Sequence Time Limit: 6 ...
- bzoj4127 Abs 树链剖分+线段树+均摊分析
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4127 题解 首先区间绝对值和可以转化为 \(2\) 倍的区间正数和 \(-\) 区间和.于是问 ...
- UOJ #228. 基础数据结构练习题 线段树 + 均摊分析 + 神题
题目链接 一个数被开方 #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",st ...
- HDU - 5306 Gorgeous Sequence (吉司机线段树)
题目链接 吉司机线段树裸题... #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3 ...
- HDU 6047 Maximum Sequence(线段树)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...
随机推荐
- postman杂记
接到测试任务,测试6个接口 rap2 上的接口比较多,整体导出内容太多 就一个接口一个接口的,复制到了postman上 rap2部分接口,开发没有备注简介内容:通知开发备注下 对接口的理解,还是靠功能 ...
- maven 异常 提示 cannot be read or is not a valid ZIP file
Archive for required library: 'D:/repository/Maven/org/springframework/spring-aop/4.3.6.RELEASE/spri ...
- C语言中,当计算字符数组长度时,用sizeof 和strlen 的原理及两者的区别
字符数组的长度计算:必须以终止符’\0'作为边界,但对字符数组赋值时,有两种方式: 1:定义时用字符初始化 (1)char chs[7] = {'a', 'c', '0', 'z', '3','d'} ...
- 浅拷贝&深拷贝
浅拷贝新的对象指向原来对象的地址 深拷贝新的对象中,原来是可变对象,会新复制一份值指向新的地址[11,22,33]若原来的对象里含有可变对象,里面的这个可变对象也会指向新的地址['qwer', 123 ...
- K8S工作原理
kubernetes(k8s)是docker容器用来编排和管理的工具 我们通过kubectl向k8s Master发出指令.kubernetes Master主要是提供API Server.Sched ...
- CSRF Failed: CSRF token missing or incorrect
Django设置本身没有关闭CSRF Django设置已经关闭CSRF,可能是由于两个项目都使用同一个端口,调试的时候就会出现Cookie里面csrftoken重用的问题,清理Cookie就好
- 20170309工作笔记--------如何用好dialog,想变什么样就变成什么样
(1)首先自定义一个dialog的div,并且写内容 (2)运用相应的代码进行控制,弹出dialog $(".tel").click(function() { $("#d ...
- 使用SecureCRT 8.5快速打开sftp传输文件
一般使用Windows系统上安装的SecureCRT 8.5软件远程连接Linux服务器,通常给Linux系统传输文件或者使用FTP,或者使用SFTP等其他第三方软件,有时Linux系统上还需要做其他 ...
- sping data jpa 共享主键 OneTonOne 延时加载
当我们使用spring boot创建项目时,系统默认使用的是如下parent. <parent> <groupId>org.springframework.boot</g ...
- ThinkPHP5 支付宝支付扩展库(超级简单,超级好用!)
ThinkPHP5 支付宝支付扩展库, 一个静态方法的调用就可以实现,包括手机网站支付.电脑网站支付.支付查询.退款.退款查询.对账单所有功能,而且是2017年7月20日最新版~我的想法是,调用一个静 ...