//#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define N 200015
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
int n,L,loc[N],sz[N],fa[N],h[N];
vector<int>g[N],son[N];
struct BIT_2D{
vector<int>bit[44];
int n=0,m=43;
void init(int _n){
n=_n; n++;
for (int i=0;i<=m;i++)
bit[i].resize(n+1,1);
}
void modify(int x,int y,int c){
x=(m-1)-x;
// cout<<"modify : "<<x<<' '<<y<<" "<<c<<'\n';
while (x<=m){
int ty=y;
while (ty<=n){
bit[x][ty]=bit[x][ty]*c%L;
ty+=ty&(-ty);
}
x+=x&(-x);
}
}
int query(int x,int y){
int ans=1;
x=(m-1)-x;
// cout<<"query : "<<x<<' '<<y<<'\n';
while (x>0){
int ty=y;
while (ty>0){
ans=ans*bit[x][ty]%L;
ty-=ty&(-ty);
}
x-=x&(-x);
}
return ans;
}
}cntl[N],cntr[N];
void dfs(int p,int lp){
fa[p]=lp;
for (auto i:g[p]){
if (i!=lp){
dfs(i,p);
son[p].push_back(i);
loc[i]=son[p].size();
}
}
sz[p]=son[p].size();
}
signed main(){
fast
cin>>n>>L;
for (int i=1;i<n;i++){
int a,b; cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
for (int i=1;i<=n;i++)
cin>>h[i];
dfs(1,0);
son[0].push_back(1);
sz[0]=1; loc[1]=1;
for (int i=0;i<=n;i++){
cntl[i].init(sz[i]);
cntr[i].init(sz[i]);
}
int q; cin>>q;
while (q--){
int type,x,d,w; cin>>type;
if (type==1){
cin>>x>>d>>w;
cntl[x].modify(d+1,1,w);
for (int i=0;i<d;i++){
if (x==0) break;
int pos=loc[x];
// cout<<x<<" "<<fa[x]<<' '<<pos<<" "<<d-i+1<<'\n';
cntl[fa[x]].modify(d-i,pos+1,w);
cntr[fa[x]].modify(d-i,sz[fa[x]]-pos+2,w);
// cout<<"dis : "<<d-i+1<<" OK\n";
x=fa[x];
}
}
else {
cin>>x;
int ans=h[x];
int tans1=cntl[x].query(1,sz[x]+1),tans2;
ans=(ans*max(tans1,1LL))%L;
// cout<<" -> "<<tans1<<'\n';
for (int i=1;i<=40;i++){
if (x==0) break;
int pos=loc[x];
tans1=cntl[fa[x]].query(i+1,pos);
tans2=cntr[fa[x]].query(i+1,sz[fa[x]]-pos+1);
// cout<<" -> "<<tans1<<' '<<tans2<<'\n';
ans=(ans*tans1%L*tans2)%L;
x=fa[x];
}
cout<<ans<<'\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
429344 KB |
Output is correct |
2 |
Correct |
213 ms |
429364 KB |
Output is correct |
3 |
Incorrect |
210 ms |
429332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
213 ms |
429428 KB |
Output is correct |
2 |
Runtime error |
1082 ms |
1048576 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
213 ms |
429428 KB |
Output is correct |
2 |
Runtime error |
1082 ms |
1048576 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
238 ms |
429356 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
429348 KB |
Output is correct |
2 |
Execution timed out |
4147 ms |
1011980 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
429344 KB |
Output is correct |
2 |
Correct |
213 ms |
429364 KB |
Output is correct |
3 |
Incorrect |
210 ms |
429332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |