#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mxn=2e5+5,sq=448;
int n,M,q,h[mxn],st[mxn],fn[mxn],cnt,dis[mxn],par[mxn];
vector<ll>v[mxn],d[mxn];
struct segment{
vector<ll>val;
void build(int id){
int i=1;
while(i<=id*2)i*=2;
while(i--)
val.push_back(1);
}
void add(int id,int L,int R,int l,int r,ll w){
if(L==l && R==r){
val[id]=(val[id]*w)%M;
return;
}
int mid=(L+R)/2;
if(l<mid)
add(id*2,L,mid,l,min(r,mid),w);
if(r>mid)
add(id*2+1,mid,R,max(l,mid),r,w);
}
ll get(int id,int L,int R,int l){
if(L+1==R)
return val[id];
int mid=(L+R)/2;
if(l<mid)
return (get(id*2,L,mid,l)*val[id])%M;
else
return (get(id*2+1,mid,R,l)*val[id])%M;
}
}seg[mxn];
void dfs(int z){
st[z]=++cnt;
d[dis[z]].push_back(st[z]);
for(auto i:v[z]){
if(i!=par[z]){
par[i]=z;
dis[i]=dis[z]+1;
dfs(i);
}
}
fn[z]=cnt;
}
void add(int x,int L,int R,ll w){
if(d[x].size()==0)return;
int l=lower_bound(d[x].begin(),d[x].end(),L)-d[x].begin();
int r=upper_bound(d[x].begin(),d[x].end(),R)-d[x].begin()-1;
r=min((int)d[x].size()-1,r);
if(l<r+1)
seg[x].add(1,0,d[x].size(),l,r+1,w);
}
void up(int x,int dd,int w){
for(;dd>=1 && x!=1;dd--){
int z=dis[x]+dd,zz=dis[x]+dd-1;
add(z,st[x],fn[x],w);
add(zz,st[x],fn[x],w);
x=par[x];
}
for(;dd>=0;dd--){
add(dis[x]+dd,st[x],fn[x],w);
}
}
ll get(int z,int x){
int l=lower_bound(d[z].begin(),d[z].end(),st[x])-d[z].begin();
return seg[z].get(1,0,d[z].size(),l);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>M;
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for(int i=1;i<=n;i++)
cin>>h[i];
dfs(1);
for(int i=0;i<=n;i++){
seg[i].build(d[i].size());
}
for(int i=1;i<=n;i++){
int l=lower_bound(d[dis[i]].begin(),d[dis[i]].end(),st[i])-d[dis[i]].begin();
seg[dis[i]].add(1,0,d[dis[i]].size(),l,l+1,h[i]);
}
cin>>q;
while(q--){
int ty,x,dd,w;
cin>>ty;
if(ty==1){
cin>>x>>dd>>w;
up(x,dd,w);
}
else{
cin>>x;
cout<<get(dis[x],x)<<'\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
5 ms |
14940 KB |
Output is correct |
5 |
Correct |
5 ms |
14940 KB |
Output is correct |
6 |
Correct |
5 ms |
14936 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14936 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14936 KB |
Output is correct |
12 |
Correct |
4 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14936 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14936 KB |
Output is correct |
17 |
Correct |
4 ms |
14936 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14936 KB |
Output is correct |
21 |
Correct |
4 ms |
15024 KB |
Output is correct |
22 |
Correct |
3 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14936 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
4 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
290 ms |
42204 KB |
Output is correct |
3 |
Correct |
431 ms |
51028 KB |
Output is correct |
4 |
Correct |
324 ms |
61196 KB |
Output is correct |
5 |
Correct |
335 ms |
50828 KB |
Output is correct |
6 |
Correct |
431 ms |
50236 KB |
Output is correct |
7 |
Correct |
411 ms |
50228 KB |
Output is correct |
8 |
Correct |
382 ms |
50532 KB |
Output is correct |
9 |
Correct |
322 ms |
67156 KB |
Output is correct |
10 |
Correct |
364 ms |
66284 KB |
Output is correct |
11 |
Correct |
310 ms |
50260 KB |
Output is correct |
12 |
Correct |
429 ms |
51028 KB |
Output is correct |
13 |
Correct |
289 ms |
50636 KB |
Output is correct |
14 |
Correct |
334 ms |
51504 KB |
Output is correct |
15 |
Correct |
357 ms |
50796 KB |
Output is correct |
16 |
Correct |
375 ms |
50864 KB |
Output is correct |
17 |
Correct |
413 ms |
50964 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
4 ms |
14944 KB |
Output is correct |
20 |
Correct |
5 ms |
14936 KB |
Output is correct |
21 |
Correct |
5 ms |
14936 KB |
Output is correct |
22 |
Correct |
6 ms |
15004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
290 ms |
42204 KB |
Output is correct |
3 |
Correct |
431 ms |
51028 KB |
Output is correct |
4 |
Correct |
324 ms |
61196 KB |
Output is correct |
5 |
Correct |
335 ms |
50828 KB |
Output is correct |
6 |
Correct |
431 ms |
50236 KB |
Output is correct |
7 |
Correct |
411 ms |
50228 KB |
Output is correct |
8 |
Correct |
382 ms |
50532 KB |
Output is correct |
9 |
Correct |
322 ms |
67156 KB |
Output is correct |
10 |
Correct |
364 ms |
66284 KB |
Output is correct |
11 |
Correct |
310 ms |
50260 KB |
Output is correct |
12 |
Correct |
429 ms |
51028 KB |
Output is correct |
13 |
Correct |
289 ms |
50636 KB |
Output is correct |
14 |
Correct |
334 ms |
51504 KB |
Output is correct |
15 |
Correct |
357 ms |
50796 KB |
Output is correct |
16 |
Correct |
375 ms |
50864 KB |
Output is correct |
17 |
Correct |
413 ms |
50964 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
4 ms |
14944 KB |
Output is correct |
20 |
Correct |
5 ms |
14936 KB |
Output is correct |
21 |
Correct |
5 ms |
14936 KB |
Output is correct |
22 |
Correct |
6 ms |
15004 KB |
Output is correct |
23 |
Correct |
7 ms |
14940 KB |
Output is correct |
24 |
Correct |
323 ms |
50220 KB |
Output is correct |
25 |
Correct |
577 ms |
51220 KB |
Output is correct |
26 |
Correct |
364 ms |
66592 KB |
Output is correct |
27 |
Correct |
412 ms |
50952 KB |
Output is correct |
28 |
Correct |
467 ms |
50556 KB |
Output is correct |
29 |
Correct |
482 ms |
50592 KB |
Output is correct |
30 |
Correct |
357 ms |
50336 KB |
Output is correct |
31 |
Correct |
333 ms |
61528 KB |
Output is correct |
32 |
Correct |
392 ms |
66272 KB |
Output is correct |
33 |
Correct |
308 ms |
50364 KB |
Output is correct |
34 |
Correct |
543 ms |
50744 KB |
Output is correct |
35 |
Correct |
3 ms |
14936 KB |
Output is correct |
36 |
Correct |
3 ms |
14940 KB |
Output is correct |
37 |
Correct |
4 ms |
15012 KB |
Output is correct |
38 |
Correct |
3 ms |
15008 KB |
Output is correct |
39 |
Correct |
4 ms |
14940 KB |
Output is correct |
40 |
Correct |
3 ms |
14940 KB |
Output is correct |
41 |
Correct |
4 ms |
14940 KB |
Output is correct |
42 |
Correct |
5 ms |
14940 KB |
Output is correct |
43 |
Correct |
4 ms |
14940 KB |
Output is correct |
44 |
Correct |
4 ms |
15032 KB |
Output is correct |
45 |
Correct |
3 ms |
14936 KB |
Output is correct |
46 |
Correct |
3 ms |
14940 KB |
Output is correct |
47 |
Correct |
4 ms |
14940 KB |
Output is correct |
48 |
Correct |
3 ms |
14940 KB |
Output is correct |
49 |
Correct |
4 ms |
15028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
427 ms |
60208 KB |
Output is correct |
3 |
Correct |
1177 ms |
56148 KB |
Output is correct |
4 |
Correct |
639 ms |
56764 KB |
Output is correct |
5 |
Correct |
1946 ms |
45868 KB |
Output is correct |
6 |
Correct |
1203 ms |
47832 KB |
Output is correct |
7 |
Correct |
725 ms |
48212 KB |
Output is correct |
8 |
Correct |
323 ms |
47252 KB |
Output is correct |
9 |
Correct |
461 ms |
58876 KB |
Output is correct |
10 |
Correct |
1053 ms |
62856 KB |
Output is correct |
11 |
Correct |
856 ms |
47476 KB |
Output is correct |
12 |
Execution timed out |
4046 ms |
47632 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
424 ms |
59168 KB |
Output is correct |
3 |
Correct |
1123 ms |
54852 KB |
Output is correct |
4 |
Correct |
555 ms |
57156 KB |
Output is correct |
5 |
Correct |
1701 ms |
47660 KB |
Output is correct |
6 |
Correct |
1066 ms |
49356 KB |
Output is correct |
7 |
Correct |
747 ms |
48660 KB |
Output is correct |
8 |
Correct |
329 ms |
48328 KB |
Output is correct |
9 |
Correct |
412 ms |
65336 KB |
Output is correct |
10 |
Correct |
1094 ms |
64308 KB |
Output is correct |
11 |
Correct |
827 ms |
50004 KB |
Output is correct |
12 |
Execution timed out |
4046 ms |
48000 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
5 ms |
14940 KB |
Output is correct |
5 |
Correct |
5 ms |
14940 KB |
Output is correct |
6 |
Correct |
5 ms |
14936 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14936 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14936 KB |
Output is correct |
12 |
Correct |
4 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14936 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14936 KB |
Output is correct |
17 |
Correct |
4 ms |
14936 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14936 KB |
Output is correct |
21 |
Correct |
4 ms |
15024 KB |
Output is correct |
22 |
Correct |
3 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14936 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
4 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
14940 KB |
Output is correct |
30 |
Correct |
290 ms |
42204 KB |
Output is correct |
31 |
Correct |
431 ms |
51028 KB |
Output is correct |
32 |
Correct |
324 ms |
61196 KB |
Output is correct |
33 |
Correct |
335 ms |
50828 KB |
Output is correct |
34 |
Correct |
431 ms |
50236 KB |
Output is correct |
35 |
Correct |
411 ms |
50228 KB |
Output is correct |
36 |
Correct |
382 ms |
50532 KB |
Output is correct |
37 |
Correct |
322 ms |
67156 KB |
Output is correct |
38 |
Correct |
364 ms |
66284 KB |
Output is correct |
39 |
Correct |
310 ms |
50260 KB |
Output is correct |
40 |
Correct |
429 ms |
51028 KB |
Output is correct |
41 |
Correct |
289 ms |
50636 KB |
Output is correct |
42 |
Correct |
334 ms |
51504 KB |
Output is correct |
43 |
Correct |
357 ms |
50796 KB |
Output is correct |
44 |
Correct |
375 ms |
50864 KB |
Output is correct |
45 |
Correct |
413 ms |
50964 KB |
Output is correct |
46 |
Correct |
4 ms |
14940 KB |
Output is correct |
47 |
Correct |
4 ms |
14944 KB |
Output is correct |
48 |
Correct |
5 ms |
14936 KB |
Output is correct |
49 |
Correct |
5 ms |
14936 KB |
Output is correct |
50 |
Correct |
6 ms |
15004 KB |
Output is correct |
51 |
Correct |
7 ms |
14940 KB |
Output is correct |
52 |
Correct |
323 ms |
50220 KB |
Output is correct |
53 |
Correct |
577 ms |
51220 KB |
Output is correct |
54 |
Correct |
364 ms |
66592 KB |
Output is correct |
55 |
Correct |
412 ms |
50952 KB |
Output is correct |
56 |
Correct |
467 ms |
50556 KB |
Output is correct |
57 |
Correct |
482 ms |
50592 KB |
Output is correct |
58 |
Correct |
357 ms |
50336 KB |
Output is correct |
59 |
Correct |
333 ms |
61528 KB |
Output is correct |
60 |
Correct |
392 ms |
66272 KB |
Output is correct |
61 |
Correct |
308 ms |
50364 KB |
Output is correct |
62 |
Correct |
543 ms |
50744 KB |
Output is correct |
63 |
Correct |
3 ms |
14936 KB |
Output is correct |
64 |
Correct |
3 ms |
14940 KB |
Output is correct |
65 |
Correct |
4 ms |
15012 KB |
Output is correct |
66 |
Correct |
3 ms |
15008 KB |
Output is correct |
67 |
Correct |
4 ms |
14940 KB |
Output is correct |
68 |
Correct |
3 ms |
14940 KB |
Output is correct |
69 |
Correct |
4 ms |
14940 KB |
Output is correct |
70 |
Correct |
5 ms |
14940 KB |
Output is correct |
71 |
Correct |
4 ms |
14940 KB |
Output is correct |
72 |
Correct |
4 ms |
15032 KB |
Output is correct |
73 |
Correct |
3 ms |
14936 KB |
Output is correct |
74 |
Correct |
3 ms |
14940 KB |
Output is correct |
75 |
Correct |
4 ms |
14940 KB |
Output is correct |
76 |
Correct |
3 ms |
14940 KB |
Output is correct |
77 |
Correct |
4 ms |
15028 KB |
Output is correct |
78 |
Correct |
4 ms |
14936 KB |
Output is correct |
79 |
Correct |
427 ms |
60208 KB |
Output is correct |
80 |
Correct |
1177 ms |
56148 KB |
Output is correct |
81 |
Correct |
639 ms |
56764 KB |
Output is correct |
82 |
Correct |
1946 ms |
45868 KB |
Output is correct |
83 |
Correct |
1203 ms |
47832 KB |
Output is correct |
84 |
Correct |
725 ms |
48212 KB |
Output is correct |
85 |
Correct |
323 ms |
47252 KB |
Output is correct |
86 |
Correct |
461 ms |
58876 KB |
Output is correct |
87 |
Correct |
1053 ms |
62856 KB |
Output is correct |
88 |
Correct |
856 ms |
47476 KB |
Output is correct |
89 |
Execution timed out |
4046 ms |
47632 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |