POJ2750--Potted Flower--线段树+DP
来源:互联网 发布:为了防止数据丢失 编辑:程序博客网 时间:2024/06/02 04:18
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.
(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.
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
53 -2 1 2 -542 -25 -52 -45 -1
Sample Output
4
4
3
5
/*线段树+DPst[id].sum=st[2*id].sum+st[2*id+1].sum;区间总和st[id].maxsum=max(max(st[2*id].maxsum,st[2*id+1].maxsum),st[2*id].rmax+st[2*id+1].lmax);st[id].lmax=max(st[2*id].lmax,st[2*id].sum+st[2*id+1].lmax);st[id].rmax=max(st[2*id+1].rmax,st[2*id+1].sum+st[2*id].rmax);这个情况是最长连续和。还有一种情况是两边的。只需要sum-最小连续和即可,最小连续和的处理方法类似PS:注意不能成环。所以如果st[id]的最大连续和==st[id].sum。就得减去最小的元素。*/#include <iostream>#include <cstdio>using namespace std;#define maxn 100008int key[maxn];//用来存初始的魅力值struct ST{int l,r,sum,lmax,rmax,maxsum;int lmin,rmin,minsum;int mm;}st[4*maxn];inline int max(int a,int b){return a>b?a:b;}inline int max(int a,int b,int c){if(a<b)a=b;if(a<c)a=c;return a;}inline int min(int a,int b){return a>b?b:a;}void buildtree(int id,int l,int r){st[id].l=l;st[id].r=r;if(l==r){st[id].lmax=st[id].rmax=st[id].sum=st[id].maxsum=key[l];st[id].lmin=st[id].rmin=st[id].minsum=key[l];st[id].mm=key[l];return;}int mid=(l+r)>>1;buildtree(2*id,l,mid);buildtree(2*id+1,mid+1,r);st[id].sum=st[2*id].sum+st[2*id+1].sum;//区间总和st[id].lmax=max(st[2*id].lmax,st[2*id].sum+st[2*id+1].lmax);st[id].rmax=max(st[2*id+1].rmax,st[2*id+1].sum+st[2*id].rmax);st[id].maxsum=max(max(st[2*id].maxsum,st[2*id+1].maxsum),st[2*id].rmax+st[2*id+1].lmax);st[id].lmin=min(st[2*id].lmin,st[2*id].sum+st[2*id+1].lmin);st[id].rmin=min(st[2*id+1].rmin,st[2*id+1].sum+st[2*id].rmin);st[id].minsum=min(min(st[2*id].minsum,st[2*id+1].minsum),st[2*id].rmin+st[2*id+1].lmin);st[id].mm=min(st[2*id].mm,st[2*id+1].mm);}void update(int id,int num,int nkey){if(st[id].l==num&&st[id].r==num){st[id].lmax=st[id].rmax=st[id].sum=st[id].maxsum=nkey;st[id].lmin=st[id].rmin=st[id].minsum=nkey;st[id].mm=nkey;return;}if(st[2*id].r>=num){update(2*id,num,nkey);}if(st[2*id+1].l<=num){update(2*id+1,num,nkey);}st[id].sum=st[2*id].sum+st[2*id+1].sum;st[id].lmax=max(st[2*id].lmax,st[2*id].sum+st[2*id+1].lmax);st[id].rmax=max(st[2*id+1].rmax,st[2*id+1].sum+st[2*id].rmax);st[id].maxsum=max(max(st[2*id].maxsum,st[2*id+1].maxsum),st[2*id].rmax+st[2*id+1].lmax);st[id].lmin=min(st[2*id].lmin,st[2*id].sum+st[2*id+1].lmin);st[id].rmin=min(st[2*id+1].rmin,st[2*id+1].sum+st[2*id].rmin);st[id].minsum=min(min(st[2*id].minsum,st[2*id+1].minsum),st[2*id].rmin+st[2*id+1].lmin);st[id].mm=min(st[2*id].mm,st[2*id+1].mm);}int query(){int fuck= max(st[1].maxsum,st[1].sum-st[1].minsum);if(fuck==st[1].sum){fuck=st[1].sum-st[1].mm;}return fuck;}int main(){int n;while(scanf("%d",&n)!=EOF){for(int i=1;i<=n;i++){scanf("%d",&key[i]);}buildtree(1,1,n);int m,u,v;scanf("%d",&m);for(int i=1;i<=m;i++){scanf("%d%d",&u,&v);update(1,u,v);printf("%d\n",query());}}return 0;}
- POJ2750--Potted Flower--线段树+DP
- poj2750--Potted Flower(线段树)
- #POJ2750#Potted Flower(线段树)
- 【POJ2750】Potted Flower——线段树
- poj2750 Potted Flower(线段树单点更新)
- POJ 2750 Potted Flower 线段树+DP
- POJ 2750 Potted Flower(线段树+dp)
- POJ2750 Potted Flower
- poj2750 Potted Flower
- (POJ2750)Potted Flower <环形最大连续子段和 线段树单点更新>
- poj2750——Potted Flower
- POJ 2750 Potted Flower(线段树 + DP)
- 【POJ 2750】 Potted Flower(线段树套dp)
- poj 2750 Potted Flower 线段树区间合并加dp
- poj2750 线段树+dp
- poj2750-Potted Flower-动态求最大连续环区间和(线段树上分治+set维护)
- poj 2750 线段树 Potted Flower
- [线段树] poj 2750 Potted Flower
- potential leak of an object allocated and stored into ...
- php的读取txt文件转换成二维数组
- .Net CLR垃圾收集原理(Understanding .net CLR garbage collection)
- 根据allocator的标准接口,编写的功能、接口不齐全的allocator
- Linux bootloader 编写方法
- POJ2750--Potted Flower--线段树+DP
- 机器学习和计算机视觉相关的数学
- windows std::cout设置输出颜色
- Apache rewrite
- 人性化老人手机设计(三)
- Qt wiget style
- 清空input标签里面的默认值
- 林达华博士对数学的见解
- java项目管理的警示