SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并
Can you answer these queries III
这道题和洛谷的小白逛公园一样的题目。
传送门:
洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间合并(单点更新、区间查询)
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e4+;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 struct Tree{
int pre,suf,sub,val;
}tree[maxn<<]; Tree pushup(Tree l,Tree r)
{
Tree rt;
rt.pre=max(l.pre,l.val+r.pre);
rt.suf=max(r.suf,r.val+l.suf);
rt.sub=max(max(l.sub,r.sub),l.suf+r.pre);
rt.val=l.val+r.val;
return rt;
} void build(int l,int r,int rt)
{
if(l==r){
scanf("%d",&tree[rt].val);
tree[rt].pre=tree[rt].suf=tree[rt].sub=tree[rt].val;
return ;
} int m=(l+r)>>;
build(lson);
build(rson);
tree[rt]=pushup(tree[rt<<],tree[rt<<|]);
} void update(int pos,int c,int l,int r,int rt)
{
if(l==r){
tree[rt].pre=tree[rt].suf=tree[rt].sub=tree[rt].val=c;
return ;
} int m=(l+r)>>;
if(pos<=m) update(pos,c,lson);
if(pos> m) update(pos,c,rson);
tree[rt]=pushup(tree[rt<<],tree[rt<<|]);
} Tree query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R){
return tree[rt];
} int m=(l+r)>>;
Tree ret,lret,rret;
int flag1=,flag2=;
if(L<=m) {lret=query(L,R,lson);flag1=;}
if(R> m) {rret=query(L,R,rson);flag2=;} if(flag1&&flag2) ret=pushup(lret,rret);
else if(flag1) ret=lret;
else if(flag2) ret=rret;
return ret;
} int main()
{
int n;
scanf("%d",&n);
build(,n,);
int m;
scanf("%d",&m);
for(int i=;i<=m;i++){
int op,l,r;
scanf("%d%d%d",&op,&l,&r);
if(op==){
update(l,r,,n,);
}
else{
Tree ans=query(l,r,,n,);
printf("%d\n",ans.sub);
}
}
return ;
}
SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并的更多相关文章
- SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)
GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- 数据结构(线段树):SPOJ GSS3 - Can you answer these queries III
GSS3 - Can you answer these queries III You are given a sequence A of N (N <= 50000) integers bet ...
- Can you answer these queries III(线段树)
Can you answer these queries III(luogu) Description 维护一个长度为n的序列A,进行q次询问或操作 0 x y:把Ax改为y 1 x y:询问区间[l ...
- spoj gss2 : Can you answer these queries II 离线&&线段树
1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...
- SPOJ GSS3 Can you answer these queries III
Time Limit: 330MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description You are g ...
- SPOJ GSS3 Can you answer these queries III ——线段树
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...
- Spoj 2713 Can you answer these queries IV 水线段树
题目链接:点击打开链接 题意: 给定n长的序列 以下2个操作 0 x y 给[x,y]区间每一个数都 sqrt 1 x y 问[x, y] 区间和 #include <stdio.h> # ...
- 线段树 SP1716 GSS3 - Can you answer these queries III
SP1716 GSS3 - Can you answer these queries III 题意翻译 n 个数,q 次操作 操作0 x y把A_xAx 修改为yy 操作1 l r询问区间[l, r] ...
随机推荐
- 解决html设置height:100%无效的问题
通常我们需要让自己的网页内容能够更好的适配各种屏幕大小,会采用height:100%,但是我们发现问题出来了,height:100%无效,其实解决办法很简单 解决:你只需要在css处添加上html, ...
- Image Scaling using Deep Convolutional Neural Networks
Image Scaling using Deep Convolutional Neural Networks This past summer I interned at Flipboard in P ...
- SpringCloud (一)Eureka注册中心搭建
前提 系统安装jdk1.8及以上,配置好maven的ide(这里用idea进行演示,maven版本3.5,配置阿里云源) 项目搭建 新建一个maven项目,创建最简单的那种就好,项目名这里为Eurek ...
- cropper.js 跨域问题
this.$clone = $clone = $('<img>'); $clone.one('load', $.proxy(function () { var naturalWidth = ...
- Windows下的MySQL删除data文件夹后……
MySQL删除data文件夹后,怎么都无法启动了,出现错误: 150106 9:28:43 [Note] Plugin 'FEDERATED' is disabled. wampmysqld: Tab ...
- indexof()函数
js 判断字符串是否包含某字符串,String对象中查找子字符,indexOf, 成功,返回索引值,失败返回 -1. 转载: http://www.cnblogs.com/fishtreeyu/arc ...
- 使用Skyworking 作全链路api调用监控,Integration of Skyworking, auditing the whole chain circuit.
Applicable scenario: Structure Map ~ Skywalking uses elasticsearch to store data, don't mistake elas ...
- CodeForces - 1004B
Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she ...
- canvas 绘制星座图(好玩)--转载
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- 59、有用过with statement吗?它的好处是什么?
python中的with语句是用来干嘛的?有什么作用? with语句的作用是通过某种方式简化异常处理,它是所谓的上下文管理器的一种 用法举例如下: with open('output.txt', 'w ...