POJ 2750 Potted Flower(线段树+dp)

126 篇文章 2 订阅
38 篇文章 0 订阅

题目大意:给定一个环形序列,每次修改一个元素,输出环上的最大连续子列的和。

解题思路:用线段树记录当前区间的非空最大子列和当前区间的非空最小子列。如果环上的数都是正整数,答案是:环上数的总和-根结点的非空最小子列;

这里面要注意的是:建树的时候要一遍输入一遍递归。

Potted Flower
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4044 Accepted: 1516

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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 10001000
#define LL __int64
//#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 500000;

using namespace std;


int Max[maxn];//区间上的最大子序列
int Min[maxn];
int lmax[maxn];//包含左端点的最大子序列
int rmax[maxn];//包含右端点的最大子序列
int lmin[maxn];
int rmin[maxn];
int sum[maxn];

void dp(int site)
{
    int l = site<<1;
    int r = site<<1|1;
    sum[site] = sum[l]+sum[r];

    Max[site] = max(max(Max[l], Max[r]), lmax[r]+rmax[l]);//最大值的选择是从左区间最大,和右区间最大,
    Min[site] = min(min(Min[l], Min[r]), lmin[r]+rmin[l]);//连续的两个区间的和的最大值选
    lmax[site] = max(lmax[l], lmax[r]+sum[l]);
    rmax[site] = max(rmax[r], rmax[l]+sum[r]);

    lmin[site] = min(lmin[l], lmin[r]+sum[l]);
    rmin[site] = min(rmin[r], rmin[l]+sum[r]);
}

void Bulid(int l, int r, int site)
{
    if(l == r)
    {
        scanf("%d",&sum[site]);
        Max[site] = Min[site] = lmax[site] = sum[site];
        rmax[site] = lmin[site] = rmin[site] = sum[site];
        return;
    }
    int mid = (l+r)>>1;
    Bulid(l, mid, site<<1);
    Bulid(mid+1, r, site<<1|1);
    dp(site);
}


void Updata(int num, int d, int l, int r, int site)
{
    if(r == l)
    {
        sum[site] = Max[site] = Min[site] = d;
        lmax[site] = rmax[site] = lmin[site] = rmin[site] = d;
        return ;
    }
    int mid = (l+r)>>1;
    if(mid >= num)
        Updata(num, d, l, mid, site<<1);
    else
        Updata(num, d, mid+1, r, site<<1|1);
    dp(site);

}

int main()
{
    int n, m, ans;
    scanf("%d",&n);
    Bulid(1,n,1);
    scanf("%d",&m);
    for(int i = 1; i <= m; i++)
    {
        int a, b;
        scanf("%d %d",&a, &b);
        Updata(a, b, 1, n, 1);
        if(sum[1] == Max[1])
            ans = sum[1]-Min[1];
        else
            ans = max(Max[1], (sum[1]-Min[1]));
        printf("%d\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值