【SPOJ】1716 Can you answer these queries III 线段树
来源:互联网 发布:星摩小妖妖淘宝 编辑:程序博客网 时间:2024/05/21 19:22
传送门:【SPOJ】1716 Can you answer these queries III
题目分析:赤果果的线段树求最大连续和。。
代码如下:
#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%d" , &ch , &x , &y ) ;if ( ch == 0 ) update ( x , y , root ) ;else printf ( "%d\n" , query ( x , y , root ) ) ;}}int main () {while ( ~scanf ( "%d" , &n ) ) solve () ;return 0 ;}
0 0
- 【SPOJ】1716 Can you answer these queries III 线段树
- 【线段树】 SPOJ 1716 Can you answer these queries III
- SPOJ/GSS3:Can you answer these queries III(线段树)
- spoj 1716. Can you answer these queries III(线段树)
- SPOJ GSS3 Can you answer these queries III (线段树)
- 【SPOJ】Can you answer these queries III【线段树】
- [SPOJ 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 1716 GSS3 Can you answer these queries III 线段树区间合并
- Spoj 1716 Can you answer these queries III
- spoj 1716 Can you answer these queries III
- Spoj 1716 Can you answer these queries III
- SPOJ GSS3 Can you answer these queries III(线段树)
- spoj 1043. Can you answer these queries I (线段树)
- spoj 1557. Can you answer these queries II(线段树)
- spoj 2713. Can you answer these queries IV(线段树)
- spoj 2916. Can you answer these queries V(线段树)
- ubuntu12.04 gedit 打开 windows 分区中的 txt 文件乱码
- 解决Emgu.CV.CvInvoke 无法加载 DLL“opencv_core242”
- 谈谈对软件开发的理解
- 类 OnekeyShare 帮助文档
- 一道java线程面试题--设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。
- 【SPOJ】1716 Can you answer these queries III 线段树
- Jquery EasyUI的添加,修改,删除,查询
- VS2012 中文版转英文版 英文版转中文版 界面语言切换
- JAVA八种基本数据类型
- c++遍历目录下文件
- Cocos2dx 3.2 横版过关游戏Brave学习笔记(五)
- java POI实现向Excel中插入图片
- WeChall Encodings: URL
- CCNP路由实验之十二 MPLS