POJ 2570 线段树
Potted Flower
Time Limit: 2000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
Description
(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
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
Sample Input
5
3 -2 1 2 -5
4
2 -2
5 -5
2 -4
5 -1
Sample Output
4
4
3
5
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define ls rt<<1
#define rs rt<<1|1 #define MAX(x,y) ((x)>(y)?(x):(y))
#define MIN(x,y) ((x)>(y)?(y):(x))
#define MAXN 100010 int N,M;
int a[MAXN]; struct seg_tree
{
int l,r;
int lmax,rmax,summax;
int lmin,rmin,summin;
int sum;
}tree[MAXN*]; void PushUp(int rt)
{
tree[rt].sum=tree[ls].sum+tree[rs].sum; tree[rt].lmax=MAX(tree[ls].lmax,tree[ls].sum+tree[rs].lmax);
tree[rt].rmax=MAX(tree[rs].rmax,tree[rs].sum+tree[ls].rmax);
tree[rt].summax=MAX(MAX(tree[ls].summax,tree[rs].summax),tree[ls].rmax+tree[rs].lmax); tree[rt].lmin=MIN(tree[ls].lmin,tree[ls].sum+tree[rs].lmin);
tree[rt].rmin=MIN(tree[rs].rmin,tree[rs].sum+tree[ls].rmin);
tree[rt].summin=MIN(MIN(tree[ls].summin,tree[rs].summin),tree[ls].rmin+tree[rs].lmin); } void build(int rt,int l,int r)
{
if(l>r) return;
tree[rt].l=l;
tree[rt].r=r;
int mid=(l+r)/;
if(l==r)
{
tree[rt].sum=a[l];
tree[rt].lmax=tree[rt].rmax=tree[rt].summax=a[l];
tree[rt].lmin=tree[rt].rmin=tree[rt].summin=a[l];
return;
}
build(rt*,l,mid);
build(rt*+,mid+,r);
PushUp(rt);
} void update(int rt,int pos,int val)
{
if(tree[rt].l==tree[rt].r && tree[rt].l==pos)
{
tree[rt].sum=val;
tree[rt].lmax=tree[rt].rmax=tree[rt].summax=val;
tree[rt].lmin=tree[rt].rmin=tree[rt].summin=val;
return;
} int m=(tree[rt].l + tree[rt].r)>>;
if(pos<=m)
update(rt*,pos,val);
else
update(rt*+,pos,val);
PushUp(rt);
} int main()
{
while(scanf("%d",&N)!=EOF)
{
for(int i=;i<=N;i++)
scanf("%d",&a[i]);
build(,,N); scanf("%d",&M);
int pos,val; while(M--)
{
scanf("%d%d",&pos,&val);
update(,pos,val); if(tree[].sum==tree[].summax)
printf("%d\n",tree[].sum-tree[].summin);
else
printf("%d\n",MAX(tree[].summax,tree[].sum-tree[].summin));
}
}
return ;
}
题意:给定一个环形序列,进行在线操作,每次修改一个元素,输出环上的最大连续子列的和,但不能是完全序列。
分析:
如果不是环的话,只是一个序列,用线段树很方便求,所以将环从某一点切开成一个序列。
那么答案的最大连续和可能包含断点(换种想法,包含断点时的最大连续和即为 sum-区间最小的连续和)
1,若是所有的数都大于0,那么最大连续和(必须断开一个)即为 总和sum-最小非空连续和。
2,若是区间最小连续和小于0,那么答案就是MAX(区间最大连续和,sum-最小非空连续和)。即分为包含断点和不包含的比较。
POJ 2570 线段树的更多相关文章
- poj 2886 线段树+反素数
Who Gets the Most Candies? Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12744 Acc ...
- poj 3468(线段树)
http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...
- POJ——3264线段树
题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,00 ...
- POJ 2828 线段树(想法)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 15422 Accepted: 7684 Desc ...
- poj 2828 线段树
http://poj.org/problem?id=2828 学到的思维: 1.变化的或者后来的优先影响前面的,那么从最后一个往前看,最后一个就成了 确定的, 而且后来的也能够确定----假设从前往后 ...
- POJ - 1177 线段树
POJ - 1177 扫描线 这道题也算是一道扫描线的经典题目了. 只不过这道题是算周长,非常有意思的一道题.我们已经知道了,一般求面积并,是如何求的,现在我们要把扫描线进行改造一下,使得能算周长. ...
- poj 2886 (线段树+反素数打表) Who Gets the Most Candies?
http://poj.org/problem?id=2886 一群孩子从编号1到n按顺时针的方向围成一个圆,每个孩子手中卡片上有一个数字,首先是编号为k的孩子出去,如果他手上的数字m是正数,那么从他左 ...
- poj 2828(线段树 逆向思考) 插队是不好的行为
http://poj.org/problem?id=2828 插队问题,n个人,下面n行每行a,b表示这个人插在第a个人的后面和这个人的编号为b,最后输出队伍的情况 涉及到节点的问题可以用到线段树,这 ...
- poj 2528(线段树+离散化) 市长的海报
http://poj.org/problem?id=2528 题目大意是市长竞选要贴海报,给出墙的长度和依次张贴的海报的长度区间(参考题目给的图),问最后你能看见的海报有几张 就是有的先贴的海报可能会 ...
随机推荐
- 从1~N中任选出三个数,最小公倍数最大
已知一个正整数N,问从1~N中任选出三个数,它们的最小公倍数最大可以为多少. 当n为奇数:n.n-1.n-2这是三个最大数,并且它们两两互质.因为连续的奇.偶.奇,互质.连续的两个数互质是因为它们的公 ...
- [SoapUI] 在SoapUI中通过Groovy脚本执行window命令杀掉进程
//杀Excel进程 String line def p = "taskkill /F /IM EXCEL.exe".execute() def bri = new Buffere ...
- 局外者看 -- 美团 vs 滴滴
1. 美团 美团外面 美团打车 美团云服务 2. 滴滴 滴滴打车 滴滴外面 滴滴云服务 这是一场企业级别的战争,而且是本土战争,时间开始于何时,不知道,现在写这个博客的时间是2018年3月21日下午. ...
- 【算法】DP解决旅行路径问题
问题描述 : After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice ...
- springMVC 学习 五 参数传递(包括restful风格)
(一)SpringMVC Controller接受参数的方式 (1) 前端传递的参数,在springMVC的controller中使用基本数据类型或者String 类型进行接受 在前端有一个form表 ...
- BP神经网络的数学常识
输入数据X1-Xn. 输入层和隐层之间的权Wji 隐层的输入数据为:∑iwjixi 隐层的输出数据为:yj = f(∑iwjixi).其中f(x)= 隐层的输入数据为:∑jwkjyj 隐层的输出数据为 ...
- 2018.12.15 codeforces 920F. SUM and REPLACE(线段树)
传送门 线段树入门题. 给你一个序列:支持区间修改成自己的约数个数,区间求和. 实际上跟区间开方一个道理. 2的约数个数为2,1的约数个数为1,因此只要区间的最大值小于3就不用修改否则就暴力修改. 因 ...
- Java基础-时间类
关于java中六个时间类的使用和区别 java.util.Date java.sql.Date ,java.sql.Time , java.sql.Timestamp java.text.Simple ...
- websocket项目电子签字使用场景
场景描述:进入页面时,如果设置强制签字,发送签字webSocket连接,同时页面有个重新签字按钮,这个按钮会多次调用 第一步:先建立一个websocket的js文件,名叫signSocket.js内容 ...
- Tomcat架构解析(一)-----Tomcat总体架构
Tomcat是非常常用的应用服务器,了解Tomcat的总体架构以及实现细节,对于理解整个java web也是有非常大的帮助. 一.Server 1.最简单的服务器结构 最简单的服务器结构如图所示: ...