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

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
  


 线段树+dp 感觉好经典啊  log(n)就能求出一串数字的最大的连续和

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define N 100010
using namespace std;
struct num
{
int l,r,sum,maxsum,minsum,maxl,maxr,minl,minr;
}a[4*N];
int b[N];
int main()
{
//freopen("data.in","r",stdin);
void init(int k,int l,int r);
void update(int k,int pos,int val);
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
init(1,1,n);
int m;
scanf("%d",&m);
while(m--)
{
int x,y;
scanf("%d %d",&x,&y);
update(1,x,y);
if(a[1].sum==a[1].maxsum)
{
printf("%d\n",a[1].sum-a[1].minsum);
}else
{
printf("%d\n",max(a[1].maxsum,a[1].sum-a[1].minsum));
}
}
}
return 0;
}
void pushup(int k)
{
int left=k<<1,right=k<<1|1;
a[k].sum=a[left].sum+a[right].sum;
a[k].maxsum=max(max(a[left].maxsum,a[right].maxsum),a[left].maxr+a[right].maxl);
a[k].minsum=min(min(a[left].minsum,a[right].minsum),a[left].minr+a[right].minl);
a[k].maxl=max(a[left].maxl,a[left].sum+a[right].maxl);
a[k].maxr=max(a[right].maxr,a[right].sum+a[left].maxr);
a[k].minl=min(a[left].minl,a[left].sum+a[right].minl);
a[k].minr=min(a[right].minr,a[right].sum+a[left].minr);
}
void init(int k,int l,int r)
{
a[k].l=l; a[k].r=r;
if(l==r)
{
a[k].sum=a[k].maxsum=a[k].minsum=a[k].maxr=a[k].maxl=a[k].minr=a[k].minl=b[l];
return ;
}
int mid=(l+r)>>1;
init(k<<1,l,mid);
init(k<<1|1,mid+1,r);
pushup(k);
}
void update(int k,int pos,int val)
{
if(a[k].l==a[k].r)
{
a[k].sum=a[k].maxsum=a[k].minsum=a[k].maxr=a[k].maxl=a[k].minr=a[k].minl=val;
return ;
}
int mid=(a[k].l+a[k].r)>>1;
if(mid>=pos)
{
update(k<<1,pos,val);
}else
{
update(k<<1|1,pos,val);
}
pushup(k);
}

POJ 2750 Potted Flower的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. POJ 2750 鸡兔同笼

    参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6414781.html POJ 2750鸡兔同笼 总时间限制:1000ms 内存限制:65536kB ...

  9. [POJ2750]Potted Flower

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

随机推荐

  1. Android 中 View移动总结:ViewDragHelper学习及用法详解

    如上图简单呈现出两个方块后,提出一个需求: 1.拖动方块时,方块(即子View)可以跟随手指移动. 2.一个方块移动时,另一个方块可以跟随移动. 3.将方块移动到左边区域(右边区域)后放开(即手指离开 ...

  2. JavaScript学习心得(二)

    一选择DOCTYPE DOCTYPE是一种标准通用标记语言的文档类型声明,目的是告诉标准通用标记语言解析器使用什么样的文档类型定义(DTD)来解析文档. 网页从DOCTYPE开始,即<!DOCT ...

  3. Apache 支持.htaccess

    ******************************************************************************* Apache 服务器 ********* ...

  4. MDK —— configuration wizard

    学习RTX 的时候发现RTX的配置文件可以MDK的图形界面来配置,感觉这个非常好,直观.方便.健壮,可以避免程序员写入错误的数据.   参考: µVision User's Guide->Uti ...

  5. PHPStorm——配置修改

    字体修改: FiraCode字体:https://github.com/tonsky/FiraCode 1.双击安装字体 2. 关闭错别字检测

  6. C++11的新特性lambda的小试牛刀RAII

    C/C++的资源是手动管理的 这导致程序员在申请资源时,最后用完了偶尔会忘记回收 C++语言的发明者倡导RAII,资源获取即初始化 使用对象来管理资源的生命周期,在超出作用域时,析构函数自动释放资源 ...

  7. jsonp跨域问题记录

    这段时间用H5做移动app开发,遇到不少之前做web的时候不曾遇到的问题,记录一下,共勉-- 首先说一个:js跨域取数的问题 描述:  之前做web都是通过后台获取数据,没考虑过跨域的问题.这次用h5 ...

  8. LightOj_1030 Discovering Gold

    题目链接 题意: 在一个1 X N 的格子上, 每个格子都有一定的黄金, 你从第一个格子出发, 问到最后一个格子得到黄金的期望. 每次前进使用骰子投点来决定前进步数, 如果投出的点前进后会超过N, 那 ...

  9. 写个简单的ANT脚本来编译项目

    <?xml version="1.0" encoding="GBK"?> <project name="j2ee project&q ...

  10. OpenJudge_cdqz 数据结构版块小结

    题目整理 Challenge 0  随机线性存储表-easy Challenge 1  链表数组-easy Challenge 2  可持久化Treap的可持久化运用-hard Challenge 3 ...