#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(1000 < (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 |
6 ms |
8148 KB |
Output is correct |
2 |
Correct |
6 ms |
8148 KB |
Output is correct |
3 |
Correct |
7 ms |
8172 KB |
Output is correct |
4 |
Correct |
8 ms |
8652 KB |
Output is correct |
5 |
Correct |
9 ms |
8604 KB |
Output is correct |
6 |
Correct |
8 ms |
8532 KB |
Output is correct |
7 |
Correct |
9 ms |
8564 KB |
Output is correct |
8 |
Correct |
8 ms |
8532 KB |
Output is correct |
9 |
Correct |
7 ms |
8276 KB |
Output is correct |
10 |
Correct |
8 ms |
8176 KB |
Output is correct |
11 |
Correct |
7 ms |
8276 KB |
Output is correct |
12 |
Correct |
8 ms |
8276 KB |
Output is correct |
13 |
Correct |
7 ms |
8276 KB |
Output is correct |
14 |
Correct |
7 ms |
8264 KB |
Output is correct |
15 |
Correct |
8 ms |
8180 KB |
Output is correct |
16 |
Correct |
7 ms |
8284 KB |
Output is correct |
17 |
Correct |
7 ms |
8172 KB |
Output is correct |
18 |
Correct |
7 ms |
8180 KB |
Output is correct |
19 |
Correct |
7 ms |
8148 KB |
Output is correct |
20 |
Correct |
7 ms |
8268 KB |
Output is correct |
21 |
Correct |
7 ms |
8180 KB |
Output is correct |
22 |
Correct |
7 ms |
8280 KB |
Output is correct |
23 |
Correct |
7 ms |
8276 KB |
Output is correct |
24 |
Correct |
7 ms |
8256 KB |
Output is correct |
25 |
Correct |
7 ms |
8176 KB |
Output is correct |
26 |
Correct |
7 ms |
8276 KB |
Output is correct |
27 |
Correct |
7 ms |
8172 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 |
4024 ms |
158996 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 |
4024 ms |
158996 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 |
4053 ms |
168952 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 |
4054 ms |
166024 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 |
Correct |
6 ms |
8148 KB |
Output is correct |
3 |
Correct |
7 ms |
8172 KB |
Output is correct |
4 |
Correct |
8 ms |
8652 KB |
Output is correct |
5 |
Correct |
9 ms |
8604 KB |
Output is correct |
6 |
Correct |
8 ms |
8532 KB |
Output is correct |
7 |
Correct |
9 ms |
8564 KB |
Output is correct |
8 |
Correct |
8 ms |
8532 KB |
Output is correct |
9 |
Correct |
7 ms |
8276 KB |
Output is correct |
10 |
Correct |
8 ms |
8176 KB |
Output is correct |
11 |
Correct |
7 ms |
8276 KB |
Output is correct |
12 |
Correct |
8 ms |
8276 KB |
Output is correct |
13 |
Correct |
7 ms |
8276 KB |
Output is correct |
14 |
Correct |
7 ms |
8264 KB |
Output is correct |
15 |
Correct |
8 ms |
8180 KB |
Output is correct |
16 |
Correct |
7 ms |
8284 KB |
Output is correct |
17 |
Correct |
7 ms |
8172 KB |
Output is correct |
18 |
Correct |
7 ms |
8180 KB |
Output is correct |
19 |
Correct |
7 ms |
8148 KB |
Output is correct |
20 |
Correct |
7 ms |
8268 KB |
Output is correct |
21 |
Correct |
7 ms |
8180 KB |
Output is correct |
22 |
Correct |
7 ms |
8280 KB |
Output is correct |
23 |
Correct |
7 ms |
8276 KB |
Output is correct |
24 |
Correct |
7 ms |
8256 KB |
Output is correct |
25 |
Correct |
7 ms |
8176 KB |
Output is correct |
26 |
Correct |
7 ms |
8276 KB |
Output is correct |
27 |
Correct |
7 ms |
8172 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 |
4024 ms |
158996 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |