This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<cstdlib>
#include<vector>
#include<cassert>
#define ll long long
using namespace std;
int n,q,a,b,c,ojc[200005],d,ojc2[81];
ll L,tab[200005],x;
ll t[200005][42];
ll f[200005][81];
ll p[200005];
vector<int> v[200005];
vector<int> v2[42];
void DFS(int x,int f){
ojc[x]=f;
for(auto i:v[x]) if(i!=f) DFS(i,x);
}
void dod2(int x,int p,int k,ll z){
assert(k==(p+1));
t[x][p]*=z,t[x][p]%=L;
}
void dod(int x,int k,ll z){
//od 0 do k
p[x]*=z,p[x]%=L;
for(auto u:v2[k])
f[x][u]*=z,f[x][u]%=L;
}
int main(){
ios_base::sync_with_stdio(0);
cin>>n>>L;
for(int i=1;i<n;i++){
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
for(int i=1;i<=n;i++){
for(int u=0;u<=41;u++) t[i][u]=1;
for(int u=0;u<80;u++) f[i][u]=1;
p[i]=1;
cin>>tab[i];
}
for(int u=1;u<=63;u++) ojc2[u]=u>>1;
ojc2[64]=0;
for(int u=65;u<=79;u++) ojc2[u]=((u-63)>>1)+63; //fenwick liczy odleglosci od 1
for(int u=1;u<=32;u++){
int p=1,k=32,m=1;
while(true){
if(k==u){
v2[u].push_back(m);
break;
}
if(p>u) break;
if(u>((p+k)>>1)){
v2[u].push_back(m*2);
m=m*2+1;
p=((p+k)>>1)+1;
}else{
m=m*2;
k=((p+k)>>1);
}
}
}
DFS(1,-1);
//return 0;
//cerr<<v2[0].size()<<" rozmiar\n";
cin>>q;
for(int i=0;i<q;i++){
cin>>a;
if(a==2){
cin>>b;
d=0,c=b;
ll wyn=tab[b];
//cerr<<wyn<<"\n";
wyn*=p[c];
wyn%=L;
while(true){
wyn*=t[c][d];
wyn%=L;
if(d!=0){
wyn*=t[c][d-1];
wyn%=L;
a=d+31;
wyn*=f[c][a];
wyn%=L;
//cerr<<"f["<<c<<"]["<<a<<"]="<<f[c][a]<<"\n";
while(a!=0){
a=ojc2[a];
//cerr<<"f["<<c<<"]["<<a<<"]="<<f[c][a]<<"\n";
wyn*=f[c][a];
wyn%=L;
}
}
c=ojc[c];
if(c==-1) break;
d++;
if(d>32) break;
}
cout<<wyn<<"\n";
}else{
cin>>b>>c>>x;
d=0;
while(true){
if(b==-1 || d>c) break;
if(b!=1 && c!=d){
//cerr<<b<<" "<<c-d-1<<"\n";
dod2(b,c-d-1,c-d,x);
}else{
//cerr<<b<<" "<<c<<"-"<<d<<" "<<" drzewo\n";
dod(b,c-d,x);
}
b=ojc[b];
d++;
}
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |