AaronPolaris

树在,山在,大地在,岁月在,OI在,我还要怎样更好的世界。

poj2750 Potted Flower

Potted Flower
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4520   Accepted: 1716

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

Source




要求支持单点修改和查询环上最大和,显然线段树模板。

需要维护区间最大值和最小值、包含左端点的最大值和最小值、包含右端点的最大值和最小值。

注意:答案不能是所有数的和,题目上有说明。




#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define LL long long
#define pa pair<int,int>
#define MAXN 100005
#define INF 1000000000
using namespace std;
int n,m,a[MAXN];
struct tree_type
{
	int l,r,sum,mx,mn,lmx,rmx,lmn,rmn;
}t[MAXN*4];
int read()
{
	int ret=0,flag=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
	return ret*flag;
}
void pushup(int k)
{
	t[k].sum=t[k<<1].sum+t[k<<1|1].sum;
	t[k].lmx=max(t[k<<1].lmx,t[k<<1].sum+t[k<<1|1].lmx);
	t[k].rmx=max(t[k<<1|1].rmx,t[k<<1|1].sum+t[k<<1].rmx);
	t[k].mx=max(max(t[k<<1].mx,t[k<<1|1].mx),t[k<<1].rmx+t[k<<1|1].lmx);
	t[k].lmn=min(t[k<<1].lmn,t[k<<1].sum+t[k<<1|1].lmn);
	t[k].rmn=min(t[k<<1|1].rmn,t[k<<1|1].sum+t[k<<1].rmn);
	t[k].mn=min(min(t[k<<1].mn,t[k<<1|1].mn),t[k<<1].rmn+t[k<<1|1].lmn);
}
void update(int k)
{
	t[k].mn=t[k].lmn=t[k].rmn=t[k].sum=a[t[k].l];
	t[k].mx=t[k].lmx=t[k].rmx=max(a[t[k].l],0);
}
void build(int k,int x,int y)
{
	t[k].l=x;t[k].r=y;
	if (x==y)
	{
		t[k].mn=t[k].lmn=t[k].rmn=t[k].sum=a[x];
		t[k].mx=t[k].lmx=t[y].rmx=max(a[x],0);
		return;
	}
	int mid=(x+y)>>1;
	build(k<<1,x,mid);
	build(k<<1|1,mid+1,y);
	pushup(k);
}
void cover(int k,int x)
{
	if (t[k].l==x&&t[k].r==x)
	{
		update(k);
		return;
	}
	int mid=(t[k].l+t[k].r)>>1;
	if (mid>=x) cover(k<<1,x);
	else cover(k<<1|1,x);
	pushup(k);
}
int query()
{
	if (t[1].mx==t[1].sum) return t[1].sum-t[1].mn;
	return max(t[1].mx,t[1].sum-t[1].mn);
}
int main()
{
	n=read();
	F(i,1,n) a[i]=read();
	build(1,1,n);
	m=read();
	F(i,1,m)
	{
		int x=read();
		a[x]=read();
		cover(1,x);
		printf("%d\n",query());
	}
	return 0;
}


阅读更多
版权声明:本文为博主原创文章,转载请注明出处:http://blog.csdn.net/aarongzk https://blog.csdn.net/AaronGZK/article/details/49935457
文章标签: poj
个人分类: 线段树
上一篇poj2886 Who Gets the Most Candies?
下一篇poj2828 Buy Tickets
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