#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
signed main(){
_3xxEYjy;
int n,mod;
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,40){
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);
}
}
vec(vi) dp(n,vi(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]*=x%mod;
dp[v][d]%=mod;
d-=1;
v=par[v];
}
};
auto get=[&](int v)->int{
int d=0;
int res=1;
while(d<40){
res*=dp[v][d]%mod;
res%=mod;
res*=dp[v][d+1]%mod;
res%=mod;
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;
int res=get(v);
res*=a[v];
res%=mod;
print(res);
}
}
//
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 |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
2 ms |
724 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
913 ms |
92984 KB |
Output is correct |
3 |
Correct |
515 ms |
98420 KB |
Output is correct |
4 |
Correct |
803 ms |
101992 KB |
Output is correct |
5 |
Correct |
659 ms |
100880 KB |
Output is correct |
6 |
Correct |
506 ms |
97704 KB |
Output is correct |
7 |
Correct |
567 ms |
98644 KB |
Output is correct |
8 |
Correct |
438 ms |
98920 KB |
Output is correct |
9 |
Correct |
1171 ms |
105080 KB |
Output is correct |
10 |
Correct |
459 ms |
103372 KB |
Output is correct |
11 |
Correct |
869 ms |
99172 KB |
Output is correct |
12 |
Correct |
467 ms |
96024 KB |
Output is correct |
13 |
Correct |
282 ms |
100720 KB |
Output is correct |
14 |
Correct |
286 ms |
100300 KB |
Output is correct |
15 |
Correct |
272 ms |
91068 KB |
Output is correct |
16 |
Correct |
291 ms |
94600 KB |
Output is correct |
17 |
Correct |
289 ms |
94940 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
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 |
913 ms |
92984 KB |
Output is correct |
3 |
Correct |
515 ms |
98420 KB |
Output is correct |
4 |
Correct |
803 ms |
101992 KB |
Output is correct |
5 |
Correct |
659 ms |
100880 KB |
Output is correct |
6 |
Correct |
506 ms |
97704 KB |
Output is correct |
7 |
Correct |
567 ms |
98644 KB |
Output is correct |
8 |
Correct |
438 ms |
98920 KB |
Output is correct |
9 |
Correct |
1171 ms |
105080 KB |
Output is correct |
10 |
Correct |
459 ms |
103372 KB |
Output is correct |
11 |
Correct |
869 ms |
99172 KB |
Output is correct |
12 |
Correct |
467 ms |
96024 KB |
Output is correct |
13 |
Correct |
282 ms |
100720 KB |
Output is correct |
14 |
Correct |
286 ms |
100300 KB |
Output is correct |
15 |
Correct |
272 ms |
91068 KB |
Output is correct |
16 |
Correct |
291 ms |
94600 KB |
Output is correct |
17 |
Correct |
289 ms |
94940 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
340 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 |
825 ms |
101128 KB |
Output is correct |
25 |
Correct |
451 ms |
99996 KB |
Output is correct |
26 |
Correct |
773 ms |
106556 KB |
Output is correct |
27 |
Correct |
652 ms |
100240 KB |
Output is correct |
28 |
Correct |
528 ms |
101024 KB |
Output is correct |
29 |
Correct |
518 ms |
100748 KB |
Output is correct |
30 |
Correct |
440 ms |
100928 KB |
Output is correct |
31 |
Correct |
1063 ms |
104348 KB |
Output is correct |
32 |
Correct |
479 ms |
105432 KB |
Output is correct |
33 |
Correct |
854 ms |
100900 KB |
Output is correct |
34 |
Correct |
471 ms |
100648 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
2 ms |
328 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
320 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
320 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 |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 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 |
Incorrect |
1299 ms |
96004 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1264 ms |
96404 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
2 ms |
724 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |