#include <bits/stdc++.h>
#define int long long
using namespace std;
struct Upd {
int i, dist, prod;
};
const int M = 2e5 + 42, N = 2 * M, T = 19, INF = 1e18 + 42, MOD = 1e9 + 7;
vector<int> val, adj[M];
int n, l, q, id = 0, h[M], pere[M], prof[M],
deb[M], fin[M], inv[M], ln2[N], mini[T][N];
void dfs(int i, int pre, int depth = 0) {
int f = id++;
inv[f] = i;
deb[i] = (int)val.size();
val.push_back(f);
pere[i] = pre;
prof[i] = depth;
for(int j : adj[i])
if(j != pre) {
dfs(j, i, depth+1);
val.push_back(f);
}
fin[i] = (int)val.size();
}
inline int dist(int a, int b) {
if(deb[a] > deb[b])
swap(a, b);
int d = deb[a], f = deb[b]+1, len = ln2[f - d],
lca = inv[min(mini[len][d], mini[len][f - (1 << len)])];
return prof[a] + prof[b] - 2 * prof[lca];
}
int ch[M][42];
vector<Upd> upd;
void update() {
for(int i = 0; i < M; i++)
for(int j = 0; j < 42; j++)
ch[i][j] = 1;
for(Upd u : upd) {
int i = u.i, d = u.dist;
do {
if(i == 0 || d < 2) {
for(int j = 0; j <= d; j++)
ch[i][j] = ch[i][j] * u.prod % l;
} else {
ch[i][d] = ch[i][d] * u.prod % l;
ch[i][d-1] = ch[i][d-1] * u.prod % l;
}
d--;
i = pere[i];
} while(i != -1 && d >= 0);
}
for(int i = 0; i < n; i++) {
int j = i, d = 0;
while(j >= 0 && d < 41) {
h[i] = h[i] * ch[j][d] % l;
j = pere[j];
d++;
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for(int i = 0; (1 << i) < N; i++)
ln2[1 << i] = i;
for(int i = 1; i < N; i++)
ln2[i] = max(ln2[i], ln2[i-1]);
cin >> n >> l;
for(int i = 0; i < n-1; i++) {
int a, b; cin >> a >> b;
a--, b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(0, -1);
int sz = (int)val.size();
for(int i = 0; i < sz; i++)
mini[0][i] = val[i];
for(int i = 1; i < 19; i++)
for(int d = 0, m = (1 << (i-1)); m < sz; d++, m++)
mini[i][d] = min(mini[i-1][d], mini[i-1][m]);
for(int i = 0; i < n; i++)
cin >> h[i];
cin >> q;
for(int i = 0; i < q; i++) {
int typ; cin >> typ;
if(typ == 1) {
int x, dist, prod;
cin >> x >> dist >> prod;
upd.push_back({x-1, dist, prod});
if(500 < (int)upd.size()) {
update();
upd.clear();
}
} else {
int x;
cin >> x;
x--;
int ans = h[x];
for(Upd u : upd) {
if(dist(u.i, x) <= u.dist)
ans = ans * u.prod % l;
}
cout << ans << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
6 ms |
8148 KB |
Output is correct |
3 |
Correct |
5 ms |
8148 KB |
Output is correct |
4 |
Correct |
36 ms |
74312 KB |
Output is correct |
5 |
Correct |
7 ms |
8532 KB |
Output is correct |
6 |
Correct |
35 ms |
74356 KB |
Output is correct |
7 |
Correct |
37 ms |
74324 KB |
Output is correct |
8 |
Correct |
7 ms |
8532 KB |
Output is correct |
9 |
Correct |
34 ms |
73992 KB |
Output is correct |
10 |
Correct |
7 ms |
8148 KB |
Output is correct |
11 |
Correct |
39 ms |
73912 KB |
Output is correct |
12 |
Correct |
7 ms |
8276 KB |
Output is correct |
13 |
Correct |
7 ms |
8276 KB |
Output is correct |
14 |
Correct |
34 ms |
73952 KB |
Output is correct |
15 |
Correct |
34 ms |
73916 KB |
Output is correct |
16 |
Correct |
7 ms |
8160 KB |
Output is correct |
17 |
Correct |
6 ms |
8276 KB |
Output is correct |
18 |
Correct |
7 ms |
8276 KB |
Output is correct |
19 |
Correct |
6 ms |
8252 KB |
Output is correct |
20 |
Correct |
36 ms |
73896 KB |
Output is correct |
21 |
Correct |
7 ms |
8276 KB |
Output is correct |
22 |
Correct |
34 ms |
73984 KB |
Output is correct |
23 |
Correct |
35 ms |
73932 KB |
Output is correct |
24 |
Correct |
6 ms |
8148 KB |
Output is correct |
25 |
Correct |
36 ms |
73996 KB |
Output is correct |
26 |
Correct |
34 ms |
73984 KB |
Output is correct |
27 |
Correct |
7 ms |
8288 KB |
Output is correct |
28 |
Correct |
7 ms |
8276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8148 KB |
Output is correct |
2 |
Execution timed out |
4059 ms |
152608 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8148 KB |
Output is correct |
2 |
Execution timed out |
4059 ms |
152608 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8148 KB |
Output is correct |
2 |
Execution timed out |
4069 ms |
162824 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8148 KB |
Output is correct |
2 |
Execution timed out |
4075 ms |
159492 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
6 ms |
8148 KB |
Output is correct |
3 |
Correct |
5 ms |
8148 KB |
Output is correct |
4 |
Correct |
36 ms |
74312 KB |
Output is correct |
5 |
Correct |
7 ms |
8532 KB |
Output is correct |
6 |
Correct |
35 ms |
74356 KB |
Output is correct |
7 |
Correct |
37 ms |
74324 KB |
Output is correct |
8 |
Correct |
7 ms |
8532 KB |
Output is correct |
9 |
Correct |
34 ms |
73992 KB |
Output is correct |
10 |
Correct |
7 ms |
8148 KB |
Output is correct |
11 |
Correct |
39 ms |
73912 KB |
Output is correct |
12 |
Correct |
7 ms |
8276 KB |
Output is correct |
13 |
Correct |
7 ms |
8276 KB |
Output is correct |
14 |
Correct |
34 ms |
73952 KB |
Output is correct |
15 |
Correct |
34 ms |
73916 KB |
Output is correct |
16 |
Correct |
7 ms |
8160 KB |
Output is correct |
17 |
Correct |
6 ms |
8276 KB |
Output is correct |
18 |
Correct |
7 ms |
8276 KB |
Output is correct |
19 |
Correct |
6 ms |
8252 KB |
Output is correct |
20 |
Correct |
36 ms |
73896 KB |
Output is correct |
21 |
Correct |
7 ms |
8276 KB |
Output is correct |
22 |
Correct |
34 ms |
73984 KB |
Output is correct |
23 |
Correct |
35 ms |
73932 KB |
Output is correct |
24 |
Correct |
6 ms |
8148 KB |
Output is correct |
25 |
Correct |
36 ms |
73996 KB |
Output is correct |
26 |
Correct |
34 ms |
73984 KB |
Output is correct |
27 |
Correct |
7 ms |
8288 KB |
Output is correct |
28 |
Correct |
7 ms |
8276 KB |
Output is correct |
29 |
Correct |
6 ms |
8148 KB |
Output is correct |
30 |
Execution timed out |
4059 ms |
152608 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |