#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)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){
if((int)val.size()<=id)return;
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((int)val.size()<=id)return 0;
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 |
Incorrect |
5 ms |
14936 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Incorrect |
296 ms |
38588 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Incorrect |
296 ms |
38588 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
387 ms |
52820 KB |
Output is correct |
3 |
Correct |
1049 ms |
47928 KB |
Output is correct |
4 |
Correct |
583 ms |
49004 KB |
Output is correct |
5 |
Incorrect |
1716 ms |
37296 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14936 KB |
Output is correct |
2 |
Correct |
421 ms |
49624 KB |
Output is correct |
3 |
Correct |
1047 ms |
44124 KB |
Output is correct |
4 |
Correct |
549 ms |
46876 KB |
Output is correct |
5 |
Incorrect |
1588 ms |
37460 KB |
Output isn't correct |
6 |
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 |
Incorrect |
5 ms |
14936 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |