线段树 区间合并

sum表示节点的和

max表示节点的最大值

lsum表示节点从左边开始的最大值

rsum表示节点从右边开始的最大值

You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A query is defined as follows: 
Query(x,y) = Max { a[i]+a[i+1]+...+a[j] ; x ≤ i ≤ j ≤ y }. 
Given M queries, your program must output the results of these queries.

Input

  • The first line of the input file contains the integer N.
  • In the second line, N numbers follow.
  • The third line contains the integer M.
  • M lines follow, where line i contains 2 numbers xi and yi.

Output

  • Your program should output the results of the M queries, one query per line.

Example

Input: 3 
-1 2 3
1
1 2 Output: 2
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <string.h>
#include <string>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <deque>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MAXN 500000 + 100
int sum[MAXN<<2],lsum[MAXN<<2],rsum[MAXN<<2],maxx[MAXN<<2];
int num[MAXN];

int Max(int a,int b){
    return a>b?a:b;
}

void PushUp(int rt){
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
    lsum[rt] = Max(lsum[rt<<1],sum[rt<<1]+lsum[rt<<1|1]);
    rsum[rt] = Max(rsum[rt<<1|1],sum[rt<<1|1]+rsum[rt<<1]);

    maxx[rt] = Max(maxx[rt<<1],maxx[rt<<1|1]);
    maxx[rt] = Max(maxx[rt],lsum[rt]);
    maxx[rt] = Max(maxx[rt],rsum[rt]);
    maxx[rt] = Max(maxx[rt],rsum[rt<<1]+lsum[rt<<1|1]);
}

void build(int l,int r,int rt){
    if(l == r){
        sum[rt] = lsum[rt] = rsum[rt] = maxx[rt] = num[l];
        return ;
    }
    int m = (l+r)>>1;
    build(lson);
    build(rson);
    PushUp(rt);
}

int query_l(int L,int R,int l,int r,int rt){
    if(L==l && r==R){
        return rsum[rt];
    }
    int m = (l+r)>>1;
    if(L > m){
        return query_l(L,R,rson);
    }
    else{
        int s = rsum[rt<<1|1];
        int ls = query_l(L,m,lson)+sum[rt<<1|1];//sum[rt<<1+1]+rsum[rt<<1];
        return Max(s,ls);
    }
}

int query_r(int L,int R,int l,int r,int rt){
    if(L==l && r==R){
        return lsum[rt];
    }
    int m = (l+r)>>1;
    if(R <= m){
        return query_r(L,R,lson);
    }
    else{
        int s = lsum[rt<<1];
        int ls = sum[rt<<1]+query_r(m+1,R,rson);//sum[rt<<1]+lsum[rt<<1|1]
        return Max(s,ls);
    }
}

int query(int L,int R,int l,int r,int rt){
    if(L==l && r==R){
        return maxx[rt];
    }
    int m = (l+r)>>1;
    if(m >= R){//等于号位置变了下都超时。。。。。。(这样查询的时间可能是最快的把)
        return query(L,R,lson);
    }
    else if(L > m){
        return query(L,R,rson);
    }
    else{
        int ls = query(L,m,lson);
        int rs = query(m+1,R,rson);
        int ms = query_l(L,m,lson)+query_r(m+1,R,rson);//rsum[rt<<1]+lsum[rt<<1|1];
        return Max(Max(ls,rs),ms);
    }
}

int main(){
    int n,m,a,b;
    int i;

    while(~scanf("%d",&n)){
        for(i=1;i<=n;i++){
            scanf("%d",&num[i]);
        }
        build(1,n,1);
        scanf("%d",&m);
        while(m--){
            scanf("%d%d",&a,&b);
            printf("%d\n",query(a,b,1,n,1));
        }
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值