#include<bits/stdc++.h>
#define MAXN 200007
using namespace std;
int n,q,type;
long long mod,w;
int a,b,s,d;
vector<int> v[MAXN];
int num[MAXN],tt,from[MAXN],to[MAXN],parent[MAXN];
long long mult[42][MAXN],delt[42][MAXN];
void dfs(int x,int p){
from[x]=n; to[x]=0;
parent[x]=p;
for(int i=0;i<v[x].size();i++){
if(v[x][i]==p)continue;
tt++; num[v[x][i]]=tt;
from[x]=min(from[x],tt);
to[x]=max(to[x],tt);
}
for(int i=0;i<v[x].size();i++){
if(v[x][i]==p)continue;
dfs(v[x][i],x);
}
}
long long power(long long a,long long b){
if(b==0)return 1;
if(b==1)return a;
if(b==2)return (a*a)%mod;
if(b%2==0)return power(power(a,b/2),2);
return (power(power(a,b/2),2)*a)%mod;
}
void query(int x,int dist,int ban,long long p){
if(x==0)return;
mult[dist][x]*=p;
mult[dist][x]%=mod;
if(dist==0)return;
if(ban!=0 and dist>0){
delt[dist-1][ban]*=p;
delt[dist-1][ban]%=mod;
}
query(parent[x],dist-1,x,p);
}
pair<long long,long long> geth(int x,int dist){
if(x==0 or dist>40)return {1,1};
long long res=1,res2=1;
for(int i=dist;i<=40;i++){
res*=mult[i][x];
res2*=delt[i][x];
res%=mod; res2%=mod;
}
pair<long long,long long> w=geth(parent[x],dist+1);
return {(w.first*res)%mod,(w.second*res2)%mod};
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>mod;
for(int i=1;i<=n-1;i++){
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
for(int i=0;i<=40;i++){
for(int f=1;f<=n;f++){
mult[i][f]=1;
delt[i][f]=1;
}
}
num[1]=1; tt=1;
dfs(1,0);
for(int i=1;i<=n;i++){
cin>>mult[0][i];
}
cin>>q;
for(int i=1;i<=q;i++){
cin>>type;
if(type==1){
cin>>s>>d>>w;
query(s,d,0,w);
}else{
cin>>s;
pair<long long,long long> w=geth(s,0);
cout<<(w.first*power(w.second,mod-2))%mod<<"\n";
}
}
return 0;
}
Compilation message
sprinkler.cpp: In function 'void dfs(int, int)':
sprinkler.cpp:17:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
sprinkler.cpp:25:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
137564 KB |
Output is correct |
2 |
Correct |
15 ms |
137308 KB |
Output is correct |
3 |
Correct |
14 ms |
137520 KB |
Output is correct |
4 |
Incorrect |
20 ms |
137564 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
137564 KB |
Output is correct |
2 |
Execution timed out |
4083 ms |
148296 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
137564 KB |
Output is correct |
2 |
Execution timed out |
4083 ms |
148296 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
137300 KB |
Output is correct |
2 |
Execution timed out |
4038 ms |
155604 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
137560 KB |
Output is correct |
2 |
Execution timed out |
4042 ms |
154656 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
137564 KB |
Output is correct |
2 |
Correct |
15 ms |
137308 KB |
Output is correct |
3 |
Correct |
14 ms |
137520 KB |
Output is correct |
4 |
Incorrect |
20 ms |
137564 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |