#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+10;
vector<int>adj[maxn];
int q,high[maxn],n,mod,timea=0,part[maxn];
pair<int,int>stf[maxn],av[maxn][42];
struct segment{
vector<long long>seg;
int sz=0;
void create(){
while(__builtin_popcount(sz)!=1){
sz++;
}
seg.resize(sz*2,1);
}
void add(int i,int l,int r,int tl,int tr,int w){
if(l>r||l>tr||r<tl||tl>tr){
return ;
}
if(l>=tl&&r<=tr){
seg[i]*=w;
seg[i]%=mod;
return ;
}
int m=(l+r)>>1;
add((i<<1),l,m,tl,tr,w);
add((i<<1)^1,m+1,r,tl,tr,w);
return ;
}
int pors(int i){
if(i==0){
return 1;
}
int ret=seg[i]*pors((i>>1))%mod;
return ret;
}
}seg[maxn];
vector<pair<int,int>>alll[maxn];
void pre(int u=1,int par=1){
part[u]=par;
timea++;
stf[u].first=timea;
alll[high[u]].push_back(make_pair(stf[u].first,u));
for(auto x:adj[u]){
if(x!=par){
high[x]=high[u]+1;
pre(x,u);
}
}
stf[u].second=timea;
}
void upd(int u,int dis,int w){
int z=0;
int lev=high[u]+dis;
while(lev>=0&&high[u]<=lev){
if(lev<maxn){
int l=av[u][lev-high[u]].first;
int r=av[u][lev-high[u]].second;
seg[lev].add(1,0,seg[lev].sz-1,l,r,w);
}
if(z==1){
u=part[u];
lev--;
z=0;
}
else{
lev--;
z=1;
}
}
}
void solve(int u){
int ind=lower_bound(alll[high[u]].begin(),alll[high[u]].end(),make_pair(stf[u].first,u))-alll[high[u]].begin();
cout<<seg[high[u]].pors(ind+seg[high[u]].sz)<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>mod;
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
pre(1);
for(int i=1;i<=n;i++){
for(int j=high[i];j<=min(high[i]+40,maxn-1);j++){
int l=lower_bound(alll[j].begin(),alll[j].end(),make_pair(stf[i].first,-1))-alll[j].begin();
int r=lower_bound(alll[j].begin(),alll[j].end(),make_pair(stf[i].second+1,-1))-alll[j].begin()-1;
av[i][j-high[i]]=make_pair(l,r);
}
}
for(int i=0;i<maxn;i++){
seg[i].sz=alll[i].size();
seg[i].create();
}
for(int i=1;i<=n;i++){
int d;
cin>>d;
upd(i,0,d);
}
cin>>q;
for(int i=0;i<q;i++){
int qq;
cin>>qq;
if(qq==1){
int u,d,w;
cin>>u>>d>>w;
if(d>40){
assert(0);
}
upd(u,d,w);
}
else{
int u;
cin>>u;
solve(u);
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
27480 KB |
Output is correct |
2 |
Correct |
14 ms |
27484 KB |
Output is correct |
3 |
Correct |
14 ms |
27484 KB |
Output is correct |
4 |
Correct |
15 ms |
27652 KB |
Output is correct |
5 |
Correct |
15 ms |
27544 KB |
Output is correct |
6 |
Correct |
16 ms |
27992 KB |
Output is correct |
7 |
Correct |
15 ms |
27740 KB |
Output is correct |
8 |
Correct |
15 ms |
27740 KB |
Output is correct |
9 |
Correct |
15 ms |
27480 KB |
Output is correct |
10 |
Correct |
15 ms |
27488 KB |
Output is correct |
11 |
Correct |
15 ms |
27484 KB |
Output is correct |
12 |
Correct |
16 ms |
27644 KB |
Output is correct |
13 |
Correct |
14 ms |
27588 KB |
Output is correct |
14 |
Correct |
14 ms |
27484 KB |
Output is correct |
15 |
Correct |
14 ms |
27672 KB |
Output is correct |
16 |
Correct |
14 ms |
27484 KB |
Output is correct |
17 |
Correct |
15 ms |
27480 KB |
Output is correct |
18 |
Correct |
15 ms |
27740 KB |
Output is correct |
19 |
Correct |
14 ms |
27484 KB |
Output is correct |
20 |
Correct |
15 ms |
27508 KB |
Output is correct |
21 |
Correct |
15 ms |
27520 KB |
Output is correct |
22 |
Correct |
15 ms |
27480 KB |
Output is correct |
23 |
Correct |
14 ms |
27536 KB |
Output is correct |
24 |
Correct |
14 ms |
27484 KB |
Output is correct |
25 |
Correct |
14 ms |
27480 KB |
Output is correct |
26 |
Correct |
14 ms |
27484 KB |
Output is correct |
27 |
Correct |
15 ms |
27480 KB |
Output is correct |
28 |
Correct |
15 ms |
27484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
27736 KB |
Output is correct |
2 |
Correct |
864 ms |
112696 KB |
Output is correct |
3 |
Correct |
1120 ms |
111324 KB |
Output is correct |
4 |
Correct |
381 ms |
122028 KB |
Output is correct |
5 |
Correct |
1010 ms |
111996 KB |
Output is correct |
6 |
Correct |
704 ms |
111308 KB |
Output is correct |
7 |
Correct |
609 ms |
111164 KB |
Output is correct |
8 |
Correct |
366 ms |
111192 KB |
Output is correct |
9 |
Correct |
423 ms |
129784 KB |
Output is correct |
10 |
Correct |
432 ms |
126868 KB |
Output is correct |
11 |
Correct |
916 ms |
112436 KB |
Output is correct |
12 |
Correct |
1024 ms |
111232 KB |
Output is correct |
13 |
Correct |
383 ms |
111256 KB |
Output is correct |
14 |
Correct |
429 ms |
111868 KB |
Output is correct |
15 |
Correct |
433 ms |
111248 KB |
Output is correct |
16 |
Correct |
442 ms |
111380 KB |
Output is correct |
17 |
Correct |
494 ms |
111704 KB |
Output is correct |
18 |
Correct |
14 ms |
27664 KB |
Output is correct |
19 |
Correct |
15 ms |
27484 KB |
Output is correct |
20 |
Correct |
15 ms |
27484 KB |
Output is correct |
21 |
Correct |
14 ms |
27480 KB |
Output is correct |
22 |
Correct |
14 ms |
27616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
27736 KB |
Output is correct |
2 |
Correct |
864 ms |
112696 KB |
Output is correct |
3 |
Correct |
1120 ms |
111324 KB |
Output is correct |
4 |
Correct |
381 ms |
122028 KB |
Output is correct |
5 |
Correct |
1010 ms |
111996 KB |
Output is correct |
6 |
Correct |
704 ms |
111308 KB |
Output is correct |
7 |
Correct |
609 ms |
111164 KB |
Output is correct |
8 |
Correct |
366 ms |
111192 KB |
Output is correct |
9 |
Correct |
423 ms |
129784 KB |
Output is correct |
10 |
Correct |
432 ms |
126868 KB |
Output is correct |
11 |
Correct |
916 ms |
112436 KB |
Output is correct |
12 |
Correct |
1024 ms |
111232 KB |
Output is correct |
13 |
Correct |
383 ms |
111256 KB |
Output is correct |
14 |
Correct |
429 ms |
111868 KB |
Output is correct |
15 |
Correct |
433 ms |
111248 KB |
Output is correct |
16 |
Correct |
442 ms |
111380 KB |
Output is correct |
17 |
Correct |
494 ms |
111704 KB |
Output is correct |
18 |
Correct |
14 ms |
27664 KB |
Output is correct |
19 |
Correct |
15 ms |
27484 KB |
Output is correct |
20 |
Correct |
15 ms |
27484 KB |
Output is correct |
21 |
Correct |
14 ms |
27480 KB |
Output is correct |
22 |
Correct |
14 ms |
27616 KB |
Output is correct |
23 |
Correct |
15 ms |
27484 KB |
Output is correct |
24 |
Correct |
967 ms |
112392 KB |
Output is correct |
25 |
Correct |
1094 ms |
111172 KB |
Output is correct |
26 |
Correct |
418 ms |
128552 KB |
Output is correct |
27 |
Correct |
1000 ms |
112212 KB |
Output is correct |
28 |
Correct |
703 ms |
111616 KB |
Output is correct |
29 |
Correct |
630 ms |
111660 KB |
Output is correct |
30 |
Correct |
359 ms |
111164 KB |
Output is correct |
31 |
Correct |
387 ms |
123524 KB |
Output is correct |
32 |
Correct |
411 ms |
126916 KB |
Output is correct |
33 |
Correct |
877 ms |
112652 KB |
Output is correct |
34 |
Correct |
1166 ms |
111140 KB |
Output is correct |
35 |
Correct |
15 ms |
27480 KB |
Output is correct |
36 |
Correct |
14 ms |
27484 KB |
Output is correct |
37 |
Correct |
14 ms |
27564 KB |
Output is correct |
38 |
Correct |
15 ms |
27484 KB |
Output is correct |
39 |
Correct |
14 ms |
27484 KB |
Output is correct |
40 |
Correct |
15 ms |
27484 KB |
Output is correct |
41 |
Correct |
14 ms |
27484 KB |
Output is correct |
42 |
Correct |
16 ms |
27604 KB |
Output is correct |
43 |
Correct |
15 ms |
27484 KB |
Output is correct |
44 |
Correct |
14 ms |
27484 KB |
Output is correct |
45 |
Correct |
14 ms |
27484 KB |
Output is correct |
46 |
Correct |
15 ms |
27496 KB |
Output is correct |
47 |
Correct |
14 ms |
27484 KB |
Output is correct |
48 |
Correct |
14 ms |
27484 KB |
Output is correct |
49 |
Correct |
14 ms |
27484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
27480 KB |
Output is correct |
2 |
Correct |
529 ms |
126780 KB |
Output is correct |
3 |
Correct |
2028 ms |
121936 KB |
Output is correct |
4 |
Correct |
815 ms |
117844 KB |
Output is correct |
5 |
Correct |
1762 ms |
104904 KB |
Output is correct |
6 |
Correct |
1076 ms |
104516 KB |
Output is correct |
7 |
Correct |
761 ms |
105012 KB |
Output is correct |
8 |
Correct |
373 ms |
104692 KB |
Output is correct |
9 |
Correct |
573 ms |
116576 KB |
Output is correct |
10 |
Correct |
1760 ms |
119912 KB |
Output is correct |
11 |
Correct |
1211 ms |
105176 KB |
Output is correct |
12 |
Correct |
3261 ms |
105240 KB |
Output is correct |
13 |
Correct |
2736 ms |
114076 KB |
Output is correct |
14 |
Correct |
2197 ms |
116708 KB |
Output is correct |
15 |
Correct |
14 ms |
27484 KB |
Output is correct |
16 |
Correct |
15 ms |
27484 KB |
Output is correct |
17 |
Correct |
15 ms |
27484 KB |
Output is correct |
18 |
Correct |
19 ms |
27484 KB |
Output is correct |
19 |
Correct |
15 ms |
27484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
27484 KB |
Output is correct |
2 |
Correct |
612 ms |
118088 KB |
Output is correct |
3 |
Correct |
2489 ms |
112296 KB |
Output is correct |
4 |
Correct |
935 ms |
115796 KB |
Output is correct |
5 |
Correct |
1694 ms |
106324 KB |
Output is correct |
6 |
Correct |
1029 ms |
106864 KB |
Output is correct |
7 |
Correct |
817 ms |
105444 KB |
Output is correct |
8 |
Correct |
340 ms |
105536 KB |
Output is correct |
9 |
Correct |
551 ms |
123988 KB |
Output is correct |
10 |
Correct |
1721 ms |
121256 KB |
Output is correct |
11 |
Correct |
1236 ms |
107732 KB |
Output is correct |
12 |
Correct |
3557 ms |
105740 KB |
Output is correct |
13 |
Correct |
2937 ms |
113976 KB |
Output is correct |
14 |
Correct |
2334 ms |
116988 KB |
Output is correct |
15 |
Correct |
14 ms |
27480 KB |
Output is correct |
16 |
Correct |
15 ms |
27596 KB |
Output is correct |
17 |
Correct |
14 ms |
27484 KB |
Output is correct |
18 |
Correct |
14 ms |
27572 KB |
Output is correct |
19 |
Correct |
14 ms |
27484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
27480 KB |
Output is correct |
2 |
Correct |
14 ms |
27484 KB |
Output is correct |
3 |
Correct |
14 ms |
27484 KB |
Output is correct |
4 |
Correct |
15 ms |
27652 KB |
Output is correct |
5 |
Correct |
15 ms |
27544 KB |
Output is correct |
6 |
Correct |
16 ms |
27992 KB |
Output is correct |
7 |
Correct |
15 ms |
27740 KB |
Output is correct |
8 |
Correct |
15 ms |
27740 KB |
Output is correct |
9 |
Correct |
15 ms |
27480 KB |
Output is correct |
10 |
Correct |
15 ms |
27488 KB |
Output is correct |
11 |
Correct |
15 ms |
27484 KB |
Output is correct |
12 |
Correct |
16 ms |
27644 KB |
Output is correct |
13 |
Correct |
14 ms |
27588 KB |
Output is correct |
14 |
Correct |
14 ms |
27484 KB |
Output is correct |
15 |
Correct |
14 ms |
27672 KB |
Output is correct |
16 |
Correct |
14 ms |
27484 KB |
Output is correct |
17 |
Correct |
15 ms |
27480 KB |
Output is correct |
18 |
Correct |
15 ms |
27740 KB |
Output is correct |
19 |
Correct |
14 ms |
27484 KB |
Output is correct |
20 |
Correct |
15 ms |
27508 KB |
Output is correct |
21 |
Correct |
15 ms |
27520 KB |
Output is correct |
22 |
Correct |
15 ms |
27480 KB |
Output is correct |
23 |
Correct |
14 ms |
27536 KB |
Output is correct |
24 |
Correct |
14 ms |
27484 KB |
Output is correct |
25 |
Correct |
14 ms |
27480 KB |
Output is correct |
26 |
Correct |
14 ms |
27484 KB |
Output is correct |
27 |
Correct |
15 ms |
27480 KB |
Output is correct |
28 |
Correct |
15 ms |
27484 KB |
Output is correct |
29 |
Correct |
14 ms |
27736 KB |
Output is correct |
30 |
Correct |
864 ms |
112696 KB |
Output is correct |
31 |
Correct |
1120 ms |
111324 KB |
Output is correct |
32 |
Correct |
381 ms |
122028 KB |
Output is correct |
33 |
Correct |
1010 ms |
111996 KB |
Output is correct |
34 |
Correct |
704 ms |
111308 KB |
Output is correct |
35 |
Correct |
609 ms |
111164 KB |
Output is correct |
36 |
Correct |
366 ms |
111192 KB |
Output is correct |
37 |
Correct |
423 ms |
129784 KB |
Output is correct |
38 |
Correct |
432 ms |
126868 KB |
Output is correct |
39 |
Correct |
916 ms |
112436 KB |
Output is correct |
40 |
Correct |
1024 ms |
111232 KB |
Output is correct |
41 |
Correct |
383 ms |
111256 KB |
Output is correct |
42 |
Correct |
429 ms |
111868 KB |
Output is correct |
43 |
Correct |
433 ms |
111248 KB |
Output is correct |
44 |
Correct |
442 ms |
111380 KB |
Output is correct |
45 |
Correct |
494 ms |
111704 KB |
Output is correct |
46 |
Correct |
14 ms |
27664 KB |
Output is correct |
47 |
Correct |
15 ms |
27484 KB |
Output is correct |
48 |
Correct |
15 ms |
27484 KB |
Output is correct |
49 |
Correct |
14 ms |
27480 KB |
Output is correct |
50 |
Correct |
14 ms |
27616 KB |
Output is correct |
51 |
Correct |
15 ms |
27484 KB |
Output is correct |
52 |
Correct |
967 ms |
112392 KB |
Output is correct |
53 |
Correct |
1094 ms |
111172 KB |
Output is correct |
54 |
Correct |
418 ms |
128552 KB |
Output is correct |
55 |
Correct |
1000 ms |
112212 KB |
Output is correct |
56 |
Correct |
703 ms |
111616 KB |
Output is correct |
57 |
Correct |
630 ms |
111660 KB |
Output is correct |
58 |
Correct |
359 ms |
111164 KB |
Output is correct |
59 |
Correct |
387 ms |
123524 KB |
Output is correct |
60 |
Correct |
411 ms |
126916 KB |
Output is correct |
61 |
Correct |
877 ms |
112652 KB |
Output is correct |
62 |
Correct |
1166 ms |
111140 KB |
Output is correct |
63 |
Correct |
15 ms |
27480 KB |
Output is correct |
64 |
Correct |
14 ms |
27484 KB |
Output is correct |
65 |
Correct |
14 ms |
27564 KB |
Output is correct |
66 |
Correct |
15 ms |
27484 KB |
Output is correct |
67 |
Correct |
14 ms |
27484 KB |
Output is correct |
68 |
Correct |
15 ms |
27484 KB |
Output is correct |
69 |
Correct |
14 ms |
27484 KB |
Output is correct |
70 |
Correct |
16 ms |
27604 KB |
Output is correct |
71 |
Correct |
15 ms |
27484 KB |
Output is correct |
72 |
Correct |
14 ms |
27484 KB |
Output is correct |
73 |
Correct |
14 ms |
27484 KB |
Output is correct |
74 |
Correct |
15 ms |
27496 KB |
Output is correct |
75 |
Correct |
14 ms |
27484 KB |
Output is correct |
76 |
Correct |
14 ms |
27484 KB |
Output is correct |
77 |
Correct |
14 ms |
27484 KB |
Output is correct |
78 |
Correct |
14 ms |
27480 KB |
Output is correct |
79 |
Correct |
529 ms |
126780 KB |
Output is correct |
80 |
Correct |
2028 ms |
121936 KB |
Output is correct |
81 |
Correct |
815 ms |
117844 KB |
Output is correct |
82 |
Correct |
1762 ms |
104904 KB |
Output is correct |
83 |
Correct |
1076 ms |
104516 KB |
Output is correct |
84 |
Correct |
761 ms |
105012 KB |
Output is correct |
85 |
Correct |
373 ms |
104692 KB |
Output is correct |
86 |
Correct |
573 ms |
116576 KB |
Output is correct |
87 |
Correct |
1760 ms |
119912 KB |
Output is correct |
88 |
Correct |
1211 ms |
105176 KB |
Output is correct |
89 |
Correct |
3261 ms |
105240 KB |
Output is correct |
90 |
Correct |
2736 ms |
114076 KB |
Output is correct |
91 |
Correct |
2197 ms |
116708 KB |
Output is correct |
92 |
Correct |
14 ms |
27484 KB |
Output is correct |
93 |
Correct |
15 ms |
27484 KB |
Output is correct |
94 |
Correct |
15 ms |
27484 KB |
Output is correct |
95 |
Correct |
19 ms |
27484 KB |
Output is correct |
96 |
Correct |
15 ms |
27484 KB |
Output is correct |
97 |
Correct |
14 ms |
27484 KB |
Output is correct |
98 |
Correct |
612 ms |
118088 KB |
Output is correct |
99 |
Correct |
2489 ms |
112296 KB |
Output is correct |
100 |
Correct |
935 ms |
115796 KB |
Output is correct |
101 |
Correct |
1694 ms |
106324 KB |
Output is correct |
102 |
Correct |
1029 ms |
106864 KB |
Output is correct |
103 |
Correct |
817 ms |
105444 KB |
Output is correct |
104 |
Correct |
340 ms |
105536 KB |
Output is correct |
105 |
Correct |
551 ms |
123988 KB |
Output is correct |
106 |
Correct |
1721 ms |
121256 KB |
Output is correct |
107 |
Correct |
1236 ms |
107732 KB |
Output is correct |
108 |
Correct |
3557 ms |
105740 KB |
Output is correct |
109 |
Correct |
2937 ms |
113976 KB |
Output is correct |
110 |
Correct |
2334 ms |
116988 KB |
Output is correct |
111 |
Correct |
14 ms |
27480 KB |
Output is correct |
112 |
Correct |
15 ms |
27596 KB |
Output is correct |
113 |
Correct |
14 ms |
27484 KB |
Output is correct |
114 |
Correct |
14 ms |
27572 KB |
Output is correct |
115 |
Correct |
14 ms |
27484 KB |
Output is correct |
116 |
Correct |
1237 ms |
114196 KB |
Output is correct |
117 |
Correct |
3653 ms |
116528 KB |
Output is correct |
118 |
Correct |
758 ms |
132688 KB |
Output is correct |
119 |
Correct |
1912 ms |
116572 KB |
Output is correct |
120 |
Correct |
1080 ms |
115944 KB |
Output is correct |
121 |
Correct |
903 ms |
115852 KB |
Output is correct |
122 |
Correct |
366 ms |
116012 KB |
Output is correct |
123 |
Correct |
587 ms |
129192 KB |
Output is correct |
124 |
Correct |
2328 ms |
126004 KB |
Output is correct |
125 |
Correct |
1279 ms |
115096 KB |
Output is correct |
126 |
Correct |
3705 ms |
116788 KB |
Output is correct |
127 |
Correct |
3673 ms |
117560 KB |
Output is correct |
128 |
Correct |
2694 ms |
116820 KB |
Output is correct |
129 |
Correct |
3053 ms |
119532 KB |
Output is correct |