Pku2750 Potted Flower

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 largeas possible. You should notice that a cane-chair cannot be a total circle, so the number of flowersbeside 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 hascaught 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 eachpotted 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 gu

arantee 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 optimumcane-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{区间和-子序列最小值,区间最大值}

有类似的题目,参考最大连续子数列和

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
    int x=0,f=1;char ch=getchar();
    for (;ch<‘0‘||ch>‘9‘;ch=getchar())  if (ch==‘-‘)    f=-1;
    for (;ch>=‘0‘&&ch<=‘9‘;ch=getchar())    x=(x<<1)+(x<<3)+ch-‘0‘;
    return x*f;
}
inline void print(int x){
    if (x>=10)     print(x/10);
    putchar(x%10+‘0‘);
}
const int N=1e5;
struct Segment{
    #define ls (p<<1)
    #define rs (p<<1|1)
    struct AC{
        int Maxl,Maxr,Max;//Max为子序列最大值,Maxl与Maxr是为了更新Max而产生,分别是从左边开始的序列最大值和从右边开始的序列最大值
        int Minl,Minr,Min;//与Max类似
        int sum;//区间和
        void init(int x){Maxl=Maxr=Max=Minl=Minr=Min=sum=x;}
    }tree[N*4+10];
    friend AC operator +(const AC &x,const AC &y){//重定义+后的更新
        AC z; z.init(0);
        z.Max=max(max(x.Max,y.Max),x.Maxr+y.Maxl);
        z.Maxl=max(x.Maxl,x.sum+y.Maxl);
        z.Maxr=max(y.Maxr,y.sum+x.Maxr);
        z.Min=min(min(x.Min,y.Min),x.Minr+y.Minl);
        z.Minl=min(x.Minl,x.sum+y.Minl);
        z.Minr=min(y.Minr,y.sum+x.Minr);
        z.sum=x.sum+y.sum;
        return z;
    }
    void build(int p,int l,int r){
        if (l==r){
            tree[p].init(read());
            return;
        }
        int mid=(l+r)>>1;
        build(ls,l,mid),build(rs,mid+1,r);
        tree[p]=tree[ls]+tree[rs];
    }
    void change(int p,int l,int r,int x,int t){
        if (l==r){
            tree[p].init(t);
            return;
        }
        int mid=(l+r)>>1;
        if (x<=mid) change(ls,l,mid,x,t);
        if (x>mid)  change(rs,mid+1,r,x,t);
        tree[p]=tree[ls]+tree[rs];
    }
    void write(){
        int Ans=tree[1].sum-tree[1].Min;
        if (tree[1].sum!=tree[1].Max)   Ans=max(Ans,tree[1].Max);
        printf("%d\n",Ans);
    }
}T;
int main(){
    int n=read();
    T.build(1,1,n);
    int m=read();
    for (int i=1;i<=m;i++){
        int x=read(),y=read();
        T.change(1,1,n,x,y);
        T.write();
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Wolfycz/p/8414585.html

时间: 2024-10-28 06:05:52

Pku2750 Potted Flower的相关文章

【POJ 2750】 Potted Flower(线段树套dp)

[POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4566   Accepted: 1739 Description The little cat takes over the management of a new park. There is a large circular statue in the center of the park, surrou

POJ 2750 Potted Flower (单点修改求线段树上最大子序列和)

题目大意: 在一个序列上每次修改一个值,然后求出它的最大的子序列和. 思路分析: 首先我们不考虑不成环的问题.那就是直接求每个区间的最大值就好了. 但是此处成环,那么看一下下面样例. 5 1 -2 -3 4 5 那么你会发现 max = sum - min 也就是和减去最小区间和也可以得到. 所以我们最后要得到的就是两个东西.注意题目中说的不能全部取得.所以还要判断一下max 是不是等于 sum的. #include <cstdio> #include <iostream> #in

POJ 题目2750 Potted Flower(线段树求环型区间中连续区间的最大和)

Potted Flower Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4470   Accepted: 1698 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 f

poj-2750 Potted Flower

题意: 给出一个n个结点的环,在这个环上取一段区间(不能是整个环),使权值和最大: m次修改某个结点权值,每次修改后输出最大值: n,m<=100000: 题解: 这道题的难点主要在于给出的是一个环而不是序列: 所以选择一个点把环拆成序列,那么就可以把问题转化成: 在一个序列中,取一段子序列,或两边都取两段序列,是权值最大: 这个答案就是在这个序列里取区间的max(最大值ma,序列和sum-最小值mi): 这些都可以用线段树维护: 但是,这道题不允许取整段序列,所以直接这么搞是不行的: 考虑什么

poj 2750 Potted Flower(线段树区间合并)

http://poj.org/problem?id=2750 有n个数围成一个圈,每次可以将a位置上的数变为b,对每个操作,输出区间的最大连续子段和,连续的子段长度不能超过n. 区间合并问题,因为是求连续子段的和.先把圈从1和n之间断开,变为一条链,先在链上求最长连续的和.这个最长连续的和取左节点最长连续和,右节点最长连续和,左节点从右边数最大连续和加上右节点从左边数最大连续和三者的最大值. 特殊情况就是当区间全为正的时候,最长连续和等于1-n的和,这违背题意,它应该等于区间总和减去区间内最小连

POJ 2750 Potted Flower (单点改动求线段树上最大子序列和)

题目大意: 在一个序列上每次改动一个值,然后求出它的最大的子序列和. 思路分析: 首先我们不考虑不成环的问题.那就是直接求每一个区间的最大值就好了. 可是此处成环,那么看一下以下例子. 5 1 -2 -3 4 5 那么你会发现 max = sum - min 也就是和减去最小区间和也能够得到. 所以我们最后要得到的就是两个东西.注意题目中说的不能所有取得.所以还要推断一下max 是不是等于 sum的. #include <cstdio> #include <iostream> #i

poj 2750(线段树的动态规划)

Potted Flower Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4186   Accepted: 1581 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 f

POJ 2570 线段树

Potted Flower Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description The little cat takes over the management of a new park. There is a large circular statue in

ural 2020 Traffic Jam in Flower Town

2020. Traffic Jam in Flower Town Time limit: 1.0 secondMemory limit: 64 MB Having returned from Sun City, Dunno told all his friends that every shorty may have a personal automobile. Immediately after that so many citizens took a fancy of becoming ro