样例全过,12pts求调
查看原帖
样例全过,12pts求调
508530
P7GAB楼主2023/11/12 21:20
//就是暴力跳,差分一下完事
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+223;
int n,q,L;
struct node {
	int to,nxt;
} edge[maxn<<1];
int head[maxn],cnt=0;
int h[maxn],fa[maxn],s[maxn][41];
void add(int u,int v) {
	cnt++;
	edge[cnt].to=v;
	edge[cnt].nxt=head[u];
	head[u]=cnt;
}
void dfs(int u,int fath) {
	fa[u]=fath;
	for(int i=head[u]; i; i=edge[i].nxt) {
		int v=edge[i].to;
		if(v!=fath)
			dfs(v,u);
	}
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>L;
	for(int i=1; i<n; i++) {
		int u,v;
		cin>>u>>v;
		add(u,v);
		add(v,u);
	}

	for(int i=1; i<=n; i++)
		cin>>h[i];
	for(int i=1; i<=40; i++) {
		int u=n,v=++n;
		add(u,v);
		add(v,u);
	}
	for(int i=1; i<=n; i++)
		for(int j=0; j<=40; j++)
			s[i][j]=1;
	dfs(n,0);
	cin>>q;
	while(q--) {
		int op,x,d,w;
		cin>>op;
		if(op==1) {
			cin>>x>>d>>w;
			while(d>=0) {
				s[x][d]=(s[x][d]*w)%L;
				if(d>0)
					s[x][d-1]=(s[x][d-1]*w)%L;
				x=fa[x];
				d--;
			}
		} else {
			cin>>x;
			int ans=h[x];
			int d=0;
			while(d<=40) {
				ans=(ans*s[x][d])%L;
				x=fa[x];
				d++;
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}
2023/11/12 21:20
加载中...