前言

线段树菜鸡报告,stO ZCDHJ Orz,GSS基本上都切完了。

Solution

考虑一下用线段树维护一段区间左边连续的Max,右边的连续Max,中间的连续Max还有总和,发现这些东西可以相互合并,然后直接写就好了。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi(){
    int f=1,sum=0;char ch=getchar();
    while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    return f*sum;
}
const int N=50010;
int a[N];
struct node{
    int sum,ls,rs,ts;
}t[N<<4];
void pushup(int o){
    t[o].sum=t[o<<1].sum+t[o<<1|1].sum;
    t[o].ts=max(t[o<<1].ts,max(t[o<<1|1].ts,t[o<<1].rs+t[o<<1|1].ls));
    t[o].ls=max(t[o<<1].ls,t[o<<1].sum+t[o<<1|1].ls);
    t[o].rs=max(t[o<<1|1].rs,t[o<<1].rs+t[o<<1|1].sum);
}
void build(int o,int l,int r){
    if(l==r){t[o].ls=t[o].rs=t[o].ts=t[o].sum=a[l];return;}
    int mid=(l+r)>>1;
    build(o<<1,l,mid);build(o<<1|1,mid+1,r);
    pushup(o);
}
node query(int o,int l,int r,int posl,int posr){
    if(posl<=l && r<=posr)return t[o];
    int mid=(l+r)>>1;
    if(posl>mid)return query(o<<1|1,mid+1,r,posl,posr);
    if(posr<=mid)return query(o<<1,l,mid,posl,posr);
    else{
        node ans,a,b;
        a=query(o<<1,l,mid,posl,mid);b=query(o<<1|1,mid+1,r,mid+1,posr);
        ans.sum=a.sum+b.sum;
        ans.ts=max(a.ts,max(b.ts,a.rs+b.ls));
        ans.ls=max(a.ls,a.sum+b.ls);
        ans.rs=max(b.rs,a.rs+b.sum);
        return ans;
    }
}
int main(){
    int n=gi();
    for(int i=1;i<=n;i++)a[i]=gi();
    build(1,1,n);int m=gi();
    while(m--){
        int l=gi(),r=gi();
        printf("%d\n",query(1,1,n,l,r).ts);
    }
    return 0;
}

GSS1 - Can you answer these queries I(线段树)的更多相关文章

  1. SPOJ GSS1 - Can you answer these queries I(线段树维护GSS)

    Can you answer these queries I SPOJ - GSS1 You are given a sequence A[1], A[2], -, A[N] . ( |A[i]| ≤ ...

  2. SPOJ GSS1 Can you answer these queries I ——线段树

    [题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...

  3. SP1043 GSS1 - Can you answer these queries I 线段树

    问题描述 LG-SP1043 题解 GSS 系列第一题. \(q\) 个询问,求 \([x,y]\) 的最大字段和. 线段树,维护 \([x,y]\) 的 \(lmax,rmax,sum,val\) ...

  4. SPOJ GSS1 Can you answer these queries I[线段树]

    Description You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A q ...

  5. SPOJ GSS1_Can you answer these queries I(线段树区间合并)

    SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...

  6. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  7. GSS4 2713. Can you answer these queries IV 线段树

    GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...

  8. GSS5 spoj 2916. Can you answer these queries V 线段树

    gss5 Can you answer these queries V 给出数列a1...an,询问时给出: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[ ...

  9. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

  10. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

随机推荐

  1. spring中配置监听队列的MQ

    一.spring中配置监听队列的MQ相关信息注:${}是读取propertites文件的常量,这里忽略.绿色部分配置在接收和发送端都要配置.  <bean id="axx" ...

  2. 数据库日志文件——数据库“xxx”的事务日志已满,原因为“LOG_BACKUP”

    治标的方案: 参考网址:http://blog.csdn.net/tianlianchao1982/article/details/7388616 http://bbs.51cto.com/threa ...

  3. js如何切割字符串

    <script language="javascript"> str="2,2,3,5,6,6"; //这是一字符串 var strs= new A ...

  4. 求N的阶乘N!中末尾0的个数

    求N的阶乘N!中末尾0的个数 有道问题是这样的:给定一个正整数N,那么N的阶乘N!末尾中有多少个0呢?例如:N=10,N=3628800,则N!的末尾有两个0:直接上干货,算法思想如下:对于任意一个正 ...

  5. laravel的函数asset()、url()

    1.asset():用于引入静态文件,如 css/JavaScript/images,文件必须存放在public文件目录下 src="{{ asset('home') }}/images/t ...

  6. laravel的foreach

    1.控制器 2.模板

  7. sql转化为时间戳

    1.转化为时间戳 UNIX_TIMESTAMP():执行使用时间格式如:2009-08-06 10:10:40 .2009-08-06

  8. swift http post json + 登录

    var nsUrl : NSURL = NSURL(string:API_HOST+"/"+LOGIN_API)! var request = NSMutableURLReques ...

  9. 2018.10.01 NOIP模拟 购买书籍(贪心+STL)

    传送门 一道有意思的贪心. 感觉使用了网络流推流反悔的思想. 考虑维护三个setsetset维护a[i]−b[i],b[i]a[i]-b[i],b[i]a[i]−b[i],b[i]和a[i]a[i]a ...

  10. Exception: java.io.FileNotFoundException: D:\Users\liuyangOS2237\Workspaces\MyEclipse%2010\Zhuce\WebRoot\WEB-INF\classes\users.xml (系统找不到指定的路径。解决

    com.exception.DaoException: java.io.FileNotFoundException: D:\Users\liuyangOS2237\Workspaces\MyEclip ...