10分求调
查看原帖
10分求调
744895
liaoxingrui楼主2024/3/7 16:48
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m,s,f,x,y,z,cnt,tot,mod;
int a[N],fa[N],dep[N],siz[N],son[N],top[N],rfn[N],head[N],tree[N<<2],lazy[N<<2];
struct node{
	int xx,yy;
}next[100005<<1];
inline void add(int x,int y){
	tot++;
	next[tot].xx=y;
	next[tot].yy=head[x];
	head[x]=tot;
}
inline void dfs1(int node){
	siz[node]=1;
	for(int i=head[node];i;i=next[i].yy){
		int w=next[i].xx;
		if(w!=fa[node]){
			fa[w]=node;
			dep[w]=dep[node]+1;
			dfs1(w);
			siz[node]+=siz[w];
			if(siz[w]>siz[son[node]])
				son[node]=w;
		}
	}
}
inline void dfs2(int node,int t){
	top[node]=t;
	cnt++;
	rfn[node]=cnt;
	if(!son[node])
		return;
	dfs2(son[node],t);
	for(int i=head[node];i;i=next[i].yy){
		int w=next[i].xx;
		if(w!=fa[node]&&w!=son[node])
			dfs2(w,w);
	}
}
inline void sjf(int node){
	tree[node]=(tree[node<<1]+tree[node<<1|1])%mod;
}
inline void cdlazy(int node,int l,int r){
	if(lazy[node]){
    	int mid=l+((r-l)>>1);
		tree[node<<1]=(tree[node<<1]+(mid-l+1)*lazy[node])%mod;
		tree[node<<1|1]=(tree[node<<1|1]+(r-mid)*lazy[node])%mod;
		lazy[node<<1]=(lazy[node<<1]+lazy[node])%mod;
		lazy[node<<1|1]=(lazy[node<<1|1]+lazy[node])%mod;
		lazy[node]=0;
	}
}
inline void js(int node,int l,int r){
	if(l==r){
		tree[node]=a[l]%mod;
		return;
	}
	int mid=l+((r-l)>>1);
	js(node<<1,l,mid);
	js(node<<1|1,mid+1,r);
	sjf(node);
}
inline void jf(int node,int t,int l,int r,int x,int y){
	if(x<=l&&r<=y){
		tree[node]=(tree[node]+(r-l+1)*t)%mod;
		lazy[node]=(lazy[node]+t)%mod;
		return;
	}
	cdlazy(node,l,r);
	int mid=l+((r-l)>>1);
	if(x<=mid)
		jf(node<<1,t,l,mid,x,y);
	if(y>mid)
		jf(node<<1|1,t,mid+1,r,x,y);
	sjf(node);
}
inline int qh(int node,int l,int r,int x,int y){
	if(x<=l&&r<=y)
		return tree[node];
	cdlazy(node,l,r);
	int mid=l+((r-l)>>1),sum=0;
	if(x<=mid)
		sum=(sum+qh(node<<1,l,mid,x,y))%mod;
	if(y>mid)
		sum=(sum+qh(node<<1|1,mid+1,r,x,y))%mod;
	return sum;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>s>>mod;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<n;i++){
		cin>>x>>y;
		add(x,y);
		add(y,x);
	}
	dfs1(s);
	dfs2(s,s);
	js(1,1,n);
	while(m--){
		cin>>f;
		if(f==1){
			cin>>x>>y>>z;
			z%=mod;
			while(top[x]!=top[y]){
				if(dep[top[x]]<dep[top[y]])
					swap(x,y);
				jf(1,z,1,n,rfn[top[x]],rfn[x]);
				x=fa[top[x]];
			}
			if(dep[x]>dep[y])
				swap(x,y);
			jf(1,z,1,n,rfn[x],rfn[y]);
		}
		else if(f==2){
			cin>>x>>y;
			int ans=0;
			while(top[x]!=top[y]){
				if(dep[top[x]]<dep[top[y]])
					swap(x,y);
				ans=(ans+qh(1,1,n,rfn[top[x]],rfn[x]))%mod;
				x=fa[top[x]];
			}
			if(dep[x]>dep[y])
				swap(x,y);
			cout<<(ans+qh(1,1,n,rfn[x],rfn[y]))%mod<<endl;
		}
		else if(f==3){
			cin>>x>>z;
			z%=mod;
			jf(1,z,1,n,rfn[x],rfn[x]+siz[x]-1);
		}
		else{
			cin>>x;
			cout<<qh(1,1,n,rfn[x],rfn[x]+siz[x]-1)<<endl;
		}
	}
	return 0;
}

只有最后一个对了,其他全错,帮忙看看,谢谢!

2024/3/7 16:48
加载中...