답안 #966182

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
966182 2024-04-19T13:39:02 Z PM1 Sprinkler (JOI22_sprinkler) C++17
0 / 100
913 ms 62544 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mxn=2e5+5,sq=448;
int n,M,q,h[mxn],st[mxn],fn[mxn],cnt,dis[mxn],par[mxn];
vector<ll>v[mxn],d[mxn],res[mxn],lazy[mxn];
void dfs(int z){
	st[z]=cnt++;
	d[dis[z]].push_back(z);
	res[dis[z]].push_back(h[z]);
	for(auto i:v[z]){
		if(i!=par[z]){
			par[i]=z;
			dis[i]=dis[z]+1;
			dfs(i);
		}
	}
	fn[z]=cnt;
}
bool cmp(int x,int y){
	return st[x]<st[y];
}
void add(int x,int L,int R,ll w){
	if(d[x].size()==0)return;
	int l=lower_bound(d[x].begin(),d[x].end(),L,cmp)-d[x].begin();
	int r=lower_bound(d[x].begin(),d[x].end(),R,cmp)-d[x].begin();
	r=max((int)d[x].size()-1,r);
	for(;l<=r && l%sq!=0;l++)
		res[x][l]=(res[x][l]*w)%M;
	for(;l+sq-1<=r;l+=sq)
		lazy[x][l/sq]=(lazy[x][l/sq]*w)%M;
	for(;l<=r;l++)
		res[x][l]=(res[x][l]*w)%M;
}
void up(int x,int dd,int w){
	int y=dd;
	while(y-- && x!=1){
		int z=dis[x]+dd;
		add(z,st[x],fn[x],w);
		x=par[x];
	}
	for(;dd>=0;dd--){
		int z=dis[x]+dd;
		add(z,st[x],fn[x],w);
	}
}
ll get(int z,int x){
	int l=lower_bound(d[z].begin(),d[z].end(),x,cmp)-d[z].begin();
	
	return (res[z][l]*lazy[z][l/sq])%M;
}
int main(){
	ios::sync_with_stdio(false);
	
	cin>>n>>M;
	for(int i=1;i<n;i++){
		int x,y;
		cin>>x>>y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for(int i=1;i<=n;i++)
		cin>>h[i];
	dfs(1);
	for(int i=0;i<=n;i++){
		int x=d[i].size();
		while(x>0){
			x-=sq;
			lazy[i].push_back(1);
		}
	}
	cin>>q;
	while(q--){
		int ty,x,dd,w;
		cin>>ty;
		if(ty==1){
			cin>>x>>dd>>w;
			up(x,dd,w);
		}

		else{
			cin>>x;
			cout<<get(dis[x],x)<<'\n';
		}//cout<<get(0,1);
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 21084 KB Output is correct
2 Incorrect 5 ms 21172 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 21084 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 21084 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 21084 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 21080 KB Output is correct
2 Incorrect 913 ms 62544 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 21084 KB Output is correct
2 Incorrect 5 ms 21172 KB Output isn't correct
3 Halted 0 ms 0 KB -