SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并
Can you answer these queries III
SPOJ - GSS3
这道题和洛谷的小白逛公园一样的题目。
传送门:
洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间合并(单点更新、区间查询)
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn=5e4+10; 5 #define lson l,m,rt<<1 6 #define rson m+1,r,rt<<1|1 7 8 struct Tree{ 9 int pre,suf,sub,val; 10 }tree[maxn<<2]; 11 12 Tree pushup(Tree l,Tree r) 13 { 14 Tree rt; 15 rt.pre=max(l.pre,l.val+r.pre); 16 rt.suf=max(r.suf,r.val+l.suf); 17 rt.sub=max(max(l.sub,r.sub),l.suf+r.pre); 18 rt.val=l.val+r.val; 19 return rt; 20 } 21 22 void build(int l,int r,int rt) 23 { 24 if(l==r){ 25 scanf("%d",&tree[rt].val); 26 tree[rt].pre=tree[rt].suf=tree[rt].sub=tree[rt].val; 27 return ; 28 } 29 30 int m=(l+r)>>1; 31 build(lson); 32 build(rson); 33 tree[rt]=pushup(tree[rt<<1],tree[rt<<1|1]); 34 } 35 36 void update(int pos,int c,int l,int r,int rt) 37 { 38 if(l==r){ 39 tree[rt].pre=tree[rt].suf=tree[rt].sub=tree[rt].val=c; 40 return ; 41 } 42 43 int m=(l+r)>>1; 44 if(pos<=m) update(pos,c,lson); 45 if(pos> m) update(pos,c,rson); 46 tree[rt]=pushup(tree[rt<<1],tree[rt<<1|1]); 47 } 48 49 Tree query(int L,int R,int l,int r,int rt) 50 { 51 if(L<=l&&r<=R){ 52 return tree[rt]; 53 } 54 55 int m=(l+r)>>1; 56 Tree ret,lret,rret; 57 int flag1=0,flag2=0; 58 if(L<=m) {lret=query(L,R,lson);flag1=1;} 59 if(R> m) {rret=query(L,R,rson);flag2=1;} 60 61 if(flag1&&flag2) ret=pushup(lret,rret); 62 else if(flag1) ret=lret; 63 else if(flag2) ret=rret; 64 return ret; 65 } 66 67 int main() 68 { 69 int n; 70 scanf("%d",&n); 71 build(1,n,1); 72 int m; 73 scanf("%d",&m); 74 for(int i=1;i<=m;i++){ 75 int op,l,r; 76 scanf("%d%d%d",&op,&l,&r); 77 if(op==0){ 78 update(l,r,1,n,1); 79 } 80 else{ 81 Tree ans=query(l,r,1,n,1); 82 printf("%d\n",ans.sub); 83 } 84 } 85 return 0; 86 }
转载于:https://www.cnblogs.com/ZERO-/p/10679888.html
SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并相关推荐
- SPOJ - GSS3 Can you answer these queries III(线段树+区间合并)
题目链接:点击查看 题目大意:给出一个长度为n的序列,进行m次操作: 1 x y 查询区间[l,r]中的最大连续子段和 0 x y 将第x个数修改为y 题目分析:因为涉及到单点修改和区间查询等操作 ...
- SP1043 GSS1 - Can you answer these queries I(线段树,区间最大子段和(静态))
题目描述 给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j]:x≤i≤j≤y} ...
- CF911G Mass Change Queries (线段树区间 合并)
题意: 给出一个数列,有q个操作,每种操作是把区间[l,r]中等于x的数改成y.输出q步操作完的数列. 题解: 100个数,很容易想到要从这里进行突破,对于某次操作我们只需要把这个区间的数x给移动到y ...
- 线性代数四之动态DP(广义矩阵加速)——Can you answer these queries III,保卫王国
动态DP--广义矩阵加速 SP1716 GSS3 - Can you answer these queries III description solution code [NOIP2018 提高组] ...
- SPOJ GSS2 Can you answer these queries II (线段树离线) - xgtao -
Can you answer these queries II 这是一道线段树的题目,维护历史版本,给出N(<=100000)个数字(-100000<=x<=100000),要求求出 ...
- HDU 4027 Can you answer these queries?(线段树/区间不等更新)
传送门 Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/6576 ...
- leetcode 729, 731, 732. My Calendar I, II, III | 729. 我的日程安排表 I, II, III(线段树)
729. My Calendar I https://leetcode.com/problems/my-calendar-i/ 题解 看了左神课之后,自己实现了下改造后的线段树(非常不优雅),因为数组 ...
- Codeforces 1114F Please, another Queries on Array? 线段树
Please, another Queries on Array? 利用欧拉函数的计算方法, 用线段树搞一搞就好啦. #include<bits/stdc++.h> #define LL ...
- spoj 2916. Can you answer these queries V(线段树)
题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出n个数,求区间最大子段和,但是限制了子段的起点终点,起点要在[x1,y1]内,终点要在[x2,y2]内. 思路 ...
最新文章
- C# 四舍五入round函数使用的代码
- python中time的用法_python中的计时器timeit的使用方法
- 3.9 YOLO算法-深度学习第四课《卷积神经网络》-Stanford吴恩达教授
- Hadoop Mapreduce 调优
- 【Django 2021年最新版教程19】数据库查询 model filter 条件或or
- 现金支票打印模板excel_Excel的正确使用技巧-Excel的提速大法
- 图片放大后模糊怎么变清晰?
- C# Resharper的简单使用介绍
- 练习题58:接口练习1:用接口、多态、方法来实现:麻雀会飞 鹦鹉会飞 鸵鸟不会飞 企鹅不会飞 直升飞机会飞
- user后面的计算机名更改,更改电脑用户名(可更改C:\Users\用户名)
- 实验吧-web-天下武功唯快不破
- Excel 文件的扩展名 .xls 与 .xlsx 的区别
- Nginx-动静分离与 URLRwrite
- 今天气温达到了39度 单位给买了雪糕降温
- win10中Charles从下载安装到证书设置和雷电模拟器或浏览器中抓包测试
- pool(三)——Timer
- java中API什么意思
- 第七届高教杯计算机绘图,第七届”高教杯“全国大学生先进成图技术与产品信息建模创新大赛机械类计算机绘图试卷.pdf...
- 网易im 会话列表不显示的问题
- Android7.0 Doze模式分析(三)alarm