UVALive - 3938:"Ray, Pass me the dishes!"
优美的线段树
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#define MAXN 500000+10
#define ll long long
#define pii pair<int,int>
#define mp make_pair
using namespace std;
int a[MAXN];
int n,m;
struct Node{
int pre_dat;
ll pre_sum;
int suf_dat;
ll suf_sum;
pii dat;
ll sum;
ll d;
Node(int p1=,ll p2=,int p3=,ll p4=,pii p5=mp(,),ll p6=,ll p7=){
pre_dat=p1,pre_sum=p2,suf_dat=p3,suf_sum=p4,dat=p5,sum=p6,d=p7;
}
}data[MAXN<<];
Node Merge(Node t1,Node t2){
if(t1.dat==mp(,))return t2;
if(t2.dat==mp(,))return t1;
Node ret;
ret.d=t1.d+t2.d;
ret.pre_dat=t1.pre_dat,ret.pre_sum=t1.pre_sum;
if(ret.pre_sum<t1.d+t2.pre_sum){
ret.pre_dat=t2.pre_dat,ret.pre_sum=t1.d+t2.pre_sum;
}
ret.suf_dat=t2.suf_dat,ret.suf_sum=t2.suf_sum;
if(ret.suf_sum<=t2.d+t1.suf_sum){
ret.suf_dat=t1.suf_dat,ret.suf_sum=t2.d+t1.suf_sum;
}
ret.dat=t1.dat,ret.sum=t1.sum;
if(ret.sum<t2.sum){
ret.dat=t2.dat,ret.sum=t2.sum;
}
if(ret.sum<(t1.suf_sum+t2.pre_sum)||ret.sum==(t1.suf_sum+t2.pre_sum)&&ret.dat>mp(t1.suf_dat,t2.pre_dat)){
ret.dat=mp(t1.suf_dat,t2.pre_dat),
ret.sum=(t1.suf_sum+t2.pre_sum);
}
return ret;
}
void build(int k,int L,int R){
if(L+==R){
data[k].pre_dat=data[k].suf_dat=L;
data[k].dat=mp(L,L);
data[k].d=data[k].pre_sum=data[k].suf_sum=data[k].sum=a[L];
return;
}
build(k<<,L,(L+R)>>);
build(k<<|,(L+R)>>,R);
data[k]=Merge(data[k<<],data[k<<|]);
}
Node query(int a,int b,int k,int L,int R){
if(b<=L||R<=a){
return Node(,,,,mp(,),,);
}
else if(a<=L&&R<=b){
return data[k];
}
else{
int mid=((L+R)>>);
Node lc=query(a,b,k<<,L,mid);
Node rc=query(a,b,k<<|,mid,R);
return Merge(lc,rc);
}
}
void solve(){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
build(,,n+);
while(m--){
int x,y;
scanf("%d%d",&x,&y);
Node ans=query(x,y+,,,n+);
printf("%d %d\n",ans.dat.first,ans.dat.second);
}
}
int main()
{
// freopen("data.in","r",stdin);
// freopen("my.out","w",stdout);
int T=;
while(~scanf("%d%d",&n,&m)){
printf("Case %d:\n",++T);
solve();
}
return ;
}
UVALive - 3938:"Ray, Pass me the dishes!"的更多相关文章
- UvaLA 3938 "Ray, Pass me the dishes!"
"Ray, Pass me the dishes!" Time Limit: 3000MS Memory Limit: Unkn ...
- UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)
"Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...
- UVA 1400 1400 - "Ray, Pass me the dishes!"(线段树)
UVA 1400 - "Ray, Pass me the dishes!" option=com_onlinejudge&Itemid=8&page=show_pr ...
- 【LA3938】"Ray, Pass me the dishes!"
原题链接 Description After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hun ...
- uvalive 3938 "Ray, Pass me the dishes!" 线段树 区间合并
题意:求q次询问的静态区间连续最大和起始位置和终止位置 输出字典序最小的解. 思路:刘汝佳白书 每个节点维护三个值 pre, sub, suf 最大的前缀和, 连续和, 后缀和 然后这个题还要记录解的 ...
- UVALive 3938 - "Ray, Pass me the dishes!" - [最大连续子列和+线段树]
题目链接:https://cn.vjudge.net/problem/UVALive-3938 参考刘汝佳书上说的: 题意: 给出一个长度为n的序列, 再给出m个询问, 每个询问是在序列 $[a,b] ...
- UVALive 3938 Ray, Pass me the dishes! (动态最大连续和)
题意:求一个动态区间的最大连续和. 静态版本的O(n)算法显示不适用了,但是可以用线段树分治,因为一个连续和要在两边的区间,要么跨越两边,对于一个结点维护最大前缀和,后缀和,子区间连续和. 题目要求输 ...
- 线段树(区间合并) LA 3989 "Ray, Pass me the dishes!"
题目传送门 题意:动态最大连续子序列和,静态的题目 分析:nlogn的归并思想.线段树维护结点的三个信息,最大前缀和,最大后缀和,该区间的最大和的两个端点,然后答案是三个的better.书上用pair ...
- UVa 1400 (线段树) "Ray, Pass me the dishes!"
求一个区间的最大连续子序列,基本想法就是分治,这段子序列可能在区间的左半边,也可能在区间的右半边,也有可能是横跨区间中点,这样就是左子区间的最大后缀加上右子区间的最大前缀之和. 线段树维护三个信息:区 ...
随机推荐
- 课堂测试ch06
课堂测试ch06 下面代码中,对数组x填充后,采用直接映射高速缓存,所有对x和y引用的命中率为(D) A. 1 B. 1/4 C. 1/2 D. 3/4 解析:在填充了之后,对于x和y数组,只有在引用 ...
- Java Collections API和泛型
Java Collections API和泛型 数据结构和算法 学会一门编程语言,你可以写出一些可以工作的代码用计算机来解决一些问题,然而想要优雅而高效的解决问题,就要学习数据结构和算法了.当然对数据 ...
- $(function(){})和window.onload的区别
(1)$(function(){}):DOM节点创建 完成才执行 (2)window.onload:页面所有资源(JS/CSS)加载完成才执行
- Mybatis学习日志
在Mybatis深入学习的一周中,总感觉跟着师傅的视屏讲解什么都能懂,但实际自己操作的时候才发现自己一脸懵逼,不知道从何入手.但还好自己做了点笔记.在此记录一下自己浅度学习Mybatis遇到几个小问题 ...
- Microsoft Soft SQL Server 大数据----分区表性能测试
分区表 MSSQL有一个大数据储存方案,可以提高效率那就是分区表. 使用起来跟普通表没有区别.至于具体原理自己度娘吧. 真正性能的提高,是依赖于硬件的加入.也是就说,当把一个表设置成分区表,每一个分区 ...
- 微信开发之SVN提交代码与FTP同步到apache的根目录
SVN是协同开发的,版本控制器,就是几个人同时开发,可以提交代码到SVN服务器,这样就可以协同开发,一般是早上上班首先更新下代码,然后自己修改代码 工作一天之后,修改代码之后,下班之前,更新代码,然后 ...
- Python内置函数(41)——id
英文文档: id(object) Return the "identity" of an object. This is an integer which is guarantee ...
- maven入门(1-1)maven是什么?
Maven是一个项目管理工具,它包含了 一个项目对象模型 (Project Object Model), 一组标准集合, 一个项目生命周期(Project Lifecycle), 一个依赖管理系统(D ...
- maven快速下载jar镜像
<!--国内镜像--><mirror> <id>CN</id> <name>OSChina Central</name> ...
- vue computed 原理
vue computed 主要依靠数据依赖来更新,这里不展示computed源代码,只展示核心思想. computed: { a(){ return this.b ++ } } data:{ b: 1 ...