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] ...
随机推荐
- Nexus(Maven仓库私服)安装
一.Nexus介绍 Nexus 是Maven仓库管理器,如果你使用Maven,你可以从Maven中央仓库 下载所需要的构件(artifact),但这通常不是一个好的做法,你应该在本地架设一个Maven ...
- python---twisted的使用,使用其模拟Scrapy
twisted的网络使用 twisted的异步使用 一:简单使用 from twisted.internet import defer from twisted.web.client import g ...
- c# 计算星座
public string xz(DateTime birthday) { float birthdayF = 0.00F; if (birthday.Month == 1 && bi ...
- PHP提取链接批量下载
2014年年初的时候,曾经受委托完成一个视频网站,那时最大的技术障碍是一个大视频比如500MB,在一个带宽环境不怎么快的服务器(比如1Mbps)上提供播放的问题. 这里会遇到两种情况,第一种情况是客户 ...
- 【51nod】1238 最小公倍数之和 V3 杜教筛
[题意]给定n,求Σi=1~nΣj=1~n lcm(i,j),n<=10^10. [算法]杜教筛 [题解]就因为写了这个非常规写法,我折腾了3天…… $$ans=\sum_{i=1}^{n}\s ...
- 【CodeForces】908 D. New Year and Arbitrary Arrangement
[题目]Good Bye 2017 D. New Year and Arbitrary Arrangement [题意]给定正整数k,pa,pb,初始有空字符串,每次有pa/(pa+pb)的可能在字符 ...
- Docker 配置国内镜像拉取中心,Configure docker to use faster registries in China.
Networking in China is really bad when it comes to using some cloud based tools like docker, it's us ...
- HTML如何编写为桌面程序
学过/用过HTML的人应该都知道HTML是标记语言,是在网页上执行/使用的,在这里小编告诉你HTML也可以用来做桌面程序,这种桌面程序一般是微客户端 工具/原料 html dreamweaver ...
- Axure RP 授权码
Axure RP 8.1.0.3372Licensee:KoshyKey:wTADPqxn3KChzJxLmUr5jTTitCgsfRkftQQ1yIG9HmK83MYSm7GPxLREGn+Ii6x ...
- Vue-Module
由于使用单一状态树,应用的所有状态会集中到一个比较大的对象.当应用变得非常复杂时,store 对象就有可能变得相当臃肿. 为了解决以上问题,Vuex 允许我们将 store 分割成模块(module) ...