#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=2e5+5;
int N,L,Q;
int A[MX], B[MX], H[MX];
vector<int> adj[MX];
int par[MX];
ll w[MX][50]; // w and w inverse
void dfs(int v, int p) {
par[v]=p;
for(auto u:adj[v]) {
if(u==p) continue;
dfs(u,v);
}
}
void upd(int v, int d, int x) {
while(d>=0 && v!=0) {
w[v][d]*=x;
w[v][d]%=L;
v=par[v];
d--;
}
}
int que(int v) {
int d=0,p=-1;
ll res=1;
while(d<=40 && v!=0) {
for(int i=d;i<=40;i++) {
if(par[v]!=0 && i-1>=d+1) continue;
res*=w[v][i];
res%=L;
}
d++;
p=v;
v=par[v];
}
return res;
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
for(int i=0;i<MX;i++)
for(int d=0;d<=41;d++)
w[i][d]=1;
cin>>N>>L;
for(int i=0;i<N-1;i++) {
cin>>A[i]>>B[i];
adj[A[i]].push_back(B[i]);
adj[B[i]].push_back(A[i]);
}
for(int i=1;i<=N;i++) cin>>w[i][0];
dfs(1,0);
cin>>Q;
for(int i=1;i<=Q;i++) {
int t;
cin>>t;
if(t==1) {
int x,d,w;
cin>>x>>d>>w;
upd(x,d,w);
} else {
int x;
cin>>x;
cout<<que(x)<<'\n';
}
}
}
Compilation message
sprinkler.cpp: In function 'int que(int)':
sprinkler.cpp:31:10: warning: variable 'p' set but not used [-Wunused-but-set-variable]
31 | int d=0,p=-1;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
84568 KB |
Output is correct |
2 |
Correct |
13 ms |
84572 KB |
Output is correct |
3 |
Correct |
12 ms |
84572 KB |
Output is correct |
4 |
Correct |
14 ms |
84688 KB |
Output is correct |
5 |
Correct |
14 ms |
84828 KB |
Output is correct |
6 |
Correct |
14 ms |
84828 KB |
Output is correct |
7 |
Correct |
14 ms |
84828 KB |
Output is correct |
8 |
Correct |
14 ms |
84736 KB |
Output is correct |
9 |
Correct |
13 ms |
84572 KB |
Output is correct |
10 |
Correct |
16 ms |
84572 KB |
Output is correct |
11 |
Correct |
14 ms |
84572 KB |
Output is correct |
12 |
Correct |
14 ms |
84768 KB |
Output is correct |
13 |
Correct |
14 ms |
84572 KB |
Output is correct |
14 |
Correct |
13 ms |
84572 KB |
Output is correct |
15 |
Correct |
13 ms |
84572 KB |
Output is correct |
16 |
Correct |
14 ms |
84572 KB |
Output is correct |
17 |
Correct |
13 ms |
84572 KB |
Output is correct |
18 |
Correct |
13 ms |
84572 KB |
Output is correct |
19 |
Correct |
13 ms |
84724 KB |
Output is correct |
20 |
Correct |
13 ms |
84824 KB |
Output is correct |
21 |
Correct |
14 ms |
84572 KB |
Output is correct |
22 |
Correct |
14 ms |
84572 KB |
Output is correct |
23 |
Correct |
14 ms |
84568 KB |
Output is correct |
24 |
Correct |
13 ms |
84572 KB |
Output is correct |
25 |
Correct |
13 ms |
84572 KB |
Output is correct |
26 |
Correct |
13 ms |
84572 KB |
Output is correct |
27 |
Correct |
15 ms |
84572 KB |
Output is correct |
28 |
Correct |
13 ms |
84572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
84568 KB |
Output is correct |
2 |
Correct |
1004 ms |
96156 KB |
Output is correct |
3 |
Correct |
329 ms |
93008 KB |
Output is correct |
4 |
Correct |
1214 ms |
102684 KB |
Output is correct |
5 |
Correct |
653 ms |
104532 KB |
Output is correct |
6 |
Correct |
418 ms |
104264 KB |
Output is correct |
7 |
Correct |
419 ms |
104972 KB |
Output is correct |
8 |
Correct |
244 ms |
105276 KB |
Output is correct |
9 |
Correct |
1888 ms |
109012 KB |
Output is correct |
10 |
Correct |
439 ms |
109084 KB |
Output is correct |
11 |
Correct |
997 ms |
104412 KB |
Output is correct |
12 |
Correct |
395 ms |
104928 KB |
Output is correct |
13 |
Correct |
178 ms |
105164 KB |
Output is correct |
14 |
Correct |
188 ms |
105920 KB |
Output is correct |
15 |
Correct |
203 ms |
104896 KB |
Output is correct |
16 |
Correct |
193 ms |
105372 KB |
Output is correct |
17 |
Correct |
214 ms |
105960 KB |
Output is correct |
18 |
Correct |
13 ms |
84572 KB |
Output is correct |
19 |
Correct |
13 ms |
84572 KB |
Output is correct |
20 |
Correct |
13 ms |
84704 KB |
Output is correct |
21 |
Correct |
13 ms |
84568 KB |
Output is correct |
22 |
Correct |
14 ms |
84572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
84568 KB |
Output is correct |
2 |
Correct |
1004 ms |
96156 KB |
Output is correct |
3 |
Correct |
329 ms |
93008 KB |
Output is correct |
4 |
Correct |
1214 ms |
102684 KB |
Output is correct |
5 |
Correct |
653 ms |
104532 KB |
Output is correct |
6 |
Correct |
418 ms |
104264 KB |
Output is correct |
7 |
Correct |
419 ms |
104972 KB |
Output is correct |
8 |
Correct |
244 ms |
105276 KB |
Output is correct |
9 |
Correct |
1888 ms |
109012 KB |
Output is correct |
10 |
Correct |
439 ms |
109084 KB |
Output is correct |
11 |
Correct |
997 ms |
104412 KB |
Output is correct |
12 |
Correct |
395 ms |
104928 KB |
Output is correct |
13 |
Correct |
178 ms |
105164 KB |
Output is correct |
14 |
Correct |
188 ms |
105920 KB |
Output is correct |
15 |
Correct |
203 ms |
104896 KB |
Output is correct |
16 |
Correct |
193 ms |
105372 KB |
Output is correct |
17 |
Correct |
214 ms |
105960 KB |
Output is correct |
18 |
Correct |
13 ms |
84572 KB |
Output is correct |
19 |
Correct |
13 ms |
84572 KB |
Output is correct |
20 |
Correct |
13 ms |
84704 KB |
Output is correct |
21 |
Correct |
13 ms |
84568 KB |
Output is correct |
22 |
Correct |
14 ms |
84572 KB |
Output is correct |
23 |
Correct |
13 ms |
84572 KB |
Output is correct |
24 |
Correct |
996 ms |
104428 KB |
Output is correct |
25 |
Correct |
333 ms |
104932 KB |
Output is correct |
26 |
Correct |
1221 ms |
109320 KB |
Output is correct |
27 |
Correct |
642 ms |
104536 KB |
Output is correct |
28 |
Correct |
432 ms |
105052 KB |
Output is correct |
29 |
Correct |
373 ms |
104512 KB |
Output is correct |
30 |
Correct |
241 ms |
105468 KB |
Output is correct |
31 |
Correct |
1931 ms |
107616 KB |
Output is correct |
32 |
Correct |
448 ms |
109344 KB |
Output is correct |
33 |
Correct |
1053 ms |
104584 KB |
Output is correct |
34 |
Correct |
352 ms |
105296 KB |
Output is correct |
35 |
Correct |
13 ms |
84572 KB |
Output is correct |
36 |
Correct |
14 ms |
84688 KB |
Output is correct |
37 |
Correct |
14 ms |
84572 KB |
Output is correct |
38 |
Correct |
15 ms |
84568 KB |
Output is correct |
39 |
Correct |
13 ms |
84568 KB |
Output is correct |
40 |
Correct |
13 ms |
84572 KB |
Output is correct |
41 |
Correct |
13 ms |
84572 KB |
Output is correct |
42 |
Correct |
13 ms |
84568 KB |
Output is correct |
43 |
Correct |
13 ms |
84648 KB |
Output is correct |
44 |
Correct |
14 ms |
84724 KB |
Output is correct |
45 |
Correct |
13 ms |
84780 KB |
Output is correct |
46 |
Correct |
14 ms |
84780 KB |
Output is correct |
47 |
Correct |
13 ms |
84572 KB |
Output is correct |
48 |
Correct |
14 ms |
84568 KB |
Output is correct |
49 |
Correct |
13 ms |
84572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
84772 KB |
Output is correct |
2 |
Correct |
1943 ms |
101324 KB |
Output is correct |
3 |
Correct |
699 ms |
105608 KB |
Output is correct |
4 |
Correct |
1271 ms |
105524 KB |
Output is correct |
5 |
Correct |
676 ms |
101588 KB |
Output is correct |
6 |
Correct |
421 ms |
101716 KB |
Output is correct |
7 |
Correct |
409 ms |
102160 KB |
Output is correct |
8 |
Correct |
233 ms |
102456 KB |
Output is correct |
9 |
Correct |
1963 ms |
104848 KB |
Output is correct |
10 |
Correct |
653 ms |
106140 KB |
Output is correct |
11 |
Correct |
981 ms |
101312 KB |
Output is correct |
12 |
Correct |
451 ms |
102140 KB |
Output is correct |
13 |
Correct |
301 ms |
103032 KB |
Output is correct |
14 |
Correct |
307 ms |
103736 KB |
Output is correct |
15 |
Correct |
13 ms |
84572 KB |
Output is correct |
16 |
Correct |
13 ms |
84572 KB |
Output is correct |
17 |
Correct |
13 ms |
84620 KB |
Output is correct |
18 |
Correct |
14 ms |
84776 KB |
Output is correct |
19 |
Correct |
13 ms |
84636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
84572 KB |
Output is correct |
2 |
Correct |
1982 ms |
102436 KB |
Output is correct |
3 |
Correct |
662 ms |
104944 KB |
Output is correct |
4 |
Correct |
1265 ms |
105732 KB |
Output is correct |
5 |
Correct |
726 ms |
103216 KB |
Output is correct |
6 |
Correct |
478 ms |
103264 KB |
Output is correct |
7 |
Correct |
446 ms |
103100 KB |
Output is correct |
8 |
Correct |
278 ms |
103364 KB |
Output is correct |
9 |
Correct |
1944 ms |
108388 KB |
Output is correct |
10 |
Correct |
748 ms |
106744 KB |
Output is correct |
11 |
Correct |
1008 ms |
103932 KB |
Output is correct |
12 |
Correct |
483 ms |
102500 KB |
Output is correct |
13 |
Correct |
324 ms |
103256 KB |
Output is correct |
14 |
Correct |
340 ms |
103752 KB |
Output is correct |
15 |
Correct |
13 ms |
84572 KB |
Output is correct |
16 |
Correct |
13 ms |
84572 KB |
Output is correct |
17 |
Correct |
13 ms |
84572 KB |
Output is correct |
18 |
Correct |
13 ms |
84692 KB |
Output is correct |
19 |
Correct |
13 ms |
84572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
84568 KB |
Output is correct |
2 |
Correct |
13 ms |
84572 KB |
Output is correct |
3 |
Correct |
12 ms |
84572 KB |
Output is correct |
4 |
Correct |
14 ms |
84688 KB |
Output is correct |
5 |
Correct |
14 ms |
84828 KB |
Output is correct |
6 |
Correct |
14 ms |
84828 KB |
Output is correct |
7 |
Correct |
14 ms |
84828 KB |
Output is correct |
8 |
Correct |
14 ms |
84736 KB |
Output is correct |
9 |
Correct |
13 ms |
84572 KB |
Output is correct |
10 |
Correct |
16 ms |
84572 KB |
Output is correct |
11 |
Correct |
14 ms |
84572 KB |
Output is correct |
12 |
Correct |
14 ms |
84768 KB |
Output is correct |
13 |
Correct |
14 ms |
84572 KB |
Output is correct |
14 |
Correct |
13 ms |
84572 KB |
Output is correct |
15 |
Correct |
13 ms |
84572 KB |
Output is correct |
16 |
Correct |
14 ms |
84572 KB |
Output is correct |
17 |
Correct |
13 ms |
84572 KB |
Output is correct |
18 |
Correct |
13 ms |
84572 KB |
Output is correct |
19 |
Correct |
13 ms |
84724 KB |
Output is correct |
20 |
Correct |
13 ms |
84824 KB |
Output is correct |
21 |
Correct |
14 ms |
84572 KB |
Output is correct |
22 |
Correct |
14 ms |
84572 KB |
Output is correct |
23 |
Correct |
14 ms |
84568 KB |
Output is correct |
24 |
Correct |
13 ms |
84572 KB |
Output is correct |
25 |
Correct |
13 ms |
84572 KB |
Output is correct |
26 |
Correct |
13 ms |
84572 KB |
Output is correct |
27 |
Correct |
15 ms |
84572 KB |
Output is correct |
28 |
Correct |
13 ms |
84572 KB |
Output is correct |
29 |
Correct |
13 ms |
84568 KB |
Output is correct |
30 |
Correct |
1004 ms |
96156 KB |
Output is correct |
31 |
Correct |
329 ms |
93008 KB |
Output is correct |
32 |
Correct |
1214 ms |
102684 KB |
Output is correct |
33 |
Correct |
653 ms |
104532 KB |
Output is correct |
34 |
Correct |
418 ms |
104264 KB |
Output is correct |
35 |
Correct |
419 ms |
104972 KB |
Output is correct |
36 |
Correct |
244 ms |
105276 KB |
Output is correct |
37 |
Correct |
1888 ms |
109012 KB |
Output is correct |
38 |
Correct |
439 ms |
109084 KB |
Output is correct |
39 |
Correct |
997 ms |
104412 KB |
Output is correct |
40 |
Correct |
395 ms |
104928 KB |
Output is correct |
41 |
Correct |
178 ms |
105164 KB |
Output is correct |
42 |
Correct |
188 ms |
105920 KB |
Output is correct |
43 |
Correct |
203 ms |
104896 KB |
Output is correct |
44 |
Correct |
193 ms |
105372 KB |
Output is correct |
45 |
Correct |
214 ms |
105960 KB |
Output is correct |
46 |
Correct |
13 ms |
84572 KB |
Output is correct |
47 |
Correct |
13 ms |
84572 KB |
Output is correct |
48 |
Correct |
13 ms |
84704 KB |
Output is correct |
49 |
Correct |
13 ms |
84568 KB |
Output is correct |
50 |
Correct |
14 ms |
84572 KB |
Output is correct |
51 |
Correct |
13 ms |
84572 KB |
Output is correct |
52 |
Correct |
996 ms |
104428 KB |
Output is correct |
53 |
Correct |
333 ms |
104932 KB |
Output is correct |
54 |
Correct |
1221 ms |
109320 KB |
Output is correct |
55 |
Correct |
642 ms |
104536 KB |
Output is correct |
56 |
Correct |
432 ms |
105052 KB |
Output is correct |
57 |
Correct |
373 ms |
104512 KB |
Output is correct |
58 |
Correct |
241 ms |
105468 KB |
Output is correct |
59 |
Correct |
1931 ms |
107616 KB |
Output is correct |
60 |
Correct |
448 ms |
109344 KB |
Output is correct |
61 |
Correct |
1053 ms |
104584 KB |
Output is correct |
62 |
Correct |
352 ms |
105296 KB |
Output is correct |
63 |
Correct |
13 ms |
84572 KB |
Output is correct |
64 |
Correct |
14 ms |
84688 KB |
Output is correct |
65 |
Correct |
14 ms |
84572 KB |
Output is correct |
66 |
Correct |
15 ms |
84568 KB |
Output is correct |
67 |
Correct |
13 ms |
84568 KB |
Output is correct |
68 |
Correct |
13 ms |
84572 KB |
Output is correct |
69 |
Correct |
13 ms |
84572 KB |
Output is correct |
70 |
Correct |
13 ms |
84568 KB |
Output is correct |
71 |
Correct |
13 ms |
84648 KB |
Output is correct |
72 |
Correct |
14 ms |
84724 KB |
Output is correct |
73 |
Correct |
13 ms |
84780 KB |
Output is correct |
74 |
Correct |
14 ms |
84780 KB |
Output is correct |
75 |
Correct |
13 ms |
84572 KB |
Output is correct |
76 |
Correct |
14 ms |
84568 KB |
Output is correct |
77 |
Correct |
13 ms |
84572 KB |
Output is correct |
78 |
Correct |
14 ms |
84772 KB |
Output is correct |
79 |
Correct |
1943 ms |
101324 KB |
Output is correct |
80 |
Correct |
699 ms |
105608 KB |
Output is correct |
81 |
Correct |
1271 ms |
105524 KB |
Output is correct |
82 |
Correct |
676 ms |
101588 KB |
Output is correct |
83 |
Correct |
421 ms |
101716 KB |
Output is correct |
84 |
Correct |
409 ms |
102160 KB |
Output is correct |
85 |
Correct |
233 ms |
102456 KB |
Output is correct |
86 |
Correct |
1963 ms |
104848 KB |
Output is correct |
87 |
Correct |
653 ms |
106140 KB |
Output is correct |
88 |
Correct |
981 ms |
101312 KB |
Output is correct |
89 |
Correct |
451 ms |
102140 KB |
Output is correct |
90 |
Correct |
301 ms |
103032 KB |
Output is correct |
91 |
Correct |
307 ms |
103736 KB |
Output is correct |
92 |
Correct |
13 ms |
84572 KB |
Output is correct |
93 |
Correct |
13 ms |
84572 KB |
Output is correct |
94 |
Correct |
13 ms |
84620 KB |
Output is correct |
95 |
Correct |
14 ms |
84776 KB |
Output is correct |
96 |
Correct |
13 ms |
84636 KB |
Output is correct |
97 |
Correct |
13 ms |
84572 KB |
Output is correct |
98 |
Correct |
1982 ms |
102436 KB |
Output is correct |
99 |
Correct |
662 ms |
104944 KB |
Output is correct |
100 |
Correct |
1265 ms |
105732 KB |
Output is correct |
101 |
Correct |
726 ms |
103216 KB |
Output is correct |
102 |
Correct |
478 ms |
103264 KB |
Output is correct |
103 |
Correct |
446 ms |
103100 KB |
Output is correct |
104 |
Correct |
278 ms |
103364 KB |
Output is correct |
105 |
Correct |
1944 ms |
108388 KB |
Output is correct |
106 |
Correct |
748 ms |
106744 KB |
Output is correct |
107 |
Correct |
1008 ms |
103932 KB |
Output is correct |
108 |
Correct |
483 ms |
102500 KB |
Output is correct |
109 |
Correct |
324 ms |
103256 KB |
Output is correct |
110 |
Correct |
340 ms |
103752 KB |
Output is correct |
111 |
Correct |
13 ms |
84572 KB |
Output is correct |
112 |
Correct |
13 ms |
84572 KB |
Output is correct |
113 |
Correct |
13 ms |
84572 KB |
Output is correct |
114 |
Correct |
13 ms |
84692 KB |
Output is correct |
115 |
Correct |
13 ms |
84572 KB |
Output is correct |
116 |
Correct |
1042 ms |
102444 KB |
Output is correct |
117 |
Correct |
513 ms |
104832 KB |
Output is correct |
118 |
Correct |
1274 ms |
108836 KB |
Output is correct |
119 |
Correct |
770 ms |
104528 KB |
Output is correct |
120 |
Correct |
470 ms |
104188 KB |
Output is correct |
121 |
Correct |
471 ms |
105044 KB |
Output is correct |
122 |
Correct |
285 ms |
105400 KB |
Output is correct |
123 |
Correct |
2000 ms |
107896 KB |
Output is correct |
124 |
Correct |
695 ms |
107612 KB |
Output is correct |
125 |
Correct |
1051 ms |
103588 KB |
Output is correct |
126 |
Correct |
508 ms |
104900 KB |
Output is correct |
127 |
Correct |
465 ms |
105564 KB |
Output is correct |
128 |
Correct |
369 ms |
106064 KB |
Output is correct |
129 |
Correct |
351 ms |
106536 KB |
Output is correct |