#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <random>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
typedef long long llong;
const int MAXN = 200000 + 10;
const int MAXD = 50;
const int INF = 1e9;
int n, MOD, q;
struct SegmentTree
{
int tree[4*MAXN];
void build(int l, int r, int node, int vals[])
{
if (l == r)
{
tree[node] = vals[l];
return;
}
int mid = (l + r) / 2;
build(l, mid, 2*node, vals);
build(mid + 1, r, 2*node + 1, vals);
tree[node] = 1;
}
void push(int node, int l, int r)
{
if (tree[node] == 1)
{
return;
}
if (l < r)
{
tree[2*node] = (1LL * tree[2*node] * tree[node]) % MOD;
tree[2*node + 1] = (1LL * tree[2*node + 1] * tree[node]) % MOD;
tree[node] = 1;
}
}
void update(int l, int r, int node, int queryL, int queryR, int val)
{
// std::cout << "update: " << l << ' ' << r << ' ' << node << '\n' << std::flush;
push(node, l, r);
if (queryL <= l && r <= queryR)
{
tree[node] = (1LL * tree[node] * val) % MOD;
push(node, l, r);
return;
}
int mid = (l + r) / 2;
if (queryL <= mid) update(l, mid, 2*node, queryL, queryR, val);
if (mid + 1 <= queryR) update(mid + 1, r, 2*node + 1, queryL, queryR, val);
}
int query(int l, int r, int node, int pos)
{
push(node, l, r);
if (l == r)
{
return tree[node];
}
int mid = (l + r) / 2;
if (pos <= mid) return query(l, mid, 2*node, pos);
else return query(mid + 1, r, 2*node + 1, pos);
}
void build(int vals[])
{
build(0, n - 1, 1, vals);
}
void update(int l, int r, int val)
{
update(0, n - 1, 1, l, r, val);
}
int query(int pos)
{
return query(0, n - 1, 1, pos);
}
};
int h[MAXN];
int p[MAXN];
int d[MAXN];
int maxD[MAXN];
int vals[MAXN];
SegmentTree tree;
std::queue <int> queue;
std::vector <int> tour;
std::vector <int> byLevel[MAXN];
std::vector <int> g[MAXN];
int in[MAXN], out[MAXN];
int tour2IN[MAXN];
int tourIN[MAXN];
int timer;
int leftIN[MAXN][MAXD];
int rightIN[MAXN][MAXD];
bool inSubtree(int x, int y)
{
return in[y] <= in[x] && in[x] <= out[y];
}
void dfs(int node, int par)
{
p[node] = par;
d[node] = d[par] + 1;
in[node] = ++timer;
maxD[node] = d[node];
for (const int &u : g[node])
{
if (par == u)
{
continue;
}
dfs(u, node);
maxD[node] = std::max(maxD[node], maxD[u]);
}
out[node] = timer;
}
void dfsIN(int node)
{
leftIN[node][0] = tour2IN[node];
rightIN[node][0] = tour2IN[node];
for (const int &u : g[node])
{
if (u == p[node])
{
continue;
}
dfsIN(u);
}
for (int i = 1 ; i < MAXD ; ++i)
{
leftIN[node][i] = INF;
rightIN[node][i] = -INF;
for (const int &u : g[node])
{
if (u == p[node])
{
continue;
}
leftIN[node][i] = std::min(leftIN[node][i], leftIN[u][i - 1]);
rightIN[node][i] = std::max(rightIN[node][i], rightIN[u][i - 1]);
}
}
}
void updateLevel(int node, int depth, int val)
{
int l = leftIN[node][depth - d[node]];
int r = rightIN[node][depth - d[node]];
if (l <= r)
{
tree.update(tourIN[byLevel[depth][l]], tourIN[byLevel[depth][r]], val);
}
}
int recurse(int node, int depth, int w)
{
if (node == 0)
{
return 0;
}
if (depth == 0)
{
tree.update(tourIN[node], tourIN[node], w);
return d[node];
}
int res = std::max(recurse(p[node], depth - 1, w) + 1, d[node]);
for (int i = res ; i <= std::min(maxD[node], d[node] + depth) ; ++i)
{
updateLevel(node, i, w);
}
return d[node] + depth;
}
void solve()
{
dfs(1, 0);
queue.push(1);
while (queue.size())
{
int top = queue.front();
queue.pop();
byLevel[d[top]].push_back(top);
tour.push_back(top);
tourIN[top] = tour.size() - 1;
tour2IN[top] = byLevel[d[top]].size() - 1;
// std::cout << "HERE: " << top << ' ' << d[top] << ' ' << tourIN[top] << '\n';
for (const int &u : g[top])
{
if (u == p[top])
{
continue;
}
queue.push(u);
}
}
dfsIN(1);
for (int i = 1 ; i <= n ; ++i)
{
vals[tourIN[i]] = h[i];
}
tree.build(vals);
std::cin >> q;
for (int i = 1 ; i <= q ; ++i)
{
int qType;
std::cin >> qType;
if (qType == 2)
{
int node;
std::cin >> node;
std::cout << tree.query(tourIN[node]) << '\n' << std::flush;
continue;
}
assert(qType == 1);
int node, depth, w;
std::cin >> node >> depth >> w;
if (w == 1) continue;
recurse(node, depth, w);
}
}
void input()
{
std::cin >> n >> MOD;
for (int i = 2 ; i <= n ; ++i)
{
int u, v;
std::cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> h[i];
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
/*
4 7
1 2
2 3
3 4
1
1
1
1
5
1 2 1 2
2 1
2 2
2 3
2 4
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9776 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
8 ms |
10324 KB |
Output is correct |
5 |
Correct |
7 ms |
10196 KB |
Output is correct |
6 |
Correct |
7 ms |
10260 KB |
Output is correct |
7 |
Correct |
7 ms |
10196 KB |
Output is correct |
8 |
Correct |
6 ms |
10196 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
6 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9800 KB |
Output is correct |
18 |
Correct |
5 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9768 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9692 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
7 ms |
9760 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
5 ms |
9684 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
7 ms |
9804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
735 ms |
108996 KB |
Output is correct |
3 |
Correct |
647 ms |
105800 KB |
Output is correct |
4 |
Correct |
712 ms |
120164 KB |
Output is correct |
5 |
Correct |
643 ms |
107500 KB |
Output is correct |
6 |
Correct |
650 ms |
107148 KB |
Output is correct |
7 |
Correct |
645 ms |
107572 KB |
Output is correct |
8 |
Correct |
765 ms |
108512 KB |
Output is correct |
9 |
Correct |
891 ms |
129004 KB |
Output is correct |
10 |
Correct |
757 ms |
124024 KB |
Output is correct |
11 |
Correct |
775 ms |
108908 KB |
Output is correct |
12 |
Correct |
634 ms |
105844 KB |
Output is correct |
13 |
Correct |
823 ms |
106812 KB |
Output is correct |
14 |
Correct |
738 ms |
106944 KB |
Output is correct |
15 |
Correct |
699 ms |
106424 KB |
Output is correct |
16 |
Correct |
699 ms |
106428 KB |
Output is correct |
17 |
Correct |
699 ms |
106636 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
735 ms |
108996 KB |
Output is correct |
3 |
Correct |
647 ms |
105800 KB |
Output is correct |
4 |
Correct |
712 ms |
120164 KB |
Output is correct |
5 |
Correct |
643 ms |
107500 KB |
Output is correct |
6 |
Correct |
650 ms |
107148 KB |
Output is correct |
7 |
Correct |
645 ms |
107572 KB |
Output is correct |
8 |
Correct |
765 ms |
108512 KB |
Output is correct |
9 |
Correct |
891 ms |
129004 KB |
Output is correct |
10 |
Correct |
757 ms |
124024 KB |
Output is correct |
11 |
Correct |
775 ms |
108908 KB |
Output is correct |
12 |
Correct |
634 ms |
105844 KB |
Output is correct |
13 |
Correct |
823 ms |
106812 KB |
Output is correct |
14 |
Correct |
738 ms |
106944 KB |
Output is correct |
15 |
Correct |
699 ms |
106424 KB |
Output is correct |
16 |
Correct |
699 ms |
106428 KB |
Output is correct |
17 |
Correct |
699 ms |
106636 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
820 ms |
108872 KB |
Output is correct |
25 |
Correct |
789 ms |
105800 KB |
Output is correct |
26 |
Correct |
803 ms |
126424 KB |
Output is correct |
27 |
Correct |
731 ms |
107332 KB |
Output is correct |
28 |
Correct |
679 ms |
107272 KB |
Output is correct |
29 |
Correct |
686 ms |
107340 KB |
Output is correct |
30 |
Correct |
819 ms |
108532 KB |
Output is correct |
31 |
Correct |
813 ms |
122480 KB |
Output is correct |
32 |
Correct |
961 ms |
124076 KB |
Output is correct |
33 |
Correct |
756 ms |
108916 KB |
Output is correct |
34 |
Correct |
798 ms |
105764 KB |
Output is correct |
35 |
Correct |
5 ms |
9684 KB |
Output is correct |
36 |
Correct |
5 ms |
9684 KB |
Output is correct |
37 |
Correct |
5 ms |
9684 KB |
Output is correct |
38 |
Correct |
6 ms |
9684 KB |
Output is correct |
39 |
Correct |
5 ms |
9684 KB |
Output is correct |
40 |
Correct |
5 ms |
9684 KB |
Output is correct |
41 |
Correct |
5 ms |
9684 KB |
Output is correct |
42 |
Correct |
5 ms |
9788 KB |
Output is correct |
43 |
Correct |
5 ms |
9684 KB |
Output is correct |
44 |
Correct |
5 ms |
9740 KB |
Output is correct |
45 |
Correct |
5 ms |
9684 KB |
Output is correct |
46 |
Correct |
5 ms |
9684 KB |
Output is correct |
47 |
Correct |
5 ms |
9684 KB |
Output is correct |
48 |
Correct |
5 ms |
9724 KB |
Output is correct |
49 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
1554 ms |
126008 KB |
Output is correct |
3 |
Execution timed out |
4064 ms |
120128 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
1590 ms |
121584 KB |
Output is correct |
3 |
Execution timed out |
4062 ms |
115256 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9776 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
8 ms |
10324 KB |
Output is correct |
5 |
Correct |
7 ms |
10196 KB |
Output is correct |
6 |
Correct |
7 ms |
10260 KB |
Output is correct |
7 |
Correct |
7 ms |
10196 KB |
Output is correct |
8 |
Correct |
6 ms |
10196 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
6 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9800 KB |
Output is correct |
18 |
Correct |
5 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9768 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9692 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
7 ms |
9760 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
5 ms |
9684 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
7 ms |
9804 KB |
Output is correct |
29 |
Correct |
4 ms |
9684 KB |
Output is correct |
30 |
Correct |
735 ms |
108996 KB |
Output is correct |
31 |
Correct |
647 ms |
105800 KB |
Output is correct |
32 |
Correct |
712 ms |
120164 KB |
Output is correct |
33 |
Correct |
643 ms |
107500 KB |
Output is correct |
34 |
Correct |
650 ms |
107148 KB |
Output is correct |
35 |
Correct |
645 ms |
107572 KB |
Output is correct |
36 |
Correct |
765 ms |
108512 KB |
Output is correct |
37 |
Correct |
891 ms |
129004 KB |
Output is correct |
38 |
Correct |
757 ms |
124024 KB |
Output is correct |
39 |
Correct |
775 ms |
108908 KB |
Output is correct |
40 |
Correct |
634 ms |
105844 KB |
Output is correct |
41 |
Correct |
823 ms |
106812 KB |
Output is correct |
42 |
Correct |
738 ms |
106944 KB |
Output is correct |
43 |
Correct |
699 ms |
106424 KB |
Output is correct |
44 |
Correct |
699 ms |
106428 KB |
Output is correct |
45 |
Correct |
699 ms |
106636 KB |
Output is correct |
46 |
Correct |
5 ms |
9684 KB |
Output is correct |
47 |
Correct |
5 ms |
9684 KB |
Output is correct |
48 |
Correct |
5 ms |
9684 KB |
Output is correct |
49 |
Correct |
5 ms |
9684 KB |
Output is correct |
50 |
Correct |
5 ms |
9684 KB |
Output is correct |
51 |
Correct |
5 ms |
9684 KB |
Output is correct |
52 |
Correct |
820 ms |
108872 KB |
Output is correct |
53 |
Correct |
789 ms |
105800 KB |
Output is correct |
54 |
Correct |
803 ms |
126424 KB |
Output is correct |
55 |
Correct |
731 ms |
107332 KB |
Output is correct |
56 |
Correct |
679 ms |
107272 KB |
Output is correct |
57 |
Correct |
686 ms |
107340 KB |
Output is correct |
58 |
Correct |
819 ms |
108532 KB |
Output is correct |
59 |
Correct |
813 ms |
122480 KB |
Output is correct |
60 |
Correct |
961 ms |
124076 KB |
Output is correct |
61 |
Correct |
756 ms |
108916 KB |
Output is correct |
62 |
Correct |
798 ms |
105764 KB |
Output is correct |
63 |
Correct |
5 ms |
9684 KB |
Output is correct |
64 |
Correct |
5 ms |
9684 KB |
Output is correct |
65 |
Correct |
5 ms |
9684 KB |
Output is correct |
66 |
Correct |
6 ms |
9684 KB |
Output is correct |
67 |
Correct |
5 ms |
9684 KB |
Output is correct |
68 |
Correct |
5 ms |
9684 KB |
Output is correct |
69 |
Correct |
5 ms |
9684 KB |
Output is correct |
70 |
Correct |
5 ms |
9788 KB |
Output is correct |
71 |
Correct |
5 ms |
9684 KB |
Output is correct |
72 |
Correct |
5 ms |
9740 KB |
Output is correct |
73 |
Correct |
5 ms |
9684 KB |
Output is correct |
74 |
Correct |
5 ms |
9684 KB |
Output is correct |
75 |
Correct |
5 ms |
9684 KB |
Output is correct |
76 |
Correct |
5 ms |
9724 KB |
Output is correct |
77 |
Correct |
5 ms |
9684 KB |
Output is correct |
78 |
Correct |
5 ms |
9684 KB |
Output is correct |
79 |
Correct |
1554 ms |
126008 KB |
Output is correct |
80 |
Execution timed out |
4064 ms |
120128 KB |
Time limit exceeded |
81 |
Halted |
0 ms |
0 KB |
- |