"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. git相关网址

    git入门教程: 廖雪峰的官方网站 https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b00 ...

  2. 使用git拉取github上的项目

    一. 安装Git 去Git官网,下载安装包,一路点next,默认安装. 安装之后,在空白处右键,菜单显示有 Git GUI Here 和 Git Bash Here ,表示Git安装成功. 二. 配置 ...

  3. react UI组件库 Salt UI

    https://salt-ui.github.io/?spm=a219a.7629140.0.0.JWztQO

  4. GYM 101128 G.Game of Cards(博弈论) 或者 UVALIVE 7278

    题目链接:http://codeforces.com/gym/101128/my 如果可以,就看这个人的代码吧,我还不是很懂唉:http://blog.csdn.net/loy_184548/arti ...

  5. 817C. Really Big Numbers 二分

    LINK 题意:给出两个数n, s,要求问1~n中\(x-bit(x)>=s\)的数有多少个.其中bit(x)指x的各位数之和 思路:首先观察能够发现,对于一个数如果满足了条件,由于x-bit( ...

  6. Python学习笔记(四十九)爬虫的自我修养(一)

    论一只爬虫的自我修养 URL的一般格式(带括号[]的为可选项): protocol://hostname[:port]/path/[;parameters][?query]#fragment URL由 ...

  7. 【CODEVS】1034 家园

    [算法]网络流-最大流(dinic) [题解] 飞船有可承载人数限制,地球为源点,月球为汇点,人像水流一样从以飞船上限为容量的边流向汇点. 人在各站点都面临着上船与否的选择,难以用DP解决最优策略,于 ...

  8. 如何写出优雅的js以及js特殊技巧

    由于代码和解释都写在了github的readme内部,这里就直接附上github:https://github.com/jiangzhenfei/pretty-js/tree/master

  9. 安装 Google BBR 加速VPS网络

    Google BBR就是谷歌公司提出的一个开源TCP拥塞控制的算法.详情可以看这儿:https://lwn.net/Articles/701165.https://blog.sometimesnaiv ...

  10. 深入解析Mysql 主从同步延迟原理及解决方案

    MySQL的主从同步是一个很成熟的架构,优点为:①在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力;②在从主服务器进行备份,避免备份期间影响主服务器服务;③当主服务器出现问题时,可以 ...