#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#define ll long long
const int mxn=2e5+5;
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 |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14784 KB |
Output is correct |
4 |
Correct |
5 ms |
15196 KB |
Output is correct |
5 |
Correct |
5 ms |
15040 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
5 ms |
14968 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 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 |
15008 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14788 KB |
Output is correct |
18 |
Correct |
3 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
4 ms |
15028 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
3 ms |
15000 KB |
Output is correct |
25 |
Correct |
3 ms |
14940 KB |
Output is correct |
26 |
Correct |
3 ms |
15000 KB |
Output is correct |
27 |
Correct |
4 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14940 KB |
Output is correct |
2 |
Correct |
299 ms |
49832 KB |
Output is correct |
3 |
Correct |
483 ms |
49868 KB |
Output is correct |
4 |
Correct |
321 ms |
60240 KB |
Output is correct |
5 |
Correct |
335 ms |
50000 KB |
Output is correct |
6 |
Correct |
424 ms |
49560 KB |
Output is correct |
7 |
Correct |
419 ms |
49580 KB |
Output is correct |
8 |
Correct |
383 ms |
49588 KB |
Output is correct |
9 |
Correct |
328 ms |
66388 KB |
Output is correct |
10 |
Correct |
370 ms |
65132 KB |
Output is correct |
11 |
Correct |
283 ms |
49756 KB |
Output is correct |
12 |
Correct |
446 ms |
49856 KB |
Output is correct |
13 |
Correct |
266 ms |
49848 KB |
Output is correct |
14 |
Correct |
347 ms |
50412 KB |
Output is correct |
15 |
Correct |
350 ms |
49628 KB |
Output is correct |
16 |
Correct |
396 ms |
49932 KB |
Output is correct |
17 |
Correct |
390 ms |
48408 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
4 ms |
14964 KB |
Output is correct |
20 |
Correct |
4 ms |
14936 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
15192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14940 KB |
Output is correct |
2 |
Correct |
299 ms |
49832 KB |
Output is correct |
3 |
Correct |
483 ms |
49868 KB |
Output is correct |
4 |
Correct |
321 ms |
60240 KB |
Output is correct |
5 |
Correct |
335 ms |
50000 KB |
Output is correct |
6 |
Correct |
424 ms |
49560 KB |
Output is correct |
7 |
Correct |
419 ms |
49580 KB |
Output is correct |
8 |
Correct |
383 ms |
49588 KB |
Output is correct |
9 |
Correct |
328 ms |
66388 KB |
Output is correct |
10 |
Correct |
370 ms |
65132 KB |
Output is correct |
11 |
Correct |
283 ms |
49756 KB |
Output is correct |
12 |
Correct |
446 ms |
49856 KB |
Output is correct |
13 |
Correct |
266 ms |
49848 KB |
Output is correct |
14 |
Correct |
347 ms |
50412 KB |
Output is correct |
15 |
Correct |
350 ms |
49628 KB |
Output is correct |
16 |
Correct |
396 ms |
49932 KB |
Output is correct |
17 |
Correct |
390 ms |
48408 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
4 ms |
14964 KB |
Output is correct |
20 |
Correct |
4 ms |
14936 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
15192 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
335 ms |
48864 KB |
Output is correct |
25 |
Correct |
597 ms |
48360 KB |
Output is correct |
26 |
Correct |
384 ms |
64520 KB |
Output is correct |
27 |
Correct |
388 ms |
48464 KB |
Output is correct |
28 |
Correct |
499 ms |
48656 KB |
Output is correct |
29 |
Correct |
485 ms |
48696 KB |
Output is correct |
30 |
Correct |
361 ms |
48048 KB |
Output is correct |
31 |
Correct |
335 ms |
60260 KB |
Output is correct |
32 |
Correct |
395 ms |
63456 KB |
Output is correct |
33 |
Correct |
312 ms |
49004 KB |
Output is correct |
34 |
Correct |
577 ms |
48480 KB |
Output is correct |
35 |
Correct |
5 ms |
14936 KB |
Output is correct |
36 |
Correct |
3 ms |
14940 KB |
Output is correct |
37 |
Correct |
3 ms |
14936 KB |
Output is correct |
38 |
Correct |
4 ms |
14940 KB |
Output is correct |
39 |
Correct |
3 ms |
14940 KB |
Output is correct |
40 |
Correct |
4 ms |
14936 KB |
Output is correct |
41 |
Correct |
4 ms |
14940 KB |
Output is correct |
42 |
Correct |
4 ms |
14940 KB |
Output is correct |
43 |
Correct |
4 ms |
14940 KB |
Output is correct |
44 |
Correct |
3 ms |
14940 KB |
Output is correct |
45 |
Correct |
3 ms |
14940 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 |
14936 KB |
Output is correct |
49 |
Correct |
4 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
442 ms |
62780 KB |
Output is correct |
3 |
Correct |
1046 ms |
59044 KB |
Output is correct |
4 |
Correct |
543 ms |
58208 KB |
Output is correct |
5 |
Correct |
1788 ms |
44072 KB |
Output is correct |
6 |
Correct |
1122 ms |
43532 KB |
Output is correct |
7 |
Correct |
667 ms |
44144 KB |
Output is correct |
8 |
Correct |
309 ms |
43000 KB |
Output is correct |
9 |
Correct |
401 ms |
54952 KB |
Output is correct |
10 |
Correct |
1103 ms |
58832 KB |
Output is correct |
11 |
Correct |
838 ms |
44056 KB |
Output is correct |
12 |
Execution timed out |
4098 ms |
43644 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15192 KB |
Output is correct |
2 |
Correct |
401 ms |
56920 KB |
Output is correct |
3 |
Correct |
1051 ms |
52252 KB |
Output is correct |
4 |
Correct |
635 ms |
54872 KB |
Output is correct |
5 |
Correct |
1640 ms |
45640 KB |
Output is correct |
6 |
Correct |
1071 ms |
48220 KB |
Output is correct |
7 |
Correct |
753 ms |
46676 KB |
Output is correct |
8 |
Correct |
303 ms |
46516 KB |
Output is correct |
9 |
Correct |
425 ms |
64008 KB |
Output is correct |
10 |
Correct |
1076 ms |
62088 KB |
Output is correct |
11 |
Correct |
866 ms |
48308 KB |
Output is correct |
12 |
Execution timed out |
4024 ms |
45876 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14784 KB |
Output is correct |
4 |
Correct |
5 ms |
15196 KB |
Output is correct |
5 |
Correct |
5 ms |
15040 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
5 ms |
14968 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 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 |
15008 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14788 KB |
Output is correct |
18 |
Correct |
3 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
4 ms |
15028 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
3 ms |
15000 KB |
Output is correct |
25 |
Correct |
3 ms |
14940 KB |
Output is correct |
26 |
Correct |
3 ms |
15000 KB |
Output is correct |
27 |
Correct |
4 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
4 ms |
14940 KB |
Output is correct |
30 |
Correct |
299 ms |
49832 KB |
Output is correct |
31 |
Correct |
483 ms |
49868 KB |
Output is correct |
32 |
Correct |
321 ms |
60240 KB |
Output is correct |
33 |
Correct |
335 ms |
50000 KB |
Output is correct |
34 |
Correct |
424 ms |
49560 KB |
Output is correct |
35 |
Correct |
419 ms |
49580 KB |
Output is correct |
36 |
Correct |
383 ms |
49588 KB |
Output is correct |
37 |
Correct |
328 ms |
66388 KB |
Output is correct |
38 |
Correct |
370 ms |
65132 KB |
Output is correct |
39 |
Correct |
283 ms |
49756 KB |
Output is correct |
40 |
Correct |
446 ms |
49856 KB |
Output is correct |
41 |
Correct |
266 ms |
49848 KB |
Output is correct |
42 |
Correct |
347 ms |
50412 KB |
Output is correct |
43 |
Correct |
350 ms |
49628 KB |
Output is correct |
44 |
Correct |
396 ms |
49932 KB |
Output is correct |
45 |
Correct |
390 ms |
48408 KB |
Output is correct |
46 |
Correct |
4 ms |
14940 KB |
Output is correct |
47 |
Correct |
4 ms |
14964 KB |
Output is correct |
48 |
Correct |
4 ms |
14936 KB |
Output is correct |
49 |
Correct |
3 ms |
14940 KB |
Output is correct |
50 |
Correct |
3 ms |
15192 KB |
Output is correct |
51 |
Correct |
3 ms |
14940 KB |
Output is correct |
52 |
Correct |
335 ms |
48864 KB |
Output is correct |
53 |
Correct |
597 ms |
48360 KB |
Output is correct |
54 |
Correct |
384 ms |
64520 KB |
Output is correct |
55 |
Correct |
388 ms |
48464 KB |
Output is correct |
56 |
Correct |
499 ms |
48656 KB |
Output is correct |
57 |
Correct |
485 ms |
48696 KB |
Output is correct |
58 |
Correct |
361 ms |
48048 KB |
Output is correct |
59 |
Correct |
335 ms |
60260 KB |
Output is correct |
60 |
Correct |
395 ms |
63456 KB |
Output is correct |
61 |
Correct |
312 ms |
49004 KB |
Output is correct |
62 |
Correct |
577 ms |
48480 KB |
Output is correct |
63 |
Correct |
5 ms |
14936 KB |
Output is correct |
64 |
Correct |
3 ms |
14940 KB |
Output is correct |
65 |
Correct |
3 ms |
14936 KB |
Output is correct |
66 |
Correct |
4 ms |
14940 KB |
Output is correct |
67 |
Correct |
3 ms |
14940 KB |
Output is correct |
68 |
Correct |
4 ms |
14936 KB |
Output is correct |
69 |
Correct |
4 ms |
14940 KB |
Output is correct |
70 |
Correct |
4 ms |
14940 KB |
Output is correct |
71 |
Correct |
4 ms |
14940 KB |
Output is correct |
72 |
Correct |
3 ms |
14940 KB |
Output is correct |
73 |
Correct |
3 ms |
14940 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 |
14936 KB |
Output is correct |
77 |
Correct |
4 ms |
14940 KB |
Output is correct |
78 |
Correct |
3 ms |
14940 KB |
Output is correct |
79 |
Correct |
442 ms |
62780 KB |
Output is correct |
80 |
Correct |
1046 ms |
59044 KB |
Output is correct |
81 |
Correct |
543 ms |
58208 KB |
Output is correct |
82 |
Correct |
1788 ms |
44072 KB |
Output is correct |
83 |
Correct |
1122 ms |
43532 KB |
Output is correct |
84 |
Correct |
667 ms |
44144 KB |
Output is correct |
85 |
Correct |
309 ms |
43000 KB |
Output is correct |
86 |
Correct |
401 ms |
54952 KB |
Output is correct |
87 |
Correct |
1103 ms |
58832 KB |
Output is correct |
88 |
Correct |
838 ms |
44056 KB |
Output is correct |
89 |
Execution timed out |
4098 ms |
43644 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |