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

http://poj.org/problem?id=2750

有n个数围成一个圈,每次可以将a位置上的数变为b,对每个操作,输出区间的最大连续子段和,连续的子段长度不能超过n。

区间合并问题,因为是求连续子段的和。先把圈从1和n之间断开,变为一条链,先在链上求最长连续的和。这个最长连续的和取左节点最长连续和,右节点最长连续和,左节点从右边数最大连续和加上右节点从左边数最大连续和三者的最大值。

特殊情况就是当区间全为正的时候,最长连续和等于1~n的和,这违背题意,它应该等于区间总和减去区间内最小连续的和。因此节点内在记录最大连续和的同时还要记录最小连续和。剩下的就是push_up和单点更新了。

#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL __int64
#define eps 1e-12
#define PI acos(-1.0)
#define PP pair<LL,LL>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 100010;
const int mod = 1000000007;

struct node
{
	int l,r;
	int lmin,rmin;//分别从左右两边数最小连续的和
	int lmax,rmax;//分别从左右两边数最大连续的和
	int sum,smax,smin;//区间总和,最大连续和,最小连续和
}tree[maxn*4];

int a[maxn];

void push_up(int v)
{
	tree[v].sum = tree[v*2].sum + tree[v*2+1].sum;
	tree[v].lmax = max(tree[v*2].lmax, tree[v*2].sum + tree[v*2+1].lmax);
	tree[v].lmin = min(tree[v*2].lmin, tree[v*2].sum + tree[v*2+1].lmin);
	tree[v].rmax = max(tree[v*2+1].rmax, tree[v*2+1].sum + tree[v*2].rmax);
	tree[v].rmin = min(tree[v*2+1].rmin, tree[v*2+1].sum + tree[v*2].rmin);
	tree[v].smax = max(max(tree[v*2].smax,tree[v*2+1].smax),tree[v*2].rmax+tree[v*2+1].lmax);
	tree[v].smin = min(min(tree[v*2].smin,tree[v*2+1].smin),tree[v*2].rmin+tree[v*2+1].lmin);
}

void build(int v, int l, int r)
{
	tree[v].l = l;
	tree[v].r = r;
	if(l == r)
	{
		tree[v].lmax = tree[v].lmin = tree[v].rmax = tree[v].rmin = a[l];
		tree[v].smax = tree[v].smin = tree[v].sum = a[l];
		return;
	}
	int mid = (l+r) >> 1;
	build(v*2,l,mid);
	build(v*2+1,mid+1,r);
	push_up(v);
}

void update(int v, int pos, int val)
{
	if(tree[v].l == tree[v].r)
	{
		tree[v].lmax = tree[v].lmin = tree[v].rmax = tree[v].rmin = val;
		tree[v].smax = tree[v].smin = tree[v].sum = val;
		return;
	}
	int mid = (tree[v].l + tree[v].r) >> 1;
	if(pos <= mid)
		update(v*2,pos,val);
	else update(v*2+1,pos,val);
	push_up(v);
}

int main()
{
	int n,m;
	int x,y;
	while(~scanf("%d",&n))
	{
		for(int i = 1; i <= n; i++)
			scanf("%d",&a[i]);
		scanf("%d",&m);
		build(1,1,n);
		for(int i = 1; i <= m; i++)
		{
			scanf("%d %d",&x,&y);
			update(1,x,y);

			if(tree[1].smax == tree[1].sum && tree[1].sum > 0)
				printf("%d\n",tree[1].sum - tree[1].smin);
			else
			{
				printf("%d\n",max(tree[1].smax,tree[1].sum - tree[1].smin));
			}
		}
	}
	return 0;
}
时间: 2024-07-28 21:48:43

poj 2750 Potted Flower(线段树区间合并)的相关文章

POJ 3667 Hotel 【线段树 区间合并 + Lazy-tag】

Hotel Time Limit: 3000MS Memory Limit: 65536K 链接:POJ 3667   Description The cows are journeying north to ThunderBay in Canada to gain cultural enrichment and enjoy a vacation on the sunnyshores of Lake Superior. Bessie, ever the competent travel agen

Poj 3667——hotel——————【线段树区间合并】

Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13124   Accepted: 5664 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

POJ 3667 Hotel (线段树区间合并 )

Language: Default Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12417   Accepted: 5346 Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lak

POJ - 3667 Hotel(线段树区间合并)

题目链接 题意: 给定一个有$N$个车位的停车场(都在一条直线上),现在有有两种操作 $1.x $     要停连续的停$x$辆车,输出第一辆车停的位置(尽量靠前),不能就输出$0$: $2.x,d$ 从x位置开始开走连续的$d$辆车. 思路: 一个线段树区间和问题,而且满足区间可加性,就要用到区间合并. 1 /* 2 * Author: windystreet 3 * Date : 2018-08-15 10:29:55 4 * Motto : Think twice, code once.

POJ - 3667 - Hotel (线段树 - 区间合并)

题目传送:Hotel 思路:线段树,区间合并,区间替换,查询最左断点,看胡浩版本的线段树好几天了,今天看这个看了好久,慢慢来吧,具体都写在注释里了 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #inc

(简单) POJ 3667 Hotel,线段树+区间合并。

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, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Stree

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

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

POJ 3667(线段树区间合并)

http://poj.org/problem?id=3667 题意:两个操作 : 1 选出靠左的长度为a的区间. 2 把从 a到a+b的区间清空. 线段树区间合并+lazy // by caonima // hehe #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; co

线段树(区间合并) POJ 3667 Hotel

题目传送门 1 /* 2 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 3 输入 2 a b:将[a,a+b-1]的房间清空 4 线段树(区间合并):lsum[]统计从左端点起最长连续空房间数,rsum[]类似,sum[]统计区间最长连续的空房间数, 5 它有三种情况:1.纯粹是左端点起的房间数:2.纯粹是右端点的房间数:3.当从左(右)房间起都连续时,加上另一个子节点 6 从左(右)房间起的数,sum[]再求最大值更新维护.理解没错,表达能力不足 7 详细解释:htt