#include<iostream>
#include<cstdlib>
#include<vector>
#include<cassert>
#define ll long long
using namespace std;
int n,q,a,b,c,ojc[200005],d,ojc2[81];
ll L,tab[200005],x;
ll t[200005][42];
ll f[200005][81];
ll p[200005];
vector<int> v[200005];
vector<int> v2[42];
void DFS(int x,int f){
ojc[x]=f;
for(auto i:v[x]) if(i!=f) DFS(i,x);
}
void dod2(int x,int p,int k,ll z){
assert(k==(p+1));
t[x][p]*=z,t[x][p]%=L;
}
void dod(int x,int k,ll z){
//od 0 do k
p[x]*=z,p[x]%=L;
for(auto u:v2[k])
f[x][u]*=z,f[x][u]%=L;
}
int main(){
ios_base::sync_with_stdio(0);
cin>>n>>L;
for(int i=1;i<n;i++){
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
for(int i=1;i<=n;i++){
for(int u=0;u<=41;u++) t[i][u]=1;
for(int u=0;u<80;u++) f[i][u]=1;
p[i]=1;
cin>>tab[i];
}
for(int u=1;u<=63;u++) ojc2[u]=u>>1;
ojc2[64]=0;
for(int u=65;u<=78;u++) ojc2[u]=((u-63)>>1)+63; //fenwick liczy odleglosci od 1
for(int u=1;u<=32;u++){
int p=1,k=32,m=1;
while(true){
if(k==u){
v2[u].push_back(m);
break;
}
if(p>u) break;
if(u>((p+k)>>1)){
v2[u].push_back(m*2);
m=m*2+1;
p=((p+k)>>1)+1;
}else{
m=m*2;
k=((p+k)>>1);
}
}
}
v2[33].push_back(1);
v2[33].push_back(71);
v2[34].push_back(1);
v2[34].push_back(67);
v2[35].push_back(1);
v2[35].push_back(67);
v2[35].push_back(73);
v2[36].push_back(1);
v2[36].push_back(65);
v2[37].push_back(1);
v2[37].push_back(65);
v2[37].push_back(75);
v2[38].push_back(1);
v2[38].push_back(69);
v2[38].push_back(65);
v2[39].push_back(1);
v2[39].push_back(65);
v2[39].push_back(69);
v2[39].push_back(77);
v2[40].push_back(1);
v2[40].push_back(64);
DFS(1,-1);
//return 0;
//cerr<<v2[0].size()<<" rozmiar\n";
cin>>q;
for(int i=0;i<q;i++){
cin>>a;
if(a==2){
cin>>b;
d=0,c=b;
ll wyn=tab[b];
//cerr<<wyn<<"\n";
wyn*=p[c];
wyn%=L;
while(true){
wyn*=t[c][d];
wyn%=L;
if(d!=0){
wyn*=t[c][d-1];
wyn%=L;
if(d<=32)
a=d+31;
else
a=d+38;
wyn*=f[c][a];
wyn%=L;
//if(c==1) cerr<<"f["<<c<<"]["<<a<<"]="<<f[c][a]<<"\n";
while(a!=0){
a=ojc2[a];
//cerr<<"f["<<c<<"]["<<a<<"]="<<f[c][a]<<"\n";
wyn*=f[c][a];
wyn%=L;
}
}
c=ojc[c];
if(c==-1) break;
d++;
if(d>40) break;
}
cout<<wyn<<"\n";
}else{
cin>>b>>c>>x;
d=0;
while(true){
if(b==-1 || d>c) break;
if(b!=1 && c!=d){
// cerr<<b<<" "<<c-d-1<<"\n";
dod2(b,c-d-1,c-d,x);
}else{
// cerr<<b<<" "<<c<<"-"<<d<<" "<<" drzewo\n";
dod(b,c-d,x);
}
b=ojc[b];
d++;
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5972 KB |
Output is correct |
5 |
Correct |
5 ms |
6064 KB |
Output is correct |
6 |
Correct |
4 ms |
6100 KB |
Output is correct |
7 |
Correct |
4 ms |
6100 KB |
Output is correct |
8 |
Correct |
4 ms |
6072 KB |
Output is correct |
9 |
Correct |
3 ms |
5032 KB |
Output is correct |
10 |
Correct |
3 ms |
5032 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
4 ms |
5040 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5036 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5036 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 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 |
3 ms |
4948 KB |
Output is correct |
25 |
Correct |
3 ms |
5028 KB |
Output is correct |
26 |
Correct |
3 ms |
5028 KB |
Output is correct |
27 |
Correct |
3 ms |
5032 KB |
Output is correct |
28 |
Correct |
3 ms |
5032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2009 ms |
211368 KB |
Output is correct |
3 |
Correct |
475 ms |
208192 KB |
Output is correct |
4 |
Correct |
1420 ms |
212592 KB |
Output is correct |
5 |
Correct |
1175 ms |
209688 KB |
Output is correct |
6 |
Correct |
686 ms |
209720 KB |
Output is correct |
7 |
Correct |
649 ms |
210020 KB |
Output is correct |
8 |
Correct |
510 ms |
210440 KB |
Output is correct |
9 |
Correct |
2371 ms |
215596 KB |
Output is correct |
10 |
Correct |
554 ms |
212140 KB |
Output is correct |
11 |
Correct |
1967 ms |
211384 KB |
Output is correct |
12 |
Correct |
557 ms |
208200 KB |
Output is correct |
13 |
Correct |
286 ms |
208748 KB |
Output is correct |
14 |
Correct |
317 ms |
208832 KB |
Output is correct |
15 |
Correct |
306 ms |
208836 KB |
Output is correct |
16 |
Correct |
316 ms |
208848 KB |
Output is correct |
17 |
Correct |
320 ms |
209336 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
4948 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 |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2009 ms |
211368 KB |
Output is correct |
3 |
Correct |
475 ms |
208192 KB |
Output is correct |
4 |
Correct |
1420 ms |
212592 KB |
Output is correct |
5 |
Correct |
1175 ms |
209688 KB |
Output is correct |
6 |
Correct |
686 ms |
209720 KB |
Output is correct |
7 |
Correct |
649 ms |
210020 KB |
Output is correct |
8 |
Correct |
510 ms |
210440 KB |
Output is correct |
9 |
Correct |
2371 ms |
215596 KB |
Output is correct |
10 |
Correct |
554 ms |
212140 KB |
Output is correct |
11 |
Correct |
1967 ms |
211384 KB |
Output is correct |
12 |
Correct |
557 ms |
208200 KB |
Output is correct |
13 |
Correct |
286 ms |
208748 KB |
Output is correct |
14 |
Correct |
317 ms |
208832 KB |
Output is correct |
15 |
Correct |
306 ms |
208836 KB |
Output is correct |
16 |
Correct |
316 ms |
208848 KB |
Output is correct |
17 |
Correct |
320 ms |
209336 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
4948 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 |
4948 KB |
Output is correct |
24 |
Correct |
2115 ms |
211328 KB |
Output is correct |
25 |
Correct |
531 ms |
208136 KB |
Output is correct |
26 |
Correct |
1521 ms |
213796 KB |
Output is correct |
27 |
Correct |
1227 ms |
209740 KB |
Output is correct |
28 |
Correct |
789 ms |
209824 KB |
Output is correct |
29 |
Correct |
697 ms |
209848 KB |
Output is correct |
30 |
Correct |
494 ms |
210400 KB |
Output is correct |
31 |
Correct |
2350 ms |
214320 KB |
Output is correct |
32 |
Correct |
518 ms |
212068 KB |
Output is correct |
33 |
Correct |
1805 ms |
211356 KB |
Output is correct |
34 |
Correct |
502 ms |
208100 KB |
Output is correct |
35 |
Correct |
3 ms |
4948 KB |
Output is correct |
36 |
Correct |
3 ms |
4948 KB |
Output is correct |
37 |
Correct |
3 ms |
4948 KB |
Output is correct |
38 |
Correct |
3 ms |
4948 KB |
Output is correct |
39 |
Correct |
3 ms |
4948 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 |
4948 KB |
Output is correct |
43 |
Correct |
3 ms |
4948 KB |
Output is correct |
44 |
Correct |
3 ms |
4956 KB |
Output is correct |
45 |
Correct |
3 ms |
4948 KB |
Output is correct |
46 |
Correct |
3 ms |
4948 KB |
Output is correct |
47 |
Correct |
4 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 |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2234 ms |
212908 KB |
Output is correct |
3 |
Correct |
906 ms |
220664 KB |
Output is correct |
4 |
Correct |
1445 ms |
220208 KB |
Output is correct |
5 |
Correct |
1247 ms |
216900 KB |
Output is correct |
6 |
Correct |
747 ms |
216928 KB |
Output is correct |
7 |
Correct |
637 ms |
217208 KB |
Output is correct |
8 |
Correct |
468 ms |
217660 KB |
Output is correct |
9 |
Correct |
2278 ms |
219656 KB |
Output is correct |
10 |
Correct |
930 ms |
221260 KB |
Output is correct |
11 |
Correct |
1866 ms |
216548 KB |
Output is correct |
12 |
Correct |
786 ms |
217504 KB |
Output is correct |
13 |
Correct |
442 ms |
218260 KB |
Output is correct |
14 |
Correct |
465 ms |
218700 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5012 KB |
Output is correct |
17 |
Correct |
3 ms |
5028 KB |
Output is correct |
18 |
Correct |
3 ms |
5028 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2239 ms |
214072 KB |
Output is correct |
3 |
Correct |
883 ms |
219860 KB |
Output is correct |
4 |
Correct |
1540 ms |
220896 KB |
Output is correct |
5 |
Correct |
1318 ms |
218504 KB |
Output is correct |
6 |
Correct |
758 ms |
218532 KB |
Output is correct |
7 |
Correct |
682 ms |
218256 KB |
Output is correct |
8 |
Correct |
504 ms |
218592 KB |
Output is correct |
9 |
Correct |
2473 ms |
223180 KB |
Output is correct |
10 |
Correct |
1091 ms |
221688 KB |
Output is correct |
11 |
Correct |
2613 ms |
219208 KB |
Output is correct |
12 |
Correct |
918 ms |
217676 KB |
Output is correct |
13 |
Correct |
491 ms |
218488 KB |
Output is correct |
14 |
Correct |
570 ms |
218984 KB |
Output is correct |
15 |
Correct |
4 ms |
5036 KB |
Output is correct |
16 |
Correct |
5 ms |
5032 KB |
Output is correct |
17 |
Correct |
5 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5096 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5972 KB |
Output is correct |
5 |
Correct |
5 ms |
6064 KB |
Output is correct |
6 |
Correct |
4 ms |
6100 KB |
Output is correct |
7 |
Correct |
4 ms |
6100 KB |
Output is correct |
8 |
Correct |
4 ms |
6072 KB |
Output is correct |
9 |
Correct |
3 ms |
5032 KB |
Output is correct |
10 |
Correct |
3 ms |
5032 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
4 ms |
5040 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5036 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5036 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 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 |
3 ms |
4948 KB |
Output is correct |
25 |
Correct |
3 ms |
5028 KB |
Output is correct |
26 |
Correct |
3 ms |
5028 KB |
Output is correct |
27 |
Correct |
3 ms |
5032 KB |
Output is correct |
28 |
Correct |
3 ms |
5032 KB |
Output is correct |
29 |
Correct |
2 ms |
4948 KB |
Output is correct |
30 |
Correct |
2009 ms |
211368 KB |
Output is correct |
31 |
Correct |
475 ms |
208192 KB |
Output is correct |
32 |
Correct |
1420 ms |
212592 KB |
Output is correct |
33 |
Correct |
1175 ms |
209688 KB |
Output is correct |
34 |
Correct |
686 ms |
209720 KB |
Output is correct |
35 |
Correct |
649 ms |
210020 KB |
Output is correct |
36 |
Correct |
510 ms |
210440 KB |
Output is correct |
37 |
Correct |
2371 ms |
215596 KB |
Output is correct |
38 |
Correct |
554 ms |
212140 KB |
Output is correct |
39 |
Correct |
1967 ms |
211384 KB |
Output is correct |
40 |
Correct |
557 ms |
208200 KB |
Output is correct |
41 |
Correct |
286 ms |
208748 KB |
Output is correct |
42 |
Correct |
317 ms |
208832 KB |
Output is correct |
43 |
Correct |
306 ms |
208836 KB |
Output is correct |
44 |
Correct |
316 ms |
208848 KB |
Output is correct |
45 |
Correct |
320 ms |
209336 KB |
Output is correct |
46 |
Correct |
4 ms |
4948 KB |
Output is correct |
47 |
Correct |
3 ms |
4948 KB |
Output is correct |
48 |
Correct |
4 ms |
4948 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 |
4948 KB |
Output is correct |
52 |
Correct |
2115 ms |
211328 KB |
Output is correct |
53 |
Correct |
531 ms |
208136 KB |
Output is correct |
54 |
Correct |
1521 ms |
213796 KB |
Output is correct |
55 |
Correct |
1227 ms |
209740 KB |
Output is correct |
56 |
Correct |
789 ms |
209824 KB |
Output is correct |
57 |
Correct |
697 ms |
209848 KB |
Output is correct |
58 |
Correct |
494 ms |
210400 KB |
Output is correct |
59 |
Correct |
2350 ms |
214320 KB |
Output is correct |
60 |
Correct |
518 ms |
212068 KB |
Output is correct |
61 |
Correct |
1805 ms |
211356 KB |
Output is correct |
62 |
Correct |
502 ms |
208100 KB |
Output is correct |
63 |
Correct |
3 ms |
4948 KB |
Output is correct |
64 |
Correct |
3 ms |
4948 KB |
Output is correct |
65 |
Correct |
3 ms |
4948 KB |
Output is correct |
66 |
Correct |
3 ms |
4948 KB |
Output is correct |
67 |
Correct |
3 ms |
4948 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 |
4948 KB |
Output is correct |
71 |
Correct |
3 ms |
4948 KB |
Output is correct |
72 |
Correct |
3 ms |
4956 KB |
Output is correct |
73 |
Correct |
3 ms |
4948 KB |
Output is correct |
74 |
Correct |
3 ms |
4948 KB |
Output is correct |
75 |
Correct |
4 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 |
2 ms |
4948 KB |
Output is correct |
79 |
Correct |
2234 ms |
212908 KB |
Output is correct |
80 |
Correct |
906 ms |
220664 KB |
Output is correct |
81 |
Correct |
1445 ms |
220208 KB |
Output is correct |
82 |
Correct |
1247 ms |
216900 KB |
Output is correct |
83 |
Correct |
747 ms |
216928 KB |
Output is correct |
84 |
Correct |
637 ms |
217208 KB |
Output is correct |
85 |
Correct |
468 ms |
217660 KB |
Output is correct |
86 |
Correct |
2278 ms |
219656 KB |
Output is correct |
87 |
Correct |
930 ms |
221260 KB |
Output is correct |
88 |
Correct |
1866 ms |
216548 KB |
Output is correct |
89 |
Correct |
786 ms |
217504 KB |
Output is correct |
90 |
Correct |
442 ms |
218260 KB |
Output is correct |
91 |
Correct |
465 ms |
218700 KB |
Output is correct |
92 |
Correct |
3 ms |
4948 KB |
Output is correct |
93 |
Correct |
3 ms |
5012 KB |
Output is correct |
94 |
Correct |
3 ms |
5028 KB |
Output is correct |
95 |
Correct |
3 ms |
5028 KB |
Output is correct |
96 |
Correct |
3 ms |
4948 KB |
Output is correct |
97 |
Correct |
3 ms |
4948 KB |
Output is correct |
98 |
Correct |
2239 ms |
214072 KB |
Output is correct |
99 |
Correct |
883 ms |
219860 KB |
Output is correct |
100 |
Correct |
1540 ms |
220896 KB |
Output is correct |
101 |
Correct |
1318 ms |
218504 KB |
Output is correct |
102 |
Correct |
758 ms |
218532 KB |
Output is correct |
103 |
Correct |
682 ms |
218256 KB |
Output is correct |
104 |
Correct |
504 ms |
218592 KB |
Output is correct |
105 |
Correct |
2473 ms |
223180 KB |
Output is correct |
106 |
Correct |
1091 ms |
221688 KB |
Output is correct |
107 |
Correct |
2613 ms |
219208 KB |
Output is correct |
108 |
Correct |
918 ms |
217676 KB |
Output is correct |
109 |
Correct |
491 ms |
218488 KB |
Output is correct |
110 |
Correct |
570 ms |
218984 KB |
Output is correct |
111 |
Correct |
4 ms |
5036 KB |
Output is correct |
112 |
Correct |
5 ms |
5032 KB |
Output is correct |
113 |
Correct |
5 ms |
5076 KB |
Output is correct |
114 |
Correct |
3 ms |
5096 KB |
Output is correct |
115 |
Correct |
3 ms |
5076 KB |
Output is correct |
116 |
Correct |
2075 ms |
217512 KB |
Output is correct |
117 |
Correct |
813 ms |
220196 KB |
Output is correct |
118 |
Correct |
1511 ms |
223876 KB |
Output is correct |
119 |
Correct |
1439 ms |
219816 KB |
Output is correct |
120 |
Correct |
836 ms |
219428 KB |
Output is correct |
121 |
Correct |
780 ms |
220332 KB |
Output is correct |
122 |
Correct |
500 ms |
220664 KB |
Output is correct |
123 |
Correct |
2406 ms |
223000 KB |
Output is correct |
124 |
Correct |
940 ms |
222404 KB |
Output is correct |
125 |
Correct |
1919 ms |
218788 KB |
Output is correct |
126 |
Correct |
767 ms |
220156 KB |
Output is correct |
127 |
Correct |
663 ms |
220516 KB |
Output is correct |
128 |
Correct |
473 ms |
221260 KB |
Output is correct |
129 |
Correct |
473 ms |
221744 KB |
Output is correct |