uva 1400 - "Ray, Pass me the dishes!"
又是一道线段树区间更新的题;
#include<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
#define maxn 500005
using namespace std;
ll sum[maxn];
struct tree
{
int l,r;
int ml,mr;
int pre,suf;
tree *left,*right;
} tr[maxn*]; int trcount; void build(tree *root,int l,int r)
{
root->l=l;
root->r=r;
if(l==r)
{
root->ml=l;
root->mr=r;
root->pre=l;
root->suf=r;
return;
}
trcount++;
root->left=tr+trcount;
trcount++;
root->right=tr+trcount;
int mid=(l+r)/;
build(root->left,l,mid);
build(root->right,mid+,r); //update the pre
if((sum[root->left->pre]-sum[root->l-])>=(sum[root->right->pre]-sum[root->l-]))
root->pre=root->left->pre;
else root->pre=root->right->pre;
//update the suf
if((sum[root->r]-sum[root->left->suf-])>=(sum[root->r]-sum[root->right->suf-]))
root->suf=root->left->suf;
else root->suf=root->right->suf;
//update the max
if((sum[root->left->mr]-sum[root->left->ml-])>=(sum[root->right->mr]-sum[root->right->ml-]))
{
root->ml=root->left->ml;
root->mr=root->left->mr;
}
else
{
root->mr=root->right->mr;
root->ml=root->right->ml;
}
//update the max
if((sum[root->mr]-sum[root->ml-])<(sum[root->right->pre]-sum[root->left->suf-]))
{
root->mr=root->right->pre;
root->ml=root->left->suf;
}
else if((sum[root->mr]-sum[root->ml-])==(sum[root->right->pre]-sum[root->left->suf-]))
{
if(root->left->suf<root->ml||(root->left->suf==root->ml&&root->right->pre<root->mr))
{
root->mr=root->right->pre;
root->ml=root->left->suf;
}
}
} void query(tree *root,int ql,int qr,int &x,int &y,int &ansl,int &ansr)
{
if((ql<=root->l)&&(root->r<=qr))
{
x=root->ml;
y=root->mr;
ansl=root->pre;
ansr=root->suf;
return;
}
int mid=(root->r+root->l)>>;
if(qr<=mid)query(root->left,ql,qr,x,y,ansl,ansr);
else if(ql>=mid+)query(root->right,ql,qr,x,y,ansl,ansr);
else
{
int x1,x2,y1,y2,pre1,pre2,suf1,suf2; query(root->left,ql,mid,x1,y1,pre1,suf1);
query(root->right,mid+,qr,x2,y2,pre2,suf2); ansl=(sum[pre1]-sum[root->l-])>=(sum[pre2]-sum[root->l-])?pre1:pre2;
ansr=(sum[root->r]-sum[suf1-])>=(sum[root->r]-sum[suf2-])?suf1:suf2; if((sum[y1]-sum[x1-])>=(sum[y2]-sum[x2-]))
{
x= x1;
y= y1;
}
else
{
x= x2;
y= y2;
}
if((sum[pre2]-sum[suf1-])>(sum[y]-sum[x-]))
{
x= suf1;
y= pre2;
}
else if((sum[pre2]-sum[suf1-])==(sum[y]-sum[x-]))
{
if((suf1<x)||((suf1==x)&&(pre2<y)))
{
x = suf1;
y = pre2;
}
}
}
} int main()
{
int n,q,ca=;
while(scanf("%d%d",&n,&q)!=EOF)
{
ll x;
trcount=;
memset(sum,,sizeof sum);
for(int i=; i<=n; i++)
{
scanf("%lld",&x);
sum[i]=sum[i-]+x;
}
build(tr,,n);
int a,b,ans1,ans2,xx,yy;
printf("Case %d:\n",ca++);
while(q--)
{
scanf("%d%d",&a,&b);
query(tr,a,b,xx,yy,ans1,ans2);
printf("%d %d\n",xx,yy);
}
}
return ;
}
uva 1400 - "Ray, Pass me the dishes!"的更多相关文章
- UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)
"Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...
- uva 1400 "Ray, Pass me the dishes!" (区间合并 最大子段和+输出左右边界)
题目链接:https://vjudge.net/problem/UVA-1400 题意:给一串序列,求最大子段,如果有多个,输出字典序最小的那个的左右端点 思路: 之前写过类似的,这个麻烦点需要输出左 ...
- 1400 - "Ray, Pass me the dishes!"
哈哈,原来题意看错了,但有多个解的时候,输出起点靠前的,如果起点一样,则输出终点靠前的,修改后AC的代码如下: #include <cstdio> #include <iostrea ...
- UVA 1400 1400 - "Ray, Pass me the dishes!"(线段树)
UVA 1400 - "Ray, Pass me the dishes!" option=com_onlinejudge&Itemid=8&page=show_pr ...
- UvaLA 3938 "Ray, Pass me the dishes!"
"Ray, Pass me the dishes!" Time Limit: 3000MS Memory Limit: Unkn ...
- 【LA3938】"Ray, Pass me the dishes!"
原题链接 Description After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hun ...
- UVa 1400 (线段树) "Ray, Pass me the dishes!"
求一个区间的最大连续子序列,基本想法就是分治,这段子序列可能在区间的左半边,也可能在区间的右半边,也有可能是横跨区间中点,这样就是左子区间的最大后缀加上右子区间的最大前缀之和. 线段树维护三个信息:区 ...
- 线段树(区间合并) LA 3989 "Ray, Pass me the dishes!"
题目传送门 题意:动态最大连续子序列和,静态的题目 分析:nlogn的归并思想.线段树维护结点的三个信息,最大前缀和,最大后缀和,该区间的最大和的两个端点,然后答案是三个的better.书上用pair ...
- uvalive 3938 "Ray, Pass me the dishes!" 线段树 区间合并
题意:求q次询问的静态区间连续最大和起始位置和终止位置 输出字典序最小的解. 思路:刘汝佳白书 每个节点维护三个值 pre, sub, suf 最大的前缀和, 连续和, 后缀和 然后这个题还要记录解的 ...
随机推荐
- Oracle错误代码案例总结及解决方案
引自:http://blog.sina.com.cn/s/blog_9daa54ec0100yr7v.html 常见错误: ORA-00001:违反唯一约束条件(主键错误) ORA-00028:无法连 ...
- MySQL5.7.12新密码登录方式及密码策略
在Centos6.6上安装MySQL5.7.12时,遇到了一个问题 安装后在/root目录下没有发现有.mysql_secret这个文件,所以没有没法按照官方文档上说的那样使用,这里记录下, 解决方式 ...
- react 编写组件 五
看以下示例了解如何定义一个组件 // 定义一个组件LikeButton var LikeButton = React.createClass({ // 给state定义初始值 getInitialSt ...
- SQL Server 2008 Values 新用途
SQL Server 2008中新增功能:可以使用单个Insert命令插入多行. Create table Demo_Values (PKID int not null identity(1,1) p ...
- C# DataTable转List And List转DataTable
// DataTable转List: IList<HousesEntity> Ilist = TableAndList.ConvertTo<HousesEntity>(dt); ...
- SQL Server内连接、外连接、交叉连接
前言 在数据库查询中,我们常常会用到的表连接查询,而我自己在工作中也是时常用这些表连接查询.而就在刚刚我却还没有搞清楚数据库表连接到底有哪几种, 这几种表连接查询方式又有什么区别,实属惭愧!借以此文以 ...
- git研究1
error:src refspec master does not match any 将本地GIT版本库PUSH到一个GITHUB上一个空的版本库时出现错误,本地版本库为空, 空目录不能提交 (只 ...
- MVC中实现部分内容异步加载
MVC中实现部分内容异步加载 action中定义一个得到结果集的方法 public ActionResult GetItemTree(string title, int itemid, int? pa ...
- Spring Mvc 笔记二之异常和文件上传
spring mvc的异常与文件上传 1.异常: spring注解版的异常有局部异常和全局异常 1.局部异常对单个controller有效;(在controller类写一 ...
- OpenCV(4)-图像掩码操作(卷积)--平滑处理
卷积定义 矩阵的掩码操作即对图像进行卷积.对图像卷积操作的意义为:邻近像素对(包括该像素自身)对新像素的影响:影响大小取决于卷积核对应位置值得大小. 例如:图像增强可以使用 \[ I(i,j)=5*I ...