#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,MOD,phi;
int put(int a, int exp)
{
if(exp==0)
return 1;
if(exp%2==0)
return put((a*a)%MOD,exp/2);
else
return (put((a*a)%MOD,exp/2)*a)%MOD;
}
int aint[800005];
int lazy[800005];
void build(int nod, int st, int dr)
{
aint[nod]=1;
lazy[nod]=1;
if(st==dr)
return;
int mij=(st+dr)/2;
build(nod*2,st,mij);
build(nod*2+1,mij+1,dr);
}
void propagate(int nod)
{
lazy[nod*2]=lazy[nod*2]*lazy[nod]%MOD;
lazy[nod*2+1]=lazy[nod*2+1]*lazy[nod]%MOD;
aint[nod*2]=aint[nod*2]*lazy[nod]%MOD;
aint[nod*2+1]=aint[nod*2+1]*lazy[nod]%MOD;
lazy[nod]=1;
}
void upd(int nod, int st, int dr, int le, int ri, int newv)
{
if(le>ri)
return;
if(le==st && dr==ri)
{
aint[nod]=aint[nod]*newv%MOD;
lazy[nod]=lazy[nod]*newv%MOD;
return;
}
propagate(nod);
int mij=(st+dr)/2;
upd(nod*2,st,mij,le,min(mij,ri),newv);
upd(nod*2+1,mij+1,dr,max(le,mij+1),ri,newv);
aint[nod]=aint[nod*2]*aint[nod*2+1]%MOD;
}
int qry(int nod, int st, int dr, int poz)
{
if(st==dr)
return aint[nod];
propagate(nod);
int mij=(st+dr)/2;
if(poz<=mij) return qry(nod*2,st,mij,poz);
else return qry(nod*2+1,mij+1,dr,poz);
}
int aib0[200005];
int qry0(int poz)
{
int aux=0;
for(int i=poz;i>0;i-=(i&(-i)))
aux+=aib0[i];
return aux;
}
void upd0(int poz, int newv)
{
for(int i=poz;i<=n;i+=(i&(-i)))
aib0[i]+=newv;
}
int h[200005];
vector<int> con[200005];
int parent[200005];
int tole[200005][45];
int tori[200005][45];
int cntd[200005];
int cntd2[200005];
int depth[200005];
int pos[200005];
void dfs(int nod)
{
cntd[depth[nod]]++;
for(auto adj:con[nod])
{
if(adj!=parent[nod])
{
depth[adj]=depth[nod]+1;
parent[adj]=nod;
dfs(adj);
}
}
}
void dfs2(int nod)
{
cntd2[depth[nod]]++;
pos[nod] = cntd[depth[nod]-1] + cntd2[depth[nod]];
//cout<<nod<<" "<<pos[nod]<<" pos\n";
for(int d=0;d<=42;d++)
{
tole[nod][d] = n+5;
tori[nod][d] = -1;
}
tole[nod][0] = tori[nod][0] = pos[nod];
for(auto adj:con[nod])
{
if(adj!=parent[nod])
{
dfs2(adj);
for(int d=1;d<=42;d++)
{
tole[nod][d] = min(tole[nod][d], tole[adj][d-1]);
tori[nod][d] = max(tori[nod][d], tori[adj][d-1]);
}
}
}
}
void faupd(int le, int ri, int newv, int inv)
{
if(le>ri)
return;
if(newv!=0)
{
upd(1,1,n,le,ri,newv);
}
else
{
upd0(le,1);
upd0(ri+1,-1);
}
}
int calc_phi(int x)
{
int d=2,prod=x;
while(d*d<=x)
{
if(x%d==0)
{
while(x%d==0)
x/=d;
prod = prod / d * (d-1);
}
if(d==2)
d--;
d+=2;
}
if(x>1)
prod = prod / x * (x-1);
return prod;
}
int getrez(int poz)
{
if(qry0(poz)>0)
return 0;
else
return qry(1,1,n,poz);
}
int le[200005],ri[200005];
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=2;i<=n;i++)
cntd[i]+=cntd[i-1];
dfs2(1);
for(int i=1;i<=n;i++)
{
cin>>h[i];
}
build(1,1,n);
int cntq,tip,d;
cin>>cntq;
for(int pas=1;pas<=cntq;pas++)
{
cin>>tip;
if(tip==1)
{
cin>>a>>d>>b;
for(int i=max((int)1,depth[a]-d);i<=min(n,depth[a]+d);i++)
{
le[i]=n+5;
ri[i]=-1;
}
int cur=a;
for(int i=0;i<=d;i++)
{
if(cur==0)
break;
for(int j=0;j<=d-i;j++)
{
if(depth[cur]+j>n)
break;
le[depth[cur]+j] = min(le[depth[cur]+j], tole[cur][j]);
ri[depth[cur]+j] = max(ri[depth[cur]+j], tori[cur][j]);
}
cur = parent[cur];
}
int inv=put(b,phi-1);
for(int i=max((int)1,depth[a]-d);i<=min(n,depth[a]+d);i++)
{
faupd(le[i],ri[i],b,inv);
}
}
else
{
cin>>a;
cout<<(h[a]*getrez(pos[a]))%MOD<<"\n";
//cout<<h[a]<<" "<<qry_brut(pos[a])<<" zzz\n";
}
}
return 0;
}
/**
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
24920 KB |
Output is correct |
2 |
Correct |
4 ms |
24924 KB |
Output is correct |
3 |
Correct |
4 ms |
24924 KB |
Output is correct |
4 |
Correct |
10 ms |
24924 KB |
Output is correct |
5 |
Correct |
11 ms |
25124 KB |
Output is correct |
6 |
Correct |
8 ms |
24924 KB |
Output is correct |
7 |
Correct |
7 ms |
24924 KB |
Output is correct |
8 |
Correct |
5 ms |
24924 KB |
Output is correct |
9 |
Correct |
4 ms |
24924 KB |
Output is correct |
10 |
Correct |
4 ms |
24920 KB |
Output is correct |
11 |
Correct |
4 ms |
24924 KB |
Output is correct |
12 |
Correct |
4 ms |
24924 KB |
Output is correct |
13 |
Correct |
4 ms |
24924 KB |
Output is correct |
14 |
Correct |
4 ms |
25092 KB |
Output is correct |
15 |
Correct |
4 ms |
25092 KB |
Output is correct |
16 |
Correct |
4 ms |
24924 KB |
Output is correct |
17 |
Correct |
4 ms |
24920 KB |
Output is correct |
18 |
Correct |
4 ms |
24924 KB |
Output is correct |
19 |
Correct |
4 ms |
25088 KB |
Output is correct |
20 |
Correct |
4 ms |
24924 KB |
Output is correct |
21 |
Correct |
4 ms |
24924 KB |
Output is correct |
22 |
Correct |
4 ms |
24920 KB |
Output is correct |
23 |
Correct |
4 ms |
24924 KB |
Output is correct |
24 |
Correct |
4 ms |
24924 KB |
Output is correct |
25 |
Correct |
4 ms |
24920 KB |
Output is correct |
26 |
Correct |
5 ms |
24920 KB |
Output is correct |
27 |
Correct |
4 ms |
24924 KB |
Output is correct |
28 |
Correct |
4 ms |
24924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
24924 KB |
Output is correct |
2 |
Correct |
524 ms |
181916 KB |
Output is correct |
3 |
Correct |
993 ms |
178612 KB |
Output is correct |
4 |
Correct |
731 ms |
189268 KB |
Output is correct |
5 |
Correct |
695 ms |
180096 KB |
Output is correct |
6 |
Correct |
635 ms |
179792 KB |
Output is correct |
7 |
Correct |
663 ms |
180348 KB |
Output is correct |
8 |
Correct |
585 ms |
180548 KB |
Output is correct |
9 |
Correct |
554 ms |
195904 KB |
Output is correct |
10 |
Correct |
1068 ms |
191580 KB |
Output is correct |
11 |
Correct |
498 ms |
181656 KB |
Output is correct |
12 |
Correct |
990 ms |
178564 KB |
Output is correct |
13 |
Correct |
1174 ms |
185748 KB |
Output is correct |
14 |
Correct |
1208 ms |
190792 KB |
Output is correct |
15 |
Correct |
1065 ms |
190436 KB |
Output is correct |
16 |
Correct |
1034 ms |
190576 KB |
Output is correct |
17 |
Correct |
1100 ms |
190992 KB |
Output is correct |
18 |
Correct |
5 ms |
24924 KB |
Output is correct |
19 |
Correct |
4 ms |
24924 KB |
Output is correct |
20 |
Correct |
4 ms |
24924 KB |
Output is correct |
21 |
Correct |
5 ms |
24924 KB |
Output is correct |
22 |
Correct |
4 ms |
24924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
24924 KB |
Output is correct |
2 |
Correct |
524 ms |
181916 KB |
Output is correct |
3 |
Correct |
993 ms |
178612 KB |
Output is correct |
4 |
Correct |
731 ms |
189268 KB |
Output is correct |
5 |
Correct |
695 ms |
180096 KB |
Output is correct |
6 |
Correct |
635 ms |
179792 KB |
Output is correct |
7 |
Correct |
663 ms |
180348 KB |
Output is correct |
8 |
Correct |
585 ms |
180548 KB |
Output is correct |
9 |
Correct |
554 ms |
195904 KB |
Output is correct |
10 |
Correct |
1068 ms |
191580 KB |
Output is correct |
11 |
Correct |
498 ms |
181656 KB |
Output is correct |
12 |
Correct |
990 ms |
178564 KB |
Output is correct |
13 |
Correct |
1174 ms |
185748 KB |
Output is correct |
14 |
Correct |
1208 ms |
190792 KB |
Output is correct |
15 |
Correct |
1065 ms |
190436 KB |
Output is correct |
16 |
Correct |
1034 ms |
190576 KB |
Output is correct |
17 |
Correct |
1100 ms |
190992 KB |
Output is correct |
18 |
Correct |
5 ms |
24924 KB |
Output is correct |
19 |
Correct |
4 ms |
24924 KB |
Output is correct |
20 |
Correct |
4 ms |
24924 KB |
Output is correct |
21 |
Correct |
5 ms |
24924 KB |
Output is correct |
22 |
Correct |
4 ms |
24924 KB |
Output is correct |
23 |
Correct |
3 ms |
24924 KB |
Output is correct |
24 |
Correct |
526 ms |
189716 KB |
Output is correct |
25 |
Correct |
1270 ms |
190400 KB |
Output is correct |
26 |
Correct |
866 ms |
204000 KB |
Output is correct |
27 |
Correct |
772 ms |
190372 KB |
Output is correct |
28 |
Correct |
766 ms |
190196 KB |
Output is correct |
29 |
Correct |
741 ms |
190000 KB |
Output is correct |
30 |
Correct |
701 ms |
190828 KB |
Output is correct |
31 |
Correct |
585 ms |
199516 KB |
Output is correct |
32 |
Correct |
1498 ms |
203448 KB |
Output is correct |
33 |
Correct |
542 ms |
189728 KB |
Output is correct |
34 |
Correct |
1338 ms |
190660 KB |
Output is correct |
35 |
Correct |
4 ms |
24920 KB |
Output is correct |
36 |
Correct |
4 ms |
24920 KB |
Output is correct |
37 |
Correct |
4 ms |
24924 KB |
Output is correct |
38 |
Correct |
4 ms |
25012 KB |
Output is correct |
39 |
Correct |
4 ms |
24924 KB |
Output is correct |
40 |
Correct |
4 ms |
24924 KB |
Output is correct |
41 |
Correct |
4 ms |
24924 KB |
Output is correct |
42 |
Correct |
4 ms |
24924 KB |
Output is correct |
43 |
Correct |
4 ms |
24924 KB |
Output is correct |
44 |
Correct |
4 ms |
25088 KB |
Output is correct |
45 |
Correct |
4 ms |
24924 KB |
Output is correct |
46 |
Correct |
4 ms |
25080 KB |
Output is correct |
47 |
Correct |
4 ms |
24924 KB |
Output is correct |
48 |
Correct |
4 ms |
24924 KB |
Output is correct |
49 |
Correct |
4 ms |
25176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
24924 KB |
Output is correct |
2 |
Correct |
875 ms |
193200 KB |
Output is correct |
3 |
Execution timed out |
4010 ms |
189040 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
24924 KB |
Output is correct |
2 |
Correct |
2578 ms |
191140 KB |
Output is correct |
3 |
Execution timed out |
4061 ms |
185592 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
24920 KB |
Output is correct |
2 |
Correct |
4 ms |
24924 KB |
Output is correct |
3 |
Correct |
4 ms |
24924 KB |
Output is correct |
4 |
Correct |
10 ms |
24924 KB |
Output is correct |
5 |
Correct |
11 ms |
25124 KB |
Output is correct |
6 |
Correct |
8 ms |
24924 KB |
Output is correct |
7 |
Correct |
7 ms |
24924 KB |
Output is correct |
8 |
Correct |
5 ms |
24924 KB |
Output is correct |
9 |
Correct |
4 ms |
24924 KB |
Output is correct |
10 |
Correct |
4 ms |
24920 KB |
Output is correct |
11 |
Correct |
4 ms |
24924 KB |
Output is correct |
12 |
Correct |
4 ms |
24924 KB |
Output is correct |
13 |
Correct |
4 ms |
24924 KB |
Output is correct |
14 |
Correct |
4 ms |
25092 KB |
Output is correct |
15 |
Correct |
4 ms |
25092 KB |
Output is correct |
16 |
Correct |
4 ms |
24924 KB |
Output is correct |
17 |
Correct |
4 ms |
24920 KB |
Output is correct |
18 |
Correct |
4 ms |
24924 KB |
Output is correct |
19 |
Correct |
4 ms |
25088 KB |
Output is correct |
20 |
Correct |
4 ms |
24924 KB |
Output is correct |
21 |
Correct |
4 ms |
24924 KB |
Output is correct |
22 |
Correct |
4 ms |
24920 KB |
Output is correct |
23 |
Correct |
4 ms |
24924 KB |
Output is correct |
24 |
Correct |
4 ms |
24924 KB |
Output is correct |
25 |
Correct |
4 ms |
24920 KB |
Output is correct |
26 |
Correct |
5 ms |
24920 KB |
Output is correct |
27 |
Correct |
4 ms |
24924 KB |
Output is correct |
28 |
Correct |
4 ms |
24924 KB |
Output is correct |
29 |
Correct |
3 ms |
24924 KB |
Output is correct |
30 |
Correct |
524 ms |
181916 KB |
Output is correct |
31 |
Correct |
993 ms |
178612 KB |
Output is correct |
32 |
Correct |
731 ms |
189268 KB |
Output is correct |
33 |
Correct |
695 ms |
180096 KB |
Output is correct |
34 |
Correct |
635 ms |
179792 KB |
Output is correct |
35 |
Correct |
663 ms |
180348 KB |
Output is correct |
36 |
Correct |
585 ms |
180548 KB |
Output is correct |
37 |
Correct |
554 ms |
195904 KB |
Output is correct |
38 |
Correct |
1068 ms |
191580 KB |
Output is correct |
39 |
Correct |
498 ms |
181656 KB |
Output is correct |
40 |
Correct |
990 ms |
178564 KB |
Output is correct |
41 |
Correct |
1174 ms |
185748 KB |
Output is correct |
42 |
Correct |
1208 ms |
190792 KB |
Output is correct |
43 |
Correct |
1065 ms |
190436 KB |
Output is correct |
44 |
Correct |
1034 ms |
190576 KB |
Output is correct |
45 |
Correct |
1100 ms |
190992 KB |
Output is correct |
46 |
Correct |
5 ms |
24924 KB |
Output is correct |
47 |
Correct |
4 ms |
24924 KB |
Output is correct |
48 |
Correct |
4 ms |
24924 KB |
Output is correct |
49 |
Correct |
5 ms |
24924 KB |
Output is correct |
50 |
Correct |
4 ms |
24924 KB |
Output is correct |
51 |
Correct |
3 ms |
24924 KB |
Output is correct |
52 |
Correct |
526 ms |
189716 KB |
Output is correct |
53 |
Correct |
1270 ms |
190400 KB |
Output is correct |
54 |
Correct |
866 ms |
204000 KB |
Output is correct |
55 |
Correct |
772 ms |
190372 KB |
Output is correct |
56 |
Correct |
766 ms |
190196 KB |
Output is correct |
57 |
Correct |
741 ms |
190000 KB |
Output is correct |
58 |
Correct |
701 ms |
190828 KB |
Output is correct |
59 |
Correct |
585 ms |
199516 KB |
Output is correct |
60 |
Correct |
1498 ms |
203448 KB |
Output is correct |
61 |
Correct |
542 ms |
189728 KB |
Output is correct |
62 |
Correct |
1338 ms |
190660 KB |
Output is correct |
63 |
Correct |
4 ms |
24920 KB |
Output is correct |
64 |
Correct |
4 ms |
24920 KB |
Output is correct |
65 |
Correct |
4 ms |
24924 KB |
Output is correct |
66 |
Correct |
4 ms |
25012 KB |
Output is correct |
67 |
Correct |
4 ms |
24924 KB |
Output is correct |
68 |
Correct |
4 ms |
24924 KB |
Output is correct |
69 |
Correct |
4 ms |
24924 KB |
Output is correct |
70 |
Correct |
4 ms |
24924 KB |
Output is correct |
71 |
Correct |
4 ms |
24924 KB |
Output is correct |
72 |
Correct |
4 ms |
25088 KB |
Output is correct |
73 |
Correct |
4 ms |
24924 KB |
Output is correct |
74 |
Correct |
4 ms |
25080 KB |
Output is correct |
75 |
Correct |
4 ms |
24924 KB |
Output is correct |
76 |
Correct |
4 ms |
24924 KB |
Output is correct |
77 |
Correct |
4 ms |
25176 KB |
Output is correct |
78 |
Correct |
3 ms |
24924 KB |
Output is correct |
79 |
Correct |
875 ms |
193200 KB |
Output is correct |
80 |
Execution timed out |
4010 ms |
189040 KB |
Time limit exceeded |
81 |
Halted |
0 ms |
0 KB |
- |