#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];
int tree[4 * MAXN + 5];
int path[MAXN + 5], start[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);
return;
}
void dfsik(int v)
{
for (int u : g[v])
{
dfsik(u);
}
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 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;
int sum = 0;
while (rows[h] > 0)
{
start[h] = sum;
sum += rows[h];
h++;
}
}
for (int i = 1; i <= n; i++) position[i] += start[height[i]];
dfsik(1);
int STEP = 1;
while (STEP < n) STEP *= 2;
for (int i = 1; i <= 2 * STEP - 1; i++) tree[i] = 1;
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;
int L = bounds.x + STEP, R = bounds.y + STEP;
while (L <= R)
{
if (L % 2 == 1)
{
tree[L] = (ll) tree[L] * (ll) mn % (ll) MOD;
L++;
}
if (R % 2 == 0)
{
tree[R] = (ll) tree[R] * (ll) mn % (ll) MOD;
R--;
}
L /= 2, R /= 2;
}
}
}
else
{
int vert;
cin >> vert;
int pos = position[vert] + STEP;
int ans = start_height[vert];
while (pos > 0)
{
ans = (ll) ans * (ll) tree[pos] % (ll) MOD;
pos /= 2;
}
cout << ans << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5332 KB |
Output is correct |
2 |
Correct |
4 ms |
5332 KB |
Output is correct |
3 |
Correct |
3 ms |
5332 KB |
Output is correct |
4 |
Correct |
4 ms |
5716 KB |
Output is correct |
5 |
Correct |
6 ms |
5716 KB |
Output is correct |
6 |
Correct |
6 ms |
5716 KB |
Output is correct |
7 |
Correct |
4 ms |
5716 KB |
Output is correct |
8 |
Correct |
6 ms |
5716 KB |
Output is correct |
9 |
Correct |
3 ms |
5332 KB |
Output is correct |
10 |
Correct |
3 ms |
5332 KB |
Output is correct |
11 |
Correct |
3 ms |
5332 KB |
Output is correct |
12 |
Correct |
4 ms |
5356 KB |
Output is correct |
13 |
Correct |
3 ms |
5332 KB |
Output is correct |
14 |
Correct |
4 ms |
5332 KB |
Output is correct |
15 |
Correct |
5 ms |
5332 KB |
Output is correct |
16 |
Correct |
3 ms |
5332 KB |
Output is correct |
17 |
Correct |
4 ms |
5288 KB |
Output is correct |
18 |
Correct |
3 ms |
5268 KB |
Output is correct |
19 |
Correct |
3 ms |
5332 KB |
Output is correct |
20 |
Correct |
3 ms |
5332 KB |
Output is correct |
21 |
Correct |
5 ms |
5332 KB |
Output is correct |
22 |
Correct |
4 ms |
5332 KB |
Output is correct |
23 |
Correct |
5 ms |
5228 KB |
Output is correct |
24 |
Correct |
3 ms |
5296 KB |
Output is correct |
25 |
Correct |
3 ms |
5332 KB |
Output is correct |
26 |
Correct |
5 ms |
5332 KB |
Output is correct |
27 |
Correct |
3 ms |
5332 KB |
Output is correct |
28 |
Correct |
3 ms |
5332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5332 KB |
Output is correct |
2 |
Correct |
482 ms |
84288 KB |
Output is correct |
3 |
Correct |
556 ms |
81264 KB |
Output is correct |
4 |
Correct |
638 ms |
89660 KB |
Output is correct |
5 |
Correct |
507 ms |
82716 KB |
Output is correct |
6 |
Correct |
494 ms |
82600 KB |
Output is correct |
7 |
Correct |
526 ms |
82980 KB |
Output is correct |
8 |
Correct |
411 ms |
83372 KB |
Output is correct |
9 |
Correct |
615 ms |
94744 KB |
Output is correct |
10 |
Correct |
754 ms |
90812 KB |
Output is correct |
11 |
Correct |
527 ms |
84336 KB |
Output is correct |
12 |
Correct |
675 ms |
81088 KB |
Output is correct |
13 |
Correct |
475 ms |
81692 KB |
Output is correct |
14 |
Correct |
445 ms |
81728 KB |
Output is correct |
15 |
Correct |
428 ms |
81772 KB |
Output is correct |
16 |
Correct |
410 ms |
81648 KB |
Output is correct |
17 |
Correct |
405 ms |
82280 KB |
Output is correct |
18 |
Correct |
3 ms |
5332 KB |
Output is correct |
19 |
Correct |
5 ms |
5256 KB |
Output is correct |
20 |
Correct |
3 ms |
5332 KB |
Output is correct |
21 |
Correct |
3 ms |
5332 KB |
Output is correct |
22 |
Correct |
4 ms |
5352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5332 KB |
Output is correct |
2 |
Correct |
482 ms |
84288 KB |
Output is correct |
3 |
Correct |
556 ms |
81264 KB |
Output is correct |
4 |
Correct |
638 ms |
89660 KB |
Output is correct |
5 |
Correct |
507 ms |
82716 KB |
Output is correct |
6 |
Correct |
494 ms |
82600 KB |
Output is correct |
7 |
Correct |
526 ms |
82980 KB |
Output is correct |
8 |
Correct |
411 ms |
83372 KB |
Output is correct |
9 |
Correct |
615 ms |
94744 KB |
Output is correct |
10 |
Correct |
754 ms |
90812 KB |
Output is correct |
11 |
Correct |
527 ms |
84336 KB |
Output is correct |
12 |
Correct |
675 ms |
81088 KB |
Output is correct |
13 |
Correct |
475 ms |
81692 KB |
Output is correct |
14 |
Correct |
445 ms |
81728 KB |
Output is correct |
15 |
Correct |
428 ms |
81772 KB |
Output is correct |
16 |
Correct |
410 ms |
81648 KB |
Output is correct |
17 |
Correct |
405 ms |
82280 KB |
Output is correct |
18 |
Correct |
3 ms |
5332 KB |
Output is correct |
19 |
Correct |
5 ms |
5256 KB |
Output is correct |
20 |
Correct |
3 ms |
5332 KB |
Output is correct |
21 |
Correct |
3 ms |
5332 KB |
Output is correct |
22 |
Correct |
4 ms |
5352 KB |
Output is correct |
23 |
Correct |
4 ms |
5336 KB |
Output is correct |
24 |
Correct |
495 ms |
84280 KB |
Output is correct |
25 |
Correct |
570 ms |
81184 KB |
Output is correct |
26 |
Correct |
564 ms |
92780 KB |
Output is correct |
27 |
Correct |
478 ms |
82632 KB |
Output is correct |
28 |
Correct |
490 ms |
83020 KB |
Output is correct |
29 |
Correct |
521 ms |
82764 KB |
Output is correct |
30 |
Correct |
397 ms |
83376 KB |
Output is correct |
31 |
Correct |
613 ms |
91756 KB |
Output is correct |
32 |
Correct |
753 ms |
90928 KB |
Output is correct |
33 |
Correct |
521 ms |
84252 KB |
Output is correct |
34 |
Correct |
546 ms |
81060 KB |
Output is correct |
35 |
Correct |
3 ms |
5332 KB |
Output is correct |
36 |
Correct |
3 ms |
5332 KB |
Output is correct |
37 |
Correct |
3 ms |
5332 KB |
Output is correct |
38 |
Correct |
3 ms |
5332 KB |
Output is correct |
39 |
Correct |
3 ms |
5332 KB |
Output is correct |
40 |
Correct |
3 ms |
5332 KB |
Output is correct |
41 |
Correct |
3 ms |
5332 KB |
Output is correct |
42 |
Correct |
3 ms |
5332 KB |
Output is correct |
43 |
Correct |
3 ms |
5332 KB |
Output is correct |
44 |
Correct |
3 ms |
5332 KB |
Output is correct |
45 |
Correct |
4 ms |
5332 KB |
Output is correct |
46 |
Correct |
3 ms |
5332 KB |
Output is correct |
47 |
Correct |
3 ms |
5332 KB |
Output is correct |
48 |
Correct |
3 ms |
5332 KB |
Output is correct |
49 |
Correct |
3 ms |
5332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5332 KB |
Output is correct |
2 |
Correct |
1048 ms |
91924 KB |
Output is correct |
3 |
Execution timed out |
4054 ms |
88860 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5332 KB |
Output is correct |
2 |
Correct |
1099 ms |
91260 KB |
Output is correct |
3 |
Execution timed out |
4048 ms |
86736 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5332 KB |
Output is correct |
2 |
Correct |
4 ms |
5332 KB |
Output is correct |
3 |
Correct |
3 ms |
5332 KB |
Output is correct |
4 |
Correct |
4 ms |
5716 KB |
Output is correct |
5 |
Correct |
6 ms |
5716 KB |
Output is correct |
6 |
Correct |
6 ms |
5716 KB |
Output is correct |
7 |
Correct |
4 ms |
5716 KB |
Output is correct |
8 |
Correct |
6 ms |
5716 KB |
Output is correct |
9 |
Correct |
3 ms |
5332 KB |
Output is correct |
10 |
Correct |
3 ms |
5332 KB |
Output is correct |
11 |
Correct |
3 ms |
5332 KB |
Output is correct |
12 |
Correct |
4 ms |
5356 KB |
Output is correct |
13 |
Correct |
3 ms |
5332 KB |
Output is correct |
14 |
Correct |
4 ms |
5332 KB |
Output is correct |
15 |
Correct |
5 ms |
5332 KB |
Output is correct |
16 |
Correct |
3 ms |
5332 KB |
Output is correct |
17 |
Correct |
4 ms |
5288 KB |
Output is correct |
18 |
Correct |
3 ms |
5268 KB |
Output is correct |
19 |
Correct |
3 ms |
5332 KB |
Output is correct |
20 |
Correct |
3 ms |
5332 KB |
Output is correct |
21 |
Correct |
5 ms |
5332 KB |
Output is correct |
22 |
Correct |
4 ms |
5332 KB |
Output is correct |
23 |
Correct |
5 ms |
5228 KB |
Output is correct |
24 |
Correct |
3 ms |
5296 KB |
Output is correct |
25 |
Correct |
3 ms |
5332 KB |
Output is correct |
26 |
Correct |
5 ms |
5332 KB |
Output is correct |
27 |
Correct |
3 ms |
5332 KB |
Output is correct |
28 |
Correct |
3 ms |
5332 KB |
Output is correct |
29 |
Correct |
3 ms |
5332 KB |
Output is correct |
30 |
Correct |
482 ms |
84288 KB |
Output is correct |
31 |
Correct |
556 ms |
81264 KB |
Output is correct |
32 |
Correct |
638 ms |
89660 KB |
Output is correct |
33 |
Correct |
507 ms |
82716 KB |
Output is correct |
34 |
Correct |
494 ms |
82600 KB |
Output is correct |
35 |
Correct |
526 ms |
82980 KB |
Output is correct |
36 |
Correct |
411 ms |
83372 KB |
Output is correct |
37 |
Correct |
615 ms |
94744 KB |
Output is correct |
38 |
Correct |
754 ms |
90812 KB |
Output is correct |
39 |
Correct |
527 ms |
84336 KB |
Output is correct |
40 |
Correct |
675 ms |
81088 KB |
Output is correct |
41 |
Correct |
475 ms |
81692 KB |
Output is correct |
42 |
Correct |
445 ms |
81728 KB |
Output is correct |
43 |
Correct |
428 ms |
81772 KB |
Output is correct |
44 |
Correct |
410 ms |
81648 KB |
Output is correct |
45 |
Correct |
405 ms |
82280 KB |
Output is correct |
46 |
Correct |
3 ms |
5332 KB |
Output is correct |
47 |
Correct |
5 ms |
5256 KB |
Output is correct |
48 |
Correct |
3 ms |
5332 KB |
Output is correct |
49 |
Correct |
3 ms |
5332 KB |
Output is correct |
50 |
Correct |
4 ms |
5352 KB |
Output is correct |
51 |
Correct |
4 ms |
5336 KB |
Output is correct |
52 |
Correct |
495 ms |
84280 KB |
Output is correct |
53 |
Correct |
570 ms |
81184 KB |
Output is correct |
54 |
Correct |
564 ms |
92780 KB |
Output is correct |
55 |
Correct |
478 ms |
82632 KB |
Output is correct |
56 |
Correct |
490 ms |
83020 KB |
Output is correct |
57 |
Correct |
521 ms |
82764 KB |
Output is correct |
58 |
Correct |
397 ms |
83376 KB |
Output is correct |
59 |
Correct |
613 ms |
91756 KB |
Output is correct |
60 |
Correct |
753 ms |
90928 KB |
Output is correct |
61 |
Correct |
521 ms |
84252 KB |
Output is correct |
62 |
Correct |
546 ms |
81060 KB |
Output is correct |
63 |
Correct |
3 ms |
5332 KB |
Output is correct |
64 |
Correct |
3 ms |
5332 KB |
Output is correct |
65 |
Correct |
3 ms |
5332 KB |
Output is correct |
66 |
Correct |
3 ms |
5332 KB |
Output is correct |
67 |
Correct |
3 ms |
5332 KB |
Output is correct |
68 |
Correct |
3 ms |
5332 KB |
Output is correct |
69 |
Correct |
3 ms |
5332 KB |
Output is correct |
70 |
Correct |
3 ms |
5332 KB |
Output is correct |
71 |
Correct |
3 ms |
5332 KB |
Output is correct |
72 |
Correct |
3 ms |
5332 KB |
Output is correct |
73 |
Correct |
4 ms |
5332 KB |
Output is correct |
74 |
Correct |
3 ms |
5332 KB |
Output is correct |
75 |
Correct |
3 ms |
5332 KB |
Output is correct |
76 |
Correct |
3 ms |
5332 KB |
Output is correct |
77 |
Correct |
3 ms |
5332 KB |
Output is correct |
78 |
Correct |
3 ms |
5332 KB |
Output is correct |
79 |
Correct |
1048 ms |
91924 KB |
Output is correct |
80 |
Execution timed out |
4054 ms |
88860 KB |
Time limit exceeded |
81 |
Halted |
0 ms |
0 KB |
- |