#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef long double ld;
const int MAXD = 40;
const int MAXN = 200000;
int MOD = 0;
vector <int> g[MAXN + 5];
int height[MAXN + 5], position[MAXN + 5], parent[MAXN + 5], start_height[MAXN + 5], rows[MAXN + 5];
pair <int, int> subtree[MAXD + 5][MAXN + 5];
vector <int> tree[MAXN + 5];
vector <pair <pair <int, int>, int> > ququ[MAXN + 5];
void dfs(int v, int pr, int hei)
{
parent[v] = pr;
height[v] = hei;
position[v] = rows[hei];
rows[hei]++;
int pos = -1;
for (int i = 0; i < (int) g[v].size(); i++)
if (g[v][i] == pr) pos = i;
if (pos != -1)
{
swap(g[v][pos], g[v].back());
g[v].pop_back();
}
for (int u : g[v])
dfs(u, v, hei + 1);
subtree[0][v] = mp(position[v], position[v]);
if (!g[v].empty())
{
subtree[1][v] = mp(position[g[v][0]], position[g[v].back()]);
int lef = 0, rig = (int) g[v].size() - 1;
for (int i = 2; i <= MAXD; i++)
{
while (lef <= rig && subtree[i - 1][g[v][lef]] == mp(-1, -1)) lef++;
while (lef <= rig && subtree[i - 1][g[v][rig]] == mp(-1, -1)) rig--;
if (lef <= rig) subtree[i][v] = mp(subtree[i - 1][g[v][lef]].x, subtree[i - 1][g[v][rig]].y);
else break;
}
}
return;
}
int get_height(vector <int> &tree, int v, int lef, int rig, int pos, int cur_val)
{
if (tree[v] != 1) cur_val = (ll) cur_val * (ll) tree[v] % (ll) MOD;
if (lef == rig) return cur_val;
int mid = (lef + rig) / 2;
if (pos <= mid) return get_height(tree, 2 * v, lef, mid, pos, cur_val);
else return get_height(tree, 2 * v + 1, mid + 1, rig, pos, cur_val);
}
void update(vector <int> &tree, int v, int lef, int rig, int A, int B, int mn)
{
if (lef > B || rig < A) return;
if (A <= lef && rig <= B)
{
tree[v] = (ll) tree[v] * (ll) mn % (ll) MOD;
return;
}
int mid = (lef + rig) / 2;
if (tree[v] != 1)
{
tree[2 * v] = (ll) tree[2 * v] * (ll) tree[v] % (ll) MOD;
tree[2 * v + 1] = (ll) tree[2 * v + 1] * (ll) tree[v] % (ll) MOD;
tree[v] = 1;
}
update(tree, 2 * v, lef, mid, A, B, mn);
update(tree, 2 * v + 1, mid + 1, rig, A, B, mn);
return;
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0);
int n, mod;
cin >> n >> mod;
MOD = mod;
for (int i = 0; i <= MAXD; i++)
for (int j = 1; j <= n; j++)
subtree[i][j] = mp(-1, -1);
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
g[u].pb(v), g[v].pb(u);
}
for (int i = 1; i <= n; i++) cin >> start_height[i];
dfs(1, -1, 0);
{
int h = 0;
while (rows[h] > 0)
{
tree[h].resize(4 * rows[h], 1);
h++;
}
}
int quer;
cin >> quer;
for (int rep = 1; rep <= quer; rep++)
{
int typ;
cin >> typ;
if (typ == 1)
{
int vert, dist, mn;
cin >> vert >> dist >> mn;
int cur = 0, curv = vert;
for (int fin = height[vert] + dist; fin >= height[vert] - dist; fin--)
{
int x = (dist + height[vert] - fin) / 2;
if (x > cur)
{
if (parent[curv] != -1) curv = parent[curv], cur++;
}
if (height[curv] > fin) break;
int y = fin - height[curv];
pair <int, int> bounds = subtree[y][curv];
if (bounds.x == -1) continue;
if (mn != 1) ququ[fin].pb(mp(mp(bounds.x, bounds.y), mn));
}
}
else
{
int vert;
cin >> vert;
int heh = height[vert];
while (!ququ[heh].empty())
{
pair <pair <int, int>, int> A = ququ[heh].back();
ququ[heh].pop_back();
update(tree[heh], 1, 0, rows[heh] - 1, A.x.x, A.x.y, A.y);
}
int ans = get_height(tree[height[vert]], 1, 0, rows[height[vert]] - 1, position[vert], 1);
ans = (ll) ans * (ll) start_height[vert] % (ll) MOD;
cout << ans << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14676 KB |
Output is correct |
2 |
Correct |
11 ms |
14676 KB |
Output is correct |
3 |
Correct |
12 ms |
14676 KB |
Output is correct |
4 |
Correct |
11 ms |
15480 KB |
Output is correct |
5 |
Correct |
12 ms |
15240 KB |
Output is correct |
6 |
Correct |
14 ms |
15200 KB |
Output is correct |
7 |
Correct |
10 ms |
15188 KB |
Output is correct |
8 |
Correct |
11 ms |
15072 KB |
Output is correct |
9 |
Correct |
10 ms |
14676 KB |
Output is correct |
10 |
Correct |
10 ms |
14676 KB |
Output is correct |
11 |
Correct |
10 ms |
14744 KB |
Output is correct |
12 |
Correct |
13 ms |
14736 KB |
Output is correct |
13 |
Correct |
9 ms |
14676 KB |
Output is correct |
14 |
Correct |
9 ms |
14676 KB |
Output is correct |
15 |
Correct |
9 ms |
14676 KB |
Output is correct |
16 |
Correct |
12 ms |
14676 KB |
Output is correct |
17 |
Correct |
13 ms |
14676 KB |
Output is correct |
18 |
Correct |
12 ms |
14676 KB |
Output is correct |
19 |
Correct |
8 ms |
14664 KB |
Output is correct |
20 |
Correct |
9 ms |
14676 KB |
Output is correct |
21 |
Correct |
9 ms |
14676 KB |
Output is correct |
22 |
Correct |
9 ms |
14676 KB |
Output is correct |
23 |
Correct |
9 ms |
14676 KB |
Output is correct |
24 |
Correct |
9 ms |
14676 KB |
Output is correct |
25 |
Correct |
9 ms |
14676 KB |
Output is correct |
26 |
Correct |
11 ms |
14676 KB |
Output is correct |
27 |
Correct |
13 ms |
14676 KB |
Output is correct |
28 |
Correct |
10 ms |
14676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14676 KB |
Output is correct |
2 |
Correct |
390 ms |
95092 KB |
Output is correct |
3 |
Correct |
554 ms |
93528 KB |
Output is correct |
4 |
Correct |
586 ms |
113904 KB |
Output is correct |
5 |
Correct |
407 ms |
93644 KB |
Output is correct |
6 |
Correct |
417 ms |
93152 KB |
Output is correct |
7 |
Correct |
419 ms |
93436 KB |
Output is correct |
8 |
Correct |
341 ms |
95896 KB |
Output is correct |
9 |
Correct |
514 ms |
119580 KB |
Output is correct |
10 |
Correct |
657 ms |
132092 KB |
Output is correct |
11 |
Correct |
361 ms |
94888 KB |
Output is correct |
12 |
Correct |
467 ms |
93780 KB |
Output is correct |
13 |
Correct |
295 ms |
102704 KB |
Output is correct |
14 |
Correct |
416 ms |
97732 KB |
Output is correct |
15 |
Correct |
422 ms |
94236 KB |
Output is correct |
16 |
Correct |
433 ms |
93256 KB |
Output is correct |
17 |
Correct |
458 ms |
93032 KB |
Output is correct |
18 |
Correct |
8 ms |
14676 KB |
Output is correct |
19 |
Correct |
11 ms |
14632 KB |
Output is correct |
20 |
Correct |
9 ms |
14676 KB |
Output is correct |
21 |
Correct |
9 ms |
14676 KB |
Output is correct |
22 |
Correct |
10 ms |
14692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14676 KB |
Output is correct |
2 |
Correct |
390 ms |
95092 KB |
Output is correct |
3 |
Correct |
554 ms |
93528 KB |
Output is correct |
4 |
Correct |
586 ms |
113904 KB |
Output is correct |
5 |
Correct |
407 ms |
93644 KB |
Output is correct |
6 |
Correct |
417 ms |
93152 KB |
Output is correct |
7 |
Correct |
419 ms |
93436 KB |
Output is correct |
8 |
Correct |
341 ms |
95896 KB |
Output is correct |
9 |
Correct |
514 ms |
119580 KB |
Output is correct |
10 |
Correct |
657 ms |
132092 KB |
Output is correct |
11 |
Correct |
361 ms |
94888 KB |
Output is correct |
12 |
Correct |
467 ms |
93780 KB |
Output is correct |
13 |
Correct |
295 ms |
102704 KB |
Output is correct |
14 |
Correct |
416 ms |
97732 KB |
Output is correct |
15 |
Correct |
422 ms |
94236 KB |
Output is correct |
16 |
Correct |
433 ms |
93256 KB |
Output is correct |
17 |
Correct |
458 ms |
93032 KB |
Output is correct |
18 |
Correct |
8 ms |
14676 KB |
Output is correct |
19 |
Correct |
11 ms |
14632 KB |
Output is correct |
20 |
Correct |
9 ms |
14676 KB |
Output is correct |
21 |
Correct |
9 ms |
14676 KB |
Output is correct |
22 |
Correct |
10 ms |
14692 KB |
Output is correct |
23 |
Correct |
9 ms |
14676 KB |
Output is correct |
24 |
Correct |
363 ms |
94924 KB |
Output is correct |
25 |
Correct |
526 ms |
94560 KB |
Output is correct |
26 |
Correct |
607 ms |
124392 KB |
Output is correct |
27 |
Correct |
434 ms |
93664 KB |
Output is correct |
28 |
Correct |
462 ms |
93316 KB |
Output is correct |
29 |
Correct |
437 ms |
93328 KB |
Output is correct |
30 |
Correct |
370 ms |
96252 KB |
Output is correct |
31 |
Correct |
510 ms |
113592 KB |
Output is correct |
32 |
Correct |
754 ms |
145772 KB |
Output is correct |
33 |
Correct |
365 ms |
95008 KB |
Output is correct |
34 |
Correct |
536 ms |
94236 KB |
Output is correct |
35 |
Correct |
9 ms |
14676 KB |
Output is correct |
36 |
Correct |
9 ms |
14672 KB |
Output is correct |
37 |
Correct |
9 ms |
14676 KB |
Output is correct |
38 |
Correct |
9 ms |
14676 KB |
Output is correct |
39 |
Correct |
9 ms |
14656 KB |
Output is correct |
40 |
Correct |
8 ms |
14676 KB |
Output is correct |
41 |
Correct |
9 ms |
14676 KB |
Output is correct |
42 |
Correct |
9 ms |
14740 KB |
Output is correct |
43 |
Correct |
9 ms |
14620 KB |
Output is correct |
44 |
Correct |
9 ms |
14676 KB |
Output is correct |
45 |
Correct |
8 ms |
14676 KB |
Output is correct |
46 |
Correct |
9 ms |
14744 KB |
Output is correct |
47 |
Correct |
9 ms |
14676 KB |
Output is correct |
48 |
Correct |
9 ms |
14676 KB |
Output is correct |
49 |
Correct |
11 ms |
14652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14644 KB |
Output is correct |
2 |
Correct |
893 ms |
156464 KB |
Output is correct |
3 |
Correct |
2606 ms |
669628 KB |
Output is correct |
4 |
Correct |
1224 ms |
227016 KB |
Output is correct |
5 |
Correct |
1358 ms |
94792 KB |
Output is correct |
6 |
Correct |
1076 ms |
95380 KB |
Output is correct |
7 |
Correct |
784 ms |
98892 KB |
Output is correct |
8 |
Correct |
335 ms |
97936 KB |
Output is correct |
9 |
Correct |
853 ms |
144196 KB |
Output is correct |
10 |
Correct |
2608 ms |
695816 KB |
Output is correct |
11 |
Correct |
779 ms |
93120 KB |
Output is correct |
12 |
Correct |
3145 ms |
142804 KB |
Output is correct |
13 |
Correct |
2182 ms |
144860 KB |
Output is correct |
14 |
Correct |
1522 ms |
190500 KB |
Output is correct |
15 |
Correct |
10 ms |
14736 KB |
Output is correct |
16 |
Correct |
9 ms |
14676 KB |
Output is correct |
17 |
Correct |
10 ms |
14676 KB |
Output is correct |
18 |
Correct |
9 ms |
14744 KB |
Output is correct |
19 |
Correct |
9 ms |
14676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14692 KB |
Output is correct |
2 |
Correct |
859 ms |
144728 KB |
Output is correct |
3 |
Correct |
2595 ms |
678500 KB |
Output is correct |
4 |
Correct |
1186 ms |
215596 KB |
Output is correct |
5 |
Correct |
1322 ms |
97752 KB |
Output is correct |
6 |
Correct |
1065 ms |
99600 KB |
Output is correct |
7 |
Correct |
791 ms |
99596 KB |
Output is correct |
8 |
Correct |
341 ms |
98652 KB |
Output is correct |
9 |
Correct |
876 ms |
157404 KB |
Output is correct |
10 |
Correct |
2633 ms |
699536 KB |
Output is correct |
11 |
Correct |
782 ms |
95772 KB |
Output is correct |
12 |
Correct |
3412 ms |
124380 KB |
Output is correct |
13 |
Correct |
2454 ms |
139472 KB |
Output is correct |
14 |
Correct |
1617 ms |
187784 KB |
Output is correct |
15 |
Correct |
9 ms |
14668 KB |
Output is correct |
16 |
Correct |
11 ms |
14688 KB |
Output is correct |
17 |
Correct |
9 ms |
14684 KB |
Output is correct |
18 |
Correct |
9 ms |
14676 KB |
Output is correct |
19 |
Correct |
9 ms |
14676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14676 KB |
Output is correct |
2 |
Correct |
11 ms |
14676 KB |
Output is correct |
3 |
Correct |
12 ms |
14676 KB |
Output is correct |
4 |
Correct |
11 ms |
15480 KB |
Output is correct |
5 |
Correct |
12 ms |
15240 KB |
Output is correct |
6 |
Correct |
14 ms |
15200 KB |
Output is correct |
7 |
Correct |
10 ms |
15188 KB |
Output is correct |
8 |
Correct |
11 ms |
15072 KB |
Output is correct |
9 |
Correct |
10 ms |
14676 KB |
Output is correct |
10 |
Correct |
10 ms |
14676 KB |
Output is correct |
11 |
Correct |
10 ms |
14744 KB |
Output is correct |
12 |
Correct |
13 ms |
14736 KB |
Output is correct |
13 |
Correct |
9 ms |
14676 KB |
Output is correct |
14 |
Correct |
9 ms |
14676 KB |
Output is correct |
15 |
Correct |
9 ms |
14676 KB |
Output is correct |
16 |
Correct |
12 ms |
14676 KB |
Output is correct |
17 |
Correct |
13 ms |
14676 KB |
Output is correct |
18 |
Correct |
12 ms |
14676 KB |
Output is correct |
19 |
Correct |
8 ms |
14664 KB |
Output is correct |
20 |
Correct |
9 ms |
14676 KB |
Output is correct |
21 |
Correct |
9 ms |
14676 KB |
Output is correct |
22 |
Correct |
9 ms |
14676 KB |
Output is correct |
23 |
Correct |
9 ms |
14676 KB |
Output is correct |
24 |
Correct |
9 ms |
14676 KB |
Output is correct |
25 |
Correct |
9 ms |
14676 KB |
Output is correct |
26 |
Correct |
11 ms |
14676 KB |
Output is correct |
27 |
Correct |
13 ms |
14676 KB |
Output is correct |
28 |
Correct |
10 ms |
14676 KB |
Output is correct |
29 |
Correct |
8 ms |
14676 KB |
Output is correct |
30 |
Correct |
390 ms |
95092 KB |
Output is correct |
31 |
Correct |
554 ms |
93528 KB |
Output is correct |
32 |
Correct |
586 ms |
113904 KB |
Output is correct |
33 |
Correct |
407 ms |
93644 KB |
Output is correct |
34 |
Correct |
417 ms |
93152 KB |
Output is correct |
35 |
Correct |
419 ms |
93436 KB |
Output is correct |
36 |
Correct |
341 ms |
95896 KB |
Output is correct |
37 |
Correct |
514 ms |
119580 KB |
Output is correct |
38 |
Correct |
657 ms |
132092 KB |
Output is correct |
39 |
Correct |
361 ms |
94888 KB |
Output is correct |
40 |
Correct |
467 ms |
93780 KB |
Output is correct |
41 |
Correct |
295 ms |
102704 KB |
Output is correct |
42 |
Correct |
416 ms |
97732 KB |
Output is correct |
43 |
Correct |
422 ms |
94236 KB |
Output is correct |
44 |
Correct |
433 ms |
93256 KB |
Output is correct |
45 |
Correct |
458 ms |
93032 KB |
Output is correct |
46 |
Correct |
8 ms |
14676 KB |
Output is correct |
47 |
Correct |
11 ms |
14632 KB |
Output is correct |
48 |
Correct |
9 ms |
14676 KB |
Output is correct |
49 |
Correct |
9 ms |
14676 KB |
Output is correct |
50 |
Correct |
10 ms |
14692 KB |
Output is correct |
51 |
Correct |
9 ms |
14676 KB |
Output is correct |
52 |
Correct |
363 ms |
94924 KB |
Output is correct |
53 |
Correct |
526 ms |
94560 KB |
Output is correct |
54 |
Correct |
607 ms |
124392 KB |
Output is correct |
55 |
Correct |
434 ms |
93664 KB |
Output is correct |
56 |
Correct |
462 ms |
93316 KB |
Output is correct |
57 |
Correct |
437 ms |
93328 KB |
Output is correct |
58 |
Correct |
370 ms |
96252 KB |
Output is correct |
59 |
Correct |
510 ms |
113592 KB |
Output is correct |
60 |
Correct |
754 ms |
145772 KB |
Output is correct |
61 |
Correct |
365 ms |
95008 KB |
Output is correct |
62 |
Correct |
536 ms |
94236 KB |
Output is correct |
63 |
Correct |
9 ms |
14676 KB |
Output is correct |
64 |
Correct |
9 ms |
14672 KB |
Output is correct |
65 |
Correct |
9 ms |
14676 KB |
Output is correct |
66 |
Correct |
9 ms |
14676 KB |
Output is correct |
67 |
Correct |
9 ms |
14656 KB |
Output is correct |
68 |
Correct |
8 ms |
14676 KB |
Output is correct |
69 |
Correct |
9 ms |
14676 KB |
Output is correct |
70 |
Correct |
9 ms |
14740 KB |
Output is correct |
71 |
Correct |
9 ms |
14620 KB |
Output is correct |
72 |
Correct |
9 ms |
14676 KB |
Output is correct |
73 |
Correct |
8 ms |
14676 KB |
Output is correct |
74 |
Correct |
9 ms |
14744 KB |
Output is correct |
75 |
Correct |
9 ms |
14676 KB |
Output is correct |
76 |
Correct |
9 ms |
14676 KB |
Output is correct |
77 |
Correct |
11 ms |
14652 KB |
Output is correct |
78 |
Correct |
9 ms |
14644 KB |
Output is correct |
79 |
Correct |
893 ms |
156464 KB |
Output is correct |
80 |
Correct |
2606 ms |
669628 KB |
Output is correct |
81 |
Correct |
1224 ms |
227016 KB |
Output is correct |
82 |
Correct |
1358 ms |
94792 KB |
Output is correct |
83 |
Correct |
1076 ms |
95380 KB |
Output is correct |
84 |
Correct |
784 ms |
98892 KB |
Output is correct |
85 |
Correct |
335 ms |
97936 KB |
Output is correct |
86 |
Correct |
853 ms |
144196 KB |
Output is correct |
87 |
Correct |
2608 ms |
695816 KB |
Output is correct |
88 |
Correct |
779 ms |
93120 KB |
Output is correct |
89 |
Correct |
3145 ms |
142804 KB |
Output is correct |
90 |
Correct |
2182 ms |
144860 KB |
Output is correct |
91 |
Correct |
1522 ms |
190500 KB |
Output is correct |
92 |
Correct |
10 ms |
14736 KB |
Output is correct |
93 |
Correct |
9 ms |
14676 KB |
Output is correct |
94 |
Correct |
10 ms |
14676 KB |
Output is correct |
95 |
Correct |
9 ms |
14744 KB |
Output is correct |
96 |
Correct |
9 ms |
14676 KB |
Output is correct |
97 |
Correct |
9 ms |
14692 KB |
Output is correct |
98 |
Correct |
859 ms |
144728 KB |
Output is correct |
99 |
Correct |
2595 ms |
678500 KB |
Output is correct |
100 |
Correct |
1186 ms |
215596 KB |
Output is correct |
101 |
Correct |
1322 ms |
97752 KB |
Output is correct |
102 |
Correct |
1065 ms |
99600 KB |
Output is correct |
103 |
Correct |
791 ms |
99596 KB |
Output is correct |
104 |
Correct |
341 ms |
98652 KB |
Output is correct |
105 |
Correct |
876 ms |
157404 KB |
Output is correct |
106 |
Correct |
2633 ms |
699536 KB |
Output is correct |
107 |
Correct |
782 ms |
95772 KB |
Output is correct |
108 |
Correct |
3412 ms |
124380 KB |
Output is correct |
109 |
Correct |
2454 ms |
139472 KB |
Output is correct |
110 |
Correct |
1617 ms |
187784 KB |
Output is correct |
111 |
Correct |
9 ms |
14668 KB |
Output is correct |
112 |
Correct |
11 ms |
14688 KB |
Output is correct |
113 |
Correct |
9 ms |
14684 KB |
Output is correct |
114 |
Correct |
9 ms |
14676 KB |
Output is correct |
115 |
Correct |
9 ms |
14676 KB |
Output is correct |
116 |
Correct |
789 ms |
93948 KB |
Output is correct |
117 |
Correct |
3442 ms |
128612 KB |
Output is correct |
118 |
Correct |
1273 ms |
241484 KB |
Output is correct |
119 |
Correct |
1417 ms |
96356 KB |
Output is correct |
120 |
Correct |
1182 ms |
97748 KB |
Output is correct |
121 |
Correct |
928 ms |
99656 KB |
Output is correct |
122 |
Correct |
348 ms |
97592 KB |
Output is correct |
123 |
Correct |
924 ms |
151228 KB |
Output is correct |
124 |
Correct |
2570 ms |
671616 KB |
Output is correct |
125 |
Correct |
803 ms |
95060 KB |
Output is correct |
126 |
Correct |
3470 ms |
127584 KB |
Output is correct |
127 |
Correct |
3526 ms |
180572 KB |
Output is correct |
128 |
Correct |
2181 ms |
161980 KB |
Output is correct |
129 |
Correct |
2613 ms |
159516 KB |
Output is correct |