#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3xxEYjy ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
int mod;
//snuke's mod int
// template <ll mod>
struct modint{
ll x; // typedef long long ll;
modint(ll x=0):x((x%mod+mod)%mod){}
modint operator-()const{return modint(-x);}
modint& operator+=(const modint a){if((x+=a.x)>=mod) x-=mod; return *this;}
modint& operator-=(const modint a){if((x+=mod-a.x)>=mod) x-=mod; return *this;}
modint& operator*=(const modint a){(x*=a.x)%=mod; return *this;}
modint operator+(const modint a)const{modint res(*this); return res+=a;}
modint operator-(const modint a)const{modint res(*this); return res-=a;}
modint operator*(const modint a)const{modint res(*this); return res*=a;}
modint pow(ll n)const{
modint res=1,x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n>>=1;
}
return res;
}
modint inv()const{return pow(mod-2);}
};
signed main(){
_3xxEYjy;
int n;
cin>>n>>mod;
vec(vi) adj(n);
rep(i,n-1){
int u,v;
cin>>u>>v;
u-=1;v-=1;
adj[u].pb(v);
adj[v].pb(u);
}
vi a(n);
rep(i,n){
cin>>a[i];
}
vi par;
{
rep(i,42){
adj.pb({});
if(i==0){
adj[n].pb(0);
adj[0].pb(n);
}else{
adj[n-1].pb(n);
adj[n].pb(n-1);
}
n+=1;
}
par=vi(n,-1);
auto dfs=[&](auto self,int v,int _par)->void{
for(auto u:adj[v]){
if(u==_par) continue;
par[u]=v;
self(self,u,v);
}
};dfs(dfs,n-1,-1);
while(sz(a)<n){
a.pb(0);
}
}
using mint=modint;
vec(vec(mint)) dp(n,vec(mint)(42,1));
auto add=[&](int v,int d,int x){
if(d==0){
a[v]*=x%mod;
a[v]%=mod;
return;
}
while(1){
if(v==-1) break;
// print(v);
if(d==0){
a[v]*=x;
a[v]%=mod;
break;
}
dp[v][d]*=mint(x);
d-=1;
v=par[v];
}
};
auto get=[&](int v)->mint{
int d=0;
mint res=1;
while(d<=40){
res*=dp[v][d];
res*=dp[v][d+1];
v=par[v];
if(v==-1) break;
d+=1;
}
return res;
};
int q;
cin>>q;
rep(_,q){
int t,v,x,d;
cin>>t;
if(t==1){
cin>>v>>d>>x;
v-=1;
add(v,d,x);
}else{
cin>>v;
v-=1;
mint res=get(v);
res*=mint(a[v]);
print(res.x);
}
}
//
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
712 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
807 ms |
93552 KB |
Output is correct |
3 |
Correct |
421 ms |
90216 KB |
Output is correct |
4 |
Correct |
718 ms |
95316 KB |
Output is correct |
5 |
Correct |
655 ms |
91836 KB |
Output is correct |
6 |
Correct |
490 ms |
91984 KB |
Output is correct |
7 |
Correct |
479 ms |
92228 KB |
Output is correct |
8 |
Correct |
406 ms |
92412 KB |
Output is correct |
9 |
Correct |
1180 ms |
99260 KB |
Output is correct |
10 |
Correct |
543 ms |
95480 KB |
Output is correct |
11 |
Correct |
978 ms |
93716 KB |
Output is correct |
12 |
Correct |
592 ms |
90344 KB |
Output is correct |
13 |
Correct |
367 ms |
90768 KB |
Output is correct |
14 |
Correct |
343 ms |
90716 KB |
Output is correct |
15 |
Correct |
357 ms |
90480 KB |
Output is correct |
16 |
Correct |
334 ms |
90576 KB |
Output is correct |
17 |
Correct |
340 ms |
91132 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
328 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
807 ms |
93552 KB |
Output is correct |
3 |
Correct |
421 ms |
90216 KB |
Output is correct |
4 |
Correct |
718 ms |
95316 KB |
Output is correct |
5 |
Correct |
655 ms |
91836 KB |
Output is correct |
6 |
Correct |
490 ms |
91984 KB |
Output is correct |
7 |
Correct |
479 ms |
92228 KB |
Output is correct |
8 |
Correct |
406 ms |
92412 KB |
Output is correct |
9 |
Correct |
1180 ms |
99260 KB |
Output is correct |
10 |
Correct |
543 ms |
95480 KB |
Output is correct |
11 |
Correct |
978 ms |
93716 KB |
Output is correct |
12 |
Correct |
592 ms |
90344 KB |
Output is correct |
13 |
Correct |
367 ms |
90768 KB |
Output is correct |
14 |
Correct |
343 ms |
90716 KB |
Output is correct |
15 |
Correct |
357 ms |
90480 KB |
Output is correct |
16 |
Correct |
334 ms |
90576 KB |
Output is correct |
17 |
Correct |
340 ms |
91132 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
328 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1012 ms |
93740 KB |
Output is correct |
25 |
Correct |
573 ms |
90260 KB |
Output is correct |
26 |
Correct |
874 ms |
97348 KB |
Output is correct |
27 |
Correct |
714 ms |
92020 KB |
Output is correct |
28 |
Correct |
564 ms |
92172 KB |
Output is correct |
29 |
Correct |
520 ms |
92212 KB |
Output is correct |
30 |
Correct |
438 ms |
92468 KB |
Output is correct |
31 |
Correct |
1089 ms |
97356 KB |
Output is correct |
32 |
Correct |
519 ms |
95444 KB |
Output is correct |
33 |
Correct |
894 ms |
93804 KB |
Output is correct |
34 |
Correct |
541 ms |
90228 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
328 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
320 KB |
Output is correct |
41 |
Correct |
1 ms |
328 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
2 ms |
340 KB |
Output is correct |
47 |
Correct |
2 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
320 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1284 ms |
96508 KB |
Output is correct |
3 |
Correct |
1970 ms |
101512 KB |
Output is correct |
4 |
Correct |
1245 ms |
101712 KB |
Output is correct |
5 |
Correct |
1042 ms |
97744 KB |
Output is correct |
6 |
Correct |
684 ms |
97720 KB |
Output is correct |
7 |
Correct |
611 ms |
97840 KB |
Output is correct |
8 |
Correct |
477 ms |
97804 KB |
Output is correct |
9 |
Correct |
1238 ms |
101664 KB |
Output is correct |
10 |
Correct |
2047 ms |
102812 KB |
Output is correct |
11 |
Correct |
1051 ms |
97676 KB |
Output is correct |
12 |
Correct |
1523 ms |
97868 KB |
Output is correct |
13 |
Correct |
729 ms |
98484 KB |
Output is correct |
14 |
Correct |
828 ms |
99824 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
324 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1331 ms |
97260 KB |
Output is correct |
3 |
Correct |
2070 ms |
100684 KB |
Output is correct |
4 |
Correct |
1240 ms |
102068 KB |
Output is correct |
5 |
Correct |
993 ms |
99308 KB |
Output is correct |
6 |
Correct |
619 ms |
99368 KB |
Output is correct |
7 |
Correct |
573 ms |
99388 KB |
Output is correct |
8 |
Correct |
455 ms |
99444 KB |
Output is correct |
9 |
Correct |
1097 ms |
105672 KB |
Output is correct |
10 |
Correct |
1746 ms |
103324 KB |
Output is correct |
11 |
Correct |
903 ms |
100128 KB |
Output is correct |
12 |
Correct |
1298 ms |
98516 KB |
Output is correct |
13 |
Correct |
723 ms |
99408 KB |
Output is correct |
14 |
Correct |
713 ms |
100016 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
712 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
807 ms |
93552 KB |
Output is correct |
31 |
Correct |
421 ms |
90216 KB |
Output is correct |
32 |
Correct |
718 ms |
95316 KB |
Output is correct |
33 |
Correct |
655 ms |
91836 KB |
Output is correct |
34 |
Correct |
490 ms |
91984 KB |
Output is correct |
35 |
Correct |
479 ms |
92228 KB |
Output is correct |
36 |
Correct |
406 ms |
92412 KB |
Output is correct |
37 |
Correct |
1180 ms |
99260 KB |
Output is correct |
38 |
Correct |
543 ms |
95480 KB |
Output is correct |
39 |
Correct |
978 ms |
93716 KB |
Output is correct |
40 |
Correct |
592 ms |
90344 KB |
Output is correct |
41 |
Correct |
367 ms |
90768 KB |
Output is correct |
42 |
Correct |
343 ms |
90716 KB |
Output is correct |
43 |
Correct |
357 ms |
90480 KB |
Output is correct |
44 |
Correct |
334 ms |
90576 KB |
Output is correct |
45 |
Correct |
340 ms |
91132 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
2 ms |
328 KB |
Output is correct |
50 |
Correct |
1 ms |
324 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1012 ms |
93740 KB |
Output is correct |
53 |
Correct |
573 ms |
90260 KB |
Output is correct |
54 |
Correct |
874 ms |
97348 KB |
Output is correct |
55 |
Correct |
714 ms |
92020 KB |
Output is correct |
56 |
Correct |
564 ms |
92172 KB |
Output is correct |
57 |
Correct |
520 ms |
92212 KB |
Output is correct |
58 |
Correct |
438 ms |
92468 KB |
Output is correct |
59 |
Correct |
1089 ms |
97356 KB |
Output is correct |
60 |
Correct |
519 ms |
95444 KB |
Output is correct |
61 |
Correct |
894 ms |
93804 KB |
Output is correct |
62 |
Correct |
541 ms |
90228 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
1 ms |
328 KB |
Output is correct |
65 |
Correct |
1 ms |
340 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
1 ms |
320 KB |
Output is correct |
69 |
Correct |
1 ms |
328 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
1 ms |
340 KB |
Output is correct |
74 |
Correct |
2 ms |
340 KB |
Output is correct |
75 |
Correct |
2 ms |
340 KB |
Output is correct |
76 |
Correct |
1 ms |
320 KB |
Output is correct |
77 |
Correct |
1 ms |
340 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
1284 ms |
96508 KB |
Output is correct |
80 |
Correct |
1970 ms |
101512 KB |
Output is correct |
81 |
Correct |
1245 ms |
101712 KB |
Output is correct |
82 |
Correct |
1042 ms |
97744 KB |
Output is correct |
83 |
Correct |
684 ms |
97720 KB |
Output is correct |
84 |
Correct |
611 ms |
97840 KB |
Output is correct |
85 |
Correct |
477 ms |
97804 KB |
Output is correct |
86 |
Correct |
1238 ms |
101664 KB |
Output is correct |
87 |
Correct |
2047 ms |
102812 KB |
Output is correct |
88 |
Correct |
1051 ms |
97676 KB |
Output is correct |
89 |
Correct |
1523 ms |
97868 KB |
Output is correct |
90 |
Correct |
729 ms |
98484 KB |
Output is correct |
91 |
Correct |
828 ms |
99824 KB |
Output is correct |
92 |
Correct |
1 ms |
324 KB |
Output is correct |
93 |
Correct |
1 ms |
340 KB |
Output is correct |
94 |
Correct |
1 ms |
324 KB |
Output is correct |
95 |
Correct |
1 ms |
340 KB |
Output is correct |
96 |
Correct |
1 ms |
340 KB |
Output is correct |
97 |
Correct |
1 ms |
340 KB |
Output is correct |
98 |
Correct |
1331 ms |
97260 KB |
Output is correct |
99 |
Correct |
2070 ms |
100684 KB |
Output is correct |
100 |
Correct |
1240 ms |
102068 KB |
Output is correct |
101 |
Correct |
993 ms |
99308 KB |
Output is correct |
102 |
Correct |
619 ms |
99368 KB |
Output is correct |
103 |
Correct |
573 ms |
99388 KB |
Output is correct |
104 |
Correct |
455 ms |
99444 KB |
Output is correct |
105 |
Correct |
1097 ms |
105672 KB |
Output is correct |
106 |
Correct |
1746 ms |
103324 KB |
Output is correct |
107 |
Correct |
903 ms |
100128 KB |
Output is correct |
108 |
Correct |
1298 ms |
98516 KB |
Output is correct |
109 |
Correct |
723 ms |
99408 KB |
Output is correct |
110 |
Correct |
713 ms |
100016 KB |
Output is correct |
111 |
Correct |
1 ms |
332 KB |
Output is correct |
112 |
Correct |
1 ms |
340 KB |
Output is correct |
113 |
Correct |
1 ms |
340 KB |
Output is correct |
114 |
Correct |
1 ms |
320 KB |
Output is correct |
115 |
Correct |
1 ms |
340 KB |
Output is correct |
116 |
Correct |
890 ms |
98784 KB |
Output is correct |
117 |
Correct |
1291 ms |
100968 KB |
Output is correct |
118 |
Correct |
1100 ms |
105536 KB |
Output is correct |
119 |
Correct |
917 ms |
100640 KB |
Output is correct |
120 |
Correct |
637 ms |
100212 KB |
Output is correct |
121 |
Correct |
634 ms |
101076 KB |
Output is correct |
122 |
Correct |
517 ms |
101064 KB |
Output is correct |
123 |
Correct |
1118 ms |
105280 KB |
Output is correct |
124 |
Correct |
1768 ms |
103684 KB |
Output is correct |
125 |
Correct |
957 ms |
99348 KB |
Output is correct |
126 |
Correct |
1359 ms |
100844 KB |
Output is correct |
127 |
Correct |
1368 ms |
101320 KB |
Output is correct |
128 |
Correct |
724 ms |
98560 KB |
Output is correct |
129 |
Correct |
754 ms |
91048 KB |
Output is correct |