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 最大的前缀和, 连续和, 后缀和 然后这个题还要记录解的 ...
随机推荐
- FineUI登入的例子中遇到的一些问题
对于在使用FineUI这个例子的时候我们首先就是要在form标签内部添加一个 第一步. <ext:PageManager ID="PageManager1" runat=&q ...
- HttpClient4.4 进行Http连接通讯
以前一直使用jdk自带的urlConnection来进行http通讯,HttpClient与之相比,HttpClient更具有灵活度和易用性.HttpClient能够方便使用连接池,使用时需要重新创建 ...
- Python入门学习教程:数据库操作,连接MySql数据库
各位志同道合的同仁可以点击上方关注↑↑↑↑↑↑ 本教程致力于程序员快速掌握Python语言编程. 本文章内容是基于上次课程Python教程:Python教程:连接数据库,对数据进行增删改查操作 和py ...
- 数据搬运工DSS~介绍
DSS介绍 DSS是为了实现异地数据同步而开发的一套.net平台的应用程序,它寄宿到windows服务上,由多个客户端和一个服务端组成,其中客户端用来收集数据(数据源端),服务端用来将数据写入指定数据 ...
- Android中使用ViewPager实现广告条
我们在使用电商或视频的手机客户端时,通常会看到广告条的效果.在网上搜索时才知道使用的是ViewPager,于是自己也做了一个Demo. 以下是效果图: 图中包括背景图片.文字描述以及白点. 其中Vie ...
- JS 浮点型数字运算(转)
示例: var num1=3.3; var num2=7.17; var ret=parseFloat(num1)+parseFloat(num2); //ret的值为:10.469999999999 ...
- 2016.08.06计算几何总结测试day1
T1 bzoj1132[POI2008]TRO 还是太弱了....测试时看到这题直接懵逼,极角排序什么的根本想不起来,只会n^3暴力怎么破......不过竟然有84.....QAQ 正解是n^2log ...
- 2、Charm Bracelet( poj 3624)简单0-1背包
题意:有n件手镯,总重量不能超过M,每个手镯有一个体重W[i]和魅力V[i],问在不超过M的情况下能获得的魅力总和 思路:把M当背包总容量,用0-1背包写 代码: #include <iostr ...
- Mac OS 安装 Port
简介 MacPorts类似与apt-get以及yum等软件包管理工具,可以方便的进行安装与卸载软件的功能,同时可以自动安装软件包的依赖,非常方便,同类的还有brew等工具. 安装 下载MacPorts ...
- DataReader 和 DataSet 的区别
摘自:http://www.cnblogs.com/zhjjNo1/archive/2009/08/26/1554420.html 第一种解释 DataReader和DataSet最大的区别在于,Da ...