poj2750 Potted Flower(区间连续子序列的和)

Potted Flower
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4793 Accepted: 1830

Description

The little cat takes over the management of a new park. There is a large circular statue in the center of the park, surrounded by N pots of flowers. Each potted flower will be assigned to an integer number (possibly negative) denoting how attractive it is. See the following graph as an example: 

(Positions of potted flowers are assigned to index numbers in the range of 1 ... N. The i-th pot and the (i + 1)-th pot are consecutive for any given i (1 <= i < N), and 1st pot is next to N-th pot in addition.) 



The board chairman informed the little cat to construct "ONE arc-style cane-chair" for tourists having a rest, and the sum of attractive values of the flowers beside the cane-chair should be as large as possible. You should notice that a cane-chair cannot be a total circle, so the number of flowers beside the cane-chair may be 1, 2, ..., N - 1, but cannot be N. In the above example, if we construct a cane-chair in the position of that red-dashed-arc, we will have the sum of 3+(-2)+1+2=4, which is the largest among all possible constructions. 

Unluckily, some booted cats always make trouble for the little cat, by changing some potted flowers to others. The intelligence agency of little cat has caught up all the M instruments of booted cats' action. Each instrument is in the form of "A B", which means changing the A-th potted flowered with a new one whose attractive value equals to B. You have to report the new "maximal sum" after each instruction. 

Input

There will be a single test data in the input. You are given an integer N (4 <= N <= 100000) in the first input line. 

The second line contains N integers, which are the initial attractive value of each potted flower. The i-th number is for the potted flower on the i-th position. 

A single integer M (4 <= M <= 100000) in the third input line, and the following M lines each contains an instruction "A B" in the form described above. 

Restriction: All the attractive values are within [-1000, 1000]. We guarantee the maximal sum will be always a positive integer. 

Output

For each instruction, output a single line with the maximum sum of attractive values for the optimum cane-chair.

Sample Input

5
3 -2 1 2 -5
4
2 -2
5 -5
2 -4
5 -1

Sample Output

4
4
3
5

这个题让你找区间最大值,但这个区间最大值比较特殊,是可以循环的一种。不过也就是加一点罢了。

区间的线段最大值=max(正常的线段区间最大值,区间和-正常的线段区间最小值)。

当然为了求得一个区间线段的最大值,我们还需要一些辅助的结点信息。

总的来说:

1.MAX / MIN分别为区间[a,b]的最大 / 最小的非空子序列的和。

2.lmax / lmin分别为区间[a,b]包含a的最大 / 最小非空子序列的和。

3.rmax / rmin分别为区间[a,b]包含b的最大 / 最小非空子序列的和。

为什么需要包含两头结点的呢?

首先我们考虑区间非空子序列的和。

可能有哪几种情况:

1.完全在左区间内。

2.完全在右区间内。

3.横跨左右两个区间。

我们保留两头结点的信息就是为了得到第三种情况的。

所以向上更新状态方程:

    sum[i]=sum[i<<1]+sum[i<<1|1];//更新区间和
    MAX[i]=max(max(MAX[i<<1],MAX[i<<1|1]),rmax[i<<1]+lmax[i<<1|1]);//更新最大连续子序列和
    MIN[i]=min(min(MIN[i<<1],MIN[i<<1|1]),rmin[i<<1]+lmin[i<<1|1]);//更新最小连续子序列和
    lmax[i]=max(lmax[i<<1],sum[i<<1]+lmax[i<<1|1]);//更新包含a的最大连续子序列和
    lmin[i]=min(lmin[i<<1],sum[i<<1]+lmin[i<<1|1]);//更新包含a的最小连续子序列和
    rmax[i]=max(rmax[i<<1|1],sum[i<<1|1]+rmax[i<<1]);//更新包含b的最大连续子序列和
    rmin[i]=min(rmin[i<<1|1],sum[i<<1|1]+rmin[i<<1]);//更新包含b的最小连续子序列和

然后重要的一点就是最后求结果的时候。

因为题目要求中明确给出不能是全部区间的和。

所以如果全为正数,应当为sum[1]-MIN[1]。

不是的话就是上面所说的max(sum[1]-MIN[1],MAX[1])。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=1e5*4+7;
int n,m;
int MAX[MAXN],MIN[MAXN];
int lmax[MAXN],rmax[MAXN];
int lmin[MAXN],rmin[MAXN];
int sum[MAXN];
void pushup(int i)
{
    sum[i]=sum[i<<1]+sum[i<<1|1];//更新区间和
    MAX[i]=max(max(MAX[i<<1],MAX[i<<1|1]),rmax[i<<1]+lmax[i<<1|1]);//更新最大连续子序列和
    MIN[i]=min(min(MIN[i<<1],MIN[i<<1|1]),rmin[i<<1]+lmin[i<<1|1]);//更新最小连续子序列和
    lmax[i]=max(lmax[i<<1],sum[i<<1]+lmax[i<<1|1]);//更新包含a的最大连续子序列和
    lmin[i]=min(lmin[i<<1],sum[i<<1]+lmin[i<<1|1]);//更新包含a的最小连续子序列和
    rmax[i]=max(rmax[i<<1|1],sum[i<<1|1]+rmax[i<<1]);//更新包含b的最大连续子序列和
    rmin[i]=min(rmin[i<<1|1],sum[i<<1|1]+rmin[i<<1]);//更新包含b的最小连续子序列和
}

void build_tree(int i,int l,int r)
{
    if(l==r)
    {
        scanf("%d",&sum[i]);
        MAX[i]=MIN[i]=lmax[i]=rmax[i]=lmin[i]=rmin[i]=sum[i];
        return ;
    }
    int mid=(l+r)>>1;
    build_tree(i<<1,l,mid);
    build_tree(i<<1|1,mid+1,r);
    pushup(i);
}

void updata(int i,int l,int r,int pos,int val)
{
    if(l==r)
    {
        MAX[i]=MIN[i]=lmax[i]=rmax[i]=lmin[i]=rmin[i]=sum[i]=val;
        return ;
    }
    int mid=(l+r)>>1;
    if(pos<=mid)updata(i<<1,l,mid,pos,val);
    else updata(i<<1|1,mid+1,r,pos,val);
    pushup(i);
}
int main()
{
    scanf("%d",&n);
    build_tree(1,1,n);
    scanf("%d",&m);
    int pos,val;
    while(m--)
    {
        scanf("%d%d",&pos,&val);
        updata(1,1,n,pos,val);
        if(sum[1]==MAX[1])printf("%d\n",sum[1]-MIN[1]);
        else printf("%d\n",max(sum[1]-MIN[1],MAX[1]));
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值