Can you answer these queries I SPOJ - GSS1 (线段树维护区间连续最大值/最大连续子段和)

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.

Sample Input
3
-1 2 3
1
1 2
Sample Output
2

题意:

裸题,没有更新,只有查询。

思路:

区间中维护一下值:

从左端点开始的连续的最大值lm

从右端点开始的连续最大值rm

区间的和sum。

区间中的连续最大值num。

那么更新操作为:

lm=max(左儿子的lm,左儿子的sum+右儿子的lm)

rm=max(右儿子的rm,右儿子的sum+左儿子的rm)

num=max(左儿子的num,右儿子的num,左儿子的rm+右儿子的lm)

询问操作也返回区间,对区间进行合并处理操作。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 50000+7;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct node
{
    int l;
    int r;
    ll num;
    ll lm;
    ll sum;
    ll rm;
}segment_tree[maxn<<2];
int n;
void pushup(int rt)
{
    segment_tree[rt].sum=segment_tree[rt<<1].sum+segment_tree[rt<<1|1].sum;
    segment_tree[rt].lm=max(segment_tree[rt<<1].lm,segment_tree[rt<<1].sum+segment_tree[rt<<1|1].lm);
    segment_tree[rt].rm=max(segment_tree[rt<<1|1].rm,segment_tree[rt<<1|1].sum+segment_tree[rt<<1].rm);
    segment_tree[rt].num=max(segment_tree[rt<<1].num,segment_tree[rt<<1|1].num);
    segment_tree[rt].num=max(segment_tree[rt].num,segment_tree[rt<<1].rm+segment_tree[rt<<1|1].lm);
}

void build(int rt,int l,int r)
{
    segment_tree[rt].l=l;
    segment_tree[rt].r=r;
    if(l==r)
    {
        scanf("%lld",&segment_tree[rt].num);
        segment_tree[rt].lm=segment_tree[rt].rm=segment_tree[rt].num;
        segment_tree[rt].sum=segment_tree[rt].num;
        return ;
    }
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    pushup(rt);
}

node ask(int rt,int l,int r)
{
    if(segment_tree[rt].l==l&&segment_tree[rt].r==r)
    {
        return segment_tree[rt];
    }
    int mid=(segment_tree[rt].r+segment_tree[rt].l)>>1;
    if(l>mid)
    {
        return ask(rt<<1|1,l,r);
    }else if(r<=mid)
    {
        return ask(rt<<1,l,r);
    }else
    {
        node res1=ask(rt<<1,l,mid);
        node res2=ask(rt<<1|1,mid+1,r);
        node res;
        res.sum=res1.sum+res2.sum;
        res.lm=max(res1.lm,res1.sum+res2.lm);
        res.rm=max(res2.rm,res2.sum+res1.rm);
        res.num=max(res1.num,res2.num);
        res.num=max(res.num,res1.rm+res2.lm);
        return res;
    }
}

int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    scanf("%d",&n);
    build(1,1,n);
    int m;
    scanf("%d",&m);
    while(m--)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        printf("%lld\n",ask(1,x,y).num);
    }
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

原文地址:https://www.cnblogs.com/qieqiemin/p/11438371.html

时间: 2024-11-06 16:09:55

Can you answer these queries I SPOJ - GSS1 (线段树维护区间连续最大值/最大连续子段和)的相关文章

H - Can you answer these queries? HDU 4027 (线段树+延迟标记+开根号的速度)

H - Can you answer these queries? Time Limit:2000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4027 Description A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our

SP1716 GSS3 - Can you answer these queries III - 动态dp,线段树

GSS3 Description 动态维护最大子段和,支持单点修改. Solution 设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) 的最大子段和,那么 \[f[i] = max(f[i - 1] + a[i], a[i])\] \[g[i] = max(g[i - 1], f[i])\] 发现只跟前一项有关.我们希望使用矩阵乘法的思路,但是矩阵乘法通常只能适用于递推问题.因此我们引入广义矩阵乘法. 矩阵乘法问题可分治的原因在于

HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)

题目 线段树 简单题意: 区间(单点?)更新,区间求和 更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都是开平方,同一个数更新有限次数就一直是1了,所以可以这样优化 #include <stdio.h> #include<math.h> #define N 100010 #define LL __int64 #define lson l,m,rt<<1 #define rson

LightOJ Array Queries 1082【线段树求区间最值】

1082 - Array Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB Given an array with N elements, indexed from 1 to N. Now you will be given some queries in the form I J, your task is to find the minimum value from index

HDOJ 题目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): 10057    Accepted Submission(s): 2305 Problem Description A lot of battleships of evil are arranged in a line befor

HDU 4027 Can you answer these queries? 线段树,区间修改

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 end

SPOJ GSS3 线段树系列1

SPOJ GSS系列真是有毒啊! 立志刷完,把线段树搞完! 来自lydrainbowcat线段树上的一道例题.(所以解法参考了lyd老师) 题意翻译 n 个数, q 次操作 操作0 x y把 Ax 修改为 y 操作1 l r询问区间 [l,r] 的最大子段和 数据规模在50000,有负数. 冷静分析 因为要维护最大子段和,那么我们可以在线段树struct中维护这么几个信息: sum(区间和).lmax(从左顶点出发的最大子段和).rmax(从右顶点出发的最大子段和).maxx(这段的最大子段和)

Sum Queries? CodeForces - 1217E (线段树)

Sum Queries? CodeForces - 1217E (线段树) 题意: 定义一个集合为\(balanced\)的,当且仅当集合内数字之和的每个十进制位,都与集合中某个数该位相同.否则,称该集合为\(unbalanced\)的. 给定一个长度为\(n\)的序列,\(q\)次询问一个区间内数字之和最小的\(unbalanced\)集合,输出数字之和.若没有输出\(-1\). \(n,q<=200000\). 题解: 可以发现,如果存在\(unbalanced\)集合,那么最小的一定是只两

Spoj 1557 Can you answer these queries II 线段树 任意区间最大子段和 不重复数字

题目链接:点击打开链接 每个点都是最大值,把一整个序列和都压缩在一个点里. #include <vector> #include <iostream> #include <algorithm> #include <string.h> #include <stdio.h> using namespace std; #define N 100005 #define Lson(x) (x<<1) #define Rson(x) (x<