COGS 775 山海经

思路:

求最大连续子段和(不能不选),只查询,无修改。要求输出该子段的起止位置。

线段树经典模型,每个节点记录权值和sum、左起最大前缀和lmax、右起最大后缀和rmax、最大子段和dat即可。

要求输出起止位置,单独维护左右端点。注意权值相同要求l、r尽量小,所以相同时选左儿子不选右儿子,维护时考虑是否加等号。

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
int n,m;
struct Tree{
int l,r,lid,rid,tl,tr;
ll sum,lmax,rmax,dat;
#define ls(p) (p<<1)
#define rs(p) (p<<1|1)
#define mid ((node[p].l+node[p].r)>>1)
}node[N<<2];
void upd(Tree &a,const Tree &b,const Tree &c){
a.sum=b.sum+c.sum;
if(b.sum+c.lmax>b.lmax){
a.lmax=b.sum+c.lmax;
a.tr=c.tr;
} else{
a.lmax=b.lmax;
a.tr=b.tr;
}
if(c.rmax>c.sum+b.rmax){
a.rmax=c.rmax;
a.tl=c.tl;
} else{
a.rmax=c.sum+b.rmax;
a.tl=b.tl;
}
if(c.dat>max(b.dat,b.rmax+c.lmax)){
a.dat=c.dat;
a.lid=c.lid;
a.rid=c.rid;
}
else{
if((b.dat>b.rmax+c.lmax)||(b.dat==b.rmax+c.lmax&&b.lid<=b.tl)){
a.dat=b.dat;
a.lid=b.lid;
a.rid=b.rid;
}
else{
a.dat=b.rmax+c.lmax;
a.lid=b.tl;
a.rid=c.tr;
}
}
}
void build(int p,int l,int r){
node[p].l=l;node[p].r=r;
if(l==r){
scanf("%lld",&node[p].sum);
node[p].dat=node[p].lmax=node[p].rmax=node[p].sum;
node[p].lid=node[p].rid=node[p].tl=node[p].tr=l;
return;
}
build(ls(p),l,mid);
build(rs(p),mid+1,r);
upd(node[p],node[ls(p)],node[rs(p)]);
}
Tree query(int p,int L,int R){
if(L<=node[p].l&&node[p].r<=R) return node[p];
Tree ans;
if(R<=mid) ans=query(ls(p),L,R);
else if(L>mid) ans=query(rs(p),L,R);
else upd(ans,query(ls(p),L,R),query(rs(p),L,R));
return ans;
}
int main(){
scanf("%d%d",&n,&m);
build(1,1,n);
for(int i=1,l,r;i<=m;++i){
scanf("%d%d",&l,&r);
Tree ans=query(1,l,r);
printf("%d %d %lld\n",ans.lid,ans.rid,ans.dat);
}
return 0;
}

COGS 775 山海经的更多相关文章

  1. 【cogs 775】山海经 ——Segment Tree

    题目链接:      TP 题解:   我数据结构真心是弱啊= =. 线段树好厉害啊,一直不会区间最大连续和,今天刚学习了一下233. 维护前缀最大和后缀最大,越界最大(?),再维护一个区间最大,瞎搞 ...

  2. [COGS 755]山海经:线段树

    网上似乎这道题的题解很少?写一个吧 我跟这道题的渊源追溯到了上个学期刚刚学线段树的那一天... 当时线段树专题前边的题都是一些板子就不一会就水过了,然后就看到了最后一题的它:山海经 那一个上午,我竭尽 ...

  3. 【COGS 254】【POI 2001】交通网络图

    http://www.cogs.top/cogs/problem/problem.php?pid=254 dist[i]表示能最早到达i点的时间.这样就可以用最短路模型来转移了. #include&l ...

  4. 【COGS】894. 追查坏牛奶

    http://cojs.tk/cogs/problem/problem.php?pid=894 题意:n个点m条边的加权网络,求最少边数的按编号字典序最小的最小割.(n<=32, m<=1 ...

  5. 【COGS】147. [USACO Jan08] 架设电话线(二分+spfa)

    http://cojs.tk/cogs/problem/problem.php?pid=147 学到新姿势了orz 这题求的是一条1-n的路径的最大路径最小. 当然是在k以外的. 我们可以转换一下. ...

  6. 【COGS & USACO Training】710. 命名那个数字(hash+水题+dfs)

    http://cojs.tk/cogs/problem/problem.php?pid=710 近日开始刷水... 此题我为了练一下hash...但是hash跑得比暴力还慢.. 不言而喻... #in ...

  7. 【COGS & USACO】896. 圈奶牛(凸包)

    http://cojs.tk/cogs/problem/problem.php?pid=896 我的计算几何入门题... 看了看白书的计算几何部分,,恩好嘛.. 乃们都用向量!!!! 干嘛非要将2个点 ...

  8. 【COGS】714. USACO 1.3.2混合牛奶(贪心+水题)

    http://cojs.tk/cogs/problem/problem.php?pid=714 在hzwer的刷题记录上,默默地先跳过2题T_T...求凸包和期望的..T_T那是个啥..得好好学习 看 ...

  9. Cogs 97. [NOIP2007] 树网的核 Floyd

    题目: http://cojs.tk/cogs/problem/problem.php?pid=97 97. [NOIP2007] 树网的核 ★☆   输入文件:core.in   输出文件:core ...

随机推荐

  1. Jdbc封装和对CURD的封装

    1.查询emp表中的所有记录为例 2.测试类 public Emp getByNameAndEmail(String name, String email){ String sql = "s ...

  2. TZ_11_Spring-Boot的属性注入方式(jdbc为例)

    1.以上一篇文档为基础 2.创建jdbc外部属性文件 application.properties此名字为默认文件名在使用是不需要使用 @Propertysource("classpath: ...

  3. [转]json对象详解

    json(javascript object notation)全称是javascript对象表示法,它是一种数据交换的文本格式,而不是一种编程语言,用于读取结构化数据.2001年由Douglas C ...

  4. ADO.NET实体数据模型

    本文说一下如何使用ADO.NET实体数据模型,并解释一些概念. 1,首先你要建立一个数据库.比如我在SQL2005上面建立了数据库student,包含两个表: 2,然后再项目上添加新建项: 3,打开新 ...

  5. Spring事务传播行为详解

    前言 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为.事务传播行为是Spring框架独有的事务增强特性,他不属于的事务实际提供方数据库行为.这是Spring ...

  6. geoserver与OpenLayers配置

          geoserver与OpenLayers配置         目录   1     准备工作.... 4 1.1      需要用到的程序和资料... 4 2     地图格式转换方式(一 ...

  7. OpenLayers使用symbolizers样式特征

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...

  8. datetime模块常用函数

    import datetime import time # 当前时间戳 now = time.time() print(now) # 时间戳转换成时间元祖 now = time.localtime(n ...

  9. 2017-09-23-ArchData

    layout: post title: ArchData category: Technical tags: [分布式,区块链,AI,大数据] ArchData 技术峰会 神经网络和函数式编程 杨博: ...

  10. sending data mysql slow Mysql查询非常慢的可能原因

    1.用explain看看mysql的执行情况,可以得知,task_id扫描了近20万条数据,而且这个task_id不是索引 2.为这个task_id所在的表,将此字段添加索引后,查询就变得很快了