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

问题描述

LG-SP1043


题解

GSS 系列第一题。

\(q\) 个询问,求 \([x,y]\) 的最大字段和。

线段树,维护 \([x,y]\) 的 \(lmax,rmax,sum,val\) ,向上合并即可。

但是注意询问过程中也需要维护这些信息。


\(\mathrm{Code}\)

#include<bits/stdc++.h>
using namespace std;

template <typename Tp>
void read(Tp &x){
    x=0;char ch=1;int fh;
    while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
    if(ch=='-') ch=getchar(),fh=-1;
    else fh=1;
    while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    x*=fh;
}

const int maxn=50007;

#define lfc (x<<1)
#define rgc ((x<<1)|1)
#define mid ((l+r)>>1)
int n;
int val[maxn<<2],lf[maxn<<2],rg[maxn<<2];
int sum[maxn<<2];
int a[maxn];

void pushup(int x){
    sum[x]=sum[lfc]+sum[rgc];
    lf[x]=max(lf[lfc],sum[lfc]+lf[rgc]);
    rg[x]=max(rg[rgc],sum[rgc]+rg[lfc]);
    val[x]=max(max(val[lfc],val[rgc]),rg[lfc]+lf[rgc]);
}

void build(int x,int l,int r){
    if(l==r){
        sum[x]=val[x]=lf[x]=rg[x]=a[l];
        return;
    }
    build(lfc,l,mid);build(rgc,mid+1,r);
    pushup(x);
}

const int INF=0x3f3f3f3f;

int L,R;

struct node{
    int val,lf,rg,sum;
};

node query(int x,int l,int r){
    if(L<=l&&r<=R) return (node){val[x],lf[x],rg[x],sum[x]};
    if(L>mid) return query(rgc,mid+1,r);
    if(R<=mid) return query(lfc,l,mid);
    node res,s1=query(lfc,l,mid),s2=query(rgc,mid+1,r);
    res.sum=s1.sum+s2.sum;
    res.val=max(max(s1.val,s2.val),s1.rg+s2.lf);
    res.lf=max(s1.lf,s1.sum+s2.lf);
    res.rg=max(s2.rg,s2.sum+s1.rg);
    return res;
}

int main(){
    read(n);
    for(int i=1;i<=n;i++) read(a[i]);
    build(1,1,n);
    int T;read(T);
    while(T--){
        read(L);read(R);
        printf("%d\n",query(1,1,n).val);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/liubainian/p/11775114.html

时间: 2024-10-29 16:54:24

SP1043 GSS1 - Can you answer these queries I 线段树的相关文章

SP1716 GSS3 - Can you answer these queries III 线段树

题目传送门:SP1043 GSS1 - Can you answer these queries I 更好的阅读体验 动态维护子段和最大值 前置知识 静态维护子段和最大值:SP1043 GSS1 - Can you answer these queries I 题解传送 题解: 提供结构体指针线段树写法: 设\(l\)为区间左端点, \(r\)为区间右端点: \(ls\)为以\(l\)为左端点的最大子段和, \(rs\)为以\(r\)为右端点的最大子段和; \(sum\)为区间和, \(val\

hdu4027 Can you answer these queries?(线段树平方减少,区间求和)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 Problem Description A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the bat

SPOJ GSS3 Can you answer these queries III (线段树)

题目大意: 求区间最大子区间的和. 思路分析: 记录左最大,右最大,区间最大. 注意Q_L  和 Q_R  就好. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define lson num<<1,s,mid #define rson num<<1|1,mid+1,e #define maxn 55555 using na

SPOJ GSS4 Can you answer these queries IV (线段树)

题目大意: 给出N个数 0     操作   把 l -----  r之间的数全部开平方 1     操作  输出 l -----r  之间的和 思路分析: 判断区间里的数字是否全相同.如果相同, 将cov 置为该数 查询的时候和更新的时候,如果碰到cov != -1 的  就直接返回就可以了 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #incl

HDU 4027 Can you answer these queries?(线段树的单点更新+区间查询)

题目链接 题意 : 给你N个数,进行M次操作,0操作是将区间内的每一个数变成自己的平方根(整数),1操作是求区间和. 思路 :单点更新,区间查询,就是要注意在更新的时候要优化,要不然会超时,因为所有的数开几次方之后都会变成1,所以到了1不用没完没了的更新. 1 //HDU 4027 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <iostream> 6 #defi

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  Solved: 76[Submit][Status][Discuss] Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和(可选空子段). 这个最大子段和有点特殊:一个数字在一段中出现了两次只算一次. 比如:1,2,3,2,2,2出现了3次,但只算一次,

HDU 4027 Can you answer these queries(线段树 成段更新)

Problem Description A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of ou

HDU 4027 Can you answer these queries? (线段树区间求和)

Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 12290    Accepted Submission(s): 2912 Problem Description A lot of battleships of evil are arranged in a line before

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/problems/GSS2/ Description Being a completist and a simplist, kid Yang Zhe cannot solve but get Wrong Answer from most of the OI problems. And he refuse