hdu 5759 2016 Multi-University Training Contest 3 Gardener Bo 解题报告

来源:互联网 发布:天猫耐克官网抢鞋软件 编辑:程序博客网 时间:2024/06/05 20:12

这题。。。。不想评价了






题意:给一棵树,两个操作,每次将一个点和他的子辈,孙辈节点的值+c 或询问 f(u),




题解:

对于每次加值的操作,我们可以分别算出对这些点加了c后,对于其他点的f值的贡献,可以看出每次修改一个点u,他会影响的只有u和u的孙辈、子辈、祖先节点,一共有4种贡献

官方题解给了公式,其实还是比较好懂的



然后对于4种贡献,我们分开维护

1:对于每个点,(size(v)+1)是固定的,所以按照bfs序建一个线段树,记录每个点孙辈节点的bfs序的两端,每次用线段树区间加x,统计答案时乘上(size(v)+1)

2:类似1,因为(2+size(v)*sons1(v))固定,仿照第一种情况,建一棵线段树记录每个点子辈节点bfs序的两端,每次也是区间+x

3:开个数组维护单点,每次修改u,直接加x,统计答案时将累计的乘上后面的那一串东西






第四种整个式子没有不变量,我们不可能往上逐个修改u的祖先,所以这里要用到树链剖分。

首先定义v为u的一个祖先,w为链 [ u,v )上深度最浅的点,son为v的重孩子

我们对u到根的路径上所有点在线段树上+x*(sons2(u)+1),然后每次询问一个点v答案的时候,在答案里加上这颗线段树上的值×(size(v)-size(son))     但是这样做有可能错

这种情况对于v和w都在同一重链上的情况是没问题的,但如果v→w是v的一条轻边,那么统计答案的时候就会少乘        ( size(son) - size(w) )  , 所以每次跳到一条轻边的时候,在这条轻边的父亲f的答案里加上 

x*(sons2(u)+1)*(size(v)-size(w))  ,这样可以补上线段树里多减的


然后........就没了

我觉得打起来感觉不太好............



code:

