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] ...
随机推荐
- nova-virt与libvirt
源码版本:H版 nova通过nova/virt/driver.py中的ComputeDriver对底层虚拟化技术进行抽象,不同的虚拟化技术在nova/virt下有不同的目录,里面均有driver.py ...
- [DeeplearningAI笔记]卷积神经网络1.2-1.3边缘检测
4.1卷积神经网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.2边缘检测示例 边缘检测可以视为横向边缘检测和纵向边缘检测如下图所示: 边缘检测的原理是通过一个特定构造的卷积核对原始图 ...
- C语言基本类型的字节数
- == 和 equals,equals 与 hashcode,HashSet 和 HashMap,HashMap 和 Hashtable
一:== 和 equals == 比较引用的地址equals 比较引用的内容 (Object 类本身除外) String obj1 = new String("xyz"); Str ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- Bayesian optimisation for smart hyperparameter search
Bayesian optimisation for smart hyperparameter search Fitting a single classifier does not take long ...
- 重构改善既有代码设计--重构手法14:Hide Delegate (隐藏委托关系)
客户通过一个委托类来调用另一个对象.在服务类上建立客户所需的所有函数,用以隐藏委托关系. 动机:封装即使不是对象的最关机特性,也是最关机特性之一.“封装”意味着每个对象都应该少了解系统的其他部分.如此 ...
- 响应式布局之媒体查询 @media
Media Queries,其作用就是允许添加表达式用以确定媒体的环境情况,以此来应用不同的样式表.换句话说,其允许我们在不改变内容的情况下,改变页面的布局以精确适应不同的设备. 媒体查询有两种玩法, ...
- CentOS 怎么设置某个目录包括子目录的写入权限 777呢
chmod -R 777 文件夹例如:chmod -R 777 /var var的权限就变成777,var下的所有子目录和文件权限都将变成777
- linux 在命令行中通过conda使用anaconda
在 ~/.bash_profile中添加 export PATH="/home/taoke/anaconda/bin:$PATH"