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

POJ Monthly--2006.01.22,Zeyuan Zhu

题目大意:求环形的区间中连续区间的最大和,,但是不能都取

maxv连续区间最大和

minv连续区间的最小和

lmax从左最大

lmin从左最小

rmax从右最大

rmin从有最小

sum区间所有数的和

ac代码

#include<stdio.h>
#include<string.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
struct s
{
	int sum;
	int maxv,minv;
	int lmin,lmax;
	int rmin,rmax;
}node[100010<<2];
void init(int tr,int num)
{
	node[tr].sum=num;
	node[tr].maxv=node[tr].minv=num;
	node[tr].lmin=node[tr].lmax=num;
	node[tr].rmin=node[tr].rmax=num;
}
void pushup(int tr)
{
	node[tr].sum=node[tr<<1].sum+node[tr<<1|1].sum;
	node[tr].lmax=max(node[tr<<1].lmax,node[tr<<1].sum+node[tr<<1|1].lmax);
	node[tr].rmax=max(node[tr<<1|1].rmax,node[tr<<1|1].sum+node[tr<<1].rmax);
	node[tr].lmin=min(node[tr<<1].lmin,node[tr<<1].sum+node[tr<<1|1].lmin);
	node[tr].rmin=min(node[tr<<1|1].rmin,node[tr<<1|1].sum+node[tr<<1].rmin);
	node[tr].maxv=max(max(node[tr<<1].maxv,node[tr<<1|1].maxv),node[tr<<1].rmax+node[tr<<1|1].lmax);
	node[tr].minv=min(min(node[tr<<1].minv,node[tr<<1|1].minv),node[tr<<1].rmin+node[tr<<1|1].lmin);
}
void build(int l,int r,int tr)
{
	if(l==r)
	{
		int num;
		scanf("%d",&num);
		init(tr,num);
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,tr<<1);
	build(mid+1,r,tr<<1|1);
	pushup(tr);
	//node[tr].sum=node[tr<<1].sum+node[tr<<1|1].sum;
}
void update(int pos,int num,int l,int r,int tr)
{
	if(l==r)
	{
		init(tr,num);
		return;
	}
	int mid=(l+r)>>1;
	if(pos<=mid)
		update(pos,num,l,mid,tr<<1);
	else
		update(pos,num,mid+1,r,tr<<1|1);
	pushup(tr);
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		build(1,n,1);
		int m;
		scanf("%d",&m);
		while(m--)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			update(a,b,1,n,1);
			if(node[1].sum==node[1].maxv)//不能都取
			{
				printf("%d\n",node[1].sum-node[1].minv);
			}
			else
				printf("%d\n",max(node[1].sum-node[1].minv,node[1].maxv));//环
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-31 22:48:20

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

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

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

POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并

题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线段树维护两个值:cover和len,cover表示该线段区间目前被覆盖的线段数目,len表示当前已覆盖的线段长度(化为离散前的真值),每次加入一条线段,将其y_low,y_high之间的区间染上line[i].cover,再以tree[1].len乘以接下来的线段的x坐标减去当前x坐标,即计算了一部

POJ 题目3667 Hotel(线段树,区间更新查询,求连续区间)

Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13805   Accepted: 5996 Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie

Spoj 2916 Can you answer these queries V 线段树 求任意重叠区间的最大子段和

题目链接:点击打开链接 题意: T个测试数据 n个数字 q个询问 每个询问 : [x1, y1] [x2, y2] 问: int ans = -inf; for(int i = x1; i <= y1; i++) for(int j = max(x2, i); j <= y2; j++) ans = max(ans, query(i, j)); 思路: query_L(int l, int r) 求的是在区间[l,r]内 ,左端点为l的最大子段和. 其他和GSS3差不多. 分类讨论一下给定的区

HDOJ 题目3308 LCIS(线段树,区间查询,区间合并)

LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5319    Accepted Submission(s): 2361 Problem Description Given n integers. You have two operations: U A B: replace the Ath number by B. (ind

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

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

POJ&#183;1151 Atlantis&#183;线段树求矩形面积并

题目在这:http://poj.org/problem?id=1151 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the is

POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

poj 3368 Frequent values(线段树解法)

题目链接:http://poj.org/problem?id=3368 题目大意:给你一段不下降的序列,求给定区间里出现次数最多的那个数字的次数. 思路:首先看到这题时,第一感觉线段树,但是仔细一看问题来啦,用线段数我怎么才能计算出某段区间里出现的那个数,因为出现最多的那个数可能不是在他它的左儿子上也不是在它的右儿子上,可能在当他们合并成一个区间时就出现啦,但是这儿我们需要注意的就是,题目给的是一段不下降的序列,那么突破口就出来啦,因为如果出现相同的数字,那么它们一定是连续的.所以我们只需要在普