[WOJ4103] 换根意义下的树链剖分


#include<bits/stdc++.h> 
#define N 100050
#define int long long
using namespace std;
int first[N], nxt[N], to[N], tot;
void add(int x,int y){ nxt[++tot] = first[x], first[x] = tot, to[tot] = y;}
int a[N],n,m,Root;
int siz[N],fa[N],dep[N],son[N],top[N],id[N],sign;
int read(){
	int cnt=0; char ch=0;
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))cnt=cnt*10+(ch-'0'),ch=getchar();
	return cnt;
}
struct Segmentree{
	int sum[N<<2], tag[N<<2];
	void Pushup(int x){ sum[x] = sum[x<<1] + sum[x<<1|1];}
	void Pushdown(int x,int l,int r){
		if(tag[x]){
			int mid = (l+r) >> 1;
			sum[x<<1] += (mid-l+1) * tag[x];
			sum[x<<1|1] += (r-mid) * tag[x];
			tag[x<<1] += tag[x]; tag[x<<1|1] += tag[x];
			tag[x] = 0; 
		}
	} 
	void Modify(int x,int l,int r,int L,int R,int val){
		if(L<=l && r<=R){ 
			sum[x] += (r-l+1) * val;
			tag[x] += val;
			return;
		} 
		Pushdown(x,l,r);
		int mid = (l+r) >> 1;
		if(L<=mid) Modify(x<<1,l,mid,L,R,val);
		if(R>mid) Modify(x<<1|1,mid+1,r,L,R,val);
		Pushup(x);
	}
	int Quary(int x,int l,int r,int L,int R){
		if(L<=l && r<=R) return sum[x];
		Pushdown(x,l,r);
		int mid = (l+r) >> 1, ans = 0;
		if(L<=mid) ans += Quary(x<<1,l,mid,L,R);
		if(R>mid) ans += Quary(x<<1|1,mid+1,r,L,R);
		return ans;
	}
}Seg; 
void dfs(int u,int f){
	siz[u] = 1;
	for(int i=first[u];i;i=nxt[i]){
		int t=to[i]; 
		dep[t] = dep[u] + 1, fa[t] = u;
		dfs(t,u); siz[u] += siz[t];
		if(siz[son[u]] < siz[t]) son[u] = t;
	}
}
void dfs2(int u,int Top){
	id[u] = ++sign; top[u] = Top;
	Seg.Modify(1,1,n,id[u],id[u],a[u]);
	if(son[u]) dfs2(son[u], Top);
	for(int i=first[u];i;i=nxt[i]){
		int t=to[i]; if(t==son[u]) continue;
		dfs2(t,t);
	}
}
int LCA(int x,int y){
	while(top[x] != top[y]){
		if(dep[top[x]] < dep[top[y]]) swap(x,y);
		x = fa[top[x]];
	} if(dep[x] > dep[y]) swap(x,y); return x;
}
int find(int now,int goal){
	while(top[now] != top[goal]){
		if(dep[top[now]] < dep[top[goal]]) swap(now, goal);
		if(fa[top[now]] == goal) return top[now];	
		now = fa[top[now]]; 
	} 
	if(dep[now] > dep[goal]) swap(now, goal); 
	return son[now];
}
void Road_Modify(int x,int y,int w){
	while(top[x] != top[y]){
		if(dep[top[x]] < dep[top[y]]) swap(x,y);
		Seg.Modify(1,1,n,id[top[x]],id[x],w);
		x = fa[top[x]];
	}
	if(id[x] > id[y]) swap(x,y); 
	Seg.Modify(1,1,n,id[x],id[y],w);
}
void Son_Modify(int x,int w){
	if(x == Root){ Seg.Modify(1,1,n,1,n,w); return;}
	int lca = LCA(x, Root);
	if(lca != x){ Seg.Modify(1,1,n, id[x], id[x]+siz[x]-1, w); return;}
	int son = find(Root, lca);
	Seg.Modify(1,1,n, 1,n,w);
	Seg.Modify(1,1,n, id[son], id[son]+siz[son]-1, -w);
}
int Road_Quary(int x,int y){
	int ans = 0;
	while(top[x] != top[y]){
		if(dep[top[x]] < dep[top[y]]) swap(x,y);
		ans += Seg.Quary(1,1,n,id[top[x]],id[x]);
		x = fa[top[x]];
	}
	if(id[x] > id[y]) swap(x,y); 
	return ans + Seg.Quary(1,1,n,id[x],id[y]);
} 
int Son_Quary(int x){
	if(x == Root) return Seg.Quary(1,1,n,1,n);
	int lca = LCA(x, Root);
	if(lca != x) return Seg.Quary(1,1,n, id[x], id[x]+siz[x]-1);
	int son = find(Root, lca);
	return Seg.Quary(1,1,n,1,n) - Seg.Quary(1,1,n,id[son],id[son]+siz[son]-1);
}
signed main(){
	n = read();
	for(int i=1;i<=n;i++) a[i] = read();
	for(int i=2;i<=n;i++) add(read(), i);
	dfs(1,0); dfs2(1,1); m = read();
	while(m--){
		int op = read();
		if(op==1){ Root = read();}
		if(op==2){ int u = read(), v = read(), w = read(); Road_Modify(u, v, w);}
		if(op==3){ int u = read(), w = read(); Son_Modify(u, w);}
		if(op==4){ int u = read(), v = read(); printf("%lld\n",Road_Quary(u,v));}
		if(op==5){ int u = read(); printf("%lld\n",Son_Quary(u));}
	} return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FSYo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值