//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ...b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 200005
#define maxd 42
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
int mod;
ll mul[maxn][maxd];
vector<int> adj[maxn];
int par[maxn];
void dfs(int n, int pa) {
par[n] = pa;
for (int v:adj[n]) {
if (v != pa) {
dfs(v, n);
}
}
for (int i = 0;i < maxd;i++) mul[n][i] = 1;
//debug(n, leaf[n]);
}
ll h[maxn];
void modify(int n, int d, int w) {
while (d >= 0 && n) {
mul[n][d] = mul[n][d] * w % mod;
if (d > 0) mul[n][d-1] = mul[n][d-1] * w % mod;
if (n == 1) {
for (int x = d - 2;x >= 0;x--) mul[n][x] = mul[n][x] * w % mod;
break;
}
n = par[n];
d--;
}
}
ll query(int n) {
ll ret = h[n];
int cur = 0;
while (cur < maxd && n) {
ret = ret * mul[n][cur] % mod;
n = par[n];
cur++;
}
return ret;
}
int main() {
io
int n;
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);
}
for (int i = 1;i <= n;i++) cin >> h[i];
dfs(1, 0);
int q;
cin >> q;
while (q--) {
int type;
cin >> type;
if (type == 1) {
int x, d, w;
cin >> x >> d >> w;
modify(x, d, w);
} else {
int x;
cin >> x;
cout << query(x) << "\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
5 ms |
5460 KB |
Output is correct |
5 |
Correct |
4 ms |
5332 KB |
Output is correct |
6 |
Correct |
4 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5356 KB |
Output is correct |
8 |
Correct |
3 ms |
5460 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5004 KB |
Output is correct |
12 |
Correct |
3 ms |
5040 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 |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5044 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5036 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
5052 KB |
Output is correct |
22 |
Correct |
3 ms |
5036 KB |
Output is correct |
23 |
Correct |
3 ms |
5040 KB |
Output is correct |
24 |
Correct |
4 ms |
5032 KB |
Output is correct |
25 |
Correct |
4 ms |
4948 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
571 ms |
91148 KB |
Output is correct |
3 |
Correct |
426 ms |
91788 KB |
Output is correct |
4 |
Correct |
519 ms |
97420 KB |
Output is correct |
5 |
Correct |
532 ms |
91160 KB |
Output is correct |
6 |
Correct |
410 ms |
89848 KB |
Output is correct |
7 |
Correct |
373 ms |
91732 KB |
Output is correct |
8 |
Correct |
306 ms |
90856 KB |
Output is correct |
9 |
Correct |
588 ms |
100156 KB |
Output is correct |
10 |
Correct |
440 ms |
100056 KB |
Output is correct |
11 |
Correct |
585 ms |
90412 KB |
Output is correct |
12 |
Correct |
444 ms |
90192 KB |
Output is correct |
13 |
Correct |
290 ms |
92032 KB |
Output is correct |
14 |
Correct |
293 ms |
92416 KB |
Output is correct |
15 |
Correct |
291 ms |
88732 KB |
Output is correct |
16 |
Correct |
318 ms |
92256 KB |
Output is correct |
17 |
Correct |
306 ms |
89452 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
19 |
Correct |
4 ms |
5036 KB |
Output is correct |
20 |
Correct |
4 ms |
5044 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 |
571 ms |
91148 KB |
Output is correct |
3 |
Correct |
426 ms |
91788 KB |
Output is correct |
4 |
Correct |
519 ms |
97420 KB |
Output is correct |
5 |
Correct |
532 ms |
91160 KB |
Output is correct |
6 |
Correct |
410 ms |
89848 KB |
Output is correct |
7 |
Correct |
373 ms |
91732 KB |
Output is correct |
8 |
Correct |
306 ms |
90856 KB |
Output is correct |
9 |
Correct |
588 ms |
100156 KB |
Output is correct |
10 |
Correct |
440 ms |
100056 KB |
Output is correct |
11 |
Correct |
585 ms |
90412 KB |
Output is correct |
12 |
Correct |
444 ms |
90192 KB |
Output is correct |
13 |
Correct |
290 ms |
92032 KB |
Output is correct |
14 |
Correct |
293 ms |
92416 KB |
Output is correct |
15 |
Correct |
291 ms |
88732 KB |
Output is correct |
16 |
Correct |
318 ms |
92256 KB |
Output is correct |
17 |
Correct |
306 ms |
89452 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
19 |
Correct |
4 ms |
5036 KB |
Output is correct |
20 |
Correct |
4 ms |
5044 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 |
603 ms |
91120 KB |
Output is correct |
25 |
Correct |
415 ms |
91176 KB |
Output is correct |
26 |
Correct |
537 ms |
99928 KB |
Output is correct |
27 |
Correct |
545 ms |
90824 KB |
Output is correct |
28 |
Correct |
391 ms |
91440 KB |
Output is correct |
29 |
Correct |
454 ms |
91156 KB |
Output is correct |
30 |
Correct |
309 ms |
91580 KB |
Output is correct |
31 |
Correct |
669 ms |
97128 KB |
Output is correct |
32 |
Correct |
450 ms |
99020 KB |
Output is correct |
33 |
Correct |
575 ms |
90928 KB |
Output is correct |
34 |
Correct |
414 ms |
91056 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 |
5044 KB |
Output is correct |
39 |
Correct |
4 ms |
5036 KB |
Output is correct |
40 |
Correct |
4 ms |
4948 KB |
Output is correct |
41 |
Correct |
3 ms |
5036 KB |
Output is correct |
42 |
Correct |
4 ms |
5028 KB |
Output is correct |
43 |
Correct |
3 ms |
4948 KB |
Output is correct |
44 |
Correct |
4 ms |
4948 KB |
Output is correct |
45 |
Correct |
4 ms |
4948 KB |
Output is correct |
46 |
Correct |
3 ms |
4948 KB |
Output is correct |
47 |
Correct |
3 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 |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
695 ms |
96696 KB |
Output is correct |
3 |
Correct |
790 ms |
95324 KB |
Output is correct |
4 |
Correct |
649 ms |
95052 KB |
Output is correct |
5 |
Correct |
573 ms |
87968 KB |
Output is correct |
6 |
Correct |
447 ms |
88124 KB |
Output is correct |
7 |
Correct |
381 ms |
88212 KB |
Output is correct |
8 |
Correct |
340 ms |
88884 KB |
Output is correct |
9 |
Correct |
763 ms |
94300 KB |
Output is correct |
10 |
Correct |
824 ms |
96588 KB |
Output is correct |
11 |
Correct |
622 ms |
87760 KB |
Output is correct |
12 |
Correct |
733 ms |
88524 KB |
Output is correct |
13 |
Correct |
539 ms |
89392 KB |
Output is correct |
14 |
Correct |
487 ms |
89988 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
5040 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 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 |
696 ms |
96336 KB |
Output is correct |
3 |
Correct |
788 ms |
93620 KB |
Output is correct |
4 |
Correct |
610 ms |
95224 KB |
Output is correct |
5 |
Correct |
617 ms |
89648 KB |
Output is correct |
6 |
Correct |
486 ms |
89676 KB |
Output is correct |
7 |
Correct |
418 ms |
89676 KB |
Output is correct |
8 |
Correct |
300 ms |
90148 KB |
Output is correct |
9 |
Correct |
719 ms |
98924 KB |
Output is correct |
10 |
Correct |
823 ms |
97548 KB |
Output is correct |
11 |
Correct |
615 ms |
90548 KB |
Output is correct |
12 |
Correct |
670 ms |
89040 KB |
Output is correct |
13 |
Correct |
476 ms |
89772 KB |
Output is correct |
14 |
Correct |
552 ms |
90204 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5044 KB |
Output is correct |
18 |
Correct |
4 ms |
5036 KB |
Output is correct |
19 |
Correct |
4 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
5 ms |
5460 KB |
Output is correct |
5 |
Correct |
4 ms |
5332 KB |
Output is correct |
6 |
Correct |
4 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5356 KB |
Output is correct |
8 |
Correct |
3 ms |
5460 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5004 KB |
Output is correct |
12 |
Correct |
3 ms |
5040 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 |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5044 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5036 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
5052 KB |
Output is correct |
22 |
Correct |
3 ms |
5036 KB |
Output is correct |
23 |
Correct |
3 ms |
5040 KB |
Output is correct |
24 |
Correct |
4 ms |
5032 KB |
Output is correct |
25 |
Correct |
4 ms |
4948 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
4948 KB |
Output is correct |
29 |
Correct |
2 ms |
4948 KB |
Output is correct |
30 |
Correct |
571 ms |
91148 KB |
Output is correct |
31 |
Correct |
426 ms |
91788 KB |
Output is correct |
32 |
Correct |
519 ms |
97420 KB |
Output is correct |
33 |
Correct |
532 ms |
91160 KB |
Output is correct |
34 |
Correct |
410 ms |
89848 KB |
Output is correct |
35 |
Correct |
373 ms |
91732 KB |
Output is correct |
36 |
Correct |
306 ms |
90856 KB |
Output is correct |
37 |
Correct |
588 ms |
100156 KB |
Output is correct |
38 |
Correct |
440 ms |
100056 KB |
Output is correct |
39 |
Correct |
585 ms |
90412 KB |
Output is correct |
40 |
Correct |
444 ms |
90192 KB |
Output is correct |
41 |
Correct |
290 ms |
92032 KB |
Output is correct |
42 |
Correct |
293 ms |
92416 KB |
Output is correct |
43 |
Correct |
291 ms |
88732 KB |
Output is correct |
44 |
Correct |
318 ms |
92256 KB |
Output is correct |
45 |
Correct |
306 ms |
89452 KB |
Output is correct |
46 |
Correct |
3 ms |
5020 KB |
Output is correct |
47 |
Correct |
4 ms |
5036 KB |
Output is correct |
48 |
Correct |
4 ms |
5044 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 |
5032 KB |
Output is correct |
52 |
Correct |
603 ms |
91120 KB |
Output is correct |
53 |
Correct |
415 ms |
91176 KB |
Output is correct |
54 |
Correct |
537 ms |
99928 KB |
Output is correct |
55 |
Correct |
545 ms |
90824 KB |
Output is correct |
56 |
Correct |
391 ms |
91440 KB |
Output is correct |
57 |
Correct |
454 ms |
91156 KB |
Output is correct |
58 |
Correct |
309 ms |
91580 KB |
Output is correct |
59 |
Correct |
669 ms |
97128 KB |
Output is correct |
60 |
Correct |
450 ms |
99020 KB |
Output is correct |
61 |
Correct |
575 ms |
90928 KB |
Output is correct |
62 |
Correct |
414 ms |
91056 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 |
5044 KB |
Output is correct |
67 |
Correct |
4 ms |
5036 KB |
Output is correct |
68 |
Correct |
4 ms |
4948 KB |
Output is correct |
69 |
Correct |
3 ms |
5036 KB |
Output is correct |
70 |
Correct |
4 ms |
5028 KB |
Output is correct |
71 |
Correct |
3 ms |
4948 KB |
Output is correct |
72 |
Correct |
4 ms |
4948 KB |
Output is correct |
73 |
Correct |
4 ms |
4948 KB |
Output is correct |
74 |
Correct |
3 ms |
4948 KB |
Output is correct |
75 |
Correct |
3 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 |
3 ms |
4948 KB |
Output is correct |
79 |
Correct |
695 ms |
96696 KB |
Output is correct |
80 |
Correct |
790 ms |
95324 KB |
Output is correct |
81 |
Correct |
649 ms |
95052 KB |
Output is correct |
82 |
Correct |
573 ms |
87968 KB |
Output is correct |
83 |
Correct |
447 ms |
88124 KB |
Output is correct |
84 |
Correct |
381 ms |
88212 KB |
Output is correct |
85 |
Correct |
340 ms |
88884 KB |
Output is correct |
86 |
Correct |
763 ms |
94300 KB |
Output is correct |
87 |
Correct |
824 ms |
96588 KB |
Output is correct |
88 |
Correct |
622 ms |
87760 KB |
Output is correct |
89 |
Correct |
733 ms |
88524 KB |
Output is correct |
90 |
Correct |
539 ms |
89392 KB |
Output is correct |
91 |
Correct |
487 ms |
89988 KB |
Output is correct |
92 |
Correct |
3 ms |
4948 KB |
Output is correct |
93 |
Correct |
4 ms |
5040 KB |
Output is correct |
94 |
Correct |
3 ms |
4948 KB |
Output is correct |
95 |
Correct |
3 ms |
4948 KB |
Output is correct |
96 |
Correct |
3 ms |
4948 KB |
Output is correct |
97 |
Correct |
3 ms |
4948 KB |
Output is correct |
98 |
Correct |
696 ms |
96336 KB |
Output is correct |
99 |
Correct |
788 ms |
93620 KB |
Output is correct |
100 |
Correct |
610 ms |
95224 KB |
Output is correct |
101 |
Correct |
617 ms |
89648 KB |
Output is correct |
102 |
Correct |
486 ms |
89676 KB |
Output is correct |
103 |
Correct |
418 ms |
89676 KB |
Output is correct |
104 |
Correct |
300 ms |
90148 KB |
Output is correct |
105 |
Correct |
719 ms |
98924 KB |
Output is correct |
106 |
Correct |
823 ms |
97548 KB |
Output is correct |
107 |
Correct |
615 ms |
90548 KB |
Output is correct |
108 |
Correct |
670 ms |
89040 KB |
Output is correct |
109 |
Correct |
476 ms |
89772 KB |
Output is correct |
110 |
Correct |
552 ms |
90204 KB |
Output is correct |
111 |
Correct |
3 ms |
4948 KB |
Output is correct |
112 |
Correct |
3 ms |
4948 KB |
Output is correct |
113 |
Correct |
3 ms |
5044 KB |
Output is correct |
114 |
Correct |
4 ms |
5036 KB |
Output is correct |
115 |
Correct |
4 ms |
4948 KB |
Output is correct |
116 |
Correct |
646 ms |
88712 KB |
Output is correct |
117 |
Correct |
787 ms |
91388 KB |
Output is correct |
118 |
Correct |
641 ms |
99424 KB |
Output is correct |
119 |
Correct |
619 ms |
90972 KB |
Output is correct |
120 |
Correct |
436 ms |
90468 KB |
Output is correct |
121 |
Correct |
471 ms |
91328 KB |
Output is correct |
122 |
Correct |
364 ms |
91608 KB |
Output is correct |
123 |
Correct |
684 ms |
98120 KB |
Output is correct |
124 |
Correct |
872 ms |
96664 KB |
Output is correct |
125 |
Correct |
610 ms |
89776 KB |
Output is correct |
126 |
Correct |
735 ms |
91156 KB |
Output is correct |
127 |
Correct |
783 ms |
91656 KB |
Output is correct |
128 |
Correct |
587 ms |
92184 KB |
Output is correct |
129 |
Correct |
590 ms |
93132 KB |
Output is correct |