#include<set>#include<map>#include<deque>#include<queue>#include<stack>#include<ctime>#include<ctime>#include<vector>#include<string>#include<bitset>#include<cstdio>#include<cstdlib>#include<cstring>#include<climits>#include<complex>#include<iostream>#include<algorithm>#define ll long longusing namespace std;inline void read( ll &x ){char c; int f=1;while( !( (c=getchar())>='0' && c<='9' ) )if( c == '-' ) f = -1;x = c-'0';while( (c=getchar())>='0' && c<='9' )(x*=10) += c-'0';x *= f;}const int maxn = 310000;const ll Mod = 1ll<<32;queue< int >Q;struct node{int y,next;}a[maxn<<1]; int bnum,z,len,first[maxn];int siz[maxn],son[maxn],fa[maxn],top[maxn],w[maxn];int son1[maxn],son2[maxn],bid[maxn];int sl1[maxn],sr1[maxn],sl2[maxn],sr2[maxn];ll tr[5][maxn<<2],tr3[maxn];ll a1[maxn],a2[maxn],a3[maxn],a4[maxn];ll s[maxn],sum[maxn];int n,m;ll M( ll x ){if( x < Mod && x > -Mod ) return x;return x-Mod*(x/Mod);}void insert( int x,int y ){len++;a[len].y = y;a[len].next =first[x]; first[x] = len;}void dfs( int x ){s[x] = sum[x];siz[x] = 1; son[x] = 0; son1[x] = son2[x] = 0;a3[x] = 2;for( int k=first[x];k;k=a[k].next ){int y = a[k].y;if( y != fa[x] ){son1[x] ++;son2[x] ++;fa[y]= x;dfs( y );s[x] = M( s[x] - siz[y]*sum[y] );a3[x] -= (siz[y]*son1[y]);sum[x] = M(sum[x]+sum[y]);son2[x] += son1[y];siz[x] += siz[y];if( siz[son[x]] < siz[y] ) son[x] = y;}}s[x] = M( s[x]+siz[x]*sum[x] );a1[x] = siz[x]+1;a2[x] = M(son1[x]*siz[x]+2);a3[x] = M(a3[x] + son2[x]*siz[x]) ;a4[x] = siz[x]-siz[son[x]];}void dfs2( int x ){sl1[x] = sl2[x] = n+1;sr1[x] = sr2[x] = 0;for( int k=first[x];k;k=a[k].next ){int y = a[k].y;if( y != fa[x] ){dfs2( y );sl1[x] = min( sl1[x],bid[y] );sl2[x] = min( sl2[x],sl1[y] );sr1[x] = max( sr1[x],bid[y] );sr2[x] = max( sr2[x],sr1[y] );}}}void build_tree( int x,int tp ){top[x] = tp; w[x] = ++z;if( son[x] ) build_tree( son[x],tp );for( int k=first[x];k;k=a[k].next ){int y=  a[k].y;if( y != son[x] && y != fa[x] )build_tree( y,y );}}void bfs(  ){bnum = 0; Q.push( 1 );while( !Q.empty() ){int x = Q.front(); Q.pop(); bid[x] = ++bnum;for( int k=first[x];k;k=a[k].next ){int y = a[k].y;if(  y != fa[x]) Q.push( y );}}}void update( int k,int x,int l,int r,int lx,int rx,ll ac ){if( lx <= l && r <= rx ) {tr[k][x] = M( tr[k][x] + ac );return ;}tr[k][x] = M( tr[k][x] );int mid = (l+r)>>1, lc = x<<1, rc = lc|1;if( rx <= mid ) update( k,lc,l,mid,lx,rx,ac );else if( lx > mid ) update( k,rc,mid+1,r,lx,rx,ac );else{update( k,lc,l,mid,lx,mid,ac );update( k,rc,mid+1,r,mid+1,rx,ac );}}void add( int x,ll ac ){while( top[x] != 1 ){int f1 = top[x];update( 4,1,1,z,w[f1],w[x],ac );x = fa[f1];s[x] = M(s[x] + ac*(siz[son[x]]-siz[f1]));}update( 4,1,1,z,w[1],w[x],ac );}ll Query( int k,int x,int l,int r,int loc,ll ret ){if( l == r ) return M(ret+tr[k][x]);ret = M( ret + tr[k][x] );int mid = ( l+r )>>1, lc = x<<1, rc=  lc|1;if( loc <= mid ) return Query( k,lc,l,mid,loc,ret );else return Query( k,rc,mid+1,r,loc,ret );}int main(){ll x,y;while( scanf("%d%d",&n,&m) != EOF ){len = 0;bnum = z = 0;for( int i=1;i<=n;i++ ) tr3[i] = first[i] = 0;for( int i=1;i<=n<<2;i++ ) tr[1][i] = tr[2][i] = tr[4][i] = 0;for( int i=2;i<=n;i++ ){read( x );insert( x,i );insert( i,x );}for( int i=1;i<=n;i++ ) read( sum[i] );dfs( 1 );build_tree( 1,1 );bfs(  );dfs2( 1 );while( m-- ){read( y );if( y == 1 ){read( x ); read( y );if( sl2[x] != n+1 ) update( 1,1,1,bnum,sl2[x],sr2[x],y );if( sl1[x] != n+1 ) update( 2,1,1,bnum,sl1[x],sr1[x],y );tr3[x] = M( tr3[x] + y );if( x != 1 ) {int f1 = fa[x], ac = y*(son2[x]+1) ;s[f1] = M(s[f1] + ac*(siz[son[f1]]-siz[x]));add( fa[x],ac );}}else{read( x );ll ret = s[x];ret = M( ret + a1[x]*Query( 1,1,1,bnum,bid[x],0ll ) );ret = M( ret + a2[x]*Query( 2,1,1,bnum,bid[x],0ll ) );ret = M( ret + a3[x]*tr3[x] );ret = M( ret + a4[x]*Query( 4,1,1,z,w[x],0ll ) );printf( "%I64d\n",M(ret+Mod) );}}}return 0;}


0 0
原创粉丝点击