#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5 + 5, maxN = 8e5 + 5, m = 43;
int n, M;
vector<int> adj[maxn];
int newid[maxn], d[maxn], fa[maxn], a[maxn];
bool vis[maxn];
pair<int,int> range[maxn][m];
void dfs(int u) {
range[u][0] = {newid[u], newid[u]};
if (adj[u].size() == 0) return;
for (int v:adj[u]) dfs(v);
int sz = adj[u].size();
int l = 0, r = sz - 1;
for (int i=1;i<m;i++) {
while (l < sz && range[adj[u][l]][i-1].first==-1) l++;
while (r >= 0 && range[adj[u][r]][i-1].first==-1) r--;
if (l>r) break;
range[u][i] = {range[adj[u][l]][i-1].first, range[adj[u][r]][i-1].second};
}
}
vector<int> seg[maxN];
void build(int id, int l, int r) {
seg[id] = {1};
if (l==r) {
seg[id] = {a[l]};
return;
}
int mid = (l+r)>>1;
build(id<<1, l, mid); build((id<<1)+1, mid+1, r);
}
void update(int id, int l, int r, int findl, int findr, int val) {
if (r<findl || findr<l) return;
if (findl<=l && r<=findr) {
seg[id].push_back(val);
return;
}
int mid = (l+r)>>1;
update((id<<1), l, mid, findl, findr, val);
update((id<<1)+1, mid+1, r, findl, findr, val);
}
ll qry(int id, int l, int r, int target) {
if (r<target || target<l) return 1;
int val = 1;
for (int i:seg[id]) {
val *= i;
if (val > M) val %= M;
if (val == 0) break;
}
seg[id] = {val};
if (l==r) return val;
int mid = (l+r)>>1;
return (val * qry(id<<1, l, mid, target) % M) * qry((id<<1)+1, mid+1, r, target) % M;
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> M;
for (int i=1;i<=n-1;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int A[n+1];
for (int i=1;i<=n;i++) cin >> A[i];
queue<int> Q;
Q.push(1);
vis[1] = true, d[1] = 0, fa[1] = -1;
int cnter = 0;
while (Q.size() > 0) {
int u = Q.front();
cnter++;
newid[u] = cnter;
for (int v:adj[u]) if (!vis[v]) {
vis[v] = true, fa[v] = u, d[v] = d[u] + 1;
adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
Q.push(v);
}
Q.pop();
}
for (int i=1;i<=n;i++) for (int j=0;j<m;j++) range[i][j] = {-1, -1};
dfs(1);
for (int i=1;i<=n;i++) a[newid[i]] = A[i];
build(1, 1, n);
int q;
cin >> q;
while (q--) {
int t;
cin >> t;
if (t==1) {
int x, D, w;
cin >> x >> D >> w;
int v = x;
for (int dep = d[x]+D; dep>=d[x]-D && dep >= 0; dep--) {
if (fa[v]!=-1 && dep-d[v] + d[x]-d[v] + 2 <= D) v = fa[v];
update(1, 1, n, range[v][dep-d[v]].first, range[v][dep-d[v]].second, w);
}
} else if (t==2) {
int x;
cin >> x;
cout << qry(1, 1, n, newid[x]) << '\n';
}
// for (int i=1;i<=n;i++) cout << qry(1, 1, n, newid[i]) << " "; cout << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23896 KB |
Output is correct |
2 |
Correct |
9 ms |
23968 KB |
Output is correct |
3 |
Correct |
9 ms |
23900 KB |
Output is correct |
4 |
Incorrect |
12 ms |
24412 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Incorrect |
440 ms |
125876 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Incorrect |
440 ms |
125876 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
1212 ms |
137108 KB |
Output is correct |
3 |
Execution timed out |
4050 ms |
235728 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
1416 ms |
140872 KB |
Output is correct |
3 |
Execution timed out |
4026 ms |
222564 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23896 KB |
Output is correct |
2 |
Correct |
9 ms |
23968 KB |
Output is correct |
3 |
Correct |
9 ms |
23900 KB |
Output is correct |
4 |
Incorrect |
12 ms |
24412 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |