洛谷3178 【HAOI2015】树上操作(树链剖分)

传送门

【题目分析】

树链剖分板题吧。

操作都很常规,单点加直接在线段树上修改,子树加就是将区间dfn[x]~dfn[x]+siz[x]-1内的所有值加key,最后路径求和即可。

注意开long long。

【代码~】

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAXN=1e5+10;
const LL MAXM=2e5+10;

LL n,q,cnt;
LL a[MAXN];
LL head[MAXN],fa[MAXN],son[MAXN],depth[MAXN],top[MAXN],siz[MAXN];
LL nxt[MAXM],to[MAXM];
LL dfn[MAXN],tot,ys[MAXN];
struct Tree{
	LL l,r;
	LL add;
	LL sum;
}tr[MAXN<<2];

LL Read(){
	LL i=0,f=1;
	char c;
	for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());
	if(c=='-')
	  f=-1,c=getchar();
	for(;c>='0'&&c<='9';c=getchar())
	  i=(i<<3)+(i<<1)+c-'0';
	return i*f;
}

void add(LL x,LL y){
	nxt[cnt]=head[x];
	head[x]=cnt;
	to[cnt]=y;
	cnt++;
}

void dfs1(LL u,LL f){
	siz[u]=1;
	for(LL i=head[u];i!=-1;i=nxt[i]){
		LL v=to[i];
		if(v==f)
		  continue;
		fa[v]=u;
		depth[v]=depth[u]+1;
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]])
		  son[u]=v;
	}
}

void dfs2(LL u,LL tp){
	top[u]=tp;
	dfn[u]=++tot;
	ys[tot]=u;
	if(!son[u])
	  return ;
	dfs2(son[u],tp);
	for(LL i=head[u];i!=-1;i=nxt[i]){
		LL v=to[i];
		if(v==fa[u]||v==son[u])
		  continue;
		dfs2(v,v);
	}
}

void push_up(LL root){
	tr[root].sum=tr[root<<1].sum+tr[root<<1|1].sum;
}

void push_now(LL root,LL key){
	tr[root].add+=key;
	tr[root].sum+=key*(tr[root].r-tr[root].l+1);
}

void push_down(LL root){
	if(tr[root].add){
		push_now(root<<1,tr[root].add);
		push_now(root<<1|1,tr[root].add);
		tr[root].add=0;
	}
}

void build(LL root,LL l,LL r){
	tr[root].l=l,tr[root].r=r;
	if(l==r){
		tr[root].sum=a[ys[l]];
		return ;
	}
	LL mid=l+r>>1;
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
	push_up(root);
}

void update(LL root,LL l,LL r,LL L,LL R,LL key){
	if(l>R||r<L)
	  return ;
	if(L<=l&&r<=R){
		push_now(root,key);
		return ;
	}
	push_down(root);
	LL mid=l+r>>1;
	if(R<=mid)
	  update(root<<1,l,mid,L,R,key);
	else{
		if(L>mid)
		  update(root<<1|1,mid+1,r,L,R,key);
		else
		  update(root<<1,l,mid,L,mid,key),update(root<<1|1,mid+1,r,mid+1,R,key);
	}
	push_up(root);
}

LL query(LL root,LL l,LL r,LL L,LL R){
	if(l>R||r<L)
	  return 0;
	if(L<=l&&r<=R)
	  return tr[root].sum;
	push_down(root);
	LL mid=l+r>>1;
	if(R<=mid)
	  return query(root<<1,l,mid,L,R);
	else{
		if(L>mid)
		  return query(root<<1|1,mid+1,r,L,R);
		else
		  return query(root<<1,l,mid,L,mid)+query(root<<1|1,mid+1,r,mid+1,R);
	}
}

LL query_path(LL x,LL y){
	LL ret=0;
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y);
		ret+=query(1,1,n,dfn[top[x]],dfn[x]);
		x=fa[top[x]];
	}
	if(depth[x]<depth[y])
	  swap(x,y);
	ret+=query(1,1,n,dfn[y],dfn[x]);
	return ret;
}

int main(){
	memset(head,-1,sizeof(head));
	n=Read(),q=Read();
	for(LL i=1;i<=n;++i)
	  a[i]=Read();
	for(LL i=1;i<n;++i){
		LL x=Read(),y=Read();
		add(x,y),add(y,x);
	}
	dfs1(1,-1);
	dfs2(1,1);
	build(1,1,n);
	while(q--){
		LL cz=Read();
		if(cz==1){
			LL x=Read(),key=Read();
			update(1,1,n,dfn[x],dfn[x],key);
		}
		else{
			if(cz==2){
				LL x=Read(),key=Read();
				update(1,1,n,dfn[x],dfn[x]+siz[x]-1,key);
			}
			else{
				LL x=Read();
				cout<<query_path(1,x)<<'\n';
			}
		}
	}
	return 0;
}

 

posted @ 2019-01-19 15:17  Ishtar~  阅读(124)  评论(0编辑  收藏  举报