又是一道线段树区间更新的题;

 #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!"的更多相关文章

  1. UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)

    "Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...

  2. uva 1400 "Ray, Pass me the dishes!" (区间合并 最大子段和+输出左右边界)

    题目链接:https://vjudge.net/problem/UVA-1400 题意:给一串序列,求最大子段,如果有多个,输出字典序最小的那个的左右端点 思路: 之前写过类似的,这个麻烦点需要输出左 ...

  3. 1400 - "Ray, Pass me the dishes!"

    哈哈,原来题意看错了,但有多个解的时候,输出起点靠前的,如果起点一样,则输出终点靠前的,修改后AC的代码如下: #include <cstdio> #include <iostrea ...

  4. UVA 1400 1400 - &quot;Ray, Pass me the dishes!&quot;(线段树)

    UVA 1400 - "Ray, Pass me the dishes!" option=com_onlinejudge&Itemid=8&page=show_pr ...

  5. UvaLA 3938 "Ray, Pass me the dishes!"

                            "Ray, Pass me the dishes!" Time Limit: 3000MS   Memory Limit: Unkn ...

  6. 【LA3938】"Ray, Pass me the dishes!"

    原题链接 Description After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hun ...

  7. UVa 1400 (线段树) "Ray, Pass me the dishes!"

    求一个区间的最大连续子序列,基本想法就是分治,这段子序列可能在区间的左半边,也可能在区间的右半边,也有可能是横跨区间中点,这样就是左子区间的最大后缀加上右子区间的最大前缀之和. 线段树维护三个信息:区 ...

  8. 线段树(区间合并) LA 3989 "Ray, Pass me the dishes!"

    题目传送门 题意:动态最大连续子序列和,静态的题目 分析:nlogn的归并思想.线段树维护结点的三个信息,最大前缀和,最大后缀和,该区间的最大和的两个端点,然后答案是三个的better.书上用pair ...

  9. uvalive 3938 "Ray, Pass me the dishes!" 线段树 区间合并

    题意:求q次询问的静态区间连续最大和起始位置和终止位置 输出字典序最小的解. 思路:刘汝佳白书 每个节点维护三个值 pre, sub, suf 最大的前缀和, 连续和, 后缀和 然后这个题还要记录解的 ...

随机推荐

  1. .Net设计模式_工厂模式(2)

    2.工厂方法模式 引言: 上一篇中我们描述了简单工厂的缺点,而解决方法就是把工厂接口化,把工厂的行为标准化,这就是工厂方法模式. 理解: 工厂能制造A和B鞋,如果消费者现在要求制造C鞋.D鞋...咋办 ...

  2. 功能测试中遇到的一些有意思的bug

    2016.1.25 1.  Xss攻击型的bug Xss攻击即跨站脚步攻击,通过插入恶意脚本 ,实现对用户浏览器的控制. Bug现象:新增物品时,物品名称输入一段JavaScript代码,在提交时此代 ...

  3. 查看kindle paperwhite2上卡索引书籍的方法

    昨天kindle耗电量突然加快,经过检查和网络搜索得知是卡索引导致的耗电量增大.我自己通过关闭索引的方式解决了这个问题. 在这个过程中发现了一个可以直接找到所有卡索引书籍的方法,在此分享一下. 首先打 ...

  4. 练习PopupWindow弹出框之实现界面加载的时候显示弹出框到指定的view下面--两种延迟方法

    今天在练习PopupWindow弹出框的时候,打算在界面加载的时候将弹出框展现出来并显示在指定的view下面. 初步方法是直接在OnResume方法里面直接执行showPopupWindows方法. ...

  5. AJAX与servlet的信息交互

    <%@ page language="java" import="java.util.*" pageEncoding="gb2312" ...

  6. OS X EL Capitan安装Cocoapods 报错ERROR

    升级OS X EL Capitan10.11之后,原来的pod不能用了,重新安装cocoapods,发现 在运行 “sudo gem install cocoapods” 的时候出现问题: ERROR ...

  7. javascript 将多维数组转换为一维数组

    /** * 2013年9月去面试的时候,有面试过这样子一道题目: * 题目是这样子的:将一个多维数组转换成一维数组并返回该数组,类似 * [1,2,3,[4,5,6,[7,8]],9]转换后为:[1, ...

  8. myeclipse、eclipse去掉无用的workSpace

    在 eclipse\configuration\.settings\org.eclipse.ui.ide.prefs 文件里面有下面这段,我们可以从 RECENT_WORKSPACES 里面看到它列出 ...

  9. Erlang官方站点

    YOUR ERLANG COMMUNITY SITE Welcome to erlangcentral.org, the Erlang community site where you can rea ...

  10. c++primer复习(一)

    1 const对象默认为文件的局部变量(P50) a.cpp ; b.cpp extern int a;//undefined reference to "a" a.cpp ; b ...