#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int val[45][200005];
int up[200005], H[200005], n, mod, q;
vector <int> adj[200005];
void dfs(int x, int par){
up[x] = par;
for(auto i : adj[x])if(i != par)dfs(i, x);
}
void solve(){
cin >> n >> mod;
up[1] = -1;
for(int i = 1; i < n; i++){
int a, b; cin >> a >> b;
adj[a].push_back(b); adj[b].push_back(a);
}
dfs(1, -1);
for(int i = 0; i <= 42; i++)for(int j = 1; j <= n; j++)val[i][j] = 1;
for(int i = 1; i <= n; i++)cin >> H[i];
cin >> q;
while(q--){
int t; cin >> t;
if(t == 1){
int a, b, c; cin >> a >> b >> c;
int cur = a;
while(cur != -1 && b >= 0){
val[b][cur] *= c;
val[b][cur] %= mod;
b--;
cur = up[cur];
}
}
else{
int x; cin >> x;
int ans = H[x], cur = x;
for(int i = 0; i <= 40; i++){
ans *= val[i][cur]; ans %= mod;
ans *= val[i + 1][cur]; ans %= mod;
if(cur == 1){
for(int j = i + 2; j <= 40; j++)ans *= val[j][cur], ans %= mod;
break;
}
cur = up[cur];
}
cout << ans << '\n';
}
}
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message
sprinkler.cpp:63:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
63 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
74328 KB |
Output is correct |
2 |
Correct |
8 ms |
74332 KB |
Output is correct |
3 |
Correct |
9 ms |
74332 KB |
Output is correct |
4 |
Correct |
10 ms |
74588 KB |
Output is correct |
5 |
Correct |
10 ms |
74588 KB |
Output is correct |
6 |
Correct |
11 ms |
74644 KB |
Output is correct |
7 |
Correct |
9 ms |
74588 KB |
Output is correct |
8 |
Correct |
9 ms |
74544 KB |
Output is correct |
9 |
Correct |
9 ms |
74328 KB |
Output is correct |
10 |
Correct |
9 ms |
74328 KB |
Output is correct |
11 |
Correct |
9 ms |
74332 KB |
Output is correct |
12 |
Correct |
9 ms |
74332 KB |
Output is correct |
13 |
Correct |
9 ms |
74332 KB |
Output is correct |
14 |
Correct |
10 ms |
74332 KB |
Output is correct |
15 |
Correct |
9 ms |
74532 KB |
Output is correct |
16 |
Correct |
9 ms |
74332 KB |
Output is correct |
17 |
Correct |
9 ms |
74332 KB |
Output is correct |
18 |
Correct |
9 ms |
74332 KB |
Output is correct |
19 |
Correct |
9 ms |
74344 KB |
Output is correct |
20 |
Correct |
8 ms |
74416 KB |
Output is correct |
21 |
Correct |
9 ms |
74332 KB |
Output is correct |
22 |
Correct |
9 ms |
74528 KB |
Output is correct |
23 |
Correct |
9 ms |
74464 KB |
Output is correct |
24 |
Correct |
8 ms |
74448 KB |
Output is correct |
25 |
Correct |
8 ms |
74332 KB |
Output is correct |
26 |
Correct |
9 ms |
74520 KB |
Output is correct |
27 |
Correct |
9 ms |
74332 KB |
Output is correct |
28 |
Correct |
9 ms |
74332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
74332 KB |
Output is correct |
2 |
Correct |
581 ms |
95508 KB |
Output is correct |
3 |
Correct |
233 ms |
96348 KB |
Output is correct |
4 |
Correct |
431 ms |
97948 KB |
Output is correct |
5 |
Correct |
478 ms |
96012 KB |
Output is correct |
6 |
Correct |
266 ms |
95416 KB |
Output is correct |
7 |
Correct |
242 ms |
96252 KB |
Output is correct |
8 |
Correct |
255 ms |
96476 KB |
Output is correct |
9 |
Correct |
577 ms |
99376 KB |
Output is correct |
10 |
Correct |
312 ms |
99504 KB |
Output is correct |
11 |
Correct |
564 ms |
95540 KB |
Output is correct |
12 |
Correct |
261 ms |
96116 KB |
Output is correct |
13 |
Correct |
171 ms |
96144 KB |
Output is correct |
14 |
Correct |
177 ms |
96604 KB |
Output is correct |
15 |
Correct |
194 ms |
95724 KB |
Output is correct |
16 |
Correct |
175 ms |
96312 KB |
Output is correct |
17 |
Correct |
179 ms |
96848 KB |
Output is correct |
18 |
Correct |
8 ms |
74328 KB |
Output is correct |
19 |
Correct |
9 ms |
74332 KB |
Output is correct |
20 |
Correct |
9 ms |
74640 KB |
Output is correct |
21 |
Correct |
8 ms |
74344 KB |
Output is correct |
22 |
Correct |
9 ms |
74320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
74332 KB |
Output is correct |
2 |
Correct |
581 ms |
95508 KB |
Output is correct |
3 |
Correct |
233 ms |
96348 KB |
Output is correct |
4 |
Correct |
431 ms |
97948 KB |
Output is correct |
5 |
Correct |
478 ms |
96012 KB |
Output is correct |
6 |
Correct |
266 ms |
95416 KB |
Output is correct |
7 |
Correct |
242 ms |
96252 KB |
Output is correct |
8 |
Correct |
255 ms |
96476 KB |
Output is correct |
9 |
Correct |
577 ms |
99376 KB |
Output is correct |
10 |
Correct |
312 ms |
99504 KB |
Output is correct |
11 |
Correct |
564 ms |
95540 KB |
Output is correct |
12 |
Correct |
261 ms |
96116 KB |
Output is correct |
13 |
Correct |
171 ms |
96144 KB |
Output is correct |
14 |
Correct |
177 ms |
96604 KB |
Output is correct |
15 |
Correct |
194 ms |
95724 KB |
Output is correct |
16 |
Correct |
175 ms |
96312 KB |
Output is correct |
17 |
Correct |
179 ms |
96848 KB |
Output is correct |
18 |
Correct |
8 ms |
74328 KB |
Output is correct |
19 |
Correct |
9 ms |
74332 KB |
Output is correct |
20 |
Correct |
9 ms |
74640 KB |
Output is correct |
21 |
Correct |
8 ms |
74344 KB |
Output is correct |
22 |
Correct |
9 ms |
74320 KB |
Output is correct |
23 |
Correct |
9 ms |
74332 KB |
Output is correct |
24 |
Correct |
572 ms |
95836 KB |
Output is correct |
25 |
Correct |
257 ms |
96084 KB |
Output is correct |
26 |
Correct |
452 ms |
99564 KB |
Output is correct |
27 |
Correct |
440 ms |
95884 KB |
Output is correct |
28 |
Correct |
262 ms |
95824 KB |
Output is correct |
29 |
Correct |
237 ms |
95828 KB |
Output is correct |
30 |
Correct |
207 ms |
96552 KB |
Output is correct |
31 |
Correct |
595 ms |
98224 KB |
Output is correct |
32 |
Correct |
347 ms |
99668 KB |
Output is correct |
33 |
Correct |
599 ms |
95664 KB |
Output is correct |
34 |
Correct |
266 ms |
96340 KB |
Output is correct |
35 |
Correct |
9 ms |
74332 KB |
Output is correct |
36 |
Correct |
9 ms |
74328 KB |
Output is correct |
37 |
Correct |
9 ms |
74332 KB |
Output is correct |
38 |
Correct |
9 ms |
74392 KB |
Output is correct |
39 |
Correct |
9 ms |
74528 KB |
Output is correct |
40 |
Correct |
9 ms |
74328 KB |
Output is correct |
41 |
Correct |
9 ms |
74332 KB |
Output is correct |
42 |
Correct |
10 ms |
74484 KB |
Output is correct |
43 |
Correct |
8 ms |
74584 KB |
Output is correct |
44 |
Correct |
9 ms |
74412 KB |
Output is correct |
45 |
Correct |
9 ms |
74328 KB |
Output is correct |
46 |
Correct |
9 ms |
74448 KB |
Output is correct |
47 |
Correct |
9 ms |
74332 KB |
Output is correct |
48 |
Correct |
9 ms |
74332 KB |
Output is correct |
49 |
Correct |
9 ms |
74344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
74328 KB |
Output is correct |
2 |
Correct |
669 ms |
96696 KB |
Output is correct |
3 |
Correct |
506 ms |
95956 KB |
Output is correct |
4 |
Correct |
533 ms |
95744 KB |
Output is correct |
5 |
Correct |
542 ms |
92816 KB |
Output is correct |
6 |
Correct |
297 ms |
93008 KB |
Output is correct |
7 |
Correct |
277 ms |
93292 KB |
Output is correct |
8 |
Correct |
205 ms |
93372 KB |
Output is correct |
9 |
Correct |
645 ms |
95220 KB |
Output is correct |
10 |
Correct |
517 ms |
96860 KB |
Output is correct |
11 |
Correct |
532 ms |
92496 KB |
Output is correct |
12 |
Correct |
501 ms |
93272 KB |
Output is correct |
13 |
Correct |
291 ms |
94292 KB |
Output is correct |
14 |
Correct |
321 ms |
95332 KB |
Output is correct |
15 |
Correct |
9 ms |
74328 KB |
Output is correct |
16 |
Correct |
9 ms |
74352 KB |
Output is correct |
17 |
Correct |
8 ms |
74396 KB |
Output is correct |
18 |
Correct |
9 ms |
74328 KB |
Output is correct |
19 |
Correct |
9 ms |
74332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
74332 KB |
Output is correct |
2 |
Correct |
578 ms |
97608 KB |
Output is correct |
3 |
Correct |
506 ms |
95228 KB |
Output is correct |
4 |
Correct |
473 ms |
96388 KB |
Output is correct |
5 |
Correct |
489 ms |
94400 KB |
Output is correct |
6 |
Correct |
334 ms |
94504 KB |
Output is correct |
7 |
Correct |
301 ms |
94576 KB |
Output is correct |
8 |
Correct |
234 ms |
94368 KB |
Output is correct |
9 |
Correct |
703 ms |
98732 KB |
Output is correct |
10 |
Correct |
541 ms |
97444 KB |
Output is correct |
11 |
Correct |
615 ms |
95128 KB |
Output is correct |
12 |
Correct |
487 ms |
93524 KB |
Output is correct |
13 |
Correct |
303 ms |
94500 KB |
Output is correct |
14 |
Correct |
319 ms |
95352 KB |
Output is correct |
15 |
Correct |
9 ms |
74328 KB |
Output is correct |
16 |
Correct |
9 ms |
74332 KB |
Output is correct |
17 |
Correct |
9 ms |
74332 KB |
Output is correct |
18 |
Correct |
9 ms |
74328 KB |
Output is correct |
19 |
Correct |
9 ms |
74332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
74328 KB |
Output is correct |
2 |
Correct |
8 ms |
74332 KB |
Output is correct |
3 |
Correct |
9 ms |
74332 KB |
Output is correct |
4 |
Correct |
10 ms |
74588 KB |
Output is correct |
5 |
Correct |
10 ms |
74588 KB |
Output is correct |
6 |
Correct |
11 ms |
74644 KB |
Output is correct |
7 |
Correct |
9 ms |
74588 KB |
Output is correct |
8 |
Correct |
9 ms |
74544 KB |
Output is correct |
9 |
Correct |
9 ms |
74328 KB |
Output is correct |
10 |
Correct |
9 ms |
74328 KB |
Output is correct |
11 |
Correct |
9 ms |
74332 KB |
Output is correct |
12 |
Correct |
9 ms |
74332 KB |
Output is correct |
13 |
Correct |
9 ms |
74332 KB |
Output is correct |
14 |
Correct |
10 ms |
74332 KB |
Output is correct |
15 |
Correct |
9 ms |
74532 KB |
Output is correct |
16 |
Correct |
9 ms |
74332 KB |
Output is correct |
17 |
Correct |
9 ms |
74332 KB |
Output is correct |
18 |
Correct |
9 ms |
74332 KB |
Output is correct |
19 |
Correct |
9 ms |
74344 KB |
Output is correct |
20 |
Correct |
8 ms |
74416 KB |
Output is correct |
21 |
Correct |
9 ms |
74332 KB |
Output is correct |
22 |
Correct |
9 ms |
74528 KB |
Output is correct |
23 |
Correct |
9 ms |
74464 KB |
Output is correct |
24 |
Correct |
8 ms |
74448 KB |
Output is correct |
25 |
Correct |
8 ms |
74332 KB |
Output is correct |
26 |
Correct |
9 ms |
74520 KB |
Output is correct |
27 |
Correct |
9 ms |
74332 KB |
Output is correct |
28 |
Correct |
9 ms |
74332 KB |
Output is correct |
29 |
Correct |
9 ms |
74332 KB |
Output is correct |
30 |
Correct |
581 ms |
95508 KB |
Output is correct |
31 |
Correct |
233 ms |
96348 KB |
Output is correct |
32 |
Correct |
431 ms |
97948 KB |
Output is correct |
33 |
Correct |
478 ms |
96012 KB |
Output is correct |
34 |
Correct |
266 ms |
95416 KB |
Output is correct |
35 |
Correct |
242 ms |
96252 KB |
Output is correct |
36 |
Correct |
255 ms |
96476 KB |
Output is correct |
37 |
Correct |
577 ms |
99376 KB |
Output is correct |
38 |
Correct |
312 ms |
99504 KB |
Output is correct |
39 |
Correct |
564 ms |
95540 KB |
Output is correct |
40 |
Correct |
261 ms |
96116 KB |
Output is correct |
41 |
Correct |
171 ms |
96144 KB |
Output is correct |
42 |
Correct |
177 ms |
96604 KB |
Output is correct |
43 |
Correct |
194 ms |
95724 KB |
Output is correct |
44 |
Correct |
175 ms |
96312 KB |
Output is correct |
45 |
Correct |
179 ms |
96848 KB |
Output is correct |
46 |
Correct |
8 ms |
74328 KB |
Output is correct |
47 |
Correct |
9 ms |
74332 KB |
Output is correct |
48 |
Correct |
9 ms |
74640 KB |
Output is correct |
49 |
Correct |
8 ms |
74344 KB |
Output is correct |
50 |
Correct |
9 ms |
74320 KB |
Output is correct |
51 |
Correct |
9 ms |
74332 KB |
Output is correct |
52 |
Correct |
572 ms |
95836 KB |
Output is correct |
53 |
Correct |
257 ms |
96084 KB |
Output is correct |
54 |
Correct |
452 ms |
99564 KB |
Output is correct |
55 |
Correct |
440 ms |
95884 KB |
Output is correct |
56 |
Correct |
262 ms |
95824 KB |
Output is correct |
57 |
Correct |
237 ms |
95828 KB |
Output is correct |
58 |
Correct |
207 ms |
96552 KB |
Output is correct |
59 |
Correct |
595 ms |
98224 KB |
Output is correct |
60 |
Correct |
347 ms |
99668 KB |
Output is correct |
61 |
Correct |
599 ms |
95664 KB |
Output is correct |
62 |
Correct |
266 ms |
96340 KB |
Output is correct |
63 |
Correct |
9 ms |
74332 KB |
Output is correct |
64 |
Correct |
9 ms |
74328 KB |
Output is correct |
65 |
Correct |
9 ms |
74332 KB |
Output is correct |
66 |
Correct |
9 ms |
74392 KB |
Output is correct |
67 |
Correct |
9 ms |
74528 KB |
Output is correct |
68 |
Correct |
9 ms |
74328 KB |
Output is correct |
69 |
Correct |
9 ms |
74332 KB |
Output is correct |
70 |
Correct |
10 ms |
74484 KB |
Output is correct |
71 |
Correct |
8 ms |
74584 KB |
Output is correct |
72 |
Correct |
9 ms |
74412 KB |
Output is correct |
73 |
Correct |
9 ms |
74328 KB |
Output is correct |
74 |
Correct |
9 ms |
74448 KB |
Output is correct |
75 |
Correct |
9 ms |
74332 KB |
Output is correct |
76 |
Correct |
9 ms |
74332 KB |
Output is correct |
77 |
Correct |
9 ms |
74344 KB |
Output is correct |
78 |
Correct |
10 ms |
74328 KB |
Output is correct |
79 |
Correct |
669 ms |
96696 KB |
Output is correct |
80 |
Correct |
506 ms |
95956 KB |
Output is correct |
81 |
Correct |
533 ms |
95744 KB |
Output is correct |
82 |
Correct |
542 ms |
92816 KB |
Output is correct |
83 |
Correct |
297 ms |
93008 KB |
Output is correct |
84 |
Correct |
277 ms |
93292 KB |
Output is correct |
85 |
Correct |
205 ms |
93372 KB |
Output is correct |
86 |
Correct |
645 ms |
95220 KB |
Output is correct |
87 |
Correct |
517 ms |
96860 KB |
Output is correct |
88 |
Correct |
532 ms |
92496 KB |
Output is correct |
89 |
Correct |
501 ms |
93272 KB |
Output is correct |
90 |
Correct |
291 ms |
94292 KB |
Output is correct |
91 |
Correct |
321 ms |
95332 KB |
Output is correct |
92 |
Correct |
9 ms |
74328 KB |
Output is correct |
93 |
Correct |
9 ms |
74352 KB |
Output is correct |
94 |
Correct |
8 ms |
74396 KB |
Output is correct |
95 |
Correct |
9 ms |
74328 KB |
Output is correct |
96 |
Correct |
9 ms |
74332 KB |
Output is correct |
97 |
Correct |
8 ms |
74332 KB |
Output is correct |
98 |
Correct |
578 ms |
97608 KB |
Output is correct |
99 |
Correct |
506 ms |
95228 KB |
Output is correct |
100 |
Correct |
473 ms |
96388 KB |
Output is correct |
101 |
Correct |
489 ms |
94400 KB |
Output is correct |
102 |
Correct |
334 ms |
94504 KB |
Output is correct |
103 |
Correct |
301 ms |
94576 KB |
Output is correct |
104 |
Correct |
234 ms |
94368 KB |
Output is correct |
105 |
Correct |
703 ms |
98732 KB |
Output is correct |
106 |
Correct |
541 ms |
97444 KB |
Output is correct |
107 |
Correct |
615 ms |
95128 KB |
Output is correct |
108 |
Correct |
487 ms |
93524 KB |
Output is correct |
109 |
Correct |
303 ms |
94500 KB |
Output is correct |
110 |
Correct |
319 ms |
95352 KB |
Output is correct |
111 |
Correct |
9 ms |
74328 KB |
Output is correct |
112 |
Correct |
9 ms |
74332 KB |
Output is correct |
113 |
Correct |
9 ms |
74332 KB |
Output is correct |
114 |
Correct |
9 ms |
74328 KB |
Output is correct |
115 |
Correct |
9 ms |
74332 KB |
Output is correct |
116 |
Correct |
670 ms |
93628 KB |
Output is correct |
117 |
Correct |
509 ms |
96068 KB |
Output is correct |
118 |
Correct |
520 ms |
99460 KB |
Output is correct |
119 |
Correct |
494 ms |
95828 KB |
Output is correct |
120 |
Correct |
343 ms |
95740 KB |
Output is correct |
121 |
Correct |
342 ms |
96440 KB |
Output is correct |
122 |
Correct |
266 ms |
96804 KB |
Output is correct |
123 |
Correct |
571 ms |
98528 KB |
Output is correct |
124 |
Correct |
568 ms |
97860 KB |
Output is correct |
125 |
Correct |
615 ms |
94832 KB |
Output is correct |
126 |
Correct |
498 ms |
96088 KB |
Output is correct |
127 |
Correct |
498 ms |
96596 KB |
Output is correct |
128 |
Correct |
330 ms |
97268 KB |
Output is correct |
129 |
Correct |
339 ms |
98144 KB |
Output is correct |