SPOJ GSS 1. Can you answer these queries I
来源:互联网 发布:唐顿庄园知乎 编辑:程序博客网 时间:2024/05/20 02:28
题意
给一个含有 n(1<=n<=50000) 个整数的序列和 m 个 query,每个 query 的格式是 a b,对于每个 query,求出在 [a, b] 内最大的连续子段和并输出
做法分析
用线段树维护一段区间 [L, R] 内的:
Lmax:包括左边 a[L] 在内的最大连续和
Rmax:包括右边 a[R} 在内的最大连续和
sum: 整段区间所有元素的和
Max: 整段区间内的最大连续子段和
在向上传递的时候:
father.Lmax=max{Lson.Lmax, Lson.sum+Rson.Lmax}
father.Rmax=max{Rson.Rmax, Rson,sum+Lson.Rmax}
father.Max=max{Lson.Max, Rson.Max, Lson.Rmax+Rson.Lmax}
father.sum=Lson.sum+Rson.sum
做这道题学到了一个新的线段树写法,返回的居然是一个节点,而不是一个具体的值,这样有效的提高了时间效率和减少了代码量。以前写的 200行+ 的代码实在是太不优雅了,被人家 70行不到 的完爆啊!
参考代码
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const int N=50010;const int INT_INF=0x3fffffff;int val[N];class seg_tree{private:struct data{int st, en, val, Max, Lmax, Rmax;} T[N<<2];public:data update(data Lson, data Rson){data fa;fa.Lmax=max(Lson.Lmax, Lson.val+Rson.Lmax);fa.Rmax=max(Rson.Rmax, Rson.val+Lson.Rmax);fa.Max=max(max(Lson.Max, Rson.Max), Lson.Rmax+Rson.Lmax);fa.val=Lson.val+Rson.val;return fa;}void build(int id, int st, int en){T[id].st=st, T[id].en=en;if(st==en){T[id].val=T[id].Max=T[id].Lmax=T[id].Rmax=val[st];return;}int mid=(st+en)>>1;build(id<<1, st, mid), build(id<<1|1, mid+1, en);data now=update(T[id<<1], T[id<<1|1]);T[id].val=now.val, T[id].Max=now.Max, T[id].Lmax=now.Lmax, T[id].Rmax=now.Rmax;}data query(int id, int L, int R){if(L<=T[id].st && T[id].en<=R){return T[id];}int mid=(T[id].st+T[id].en)>>1;if(R<=mid) return query(id<<1, L, R);else if(L>mid) return query(id<<1|1, L, R);else return update(query(id<<1, L, mid), query(id<<1|1, mid+1, R));}} seg;int main(){int n; scanf("%d", &n);for(int i=1; i<=n; i++)scanf("%d", &val[i]);seg.build(1, 1, n);int m; scanf("%d", &m);for(int i=1, L, R; i<=m; i++){scanf("%d%d", &L, &R);printf("%d\n", seg.query(1, L, R).Max);}return 0;}
- SPOJ GSS 1. Can you answer these queries I
- SPOJ GSS 5. Can you answer these queries V
- SPOJ GSS1 - Can you answer these queries I(线段树维护GSS)
- spoj SPOJ 1043 Can you answer these queries I
- spoj 1043 Can you answer these queries I
- SPOJ 1043 1043. Can you answer these queries I
- SPOJ GSS1 Can you answer these queries I
- SPOJ GSS1 Can you answer these queries I
- spoj 1043. Can you answer these queries I (线段树)
- 【SPOJ】Can you answer these queries I【线段树】
- [SPOJ GSS1] Can you answer these queries I [线段树]
- 【SPOJ】 1043 Can you answer these queries I 线段树
- spoj 1043——Can you answer these queries I
- SPOJ GSS1 Can you answer these queries I
- SPOJ GSS1 Can you answer these queries I
- SPOJ GSS1 Can you answer these queries I
- SPOJ GSS1 Can you answer these queries I
- SPOJ GSS1 Can you answer these queries I
- 表单的焦点顺序和对应enter键提交
- 几个常用的JavaScript字符串处理函数– split()、join()、substring()和 .
- android手机抓包过程
- nginx安装配置
- 使用宏对word中的图片批量调整大小
- SPOJ GSS 1. Can you answer these queries I
- 线程系列二
- Memcached中一致性哈希(Consistent Hashing)的运用
- 推广优化循序渐进
- EL表达式
- Amazon Leadership Principles
- [全程建模]业务建模和用例模型以及需求规格说明书的关系
- Visual Studio 2010 下载,安装,激活,序列号,注册码,下载地址
- django 静态文件的处理 DEBUG False 页面变乱