码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-17 18:39:42      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   for   

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 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.) 

bubuko.com,布布扣


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

Source

POJ Monthly--2006.01.22,Zeyuan Zhu

正常看得懂吧,代码有点小解释。

AC代码:

#include<algorithm>
#include<stdio.h>
#include<iostream>
using namespace std;
struct Node{
    int left,right;
    int mx,mn,sum;
    int nmx,nmn,rmx,lmx,rmn,lmn;
}a[400010];
void push(int cur){
    a[cur].mn=min(a[cur<<1].mn,a[cur<<1|1].mn);    //最小值
    a[cur].mx=max(a[cur<<1].mx,a[cur<<1|1].mx);    //最大值
    a[cur].sum=a[cur<<1].sum+a[cur<<1|1].sum;      //和

    a[cur].rmx=max(a[cur<<1|1].rmx,a[cur<<1].rmx+a[cur<<1|1].sum);   //从右向左的最大连续和
    a[cur].lmx=max(a[cur<<1].lmx,a[cur<<1].sum+a[cur<<1|1].lmx);     //从左向右的最大连续和

    a[cur].rmn=min(a[cur<<1|1].rmn,a[cur<<1].rmn+a[cur<<1|1].sum);   //从右向左的最小连续和
    a[cur].lmn=min(a[cur<<1].lmn,a[cur<<1].sum+a[cur<<1|1].lmn);     //从左向右的最小连续和

    a[cur].nmx=max(max(a[cur<<1].nmx,a[cur<<1|1].nmx),a[cur<<1].rmx+a[cur<<1|1].lmx);    //最大连续和
    a[cur].nmn=min(min(a[cur<<1].nmn,a[cur<<1|1].nmn),a[cur<<1].rmn+a[cur<<1|1].lmn);    //最小连续和
}
void built(int cur,int x,int y){
    a[cur].left=x; a[cur].right=y;
    if(x==y){
        scanf("%d",&a[cur].sum);
        a[cur].mx=a[cur].mn=a[cur].nmx=a[cur].nmn=a[cur].rmx=a[cur].lmx=a[cur].rmn=a[cur].lmn=a[cur].sum;
        return ;
    }
    int mid=(x+y)>>1;
    built(cur<<1,x,mid);
    built(cur<<1|1,mid+1,y);
    push(cur);
}
void update(int cur,int x,int val){
    if(a[cur].left==x && x==a[cur].right){
        a[cur].sum=val;
        a[cur].mx=a[cur].mn=a[cur].nmx=a[cur].nmn=a[cur].rmx=a[cur].lmx=a[cur].rmn=a[cur].lmn=a[cur].sum;
        return ;
    }
    int mid=(a[cur].left+a[cur].right)>>1;
    if(x<=mid)
        update(cur<<1,x,val);
    else
        update(cur<<1|1,x,val);
    push(cur);
}
int main(){
    int n,m;
    scanf("%d",&n);
    built(1,1,n);
    scanf("%d",&m);
    while(m--){
        int x,val;
        scanf("%d%d",&x,&val);
        update(1,x,val);
        if(a[1].sum==a[1].nmx){
            printf("%d\n",a[1].sum-a[1].nmn);
        }
        else{
            printf("%d\n",max(a[1].nmx,a[1].sum-a[1].nmn));
        }
    }
    return 0;
}


poj 2750(线段树的动态规划),布布扣,bubuko.com

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

标签:des   style   http   color   os   io   strong   for   

原文地址:http://blog.csdn.net/my_acm/article/details/38639921

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!