POJ2750-Potted Flower

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

分析:

题意:
给一个环状的数列,求最大连续子序列和,但是不能成环!

解析:(最大子列和居然可以用线段树做!!!!学到了)
(1)由于不能成环,所以如果环上的数全为正数,则要减去最小的数(这里是数列和减去最小子列和)
(2)数列是成环的,所以最大子列和可能由给出的数列的头部和尾部组成,所以要比较给出的(数列的最大子列和)与给出数列的(最大左子列和(包含左端点)与最大右子列和(包含右端点``)的和)的大小(这里提供两种办法:1.数列和减去最小子列和 2.最大左子列和与最大右子列和的和)

#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 100005

using namespace std;

struct node{
	int sum;
	int maxsum,minsum;
	int lmaxsum,lminsum;
	int rmaxsum,rminsum;
};

node tree[N<<2];

void updata(int l,int r,int i,int val,int x)
{
	if(l==r)
	{
		tree[i].lmaxsum=tree[i].lminsum=tree[i].maxsum=tree[i].minsum=tree[i].rmaxsum=tree[i].rminsum=tree[i].sum=val;
		return;
	}
	int mid=(l+r)>>1;
	if(x<=mid)
		updata(l,mid,i<<1,val,x);
	else
		updata(mid+1,r,i<<1|1,val,x);
	tree[i].lmaxsum=max(tree[i<<1].lmaxsum,tree[i<<1].sum+tree[i<<1|1].lmaxsum);
	tree[i].lminsum=min(tree[i<<1].lminsum,tree[i<<1].sum+tree[i<<1|1].lminsum);
	tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
	tree[i].rmaxsum=max(tree[i<<1|1].rmaxsum,tree[i<<1].rmaxsum+tree[i<<1|1].sum);
	tree[i].rminsum=min(tree[i<<1|1].rminsum,tree[i<<1|1].sum+tree[i<<1].rminsum);
	tree[i].maxsum=max(tree[i<<1].maxsum,max(tree[i<<1].rmaxsum+tree[i<<1|1].lmaxsum,tree[i<<1|1].maxsum));
	tree[i].minsum=min(tree[i<<1].minsum,min(tree[i<<1].rminsum+tree[i<<1|1].lminsum,tree[i<<1|1].minsum));
}

int main()
{
	int n,m,val,id;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&val);
		updata(1,n,1,val,i);
	}
	scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&id,&val);
		updata(1,n,1,val,id);
		if(tree[1].sum==tree[1].maxsum&&tree[1].sum>0)
			printf("%d\n",tree[1].sum-tree[1].minsum);
		else
			printf("%d\n",max(tree[1].maxsum,tree[1].sum-tree[1].minsum));
	}
	return 0;
 } 
发布了46 篇原创文章 · 获赞 16 · 访问量 414

猜你喜欢

转载自blog.csdn.net/weixin_43357583/article/details/105000092