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 ...
随机推荐
- 快速编写 <a> ————CSS3
a{ text-decoration:none; } a:link{ color:white; } a:visited { color:white; } a:hover { color:blue; } ...
- 【HANA系列】SAP HANA SQL获取当前日期加若干天后的日期
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL获取当前 ...
- 递归算法之排列组合-求一个集合S的m个元素的组合和所有可能的组合情况
求一个集合S的m个元素组合的所有情况,并打印出来,非常适合采用递归的思路进行求解.因为集合的公式,本身就是递归推导的: C(n,m) = C(n-1,m-1) + C(n-1,m). 根据该公式,每次 ...
- Centos7安装protobuf3.6.1
简介 最近学习go语言,需要安装protobuf,但是网上的教程很多都不太适用于centos7 的系统.现在总结下protobuf在centos7下的安装教程. protobuf是Google开发出来 ...
- 为什么存储过程比sql语句效率高?
存储过程经过预编译处理 而SQL查询没有 SQL语句需要先被数据库引擎处理成低级的指令 然后才执行 -------------------------------------------------- ...
- lb开金矿 QDUOJ 数论
lb开金矿 QDUOJ 数论 原题链接,点我进去 题意 大家都知道lb有n个小弟(编号从2到n+1),他们可以按照规则传递信息:某天编号为i的小弟收到信息后,那么第二天他会给编号为j的小弟传达信息,其 ...
- P1540翻译机器
这是2010提高组第一题,是一个使用队列的模拟题(然而洛谷很多大佬用了最短路) 这道题首先要判断内存中是否已有解释(因为题目已经说了长度很小,所以可以用桶排序),没有的话便去外存找,找到后,存到内存的 ...
- Dire Wolf——HDU5115(区间DP)
题意 就是有一对狼,每个狼有初始的攻击力,并且还能给左右两边的狼提供攻击力加成,当冒险家杀死一头狼的时候他也会受到这个狼目前攻击力的伤害 实例解析 33 5 78 2 0 有三头狼,刚开始第二头狼给他 ...
- java流stream中的collect()方法详解
public class StreamTest { /** * stream.collect() 的本质由三个参数构成, * 1. Supplier 生产者, 返回最终结果 * 2. BiConsum ...
- Python 自定义三方库
一.注册一个pypi账号 https://pypi.org/ 二.github上创建一个项目 https://github.com/ 三.编写自己的python项目 项目结构(参考):https:// ...