WOJ2231 树上修改3(没有2是什么鬼,方法同上一篇)

树上修改1传送门:https://blog.csdn.net/g21glf/article/details/82958565

【题目描述】

有n个节点N-1条边,这是一颗树,有2个操作:

1 x y v:表示将节点x到y最短路径上所有的点的权值+v

2 x:表示查询子树x的权值和

开始的时候每个节点的权值是0

【输入格式】

第一行是数N,表示N个节点 接下来n-1行,每行描述了n-1条边。

接下来是一个数q表示有q次查询与询问 接下来q行,格式如题

【输出格式】

对于每一个询问,输出子树权值和

【样例输入】

3

1 2

2 3

3

1 1 2 5

1 1 3 2

2 2

【样例输出】

9

【备注】

q,n<=1e5 权值修改范围在100

【题目分析】

这道题与上一篇类似,不过这次是维护一棵树使其可以支持链加和子树求和,方法同上,实现有细节。

对于每一次链加,不同于上一篇,我们在lca处减一次,在f[lca][0]处减一次,不能减去两次再单独记录因为本题询问的是子树和。对于每次修改的贡献,参见下图:

可以发现,如果(x,W)对y有贡献的时候当且仅当y是x的祖先,这时x对y的贡献就是W*(depth[x]-depth[y]+1),分离一下变量我们就可以得到贡献即为这个式子:depth*W+(1-depth[y])*W,所以我们只用维护两个区间和:depth[x]*W和W即可。

【代码~】

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
const int MAXM=4e5+10;

int n,m,cnt;
int head[MAXN],depth[MAXN],app[MAXN];
int nxt[MAXM],to[MAXM];
int l[MAXN],r[MAXN],f[MAXN][20],tot;
int tr1[MAXM],tr2[MAXM];

int lowbit(int x)
{
	return x&(-x);
}

int sum(int *t,int x)
{
	int ret=0;
	while(x)
	{
		ret+=t[x];
		x-=lowbit(x);
	}
	return ret;
}

void change(int *t,int x,int v)
{
	if(x==0)
	  return ;
	while(x<=n)
	{
		t[x]+=v;
		x+=lowbit(x);
	}
}

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

void add(int x,int y)
{
	Add(x,y);
	Add(y,x);
}

void dfs(int x,int fa)
{
	l[x]=++tot;
	f[x][0]=fa;
	for(int i=1;i<19;++i)
	  f[x][i]=f[f[x][i-1]][i-1];
	depth[x]=depth[fa]+1;
	for(int i=head[x];i!=-1;i=nxt[i])
	{
		int v=to[i];
		if(v!=fa)
		  dfs(v,x);
	}
	r[x]=tot;
}

int lca(int x,int y)
{
	if(depth[x]<depth[y])
	  swap(x,y);
	for(int i=18;i>=0;--i)
	  if(depth[f[x][i]]>=depth[y])
	    x=f[x][i];
	if(x==y)
	  return x;
	for(int i=18;i>=0;--i)
	  if(f[x][i]!=f[y][i])
	    x=f[x][i],y=f[y][i];
	return f[x][0];
}

int main()
{
	memset(head,-1,sizeof(head));
	scanf("%d",&n);
	for(int i=1;i<n;++i)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);
	}
	dfs(1,0);
	scanf("%d",&m);
	for(int i=1;i<=m;++i)
	{
		int cz;
		scanf("%d",&cz);
		if(cz==1)
		{
			int x,y,v;
			scanf("%d%d%d",&x,&y,&v);
			int lc=lca(x,y);
			change(tr1,l[x],depth[x]*v);
			change(tr1,l[y],depth[y]*v);
			change(tr1,l[lc],-depth[lc]*v);
			change(tr1,l[f[lc][0]],-depth[f[lc][0]]*v);
			change(tr2,l[x],v);
			change(tr2,l[y],v);
			change(tr2,l[lc],-v);
			change(tr2,l[f[lc][0]],-v);
		}
		else
		{
			int x;
			scanf("%d",&x);
			printf("%d\n",sum(tr1,r[x])-sum(tr1,l[x]-1)+(sum(tr2,r[x])-sum(tr2,l[x]-1))*(1-depth[x]));
		}
	}
	return 0;
}

update(2019.1.19)

发现自己写的差分自己都看不懂了。。。。

所以直接线段树来一发

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

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

int Read(){
	int 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(int x,int y){
	nxt[cnt]=head[x];
	head[x]=cnt;
	to[cnt]=y;
	cnt++;
}

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

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

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

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

void push_down(int 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(int root,int l,int r){
	tr[root].l=l,tr[root].r=r;
	if(l==r)
	  return ;
	int mid=l+r>>1;
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
}

void update(int root,int l,int r,int L,int R,int key){
	if(l>R||r<L)
	  return ;
	if(L<=l&&r<=R){
		push_now(root,key);
		return ;
	}
	push_down(root);
	int 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);
}

int query(int root,int l,int r,int L,int R){
	if(l>R||r<L)
	  return 0;
	if(L<=l&&r<=R)
	  return tr[root].sum;
	push_down(root);
	int 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);
	}
}

void update_path(int x,int y,int key){
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y);
		update(1,1,n,dfn[top[x]],dfn[x],key);
		x=fa[top[x]];
	}
	if(depth[x]<depth[y])
	  swap(x,y);
	update(1,1,n,dfn[y],dfn[x],key);
}

int query_son(int x){
	return query(1,1,n,dfn[x],dfn[x]+siz[x]-1);
}

int main(){
	memset(head,-1,sizeof(head));
	n=Read();
	for(int i=1;i<n;++i){
		int x=Read(),y=Read();
		add(x,y),add(y,x);
	}
	dfs1(1,-1),dfs2(1,1);
	build(1,1,n);
	int q=Read();
	while(q--){
		int cz=Read();
		if(cz==1){
			int x=Read(),y=Read(),key=Read();
			update_path(x,y,key);
		}
		else{
			int x=Read();
			cout<<query_son(x)<<'\n';
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值