#include <bits/stdc++.h>
#define int long long
using namespace std;
int mod;
struct fen
{
int n;
vector <int> f;
void reset(int sl)
{
f.assign(sl+1,1);
n=sl;
}
void add(int x, int val)
{
// cerr<<n<<endl;
// cerr<<"+ "<<x<<' '<<val<<" : "<<n<<endl;
while (x<=n)
{
f[x]=(f[x]*val)%mod;
x+=(x&-x);
}
}
int get(int x)
{
int temp=1;
while (x>0)
{
temp=(temp*f[x])%mod;
x-=(x&-x);
}
return temp;
}
};
struct fen2
{
int n;
vector <int> f;
void reset(int sl)
{
f.assign(sl+1,1);
n=sl;
}
void add(int x, int val)
{
while (x>0)
{
f[x]=(f[x]*val)%mod;
x-=(x&-x);
}
}
int get(int x)
{
int temp=1;
while (x<=n)
{
temp=(temp*f[x])%mod;
x+=(x&-x);
}
return temp;
}
};
const int maxn = 2e5 + 10;
int n,q;
int a[maxn];
int ppow(int x, int y)
{
int temp=1LL;
while (y)
{
if (y&1) temp=temp*x%mod;
x=x*x%mod;
y/=2;
}
return temp;
}
vector <fen> freq[maxn];
vector <fen2> freq2[maxn];
vector <int> freqs[maxn];
vector <int> adj[maxn],depth[maxn];
int s[maxn],par[maxn],cid[maxn];
bool dau[maxn];
int get_sz(int x, int p)
{
s[x]=1;
for (int i:adj[x])
if (i!=p&&!dau[i]) s[x]+=get_sz(i,x);
return s[x];
}
int find_centroid(int x, int p, int sz)
{
for (int i:adj[x])
if (i!=p&&!dau[i]&&2*s[i]>sz) return find_centroid(i,x,sz);
return x;
}
void dfs(int x, int p, int dep)
{
depth[x].push_back(dep);
for (int i:adj[x])
if (i!=p&&!dau[i]) dfs(i,x,dep+1);
}
void decompose(int x, int p, int id)
{
x=find_centroid(x,x,get_sz(x,x));
par[x]=p;
dau[x]=1;
depth[x].push_back(0);
cid[x]=id;
int cnt=0;
for (int i:adj[x])
if (!dau[i])
{
dfs(i,x,1);
decompose(i,x,cnt);
cnt++;
}
for (int i=0; i<=2; i++)
{
freqs[x].push_back(1LL);
freq[x].emplace_back();
// cerr<<"reset "<<i<<' '<<x<<' '<<cnt<<endl;
freq[x].back().reset(cnt);
// cerr<<freq[x].back().n<<endl;
freq2[x].emplace_back();
freq2[x].back().reset(cnt);
}
}
void update(int x, int D, int w)
{
int p=x; int id=-1;
for (int i=depth[x].size()-1; i>=0; i--)
{
int d = depth[x][i];
if (d<=D)
{
// cout<<"+ "<<D-d<<' '<<p<<' '<<id<<' '<<w<<'\n';
if (id!=-1)
{
freq[p][D-d].add(id+1,w);
freq2[p][D-d].add(id+1,w);
}
else
freqs[p][D-d]=freqs[p][D-d]*w%mod;
}
id=cid[p];
p=par[p];
}
}
int get(int x)
{
int p=x; int id=-1;
int ans = a[x];
for (int i=depth[x].size()-1; i>=0; i--)
{
int D = depth[x][i];
// cout<<p<<" - "<<x<<" = "<<D<<'\n';
for (int d=D; d<=2; d++)
{
ans = (ans*freqs[p][d])%mod;
if (id!=-1)
{
ans = (ans*freq[p][d].get(id))%mod;
ans = (ans*freq2[p][d].get(id+2))%mod;
}
else ans = (ans*freq[p][d].get(freq[p][d].n))%mod;
}
id=cid[p];
p=par[p];
}
return ans;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin>>n>>mod;
for (int i=1; i<n; i++)
{
int u,v; cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i=1; i<=n; i++) cin>>a[i];
decompose(1,-1,-1);
cin>>q;
for (int i=1; i<=q; i++)
{
int t; cin>>t;
if (t==1)
{
int x,d,w; cin>>x>>d>>w;
// W=w;
update(x,d,w);
}
else
{
int x; cin>>x;
int ans = get(x);
// cout<<x<<" : "<<cnt<<'\n';
cout<<ans<<'\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
33 ms |
48012 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23824 KB |
Output is correct |
2 |
Correct |
1517 ms |
183692 KB |
Output is correct |
3 |
Correct |
1519 ms |
184300 KB |
Output is correct |
4 |
Correct |
1794 ms |
204788 KB |
Output is correct |
5 |
Correct |
1618 ms |
183824 KB |
Output is correct |
6 |
Correct |
1563 ms |
181372 KB |
Output is correct |
7 |
Correct |
1342 ms |
179188 KB |
Output is correct |
8 |
Correct |
1044 ms |
169220 KB |
Output is correct |
9 |
Correct |
2004 ms |
208660 KB |
Output is correct |
10 |
Correct |
1917 ms |
208268 KB |
Output is correct |
11 |
Correct |
1470 ms |
183632 KB |
Output is correct |
12 |
Correct |
1465 ms |
184240 KB |
Output is correct |
13 |
Correct |
732 ms |
168508 KB |
Output is correct |
14 |
Correct |
805 ms |
170756 KB |
Output is correct |
15 |
Correct |
996 ms |
171740 KB |
Output is correct |
16 |
Correct |
974 ms |
174560 KB |
Output is correct |
17 |
Correct |
990 ms |
176780 KB |
Output is correct |
18 |
Correct |
13 ms |
23820 KB |
Output is correct |
19 |
Correct |
15 ms |
23824 KB |
Output is correct |
20 |
Correct |
13 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
13 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23824 KB |
Output is correct |
2 |
Correct |
1517 ms |
183692 KB |
Output is correct |
3 |
Correct |
1519 ms |
184300 KB |
Output is correct |
4 |
Correct |
1794 ms |
204788 KB |
Output is correct |
5 |
Correct |
1618 ms |
183824 KB |
Output is correct |
6 |
Correct |
1563 ms |
181372 KB |
Output is correct |
7 |
Correct |
1342 ms |
179188 KB |
Output is correct |
8 |
Correct |
1044 ms |
169220 KB |
Output is correct |
9 |
Correct |
2004 ms |
208660 KB |
Output is correct |
10 |
Correct |
1917 ms |
208268 KB |
Output is correct |
11 |
Correct |
1470 ms |
183632 KB |
Output is correct |
12 |
Correct |
1465 ms |
184240 KB |
Output is correct |
13 |
Correct |
732 ms |
168508 KB |
Output is correct |
14 |
Correct |
805 ms |
170756 KB |
Output is correct |
15 |
Correct |
996 ms |
171740 KB |
Output is correct |
16 |
Correct |
974 ms |
174560 KB |
Output is correct |
17 |
Correct |
990 ms |
176780 KB |
Output is correct |
18 |
Correct |
13 ms |
23820 KB |
Output is correct |
19 |
Correct |
15 ms |
23824 KB |
Output is correct |
20 |
Correct |
13 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
13 ms |
23764 KB |
Output is correct |
23 |
Correct |
13 ms |
23812 KB |
Output is correct |
24 |
Correct |
1420 ms |
183844 KB |
Output is correct |
25 |
Correct |
1634 ms |
184220 KB |
Output is correct |
26 |
Correct |
1758 ms |
208612 KB |
Output is correct |
27 |
Correct |
1633 ms |
183912 KB |
Output is correct |
28 |
Correct |
1347 ms |
182448 KB |
Output is correct |
29 |
Correct |
1233 ms |
178536 KB |
Output is correct |
30 |
Correct |
949 ms |
169156 KB |
Output is correct |
31 |
Correct |
1718 ms |
204992 KB |
Output is correct |
32 |
Correct |
1855 ms |
208360 KB |
Output is correct |
33 |
Correct |
1368 ms |
183700 KB |
Output is correct |
34 |
Correct |
1480 ms |
184120 KB |
Output is correct |
35 |
Correct |
13 ms |
23764 KB |
Output is correct |
36 |
Correct |
13 ms |
23824 KB |
Output is correct |
37 |
Correct |
13 ms |
23764 KB |
Output is correct |
38 |
Correct |
14 ms |
23824 KB |
Output is correct |
39 |
Correct |
16 ms |
23764 KB |
Output is correct |
40 |
Correct |
14 ms |
23764 KB |
Output is correct |
41 |
Correct |
14 ms |
23764 KB |
Output is correct |
42 |
Correct |
14 ms |
23848 KB |
Output is correct |
43 |
Correct |
14 ms |
23800 KB |
Output is correct |
44 |
Correct |
12 ms |
23824 KB |
Output is correct |
45 |
Correct |
13 ms |
23820 KB |
Output is correct |
46 |
Correct |
13 ms |
23852 KB |
Output is correct |
47 |
Correct |
13 ms |
23820 KB |
Output is correct |
48 |
Correct |
13 ms |
23892 KB |
Output is correct |
49 |
Correct |
13 ms |
23836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23844 KB |
Output is correct |
2 |
Runtime error |
1299 ms |
398828 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
35 ms |
48124 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
33 ms |
48012 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |