求助大佬。。。
查看原帖
求助大佬。。。
138799
jzdywf楼主2022/2/8 12:10

测评记录 帮帮孩子吧,人已经傻了

#include<bits/stdc++.h>
using namespace std;
int cnt,tot;
int id[110000];
int rk[110000];
int fa[110000];
int dep[110000];
int top[110000];
int son[110000];
int head[110000];
int size[110000];
long long a[110000];
long long n,m,root,p;
struct edge{
	int to;
	int nxt;
}e[210000];
struct tree{
	long long sum;
	long long lazy;
	int left,right;
}tree[410000];
long long read(){
	long long r=0,f=1;
	char c=getchar();
	while((c<'0'||c>'9')&&c!='-'){
		c=getchar();
	}
	if(c=='-'){
		f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		r=(r<<3)+(r<<1)+c-'0';
		c=getchar();
	}
	return r*f;
}
void addedge(int u,int v){
	cnt++;
	e[cnt].to=v;
	e[cnt].nxt=head[u];
	head[u]=cnt;
	return;
}
void dfs1(int u,int father){
	size[u]=1;
	fa[u]=father;
	dep[u]=dep[father]+1;
	for(int i=head[u];i;i=e[i].nxt){
		if(e[i].to==father){
			continue;
		}
		dfs1(e[i].to,u);
		size[u]+=size[e[i].to];
		if(size[e[i].to]>size[son[u]]){
			son[u]=e[i].to;
		}
	}
	return;
}
void dfs2(int u,int t){
	tot++;
	top[u]=t;
	id[u]=tot;
	rk[tot]=a[u];
	if(son[u]){
		dfs2(son[u],t);
	}
	for(int i=head[u];i;i=e[i].nxt){
		if(e[i].to!=son[u]&&e[i].to!=fa[u]){
			dfs2(e[i].to,e[i].to);
		}
	}
	return;
}
void build(int k,int l,int r){
	tree[k].left=l,tree[k].right=r;
	if(l==r){
		tree[k].sum=rk[l];
		return;
	}
	int mid=(l+r)>>1;
	build(k<<1,l,mid);
	build(k<<1|1,mid+1,r);
	tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;
	tree[k].sum%=p;
	return;
}
void pushdown(int k){
	if(tree[k].lazy){
		tree[k<<1].sum+=(tree[k<<1].right-tree[k<<1].left+1)*tree[k].lazy;
		tree[k<<1|1].sum+=(tree[k<<1|1].right-tree[k<<1|1].left+1)*tree[k].lazy;
		tree[k<<1].lazy+=tree[k].lazy,tree[k<<1|1].lazy+=tree[k].lazy,tree[k].lazy=0;
		tree[k<<1].sum%=p,tree[k<<1|1].sum%=p,tree[k<<1].lazy%=p,tree[k<<1|1].lazy%=p;
	}
	return;
}
void add(int k,int l,int r,int d){
	if(l<=tree[k].left&&tree[k].right<=r){
		tree[k].sum+=(tree[k].right-tree[k].left+1)*d;
		tree[k].lazy+=d;
		tree[k].lazy%=p;
		tree[k].sum%=p;
		return;
	}
	pushdown(k);
	if(tree[k<<1].right>=l){
		add(k<<1,l,r,d);
	}
	if(tree[k<<1|1].left<=r){
		add(k<<1|1,l,r,d);
	}
	tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;
	tree[k].sum%=p;
	return;
}
void change(int x,int y,int z){
	int fx=top[x],fy=top[y];
	while(fx!=fy){
		if(dep[fx]<dep[fy]){
			swap(x,y);
			swap(fx,fy);
		}
		add(1,id[fx],id[x],z);
		x=fa[x],fx=top[x];
	}
	if(id[x]>id[y]){
		swap(x,y);
	}
	add(1,id[x],id[y],z);
	return;
}
long long query(int k,int l,int r){
	if(l<=tree[k].left&&tree[k].right<=r){
		return tree[k].sum;
	}
	pushdown(k);
	long long ans=0;
	if(tree[k<<1].right>=l){
		ans+=query(k<<1,l,r);
		ans%=p;
	}
	if(tree[k<<1|1].left<=r){
		ans+=query(k<<1|1,l,r);
		ans%=p;
	}
	return ans%p;
}
long long ask(int x,int y){
	long long ans=0;
	int fx=top[x],fy=top[y];
	while(fx!=fy){
		if(dep[fx]<dep[fy]){
			swap(x,y);
			swap(fx,fy);
		}
		ans+=query(1,id[fx],id[x]);
		ans%=p;
		x=fa[fx],fx=top[x];
	}
	if(id[x]>id[y]){
		swap(x,y);
	}
	ans+=query(1,id[x],id[y]);
	return ans%p;
}
int main(){
	n=read(),m=read(),root=read(),p=read();
	for(int i=1;i<=n;i++){
		a[i]=read();
	}
	for(int i=1;i<n;i++){
		int u=read(),v=read();
		addedge(u,v);
		addedge(v,u);
	}
	dfs1(root,0);
	dfs2(root,root);
	build(1,1,n);
	while(m--){
		int opt,x,y,z;
		opt=read();
		if(opt==1){
			x=read(),y=read(),z=read();
			change(x,y,z);
		}
		if(opt==2){
			x=read(),y=read();
			cout<<ask(x,y)<<endl;
		}
		if(opt==3){
			x=read(),y=read();
			add(1,id[x],id[x]+size[x]-1,y);
		}
		if(opt==4){
			x=read();
			cout<<query(1,id[x],id[x]+size[x]-1)<<endl;
		}
	}
	return 0;
}
2022/2/8 12:10
加载中...