SPOJ GSS3 Can you answer these queries III
来源:互联网 发布:买理财产品赚钱吗 知乎 编辑:程序博客网 时间:2024/05/16 16:55
题意:和上一篇中的意思一样,只是多了一种操作:单点更新 ,查找到叶子位置,改变一下就行~~~
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19937
代码如下
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>using namespace std;#define maxn 50010#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1struct SegNode{ int lsum,rsum,sum,ans;};SegNode tree[maxn<<2];void PushUp(int rt){ tree[rt].sum = tree[rt<<1].sum + tree[rt<<1|1].sum; tree[rt].lsum = max(tree[rt<<1].lsum , tree[rt<<1].sum + tree[rt<<1|1].lsum); tree[rt].rsum = max(tree[rt<<1|1].rsum , tree[rt<<1|1].sum + tree[rt<<1].rsum); tree[rt].ans = max(max(tree[rt<<1].ans , tree[rt<<1|1].ans),tree[rt<<1].rsum + tree[rt<<1|1].lsum);}void build(int l,int r,int rt){ if(l == r){ scanf("%d",&tree[rt].sum); tree[rt].lsum = tree[rt].rsum = tree[rt].ans = tree[rt].sum; return ; } int m = (l + r)/2; build(lson); build(rson); PushUp(rt);}void update(int pos,int val,int l,int r,int rt){ if(l == r){ tree[rt].lsum = tree[rt].rsum = tree[rt].ans = tree[rt].sum = val; return ; } int m = (l + r)/2; if(pos <= m) update(pos , val , lson); else update(pos , val , rson); PushUp(rt);}SegNode query(int L,int R,int l,int r,int rt){ if(L <= l && r <= R){ return tree[rt]; } int m = (l + r)/2; if(R <= m) return query(L , R , lson); else if(L > m) return query(L , R , rson); else{ SegNode result,left,right; left = query(L , m ,lson); right = query(m + 1, R, rson); result.sum = left.sum + right.sum; result.lsum = max(left.lsum , left.sum + right.lsum); result.rsum = max(right.rsum , right.sum + left.rsum); result.ans = max(max(left.ans , right.ans), left.rsum + right.lsum); return result; }}int n,m,op,a,b;int main(){ while(~scanf("%d",&n)){ build(1 , n , 1); scanf("%d",&m); while(m --){ scanf("%d %d %d",&op,&a,&b); if(op == 1) printf("%d\n",query(a , b, 1 , n , 1).ans); else update(a, b, 1, n, 1); } } return 0;}
- SPOJ/GSS3:Can you answer these queries III(线段树)
- SPOJ GSS3 Can you answer these queries III
- SPOJ GSS3 Can you answer these queries III
- SPOJ GSS3 Can you answer these queries III
- SPOJ GSS3 Can you answer these queries III
- SPOJ GSS3 Can you answer these queries III
- SPOJ GSS3 Can you answer these queries III (线段树)
- [SPOJ GSS3] Can you answer these queries III [线段树]
- SPOJ GSS3 Can you answer these queries III
- SPOJ GSS3 Can you answer these queries III
- SPOJ GSS3 Can you answer these queries III
- GSS3 - Can you answer these queries III
- SPOJ 1716 Can you answer these queries III(GSS3 线段树)
- spoj(GSS3) 1716 Can you answer these queries III(线段树求最大子段和)
- SPOJ GSS3 Can you answer these queries III(线段树)
- SPOJ 1716 GSS3 Can you answer these queries III 线段树区间合并
- spoj 1716. Can you answer these queries III
- spoj 1716. Can you answer these queries III(线段树)
- Juery解决tablesorter中文排序和字符范围
- Android平台根目录文件
- java之volatile解析
- “回车”与“换行”
- Nginx 开启 debug 日志的办法
- SPOJ GSS3 Can you answer these queries III
- 为什么Android的Adapter中,bindview被调用了多次
- 编程珠玑第二章
- 笔试题果然有坑啊
- Android中Service类onStartCommand的返回值问题
- form常用提交的2种方法
- Launching the App Store from an iPhone application
- 安卓开发37:自定义的HorizontalScrollView类,使其pageScroll的时候焦点不选中
- AndroidRuntime: Shutting down VM