#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];
int path[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)
{
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;
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 tek = vert;
int uk = 0;
path[uk] = tek;
uk++;
for (int i = 1; i <= dist; i++)
{
if (parent[tek] == -1) break;
tek = parent[tek];
path[uk] = tek;
uk++;
}
for (int fin = height[vert] - dist; fin <= height[vert] + dist; fin++)
{
int x = (dist + height[vert] - fin) / 2;
if (x >= uk) x = uk - 1;
if (height[path[x]] > fin) continue;
int y = fin - height[path[x]];
pair <int, int> bounds = subtree[y][path[x]];
if (bounds.x == -1) continue;
update(tree[fin], 1, 0, rows[fin] - 1, bounds.x, bounds.y, mn);
}
}
else
{
int vert;
cin >> vert;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9940 KB |
Output is correct |
2 |
Correct |
5 ms |
9940 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
7 ms |
10480 KB |
Output is correct |
5 |
Correct |
7 ms |
10324 KB |
Output is correct |
6 |
Correct |
7 ms |
10324 KB |
Output is correct |
7 |
Correct |
6 ms |
10324 KB |
Output is correct |
8 |
Correct |
6 ms |
10324 KB |
Output is correct |
9 |
Correct |
5 ms |
9940 KB |
Output is correct |
10 |
Correct |
6 ms |
9940 KB |
Output is correct |
11 |
Correct |
6 ms |
9940 KB |
Output is correct |
12 |
Correct |
6 ms |
9940 KB |
Output is correct |
13 |
Correct |
5 ms |
9940 KB |
Output is correct |
14 |
Correct |
6 ms |
9936 KB |
Output is correct |
15 |
Correct |
5 ms |
9940 KB |
Output is correct |
16 |
Correct |
7 ms |
10048 KB |
Output is correct |
17 |
Correct |
6 ms |
9940 KB |
Output is correct |
18 |
Correct |
6 ms |
9940 KB |
Output is correct |
19 |
Correct |
5 ms |
9940 KB |
Output is correct |
20 |
Correct |
7 ms |
9940 KB |
Output is correct |
21 |
Correct |
6 ms |
9940 KB |
Output is correct |
22 |
Correct |
7 ms |
10032 KB |
Output is correct |
23 |
Correct |
6 ms |
9940 KB |
Output is correct |
24 |
Correct |
5 ms |
9940 KB |
Output is correct |
25 |
Correct |
6 ms |
9940 KB |
Output is correct |
26 |
Correct |
6 ms |
9940 KB |
Output is correct |
27 |
Correct |
5 ms |
9980 KB |
Output is correct |
28 |
Correct |
6 ms |
9940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9940 KB |
Output is correct |
2 |
Correct |
427 ms |
90176 KB |
Output is correct |
3 |
Correct |
474 ms |
86940 KB |
Output is correct |
4 |
Correct |
527 ms |
100960 KB |
Output is correct |
5 |
Correct |
451 ms |
88588 KB |
Output is correct |
6 |
Correct |
405 ms |
88500 KB |
Output is correct |
7 |
Correct |
479 ms |
89012 KB |
Output is correct |
8 |
Correct |
330 ms |
89244 KB |
Output is correct |
9 |
Correct |
525 ms |
109012 KB |
Output is correct |
10 |
Correct |
535 ms |
104240 KB |
Output is correct |
11 |
Correct |
384 ms |
90100 KB |
Output is correct |
12 |
Correct |
560 ms |
86988 KB |
Output is correct |
13 |
Correct |
277 ms |
87504 KB |
Output is correct |
14 |
Correct |
435 ms |
87648 KB |
Output is correct |
15 |
Correct |
389 ms |
87504 KB |
Output is correct |
16 |
Correct |
467 ms |
87440 KB |
Output is correct |
17 |
Correct |
480 ms |
88056 KB |
Output is correct |
18 |
Correct |
6 ms |
9940 KB |
Output is correct |
19 |
Correct |
7 ms |
9940 KB |
Output is correct |
20 |
Correct |
5 ms |
9940 KB |
Output is correct |
21 |
Correct |
5 ms |
9940 KB |
Output is correct |
22 |
Correct |
5 ms |
9940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9940 KB |
Output is correct |
2 |
Correct |
427 ms |
90176 KB |
Output is correct |
3 |
Correct |
474 ms |
86940 KB |
Output is correct |
4 |
Correct |
527 ms |
100960 KB |
Output is correct |
5 |
Correct |
451 ms |
88588 KB |
Output is correct |
6 |
Correct |
405 ms |
88500 KB |
Output is correct |
7 |
Correct |
479 ms |
89012 KB |
Output is correct |
8 |
Correct |
330 ms |
89244 KB |
Output is correct |
9 |
Correct |
525 ms |
109012 KB |
Output is correct |
10 |
Correct |
535 ms |
104240 KB |
Output is correct |
11 |
Correct |
384 ms |
90100 KB |
Output is correct |
12 |
Correct |
560 ms |
86988 KB |
Output is correct |
13 |
Correct |
277 ms |
87504 KB |
Output is correct |
14 |
Correct |
435 ms |
87648 KB |
Output is correct |
15 |
Correct |
389 ms |
87504 KB |
Output is correct |
16 |
Correct |
467 ms |
87440 KB |
Output is correct |
17 |
Correct |
480 ms |
88056 KB |
Output is correct |
18 |
Correct |
6 ms |
9940 KB |
Output is correct |
19 |
Correct |
7 ms |
9940 KB |
Output is correct |
20 |
Correct |
5 ms |
9940 KB |
Output is correct |
21 |
Correct |
5 ms |
9940 KB |
Output is correct |
22 |
Correct |
5 ms |
9940 KB |
Output is correct |
23 |
Correct |
5 ms |
9940 KB |
Output is correct |
24 |
Correct |
465 ms |
90088 KB |
Output is correct |
25 |
Correct |
713 ms |
86920 KB |
Output is correct |
26 |
Correct |
666 ms |
106576 KB |
Output is correct |
27 |
Correct |
499 ms |
88612 KB |
Output is correct |
28 |
Correct |
472 ms |
88600 KB |
Output is correct |
29 |
Correct |
504 ms |
88548 KB |
Output is correct |
30 |
Correct |
421 ms |
89212 KB |
Output is correct |
31 |
Correct |
485 ms |
103252 KB |
Output is correct |
32 |
Correct |
670 ms |
104216 KB |
Output is correct |
33 |
Correct |
443 ms |
90300 KB |
Output is correct |
34 |
Correct |
605 ms |
87012 KB |
Output is correct |
35 |
Correct |
6 ms |
9940 KB |
Output is correct |
36 |
Correct |
6 ms |
10044 KB |
Output is correct |
37 |
Correct |
6 ms |
9940 KB |
Output is correct |
38 |
Correct |
6 ms |
9940 KB |
Output is correct |
39 |
Correct |
5 ms |
9940 KB |
Output is correct |
40 |
Correct |
5 ms |
9940 KB |
Output is correct |
41 |
Correct |
6 ms |
9940 KB |
Output is correct |
42 |
Correct |
6 ms |
9972 KB |
Output is correct |
43 |
Correct |
7 ms |
9940 KB |
Output is correct |
44 |
Correct |
5 ms |
9940 KB |
Output is correct |
45 |
Correct |
5 ms |
9940 KB |
Output is correct |
46 |
Correct |
5 ms |
9940 KB |
Output is correct |
47 |
Correct |
5 ms |
9940 KB |
Output is correct |
48 |
Correct |
5 ms |
9940 KB |
Output is correct |
49 |
Correct |
5 ms |
9940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9940 KB |
Output is correct |
2 |
Correct |
781 ms |
106116 KB |
Output is correct |
3 |
Correct |
3105 ms |
100632 KB |
Output is correct |
4 |
Correct |
1213 ms |
101996 KB |
Output is correct |
5 |
Correct |
2076 ms |
87180 KB |
Output is correct |
6 |
Correct |
1101 ms |
87120 KB |
Output is correct |
7 |
Correct |
750 ms |
87264 KB |
Output is correct |
8 |
Correct |
372 ms |
87572 KB |
Output is correct |
9 |
Correct |
809 ms |
100596 KB |
Output is correct |
10 |
Correct |
2769 ms |
103708 KB |
Output is correct |
11 |
Correct |
1085 ms |
87292 KB |
Output is correct |
12 |
Execution timed out |
4059 ms |
86692 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9940 KB |
Output is correct |
2 |
Correct |
864 ms |
102324 KB |
Output is correct |
3 |
Correct |
3340 ms |
96772 KB |
Output is correct |
4 |
Correct |
1375 ms |
99816 KB |
Output is correct |
5 |
Correct |
2031 ms |
88524 KB |
Output is correct |
6 |
Correct |
1050 ms |
88556 KB |
Output is correct |
7 |
Correct |
806 ms |
88580 KB |
Output is correct |
8 |
Correct |
315 ms |
88900 KB |
Output is correct |
9 |
Correct |
788 ms |
107556 KB |
Output is correct |
10 |
Correct |
2687 ms |
104780 KB |
Output is correct |
11 |
Correct |
1096 ms |
90200 KB |
Output is correct |
12 |
Execution timed out |
4067 ms |
86924 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9940 KB |
Output is correct |
2 |
Correct |
5 ms |
9940 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
7 ms |
10480 KB |
Output is correct |
5 |
Correct |
7 ms |
10324 KB |
Output is correct |
6 |
Correct |
7 ms |
10324 KB |
Output is correct |
7 |
Correct |
6 ms |
10324 KB |
Output is correct |
8 |
Correct |
6 ms |
10324 KB |
Output is correct |
9 |
Correct |
5 ms |
9940 KB |
Output is correct |
10 |
Correct |
6 ms |
9940 KB |
Output is correct |
11 |
Correct |
6 ms |
9940 KB |
Output is correct |
12 |
Correct |
6 ms |
9940 KB |
Output is correct |
13 |
Correct |
5 ms |
9940 KB |
Output is correct |
14 |
Correct |
6 ms |
9936 KB |
Output is correct |
15 |
Correct |
5 ms |
9940 KB |
Output is correct |
16 |
Correct |
7 ms |
10048 KB |
Output is correct |
17 |
Correct |
6 ms |
9940 KB |
Output is correct |
18 |
Correct |
6 ms |
9940 KB |
Output is correct |
19 |
Correct |
5 ms |
9940 KB |
Output is correct |
20 |
Correct |
7 ms |
9940 KB |
Output is correct |
21 |
Correct |
6 ms |
9940 KB |
Output is correct |
22 |
Correct |
7 ms |
10032 KB |
Output is correct |
23 |
Correct |
6 ms |
9940 KB |
Output is correct |
24 |
Correct |
5 ms |
9940 KB |
Output is correct |
25 |
Correct |
6 ms |
9940 KB |
Output is correct |
26 |
Correct |
6 ms |
9940 KB |
Output is correct |
27 |
Correct |
5 ms |
9980 KB |
Output is correct |
28 |
Correct |
6 ms |
9940 KB |
Output is correct |
29 |
Correct |
5 ms |
9940 KB |
Output is correct |
30 |
Correct |
427 ms |
90176 KB |
Output is correct |
31 |
Correct |
474 ms |
86940 KB |
Output is correct |
32 |
Correct |
527 ms |
100960 KB |
Output is correct |
33 |
Correct |
451 ms |
88588 KB |
Output is correct |
34 |
Correct |
405 ms |
88500 KB |
Output is correct |
35 |
Correct |
479 ms |
89012 KB |
Output is correct |
36 |
Correct |
330 ms |
89244 KB |
Output is correct |
37 |
Correct |
525 ms |
109012 KB |
Output is correct |
38 |
Correct |
535 ms |
104240 KB |
Output is correct |
39 |
Correct |
384 ms |
90100 KB |
Output is correct |
40 |
Correct |
560 ms |
86988 KB |
Output is correct |
41 |
Correct |
277 ms |
87504 KB |
Output is correct |
42 |
Correct |
435 ms |
87648 KB |
Output is correct |
43 |
Correct |
389 ms |
87504 KB |
Output is correct |
44 |
Correct |
467 ms |
87440 KB |
Output is correct |
45 |
Correct |
480 ms |
88056 KB |
Output is correct |
46 |
Correct |
6 ms |
9940 KB |
Output is correct |
47 |
Correct |
7 ms |
9940 KB |
Output is correct |
48 |
Correct |
5 ms |
9940 KB |
Output is correct |
49 |
Correct |
5 ms |
9940 KB |
Output is correct |
50 |
Correct |
5 ms |
9940 KB |
Output is correct |
51 |
Correct |
5 ms |
9940 KB |
Output is correct |
52 |
Correct |
465 ms |
90088 KB |
Output is correct |
53 |
Correct |
713 ms |
86920 KB |
Output is correct |
54 |
Correct |
666 ms |
106576 KB |
Output is correct |
55 |
Correct |
499 ms |
88612 KB |
Output is correct |
56 |
Correct |
472 ms |
88600 KB |
Output is correct |
57 |
Correct |
504 ms |
88548 KB |
Output is correct |
58 |
Correct |
421 ms |
89212 KB |
Output is correct |
59 |
Correct |
485 ms |
103252 KB |
Output is correct |
60 |
Correct |
670 ms |
104216 KB |
Output is correct |
61 |
Correct |
443 ms |
90300 KB |
Output is correct |
62 |
Correct |
605 ms |
87012 KB |
Output is correct |
63 |
Correct |
6 ms |
9940 KB |
Output is correct |
64 |
Correct |
6 ms |
10044 KB |
Output is correct |
65 |
Correct |
6 ms |
9940 KB |
Output is correct |
66 |
Correct |
6 ms |
9940 KB |
Output is correct |
67 |
Correct |
5 ms |
9940 KB |
Output is correct |
68 |
Correct |
5 ms |
9940 KB |
Output is correct |
69 |
Correct |
6 ms |
9940 KB |
Output is correct |
70 |
Correct |
6 ms |
9972 KB |
Output is correct |
71 |
Correct |
7 ms |
9940 KB |
Output is correct |
72 |
Correct |
5 ms |
9940 KB |
Output is correct |
73 |
Correct |
5 ms |
9940 KB |
Output is correct |
74 |
Correct |
5 ms |
9940 KB |
Output is correct |
75 |
Correct |
5 ms |
9940 KB |
Output is correct |
76 |
Correct |
5 ms |
9940 KB |
Output is correct |
77 |
Correct |
5 ms |
9940 KB |
Output is correct |
78 |
Correct |
5 ms |
9940 KB |
Output is correct |
79 |
Correct |
781 ms |
106116 KB |
Output is correct |
80 |
Correct |
3105 ms |
100632 KB |
Output is correct |
81 |
Correct |
1213 ms |
101996 KB |
Output is correct |
82 |
Correct |
2076 ms |
87180 KB |
Output is correct |
83 |
Correct |
1101 ms |
87120 KB |
Output is correct |
84 |
Correct |
750 ms |
87264 KB |
Output is correct |
85 |
Correct |
372 ms |
87572 KB |
Output is correct |
86 |
Correct |
809 ms |
100596 KB |
Output is correct |
87 |
Correct |
2769 ms |
103708 KB |
Output is correct |
88 |
Correct |
1085 ms |
87292 KB |
Output is correct |
89 |
Execution timed out |
4059 ms |
86692 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |