【SPOJ】6779 Can you answer these queries VII 树链剖分+线段树 求树上的最大子段和
来源:互联网 发布:记忆力软件训练 编辑:程序博客网 时间:2024/06/03 16:24
传送门:【SPOJ】6779 Can you answer these queries VII
题目分析:树链剖分然后就是线段树求最大子段和了,注意细节方面就好。
代码跑了倒数,难得,可是不知道怎么优化,先放放好了
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;typedef long long LL ;#define travel( e , H , u ) for ( Edge* e = H[u] ; e ; e = e -> next )#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )#define FOR( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )#define clr( a , x ) memset ( a , x , sizeof a )#define cpy( a , x ) memcpy ( 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 rt o , l , r#define root 1 , 1 , n#define mid ( ( l + r ) >> 1 )const int MAXN = 100005 ;const int MAXE = 200005 ;const int INF = 0x3f3f3f3f ;struct Edge {int v ;Edge* next ;} E[MAXE] , *H[MAXN] , *edge ;int Lmax[MAXN << 2] ;int maxv[MAXN << 2] ;int Rmax[MAXN << 2] ;int sum[MAXN << 2] ;int set[MAXN << 2] ;int siz[MAXN] ;int pos[MAXN] ;int val[MAXN] ;int top[MAXN] ;int son[MAXN] ;int pre[MAXN] ;int dep[MAXN] ;int idx[MAXN] ;int tree_idx ;int n , q ;void clear () {edge = E ;tree_idx = 0 ;clr ( H , 0 ) ;siz[0] = 0 ;dep[1] = 0 ;pre[1] = 0 ;}void addedge ( int u , int v ) {edge -> v = v ;edge -> next = H[u] ;H[u] = edge ++ ;edge -> v = u ;edge -> next = H[v] ;H[v] = edge ++ ;}void dfs ( int u ) {siz[u] = 1 ;son[u] = 0 ;travel ( e , H , u ) {int v = e -> v ;if ( v != pre[u] ) {pre[v] = u ;dep[v] = dep[u] + 1 ;dfs ( v ) ;siz[u] += siz[v] ;if ( siz[v] > siz[son[u]] ) son[u] = v ;}}}void rewrite ( int u , int top_element ) {top[u] = top_element ;pos[u] = ++ tree_idx ;idx[tree_idx] = u ;if ( son[u] ) rewrite ( son[u] , top_element ) ;travel ( e , H , u ) {int v = e -> v ;if ( v != pre[u] && v != son[u] ) rewrite ( v , v ) ;}}void fun ( int o , int l , int r , int v ) {set[o] = v ;Lmax[o] = maxv[o] = Rmax[o] = sum[o] = v * ( r - l + 1 ) ;}void pushup ( int o ) {sum[o] = sum[ls] + sum[rs] ;maxv[o] = max ( maxv[ls] , maxv[rs] ) ;Lmax[o] = max ( Lmax[ls] , Lmax[rs] + sum[ls] ) ;Rmax[o] = max ( Rmax[rs] , Rmax[ls] + sum[rs] ) ;maxv[o] = max ( Rmax[ls] + Lmax[rs] , maxv[o] ) ;}void pushdown ( int o , int l , int r ) {if ( set[o] != INF ) {int m = mid ;fun ( lson , set[o] ) ;fun ( rson , set[o] ) ;set[o] = INF ;}}void build ( int o , int l , int r ) {set[o] = INF ;if ( l == r ) {sum[o] = Lmax[o] = maxv[o] = Rmax[o] = val[idx[l]] ;return ;}int m = mid ;build ( lson ) , build ( rson ) ;pushup ( o ) ;}void sub_update ( int L , int R , int v , int o , int l , int r ) {if ( L <= l && r <= R ) {fun ( rt , v ) ;return ;}int m = mid ;pushdown ( rt ) ;if ( L <= m ) sub_update ( L , R , v , lson ) ;if ( m < R ) sub_update ( L , R , v , rson ) ;pushup ( o ) ;}void update ( int x , int y , int v ) {while ( top[x] != top[y] ) {if ( dep[top[x]] < dep[top[y]] ) swap ( x , y ) ;sub_update ( pos[top[x]] , pos[x] , v , root ) ;x = pre[top[x]] ;}if ( dep[x] > dep[y] ) swap ( x , y ) ;sub_update ( pos[x] , pos[y] , v , root ) ;}int query_sum ( int L , int R , int o , int l , int r ) {if ( L <= l && r <= R ) return sum[o] ;int m = mid ;pushdown ( rt ) ;if ( R <= m ) return query_sum ( L , R , lson ) ;if ( m < L ) return query_sum ( L , R , rson ) ;return query_sum ( L , R , lson ) + query_sum ( L , R , rson ) ;}int query_Lmax ( int L , int R , int o , int l , int r ) {if ( L <= l && r <= R ) return Lmax[o] ;int m = mid ;pushdown ( rt ) ;if ( R <= m ) return query_Lmax ( L , R , lson ) ;if ( m < L ) return query_Lmax ( L , R , rson ) ;return max ( query_Lmax ( L , R , lson ) , query_Lmax ( L , R , rson ) + query_sum ( L , R , lson ) ) ;}int query_Rmax ( int L , int R , int o , int l , int r ) {if ( L <= l && r <= R ) return Rmax[o] ;int m = mid ;pushdown ( rt ) ;if ( R <= m ) return query_Rmax ( L , R , lson ) ;if ( m < L ) return query_Rmax ( L , R , rson ) ;return max ( query_Rmax ( L , R , lson ) + query_sum ( L , R , rson ) , query_Rmax ( L , R , rson ) ) ;}int query_maxv ( int L , int R , int o , int l , int r ) {if ( L <= l && r <= R ) return maxv[o] ;int m = mid ;pushdown ( rt ) ;if ( R <= m ) return query_maxv ( L , R , lson ) ;if ( m < L ) return query_maxv ( L , R , rson ) ;int res = max ( query_maxv ( L , R , lson ) , query_maxv ( L , R , rson ) ) ;return max ( res , query_Rmax ( L , R , lson ) + query_Lmax ( L , R , rson ) ) ;}int query ( int x , int y ) {int res = 0 , lmax = 0 , rmax = 0 ;while ( top[x] != top[y] ) {if ( dep[top[x]] < dep[top[y]] ) {swap ( x , y ) ;swap ( lmax , rmax ) ;}int L = pos[top[x]] , R = pos[x] ;res = max ( res , query_maxv ( L , R , root ) ) ;res = max ( res , max ( 0 , query_Rmax ( L , R , root ) ) + lmax ) ;lmax = max ( max ( 0 , query_Lmax ( L , R , root ) ) , query_sum ( L , R , root ) + lmax ) ;x = pre[top[x]] ;}if ( dep[x] > dep[y] ) {swap ( x , y ) ;swap ( lmax , rmax ) ;}int L = pos[x] , R = pos[y] ;res = max ( res , query_maxv ( L , R , root ) ) ;res = max ( res , lmax + query_sum ( L , R , root ) + rmax ) ;res = max ( res , lmax + max ( 0 , query_Lmax ( L , R , root ) ) ) ;res = max ( res , rmax + max ( 0 , query_Rmax ( L , R , root ) ) ) ;return res ;}void solve () {int op , u , v , c ;clear () ;FOR ( i , 1 , n ) scanf ( "%d" , &val[i] ) ;rep ( i , 1 , n ) {scanf ( "%d%d" , &u , &v ) ;addedge ( u , v ) ;}dfs ( 1 ) ;rewrite ( 1 , 1 ) ;build ( root ) ;scanf ( "%d" , &q ) ;while ( q -- ) {scanf ( "%d%d%d" , &op , &u , &v ) ;if ( op == 1 ) printf ( "%d\n" , query ( u , v ) ) ;else {scanf ( "%d" , &c ) ;update ( u , v , c ) ;}}}int main () {while ( ~scanf ( "%d" , &n ) ) solve () ;return 0 ;}
0 0
- 【SPOJ】6779 Can you answer these queries VII 树链剖分+线段树 求树上的最大子段和
- Spoj 6779 Can you answer these queries VII 树链剖分 在树上任意路径的最大子段和 区间修改点权
- spoj(GSS3) 1716 Can you answer these queries III(线段树求最大子段和)
- Spoj 2916 Can you answer these queries V 线段树 求任意重叠区间的最大子段和
- SPOJ 1043 Can you answer these queries I 求任意区间最大连续子段和 线段树
- Spoj 1716 Can you answer these queries III 线段树 单点修改 区间求最大子段和
- 【SPOJ-GSS1】Can you answer these queries I【线段树】【最大子段和】
- SPOJ-GSS7 Can you answer these queries VII(树上区间最大和)
- Spoj 1557 Can you answer these queries II 线段树 任意区间最大子段和 不重复数字
- SPOJ GSS1 Can you answer these queries I (线段树求区间最大连续和)
- SPOJ 6779 Can you answer these queries VII 树链剖分
- SPOJ 1557 Can you answer these queries II(离线处理+线段树求历史最大)
- spoj 6779. Can you answer these queries VII(树链剖分)
- 动态树LCT(SPOJ 6779 Can you answer these queries VII)
- 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 1716. Can you answer these queries III(线段树)
- 马化腾罕见撰文:互联网最大红利
- Junit测试,断言
- HDU 1730 Northcott Game(nim博弈)
- [译]在Tiled Map中使用碰撞检测(一) 创建基于Tiled Map的游戏
- 网络的connect如何超时设置?MFC socket异步 timeout select
- 【SPOJ】6779 Can you answer these queries VII 树链剖分+线段树 求树上的最大子段和
- throw 与 throws的区别与联系是什么?
- node.js搭建https代理服务器
- Oracle内置函数SQLCODE和SQLERRM的使用
- 设置Linux下Mysql表名不区分大小写
- 汉诺塔游戏求解
- node-webkit让web技术编写桌面应用
- [译]在Tiled Map中使用碰撞检测(二) TMX地图中的碰撞检测
- transform perspective顺序问题,perspective需要在前面