【SPOJ】2916 Can you answer these queries V 线段树
2014-09-04 13:04
531 查看
传送门:【SPOJ】2916 Can you answer these queries V
题目分析:仔细一点就行了。。
共分两种情况:
1.y1 <= x2,此时[ y1 , x2 ]内的数一定要取,两边看情况。
2.x2 < y1,此时分成三个区间[ x1 , x2 ] , [ x2 , y1 ] , [ y1 , y2 ],然后一共有三种选择:
1.[ x2 , y1 ]中选取最大连续和
2.[ x1 , x2 ]中选取最大右连续和,[ x2 , y2 ]中选取最大左连续和
3.[ x1 , y1 ]中选取最大右连续和,[ y1 , y2 ]中选取最大左连续和
代码如下:
题目分析:仔细一点就行了。。
共分两种情况:
1.y1 <= x2,此时[ y1 , x2 ]内的数一定要取,两边看情况。
2.x2 < y1,此时分成三个区间[ x1 , x2 ] , [ x2 , y1 ] , [ y1 , y2 ],然后一共有三种选择:
1.[ x2 , y1 ]中选取最大连续和
2.[ x1 , x2 ]中选取最大右连续和,[ x2 , y2 ]中选取最大左连续和
3.[ x1 , y1 ]中选取最大右连续和,[ y1 , y2 ]中选取最大左连续和
代码如下:
#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 , r const int MAXN = 10005 ; int maxv[MAXN << 2] , lmax[MAXN << 2] , rmax[MAXN << 2] , num[MAXN] , sum[MAXN] ; void build ( int o , int l , int r ) { if ( l == r ) { maxv[o] = lmax[o] = rmax[o] = num[l] ; return ; } int m = mid ; build ( lson ) , build ( rson ) ; lmax[o] = max ( lmax[ls] , sum[m] - sum[l - 1] + lmax[rs] ) ; rmax[o] = max ( rmax[rs] , sum[r] - sum[m] + rmax[ls] ) ; maxv[o] = max ( max ( maxv[ls] , maxv[rs] ) , rmax[ls] + lmax[rs] ) ; } int Lquery ( int L , int R , int o , int l , int r ) { if ( L > R ) return 0 ; if ( L <= l && r <= R ) return lmax[o] ; int m = mid ; if ( R <= m ) return Lquery ( L , R , lson ) ; if ( m < L ) return Lquery ( L , R , rson ) ; return max ( Lquery ( L , m , lson ) , Lquery ( m + 1 , R , rson ) + sum[m] - sum[L - 1] ) ; } int Rquery ( int L , int R , int o , int l , int r ) { if ( L > R ) return 0 ; if ( L <= l && r <= R ) return rmax[o] ; int m = mid ; if ( R <= m ) return Rquery ( L , R , lson ) ; if ( m < L ) return Rquery ( L , R , rson ) ; return max ( Rquery ( m + 1 , R , rson ) , Rquery ( L , m , lson ) + sum[R] - sum[m] ) ; } int query ( int L , int R , int o , int l , int r ) { if ( L > R ) return 0 ; 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 ) ; int ans = max ( query ( L , m , lson ) , query ( m + 1 , R , rson ) ) ; return max ( ans , Rquery ( L , m , lson ) + Lquery ( m + 1 , R , rson ) ) ; } void solve () { int n , m ; int x1 , x2 , y1 , y2 ; scanf ( "%d" , &n ) ; FOR ( i , 1 , n ) scanf ( "%d" , &num[i] ) ; FOR ( i , 1 , n ) sum[i] = num[i] + sum[i - 1] ; build ( root ) ; scanf ( "%d" , &m ) ; while ( m -- ) { scanf ( "%d%d%d%d" , &x1 , &y1 , &x2 , &y2 ) ; int ans = 0 ; if ( y1 <= x2 ) { ans = Rquery ( x1 , y1 , root ) + Lquery ( x2 , y2 , root ) + sum[x2 - 1] - sum[y1] ; } else { ans = query ( x2 , y1 , root ) ;//1 ans = max ( ans , Rquery ( x1 , x2 , root ) + Lquery ( x2 , y2 , root ) - num[x2] ) ;//2 ans = max ( ans , Rquery ( x1 , y1 , root ) + Lquery ( y1 , y2 , root ) - num[y1] ) ;//3 } printf ( "%d\n" , ans ) ; } } int main () { int T ; scanf ( "%d" , &T ) ; while ( T -- ) solve () ; return 0 ; }
相关文章推荐
- spoj 2916. Can you answer these queries V(线段树)
- SPOJ 2916 Can you answer these queries V(线段树-分类讨论)
- Spoj 2916 Can you answer these queries V 线段树 求任意重叠区间的最大子段和
- SPOJ 2916 Can you answer these queries V(GSS5 线段树)
- GSS5 spoj 2916. Can you answer these queries V 线段树
- 【SPOJ】Can you answer these queries I【线段树】
- 【SPOJ】 1043 Can you answer these queries I 线段树
- 【SPOJ】Can you answer these queries III【线段树】
- 【SPOJ】Can you answer these queries IV【线段树】
- SPOJ GSS7 Can you answer these queries VII ——树链剖分 线段树
- Spoj 1716 Can you answer these queries III 线段树 单点修改 区间求最大子段和
- spoj 1557 Can you answer these queries II (gss2)线段树
- spoj 1043 Can you answer these queries I(线段树)
- [SPOJ GSS1] Can you answer these queries I [线段树]
- spoj 1043. Can you answer these queries I (线段树)
- SPOJ 1557 Can you answer these queries II(GSS2 线段树)
- SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)
- spoj Can you answer these queries I(线段树 单点更新 区间查询)
- SPOJ GSS3 Can you answer these queries III (线段树)
- [SPOJ GSS2] Can you answer these queries II [线段树]