SPOJ GSS3 Can you answer these queries III
来源:互联网 发布:图书管理信息系统java 编辑:程序博客网 时间:2024/04/29 23:45
题意:和上一篇中的意思一样,只是多了一种操作:单点更新 ,查找到叶子位置,改变一下就行~~~
题目链接: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(线段树)
- Android使用代码来写布局实例
- 数据结构中La表的数据合并到Lb表中
- Android XmlPullParser的简单应用
- 带有详细书签的IT电子书大全
- Android之画小球图实例[后续添加小球移动功能]
- SPOJ GSS3 Can you answer these queries III
- HDU 4101 Ali and Baba
- rac 中unknown 状态处理
- php中exec和system shell_exec区别
- 抓紧时间学习!
- HDU2825--Wireless Password
- Deploying a Rails application in Tomcat with JRuby: A concise tutorial
- WinFx
- C#连接EXCEL和ACCESS字符串2003及2007版