이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 2e5+10;
const int mxd = 45;
ll L;
ll dp[mxn][mxd];
ll arr[mxn];
int N;
vector<int> tree[mxn];
int par[mxn];
void dfs(int now){
for(auto nxt:tree[now]){
if(nxt == par[now])continue;
par[nxt] = now;
dfs(nxt);
}
return;
}
//d(a,p)+d(b,p) = r -> d(a,p) = r-d(b,p)
void modify(int now,int r,int val){
int d = 0;
while(r-d>=0&&now){
if(!par[now]){
for(int j = 0;j<=r-d;j++)dp[now][j] = dp[now][j]*val%L;
}
else{
dp[now][r-d] = dp[now][r-d]*val%L;
if(r-d)dp[now][r-d-1] = dp[now][r-d-1]*val%L;
}
now = par[now];
d++;
}
return;
}
ll getans(int now){
int d = 0;
ll re = arr[now];
while(d<mxd&&now){
re = re*dp[now][d]%L;
now = par[now];
d++;
}
return re;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
for(auto &i:dp)for(auto &j:i)j = 1;
cin>>N>>L;
for(int i = 1;i<N;i++){
int a,b;
cin>>a>>b;
tree[a].push_back(b);
tree[b].push_back(a);
}
for(int i = 1;i<=N;i++)cin>>arr[i];
dfs(1);
int Q;
cin>>Q;
while(Q--){
int tp;
cin>>tp;
if(tp == 1){
int a,b,v;
cin>>a>>b>>v;
modify(a,b,v);
}
else{
int k;
cin>>k;
cout<<getans(k)<<'\n';
}
}
}
# | 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... |