"Ray, Pass me the dishes!"

UVA - 1400

题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点。要求字典序最小,要求左右端点尽量靠左。

比如:

3 3 3 -9 3 3 3

输出的是1 3,而不是1 7

3 3 3 -9 3 3 4

输出的是1 7,而不是5 7

大体意思就是这样。

有一个坑,我没读好题,输出Case是每一组样例输出一个,而不是每查询一次就输出一次Case。。。

其他没什么了。具体代码写了注释。

代码:

 //线段树-线段树区间合并-判断位置
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 struct Tree{
ll pre,suf,sub,val;//pre最大前缀和,suf最大后缀和,sub最大子段和,val当前区间和
int lch,rch,lx,rx;//最大子段的左端点,最大子段的右端点,最大前缀和的右端点,最大后缀和的左端点
}tree[maxn<<]; Tree pushup(Tree l,Tree r)
{
Tree rt;
// rt.pre=max(l.pre,l.val+r.pre);
// rt.suf=max(r.suf,r.val+l.suf);
// rt.sub=max(max(l.sub,r.sub),l.suf+r.pre);
//根据上面的操作进行变形
if(l.pre>=l.val+r.pre){rt.pre=l.pre;rt.lx=l.lx;}//前缀和
else{rt.pre=l.val+r.pre;rt.lx=r.lx;}
if(r.val+l.suf>=r.suf){rt.suf=r.val+l.suf;rt.rx=l.rx;}//后缀和
else{rt.suf=r.suf;rt.rx=r.rx;}
if(l.sub>=r.sub&&l.sub>=(l.suf+r.pre)){rt.sub=l.sub;rt.lch=l.lch;rt.rch=l.rch;}//子段和
else if((l.suf+r.pre>=l.sub)&&(l.suf+r.pre>=r.sub)){rt.sub=l.suf+r.pre;rt.lch=l.rx;rt.rch=r.lx;}
else{rt.sub=r.sub;rt.lch=r.lch;rt.rch=r.rch;}
rt.val=l.val+r.val;
return rt;
} void build(int l,int r,int rt)
{
if(l==r){
scanf("%lld",&tree[rt].val);
tree[rt].pre=tree[rt].suf=tree[rt].sub=tree[rt].val;
tree[rt].lch=tree[rt].rch=tree[rt].lx=tree[rt].rx=l;
return ;
} int m=(l+r)>>;
build(lson);
build(rson);
tree[rt]=pushup(tree[rt<<],tree[rt<<|]);
} Tree query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R){
return tree[rt];
} int m=(l+r)>>;
Tree ret,lret,rret;
int flag1=,flag2=;
if(L<=m) {lret=query(L,R,lson);flag1=;}
if(R> m) {rret=query(L,R,rson);flag2=;} if(flag1&&flag2) ret=pushup(lret,rret);
else if(flag1) ret=lret;
else if(flag2) ret=rret;
return ret;
} int main()
{
int n,m,cas=;
while(~scanf("%d%d",&n,&m)){
printf("Case %d:\n",++cas);
build(,n,);
for(int i=;i<=m;i++){
int l,r;
scanf("%d%d",&l,&r);
Tree ans=query(l,r,,n,);
printf("%d %d\n",ans.lch,ans.rch);
}
}
}

开溜。

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

  1. 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 ...

  2. uva 1400 - "Ray, Pass me the dishes!"

    又是一道线段树区间更新的题: #include<cstdio> #include<algorithm> #include<cstring> #define ll l ...

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

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

  4. UVALive3938 &quot;Ray, Pass me the dishes!&quot; 线段树动态区间最大和

    AC得相当辛苦的一道题.似乎不难,可是须要想细致, 開始的时候的错误思路----是受之前做过的区间最长连续子串影响http://blog.csdn.net/u011026968/article/det ...

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

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

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

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

  7. UVALive 7148 LRIP【树分治+线段树】

    题意就是要求一棵树上的最长不下降序列,同时不下降序列的最小值与最大值不超过D. 做法是树分治+线段树,假设树根是x,y是其当前需要处理的子树,对于子树y,需要处理出两个数组MN,MX,MN[i]表示以 ...

  8. [IOI2018]会议——分治+线段树

    题目链接: [IOI2018]meetings 题目大意:有$n$座山峰,每座山峰有一个高度,有$q$次询问,每次需要确定一个开会山峰使$[l,r]$所有山峰上的人都前往开会山峰,一个山峰的人去开会的 ...

  9. 【loj6145】「2017 山东三轮集训 Day7」Easy 动态点分治+线段树

    题目描述 给你一棵 $n$ 个点的树,边有边权.$m$ 次询问,每次给出 $l$ .$r$ .$x$ ,求 $\text{Min}_{i=l}^r\text{dis}(i,x)$ . $n,m\le ...

随机推荐

  1. 跟我一起写Makefile(四)

    书写命令———— 每条规则中的命令和操作系统Shell的命令行是一致的.make会一按顺序一条一条的执行命令,每条命令的开头必须以[Tab]键开头,除非,命令是紧跟在依赖规则后面的分号后的.在命令行之 ...

  2. CSS居中之美

    关于居中,你会想到什么? div{margin: auto;} 常见的居中方法 水平居中 .demo{ text-align: center; margin: auto; position: abso ...

  3. 说一说ASP.NET web.config 加密及解密方法 (代码)

    /// <summary> /// 保护web.config的加密和解密 /// </summary> public class ProtectHelper { /// < ...

  4. iOS静态库 ---iOS-Apple苹果官方文档翻译

    iOS静态库 ---iOS-Apple苹果官方文档翻译 •什么是库? 库是共享程序代码的方式,一般分为静态库和动态库.静态库与动态库的区别? 静态库:链接时完整地拷贝至可执行文件中,被多次使⽤用就为什 ...

  5. 03.WebView演练-iOS开发Demo(示例程序)源代码

    技术博客http://www.cnblogs.com/ChenYilong/   新浪微博http://weibo.com/luohanchenyilong   //转载请注明出处--本文永久链接:h ...

  6. styled-components真的好吗?

    最近在学习react,然后遇到react中css该怎么写这个问题,上知乎上看了好多大牛都说styled-components好用是大势所趋. 但我自己用了感觉体验却很差,我在这里说说我为啥觉得styl ...

  7. BeanPostProcessor的五大接口

    BeanPostProcessor 关于对象初始化前后的回调. public interface BeanPostProcessor { //该方法在bean实例化完毕(且已经注入完毕),在after ...

  8. Mysql中的primary key 与auto_increment

    mysql> create table cc(id int auto_increment); ERROR (): Incorrect table definition; there can be ...

  9. Java Eclipse 配置

    1.清除多余记录 最近用eclipse打包jar的时候,需要指定一个main函数.需要先运行一下main函数,eclipse的Runnable JAR File Specification 下的Lau ...

  10. Oracle创建WM_CONCAT函数

    Oracle创建WM_CONCAT函数 WM_CONCAT这个函数会出错,所以从 11g开始.官方不认可 WM_CONCAT.然后就没这个函数了, 下面就是创建WM_CONCAT这个函数的步骤 第一步 ...