#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include "bits/stdc++.h"
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
//using namespace __gnu_pbds;
ll mod;
ll add(ll x,ll y){
x+=y;
if(x<0){
x%=mod;
x+=mod;
}else{
if(x>=mod) x%=mod;
}
return x;
}
ll mul(ll a,ll b){
ll ans = (a*b)%mod;
if(ans<0) ans+=mod;
return ans;
}
#define maxn 200005
#define maxd 45
ll n,q;
vector<ll> g[maxn];
ll dp[maxn][maxd];
ll par[maxn];
void dfs(ll u,ll p){
par[u] = p;
for(ll s : g[u]){
if(s==p) continue;
dfs(s,u);
}
}
void tc(){
cin >> n >> mod;
for(ll i = 1;i<=n-1;i++){
ll x,y; cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
for(ll i = 1;i<=n;i++) for(ll j = 0;j<maxd;j++) dp[i][j] = 1;
for(ll i = 1;i<=n;i++) cin >> dp[i][0];
dfs(1,1);
cin >> q;
while(q--){
ll tip; cin >> tip;
if(tip==1){
ll x,y,d;
cin >> x >> d >> y;
while(1){
dp[x][d] = mul(dp[x][d],y);
if(!d) break;
d--;
dp[x][d] = mul(dp[x][d],y);
if(x!=1) x = par[x];
else{
if(!d) break;
d--;
}
}
}else{
ll x;
cin >> x;
ll ans = 1;
for(ll i = 0;i<maxd;i++){
ans = mul(ans,dp[x][i]);
if(x==1) break;
x = par[x];
}
cout<<ans<<endl;
}
}
}
int main(){
daj_mi_malo_vremena
int t; t = 1;
while(t--){
tc();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
4 ms |
5468 KB |
Output is correct |
5 |
Correct |
4 ms |
5472 KB |
Output is correct |
6 |
Correct |
4 ms |
5472 KB |
Output is correct |
7 |
Correct |
4 ms |
5428 KB |
Output is correct |
8 |
Correct |
4 ms |
5384 KB |
Output is correct |
9 |
Correct |
3 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
5040 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
5064 KB |
Output is correct |
14 |
Correct |
3 ms |
5040 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
5036 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
19 |
Correct |
4 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
5036 KB |
Output is correct |
21 |
Correct |
3 ms |
5028 KB |
Output is correct |
22 |
Correct |
3 ms |
4956 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
3 ms |
4948 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
3 ms |
5036 KB |
Output is correct |
27 |
Correct |
3 ms |
5028 KB |
Output is correct |
28 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
646 ms |
96356 KB |
Output is correct |
3 |
Correct |
390 ms |
96716 KB |
Output is correct |
4 |
Correct |
628 ms |
98764 KB |
Output is correct |
5 |
Correct |
525 ms |
96312 KB |
Output is correct |
6 |
Correct |
390 ms |
95928 KB |
Output is correct |
7 |
Correct |
401 ms |
96720 KB |
Output is correct |
8 |
Correct |
286 ms |
97096 KB |
Output is correct |
9 |
Correct |
681 ms |
100380 KB |
Output is correct |
10 |
Correct |
429 ms |
100380 KB |
Output is correct |
11 |
Correct |
713 ms |
96052 KB |
Output is correct |
12 |
Correct |
481 ms |
96576 KB |
Output is correct |
13 |
Correct |
283 ms |
96608 KB |
Output is correct |
14 |
Correct |
250 ms |
97192 KB |
Output is correct |
15 |
Correct |
257 ms |
96388 KB |
Output is correct |
16 |
Correct |
247 ms |
96964 KB |
Output is correct |
17 |
Correct |
267 ms |
97396 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
5032 KB |
Output is correct |
20 |
Correct |
3 ms |
5036 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
646 ms |
96356 KB |
Output is correct |
3 |
Correct |
390 ms |
96716 KB |
Output is correct |
4 |
Correct |
628 ms |
98764 KB |
Output is correct |
5 |
Correct |
525 ms |
96312 KB |
Output is correct |
6 |
Correct |
390 ms |
95928 KB |
Output is correct |
7 |
Correct |
401 ms |
96720 KB |
Output is correct |
8 |
Correct |
286 ms |
97096 KB |
Output is correct |
9 |
Correct |
681 ms |
100380 KB |
Output is correct |
10 |
Correct |
429 ms |
100380 KB |
Output is correct |
11 |
Correct |
713 ms |
96052 KB |
Output is correct |
12 |
Correct |
481 ms |
96576 KB |
Output is correct |
13 |
Correct |
283 ms |
96608 KB |
Output is correct |
14 |
Correct |
250 ms |
97192 KB |
Output is correct |
15 |
Correct |
257 ms |
96388 KB |
Output is correct |
16 |
Correct |
247 ms |
96964 KB |
Output is correct |
17 |
Correct |
267 ms |
97396 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
5032 KB |
Output is correct |
20 |
Correct |
3 ms |
5036 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
4948 KB |
Output is correct |
23 |
Correct |
3 ms |
5032 KB |
Output is correct |
24 |
Correct |
663 ms |
96120 KB |
Output is correct |
25 |
Correct |
409 ms |
96712 KB |
Output is correct |
26 |
Correct |
594 ms |
100392 KB |
Output is correct |
27 |
Correct |
527 ms |
96380 KB |
Output is correct |
28 |
Correct |
406 ms |
96528 KB |
Output is correct |
29 |
Correct |
343 ms |
96292 KB |
Output is correct |
30 |
Correct |
276 ms |
97032 KB |
Output is correct |
31 |
Correct |
706 ms |
98864 KB |
Output is correct |
32 |
Correct |
433 ms |
100548 KB |
Output is correct |
33 |
Correct |
642 ms |
96076 KB |
Output is correct |
34 |
Correct |
397 ms |
96608 KB |
Output is correct |
35 |
Correct |
3 ms |
4948 KB |
Output is correct |
36 |
Correct |
3 ms |
5028 KB |
Output is correct |
37 |
Correct |
3 ms |
4948 KB |
Output is correct |
38 |
Correct |
3 ms |
4948 KB |
Output is correct |
39 |
Correct |
4 ms |
5032 KB |
Output is correct |
40 |
Correct |
3 ms |
4948 KB |
Output is correct |
41 |
Correct |
3 ms |
4948 KB |
Output is correct |
42 |
Correct |
3 ms |
5032 KB |
Output is correct |
43 |
Correct |
3 ms |
4948 KB |
Output is correct |
44 |
Correct |
3 ms |
4948 KB |
Output is correct |
45 |
Correct |
3 ms |
4948 KB |
Output is correct |
46 |
Correct |
3 ms |
4948 KB |
Output is correct |
47 |
Correct |
3 ms |
4948 KB |
Output is correct |
48 |
Correct |
3 ms |
4948 KB |
Output is correct |
49 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
822 ms |
97568 KB |
Output is correct |
3 |
Correct |
954 ms |
96932 KB |
Output is correct |
4 |
Correct |
662 ms |
96600 KB |
Output is correct |
5 |
Correct |
630 ms |
93428 KB |
Output is correct |
6 |
Correct |
393 ms |
93656 KB |
Output is correct |
7 |
Correct |
383 ms |
93804 KB |
Output is correct |
8 |
Correct |
293 ms |
93944 KB |
Output is correct |
9 |
Correct |
807 ms |
95864 KB |
Output is correct |
10 |
Correct |
994 ms |
97452 KB |
Output is correct |
11 |
Correct |
752 ms |
93084 KB |
Output is correct |
12 |
Correct |
887 ms |
93860 KB |
Output is correct |
13 |
Correct |
511 ms |
94768 KB |
Output is correct |
14 |
Correct |
519 ms |
95768 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
767 ms |
98228 KB |
Output is correct |
3 |
Correct |
1068 ms |
95932 KB |
Output is correct |
4 |
Correct |
793 ms |
97076 KB |
Output is correct |
5 |
Correct |
713 ms |
95032 KB |
Output is correct |
6 |
Correct |
467 ms |
95168 KB |
Output is correct |
7 |
Correct |
414 ms |
95024 KB |
Output is correct |
8 |
Correct |
294 ms |
95028 KB |
Output is correct |
9 |
Correct |
1002 ms |
99700 KB |
Output is correct |
10 |
Correct |
981 ms |
98104 KB |
Output is correct |
11 |
Correct |
843 ms |
95648 KB |
Output is correct |
12 |
Correct |
810 ms |
94156 KB |
Output is correct |
13 |
Correct |
447 ms |
94976 KB |
Output is correct |
14 |
Correct |
492 ms |
95776 KB |
Output is correct |
15 |
Correct |
3 ms |
5036 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
4 ms |
4948 KB |
Output is correct |
18 |
Correct |
5 ms |
4948 KB |
Output is correct |
19 |
Correct |
5 ms |
5032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
4 ms |
5468 KB |
Output is correct |
5 |
Correct |
4 ms |
5472 KB |
Output is correct |
6 |
Correct |
4 ms |
5472 KB |
Output is correct |
7 |
Correct |
4 ms |
5428 KB |
Output is correct |
8 |
Correct |
4 ms |
5384 KB |
Output is correct |
9 |
Correct |
3 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
5040 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
5064 KB |
Output is correct |
14 |
Correct |
3 ms |
5040 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
5036 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
19 |
Correct |
4 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
5036 KB |
Output is correct |
21 |
Correct |
3 ms |
5028 KB |
Output is correct |
22 |
Correct |
3 ms |
4956 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
3 ms |
4948 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
3 ms |
5036 KB |
Output is correct |
27 |
Correct |
3 ms |
5028 KB |
Output is correct |
28 |
Correct |
3 ms |
4948 KB |
Output is correct |
29 |
Correct |
3 ms |
5024 KB |
Output is correct |
30 |
Correct |
646 ms |
96356 KB |
Output is correct |
31 |
Correct |
390 ms |
96716 KB |
Output is correct |
32 |
Correct |
628 ms |
98764 KB |
Output is correct |
33 |
Correct |
525 ms |
96312 KB |
Output is correct |
34 |
Correct |
390 ms |
95928 KB |
Output is correct |
35 |
Correct |
401 ms |
96720 KB |
Output is correct |
36 |
Correct |
286 ms |
97096 KB |
Output is correct |
37 |
Correct |
681 ms |
100380 KB |
Output is correct |
38 |
Correct |
429 ms |
100380 KB |
Output is correct |
39 |
Correct |
713 ms |
96052 KB |
Output is correct |
40 |
Correct |
481 ms |
96576 KB |
Output is correct |
41 |
Correct |
283 ms |
96608 KB |
Output is correct |
42 |
Correct |
250 ms |
97192 KB |
Output is correct |
43 |
Correct |
257 ms |
96388 KB |
Output is correct |
44 |
Correct |
247 ms |
96964 KB |
Output is correct |
45 |
Correct |
267 ms |
97396 KB |
Output is correct |
46 |
Correct |
3 ms |
4948 KB |
Output is correct |
47 |
Correct |
4 ms |
5032 KB |
Output is correct |
48 |
Correct |
3 ms |
5036 KB |
Output is correct |
49 |
Correct |
3 ms |
4948 KB |
Output is correct |
50 |
Correct |
3 ms |
4948 KB |
Output is correct |
51 |
Correct |
3 ms |
5032 KB |
Output is correct |
52 |
Correct |
663 ms |
96120 KB |
Output is correct |
53 |
Correct |
409 ms |
96712 KB |
Output is correct |
54 |
Correct |
594 ms |
100392 KB |
Output is correct |
55 |
Correct |
527 ms |
96380 KB |
Output is correct |
56 |
Correct |
406 ms |
96528 KB |
Output is correct |
57 |
Correct |
343 ms |
96292 KB |
Output is correct |
58 |
Correct |
276 ms |
97032 KB |
Output is correct |
59 |
Correct |
706 ms |
98864 KB |
Output is correct |
60 |
Correct |
433 ms |
100548 KB |
Output is correct |
61 |
Correct |
642 ms |
96076 KB |
Output is correct |
62 |
Correct |
397 ms |
96608 KB |
Output is correct |
63 |
Correct |
3 ms |
4948 KB |
Output is correct |
64 |
Correct |
3 ms |
5028 KB |
Output is correct |
65 |
Correct |
3 ms |
4948 KB |
Output is correct |
66 |
Correct |
3 ms |
4948 KB |
Output is correct |
67 |
Correct |
4 ms |
5032 KB |
Output is correct |
68 |
Correct |
3 ms |
4948 KB |
Output is correct |
69 |
Correct |
3 ms |
4948 KB |
Output is correct |
70 |
Correct |
3 ms |
5032 KB |
Output is correct |
71 |
Correct |
3 ms |
4948 KB |
Output is correct |
72 |
Correct |
3 ms |
4948 KB |
Output is correct |
73 |
Correct |
3 ms |
4948 KB |
Output is correct |
74 |
Correct |
3 ms |
4948 KB |
Output is correct |
75 |
Correct |
3 ms |
4948 KB |
Output is correct |
76 |
Correct |
3 ms |
4948 KB |
Output is correct |
77 |
Correct |
3 ms |
4948 KB |
Output is correct |
78 |
Correct |
3 ms |
4948 KB |
Output is correct |
79 |
Correct |
822 ms |
97568 KB |
Output is correct |
80 |
Correct |
954 ms |
96932 KB |
Output is correct |
81 |
Correct |
662 ms |
96600 KB |
Output is correct |
82 |
Correct |
630 ms |
93428 KB |
Output is correct |
83 |
Correct |
393 ms |
93656 KB |
Output is correct |
84 |
Correct |
383 ms |
93804 KB |
Output is correct |
85 |
Correct |
293 ms |
93944 KB |
Output is correct |
86 |
Correct |
807 ms |
95864 KB |
Output is correct |
87 |
Correct |
994 ms |
97452 KB |
Output is correct |
88 |
Correct |
752 ms |
93084 KB |
Output is correct |
89 |
Correct |
887 ms |
93860 KB |
Output is correct |
90 |
Correct |
511 ms |
94768 KB |
Output is correct |
91 |
Correct |
519 ms |
95768 KB |
Output is correct |
92 |
Correct |
3 ms |
4948 KB |
Output is correct |
93 |
Correct |
4 ms |
4992 KB |
Output is correct |
94 |
Correct |
3 ms |
4948 KB |
Output is correct |
95 |
Correct |
3 ms |
4948 KB |
Output is correct |
96 |
Correct |
3 ms |
5032 KB |
Output is correct |
97 |
Correct |
3 ms |
4952 KB |
Output is correct |
98 |
Correct |
767 ms |
98228 KB |
Output is correct |
99 |
Correct |
1068 ms |
95932 KB |
Output is correct |
100 |
Correct |
793 ms |
97076 KB |
Output is correct |
101 |
Correct |
713 ms |
95032 KB |
Output is correct |
102 |
Correct |
467 ms |
95168 KB |
Output is correct |
103 |
Correct |
414 ms |
95024 KB |
Output is correct |
104 |
Correct |
294 ms |
95028 KB |
Output is correct |
105 |
Correct |
1002 ms |
99700 KB |
Output is correct |
106 |
Correct |
981 ms |
98104 KB |
Output is correct |
107 |
Correct |
843 ms |
95648 KB |
Output is correct |
108 |
Correct |
810 ms |
94156 KB |
Output is correct |
109 |
Correct |
447 ms |
94976 KB |
Output is correct |
110 |
Correct |
492 ms |
95776 KB |
Output is correct |
111 |
Correct |
3 ms |
5036 KB |
Output is correct |
112 |
Correct |
3 ms |
5028 KB |
Output is correct |
113 |
Correct |
4 ms |
4948 KB |
Output is correct |
114 |
Correct |
5 ms |
4948 KB |
Output is correct |
115 |
Correct |
5 ms |
5032 KB |
Output is correct |
116 |
Correct |
677 ms |
94080 KB |
Output is correct |
117 |
Correct |
799 ms |
96768 KB |
Output is correct |
118 |
Correct |
701 ms |
100396 KB |
Output is correct |
119 |
Correct |
630 ms |
96392 KB |
Output is correct |
120 |
Correct |
453 ms |
96076 KB |
Output is correct |
121 |
Correct |
410 ms |
96828 KB |
Output is correct |
122 |
Correct |
291 ms |
96864 KB |
Output is correct |
123 |
Correct |
835 ms |
99484 KB |
Output is correct |
124 |
Correct |
946 ms |
98620 KB |
Output is correct |
125 |
Correct |
768 ms |
95372 KB |
Output is correct |
126 |
Correct |
775 ms |
96648 KB |
Output is correct |
127 |
Correct |
821 ms |
97152 KB |
Output is correct |
128 |
Correct |
548 ms |
97776 KB |
Output is correct |
129 |
Correct |
584 ms |
98812 KB |
Output is correct |