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

2024-08-28 10:18

本文主要是介绍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;
}


这篇关于poj 2750 Potted Flower(线段树区间合并)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1114477

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];