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<bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
using namespace std;
#define int long long
int n,MOD;
int h[200005];
vector<int> con[200005];
int parent[200005];
int inm[200005][45];
int depth[200005];
void dfs(int nod)
{
for(auto adj:con[nod])
{
if(adj!=parent[nod])
{
depth[adj]=depth[nod]+1;
parent[adj]=nod;
dfs(adj);
}
}
for(int d=0;d<=42;d++)
inm[nod][d]=1;
}
int getrez(int nod)
{
int prod=1;
for(int i=0;i<=40;i++)
{
if(nod==0)
break;
prod=prod*inm[nod][i]%MOD;
nod=parent[nod];
}
return prod;
}
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>MOD;
//phi = calc_phi(MOD);
int a,b;
for(int i=1;i<n;i++)
{
cin>>a>>b;
con[a].push_back(b);
con[b].push_back(a);
}
depth[1]=1;
dfs(1);
for(int i=1;i<=n;i++)
{
cin>>h[i];
}
int cntq,tip,d;
cin>>cntq;
for(int pas=1;pas<=cntq;pas++)
{
cin>>tip;
if(tip==1)
{
cin>>a>>d>>b;
int cur=a;
for(int i=0;i<=d;i++)
{
if(cur==0)
break;
///j > -1 + d - (i+1)
int lim=0;
if(parent[cur]!=0)
lim=max(lim,d-i-1);
for(int j=lim;j<=d-i;j++)
{
inm[cur][j]=(inm[cur][j]*b)%MOD;
}
cur = parent[cur];
}
}
else
{
cin>>a;
cout<<(1LL*h[a]*getrez(a))%MOD<<"\n";
//cout<<h[a]<<" "<<qry_brut(pos[a])<<" zzz\n";
}
}
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... |