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 largeas possible. You should notice that a cane-chair cannot be a total circle, so the number of flowersbeside 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 hascaught 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 eachpotted 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 gu

arantee 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 optimumcane-chair.

Sample Input

5

3 -2 1 2 -5

4

2 -2

5 -5

2 -4

5 -1

Sample Output

4

4

3

5


破环成链,记录子序列最大值和最小值,以及区间和。

当环上所有的数都是正数时,答案为 区间和-子序列最小值

否则,答案为 max{区间和-子序列最小值,区间最大值}

有类似的题目,参考最大连续子数列和

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=1e5;
struct Segment{
#define ls (p<<1)
#define rs (p<<1|1)
struct AC{
int Maxl,Maxr,Max;//Max为子序列最大值,Maxl与Maxr是为了更新Max而产生,分别是从左边开始的序列最大值和从右边开始的序列最大值
int Minl,Minr,Min;//与Max类似
int sum;//区间和
void init(int x){Maxl=Maxr=Max=Minl=Minr=Min=sum=x;}
}tree[N*4+10];
friend AC operator +(const AC &x,const AC &y){//重定义+后的更新
AC z; z.init(0);
z.Max=max(max(x.Max,y.Max),x.Maxr+y.Maxl);
z.Maxl=max(x.Maxl,x.sum+y.Maxl);
z.Maxr=max(y.Maxr,y.sum+x.Maxr);
z.Min=min(min(x.Min,y.Min),x.Minr+y.Minl);
z.Minl=min(x.Minl,x.sum+y.Minl);
z.Minr=min(y.Minr,y.sum+x.Minr);
z.sum=x.sum+y.sum;
return z;
}
void build(int p,int l,int r){
if (l==r){
tree[p].init(read());
return;
}
int mid=(l+r)>>1;
build(ls,l,mid),build(rs,mid+1,r);
tree[p]=tree[ls]+tree[rs];
}
void change(int p,int l,int r,int x,int t){
if (l==r){
tree[p].init(t);
return;
}
int mid=(l+r)>>1;
if (x<=mid) change(ls,l,mid,x,t);
if (x>mid) change(rs,mid+1,r,x,t);
tree[p]=tree[ls]+tree[rs];
}
void write(){
int Ans=tree[1].sum-tree[1].Min;
if (tree[1].sum!=tree[1].Max) Ans=max(Ans,tree[1].Max);
printf("%d\n",Ans);
}
}T;
int main(){
int n=read();
T.build(1,1,n);
int m=read();
for (int i=1;i<=m;i++){
int x=read(),y=read();
T.change(1,1,n,x,y);
T.write();
}
return 0;
}

[POJ2750]Potted Flower的更多相关文章

  1. POJ 2750 Potted Flower

    Potted Flower Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3872   Accepted: 1446 Des ...

  2. (简单) POJ 2750 Potted Flower,环+线段树。

    Description The little cat takes over the management of a new park. There is a large circular statue ...

  3. 【POJ 2750】 Potted Flower(线段树套dp)

    [POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4566   ...

  4. poj2750 线段树 +DP Potted Flower

    问题描述:给定一个环形序列,进行在线操作,每次修改一个元素,输出环上的最大连续子列的和,但不能是完全序列. 算法:把环从一个地方,切断拉成一条直线,用线段树记录当前区间的非空最大子列和当前区间的非空最 ...

  5. POJ 2750 Potted Flower (线段树区间合并)

    开始懵逼找不到解法,看了网上大牛们的题解才发现是区间合并...  给你n个数形成一个数列环,然后每次进行一个点的修改,并输出这个数列的最大区间和(注意是环,并且区间最大只有n-1个数) 其实只需要维护 ...

  6. POJ 2750 Potted Flower(线段树的区间合并)

    点我看题目链接 题意 : 很多花盆组成的圆圈,每个花盆都有一个值,给你两个数a,b代表a位置原来的数换成b,然后让你从圈里找出连续的各花盆之和,要求最大的. 思路 :这个题比较那啥,差不多可以用DP的 ...

  7. POJ.2750.Potted Flower(线段树 最大环状子段和)

    题目链接 /* 13904K 532ms 最大 环状 子段和有两种情况,比如对于a1,a2,a3,a4,a5 一是两个端点都取,如a4,a5,a1,a2,那就是所有数的和减去不选的,即可以计算总和减最 ...

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

    题目大意: 在一个序列上每次改动一个值,然后求出它的最大的子序列和. 思路分析: 首先我们不考虑不成环的问题.那就是直接求每一个区间的最大值就好了. 可是此处成环,那么看一下以下例子. 5 1 -2 ...

  9. POJ 2750 Potted Flower(线段树+dp)

    题目链接 虽然是看的别的人思路,但是做出来还是挺高兴的. 首先求环上最大字段和,而且不能是含有全部元素.本来我的想法是n个元素变为2*n个元素那样做的,这样并不好弄.实际可以求出最小值,总和-最小,就 ...

随机推荐

  1. json解析bug之ERROR ExceptionController:185 - not close json text, token : :

    错误:ERROR ExceptionController:185 - not close json text, token : : 原因:json数据格式有误.!我的错误是,缺少了一个包括json数据 ...

  2. 学习swift从青铜到王者之swift闭包06

    语法表达式 一般形式:{ (parameters) -> returnType in statements } 这里的参数(parameters),可以是in-out(输入输出参数),但不能设定 ...

  3. kvm虚拟化学习笔记(一)之kvm虚拟化环境安装

    平时一直玩RHEL/CentOS/OEL系列的操作,玩虚拟化也是采这一类系统,kvm在RHEL6系列操作系统支持比较好,本文采用采用OEL6.3操作系统,网上所有文章都说KVM比xen简单,我怎么感觉 ...

  4. 【APUE】【转】守护进程编写

    http://blog.csdn.net/zg_hover/article/details/2553321 守护进程(Daemon)是运行在后台的一种特殊进程.它独立于控制终端并且周期性地执行某种任务 ...

  5. Markdown 语法和代码高亮

    安装 Python Markdown 安装命令 pip install markdown 视图中渲染 Markdown blog/views.py import markdown from djang ...

  6. Zookeeper 3.4 官方文档翻译

    说明 个人英语水平非常一般,理解可能有偏差,假设有翻译不恰当之处,请看官指点. 1.简单介绍 分布式系统就像动物园.当中每台server就像一仅仅动物,Zookeeper就像动物园管理员,协调.服务于 ...

  7. performSelector调用和直接调用的区别

    今天在准备出笔试题的过程中随便搜了一下其他的笔试题,看到其中一个就是关于performSelector与直接调用的区别. 个人感觉这其实是一个陷阱题,因为大部分应用场景下,用哪一种都可以,可以说是没有 ...

  8. Django项目开发-小技巧

    当你开发完一个Django项目之后肯定要吧他丢到服务器让跑起来,但是你在自己的环境下安装了好多的包,是不是在服务器中也要一个个的安装了, pip freeze > read.txt #这条命令会 ...

  9. STL vector的介绍(1)

    尝试下翻译STL里面的一些easy和算法.四级过了.六级刚考.顺便练练自己的英语水平.翻译的不好的地方请大神多多不吝赐教哈.方便我改正. 原来均来自:http://www.cplusplus.com/ ...

  10. angularjs学习之六(angularjs中directive指令的一般编程事件绑定 模板使用等)

    angular js 中模板的使用.事件绑定以及指令与指令之间的交互 相应教学视频地址(需FQ):v=aG8VD0KvUw4">angularjs教学视频 <!doctype h ...