【SPOJ】 1043 Can you answer these queries I 线段树
来源:互联网 发布:淘宝聚沙塔 编辑:程序博客网 时间:2024/05/29 18:01
传送门:【SPOJ】 1043 Can you answer these queries I
题目分析:线段树查询任意区间内的最大子区间连续和。
代码如下:
#include <cmath>#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;#define REP( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )#define FOR( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )#define REV( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )#define CLR( a , x ) memset ( a , x , sizeof a )#define ls ( o << 1 )#define rs ( o << 1 | 1 )#define lson ls , l , m#define rson rs , m + 1 , r#define mid ( ( l + r ) >> 1 )#define root 1 , 1 , n#define rt o , l , rconst int MAXN = 50005 ;int sum[MAXN << 2] , maxv[MAXN << 2] , lmax[MAXN << 2] , rmax[MAXN << 2] ;int n ;void pushup ( int o ) {sum[o] = sum[ls] + sum[rs] ;lmax[o] = max ( lmax[ls] , sum[ls] + lmax[rs] ) ;rmax[o] = max ( rmax[rs] , sum[rs] + rmax[ls] ) ;maxv[o] = max ( max ( maxv[ls] , maxv[rs] ) , rmax[ls] + lmax[rs] ) ;}void build ( int o , int l , int r ) {if ( l == r ) {scanf ( "%d" , &sum[o] ) ;lmax[o] = maxv[o] = rmax[o] = sum[o] ;return ;}int m = mid ;build ( lson ) ;build ( rson ) ;pushup ( o ) ;}void update ( int x , int v , int o , int l , int r ) {if ( l == r ) {sum[o] = lmax[o] = maxv[o] = rmax[o] = v ;return ;}int m = mid ;if ( x <= m ) update ( x , v , lson ) ;else update ( x , v , rson ) ;pushup ( o ) ;}int query_sum ( int L , int R , int o , int l , int r ) {if ( L <= l && r <= R ) return sum[o] ;int m = mid ;if ( R <= m ) return query_sum ( L , R , lson ) ;if ( m < L ) return query_sum ( L , R , rson ) ;return query_sum ( L , m , lson ) + query_sum ( m + 1 , R , rson ) ;}int max_prefix ( int L , int R , int o , int l , int r ) {if ( L <= l && r <= R ) return lmax[o] ;int m = mid ;if ( R <= m ) return max_prefix ( L , R , lson ) ;if ( m < L ) return max_prefix ( L , R , rson ) ;return max ( max_prefix ( L , m , lson ) , max_prefix ( m + 1 , R , rson ) + query_sum ( L , m , lson ) ) ;}int max_suffix ( int L , int R , int o , int l , int r ) {if ( L <= l && r <= R ) return rmax[o] ;int m = mid ;if ( R <= m ) return max_suffix ( L , R , lson ) ;if ( m < L ) return max_suffix ( L , R , rson ) ;return max ( max_suffix ( m + 1 , R , rson ) , max_suffix ( L , m , lson ) + query_sum ( m + 1 , R , rson ) ) ;}int query ( int L , int R , int o , int l , int r ) {if ( L <= l && r <= R ) return maxv[o] ;int m = mid ;if ( R <= m ) return query ( L , R , lson ) ;if ( m < L ) return query ( L , R , rson ) ;return max ( max ( query ( L , m , lson ) , query ( m + 1 , R , rson ) ) , max_suffix ( L , m , lson ) + max_prefix ( m + 1 , R , rson ) ) ;}void solve () {int q , ch , x , y ;build ( root ) ;scanf ( "%d" , &q ) ;while ( q -- ) {scanf ( "%d%d" , &x , &y ) ;printf ( "%d\n" , query ( x , y , root ) ) ;}}int main () {while ( ~scanf ( "%d" , &n ) ) solve () ;return 0 ;}
0 0
- 【SPOJ】 1043 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(GSS1 线段树)
- 【线段树】 SPOJ 1043 Can you answer these queries I 区间合并
- SPOJ 1043 Can you answer these queries I (超强线段树)
- spoj SPOJ 1043 Can you answer these queries I
- SPOJ/GSS3:Can you answer these queries III(线段树)
- spoj 1557. Can you answer these queries II(线段树)
- spoj 1716. Can you answer these queries III(线段树)
- spoj 2713. Can you answer these queries IV(线段树)
- spoj 2916. Can you answer these queries V(线段树)
- SPOJ GSS3 Can you answer these queries III (线段树)
- SPOJ GSS4 Can you answer these queries IV (线段树)
- 【SPOJ】Can you answer these queries III【线段树】
- [SPOJ GSS2] Can you answer these queries II [线段树]
- [SPOJ GSS3] Can you answer these queries III [线段树]
- mciSendCommand函数实例
- Android Handler Runnable和Thread之间的区别和联系详解
- 未能加载文件或程序集“XXXXXX”或它的某一个依赖项。试图加载格式不正确的程序
- 独立游戏demo-FoodStall3.0
- Android ListView中的项中包含RadioGroup时,RadioButton混乱选中的问题的解决方法
- 【SPOJ】 1043 Can you answer these queries I 线段树
- 剑指offer--例题
- Mapreduce计算框架涉及的技术(3)--以多视图方式看mapreduce
- vc实现控制台绘图
- TortoiseGit拉代码需要密码解决方法
- PID26 线性DP
- 迅雷近几年笔试题及其分析
- (白书训练计划)UVa 11572 Unique Snowflakes(窗口滑动法)
- 各大电商的缓存策略