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 ...
随机推荐
- 【linux开发】Linux下配置java环境 安装eclipse
配置JDK环境 本文转自:http://www.cnblogs.com/fnng/archive/2013/01/30/2883815.html,有修改 下载 登录oracle的网站去下载JDK1.8 ...
- 第五周课程总结&试验报告(三
实验三 String类的应用 实验目的 掌握类String类的使用: 学会使用JDK帮助文档: 实验内容 1.已知字符串:"this is a test of java".按要求执 ...
- Springboot2.x集成Redis集群模式
Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...
- Spark启动流程(Standalone)-分析
1.start-all.sh脚本,实际上执行java -cp Master 和 java -cp Worker 2.Master 启动时首先穿件一个RpcEnv对象,负责管理所有通信逻辑 3.Mast ...
- phpStudy配置sql、oracle---博主摘录
引用 :https://www.cnblogs.com/myBlogInWork/p/8657125.html 由于工作需要,要用到php+oracle写个项目,故而有了以下内容: 本来以为php有默 ...
- Scrapy 教程(三)-网站解析
有经验的人都知道,解析网站需要尝试,看看得到的数据是不是想要的,那么在scrapy中怎么尝试呢? 调试工具-shell 主要用于编写解析器 命令行进入shell scrapy shell url 这个 ...
- [Nest] 01.初见nest.js
github nest 介绍 Nest 是一个用于构建高效,可扩展的 Node.js 服务器端应用程序的框架.它使用渐进式 JavaScript,内置并完全支持 TypeScript(但仍然允许开发人 ...
- Zookeeper入门概要
ZooKeeper是一个开源的分布式协调服务,由雅虎创建,是Google Chubby的开源实现.ZooKeeper的设计目标是将那些复杂且容易出错的分布式一致性服务封装起来,构成一个高效可靠的原语集 ...
- 在XCode中使用XCTest
测试驱动开发并不是一个很新鲜的概念了.在我最开始学习程序编写时,最喜欢干的事情就是编写一段代码,然后运行观察结果是否正确.我所学习第一门语言是c语言,用的最多的是在算法设计上,那时候最常做的事情就是编 ...
- 用R语言提取数据框中日期对应年份(列表转矩阵)
用R语言提取数据框中日期对应年份(列表转矩阵) 在数据处理中常会遇到要对数据框中的时间做聚类处理,如从"%m/%d/%Y"中提取年份. 对应操作为:拆分成列表——列表转矩阵——利用 ...