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>
using namespace std;
typedef long long ll;
const int MX=2e5+5;
int N,L,Q;
int A[MX], B[MX], H[MX];
vector<int> adj[MX];
int par[MX];
ll w[MX][50]; // w and w inverse
void dfs(int v, int p) {
par[v]=p;
for(auto u:adj[v]) {
if(u==p) continue;
dfs(u,v);
}
}
void upd(int v, int d, int x) {
while(d>=0 && v!=0) {
w[v][d]*=x;
w[v][d]%=L;
v=par[v];
d--;
}
}
int que(int v) {
int d=0,p=-1;
ll res=1;
while(d<=40 && v!=0) {
for(int i=d;i<=40;i++) {
if(par[v]!=0 && i-1>=d+1) continue;
res*=w[v][i];
res%=L;
}
d++;
p=v;
v=par[v];
}
return res;
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
for(int i=0;i<MX;i++)
for(int d=0;d<=41;d++)
w[i][d]=1;
cin>>N>>L;
for(int i=0;i<N-1;i++) {
cin>>A[i]>>B[i];
adj[A[i]].push_back(B[i]);
adj[B[i]].push_back(A[i]);
}
for(int i=1;i<=N;i++) cin>>w[i][0];
dfs(1,0);
cin>>Q;
for(int i=1;i<=Q;i++) {
int t;
cin>>t;
if(t==1) {
int x,d,w;
cin>>x>>d>>w;
upd(x,d,w);
} else {
int x;
cin>>x;
cout<<que(x)<<'\n';
}
}
}
Compilation message (stderr)
sprinkler.cpp: In function 'int que(int)':
sprinkler.cpp:31:10: warning: variable 'p' set but not used [-Wunused-but-set-variable]
31 | int d=0,p=-1;
| ^
# | 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... |