#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define f first
#define s second
#define pii pair <int,int>
#define en '\n'
#define all(v) v.begin(),v.end()
void boos() {
ios_base :: sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const int N = 4e5 + 100;
const int bl = 400;
vector <int> g[N];
ll a[N],L;
int h[N],tin[N],tout[N],Tima,pr[N],mxh;
int n,k[N];
vector <ll> t[N],t1[N];
vector <int> q[N];
void dfs_build(int v,int p) {
tin[v] = ++Tima;
q[h[v]].pb(tin[v]);
for(auto to : g[v]) {
if(to == p)continue;
h[to] = h[v] + 1;
pr[to] = v;
dfs_build(to,v);
}
tout[v] = Tima;
}
void push(int d,int v) {
t[d][v + v] = (t[d][v + v] * t1[d][v]) % L;
t[d][v + v + 1] = (t[d][v + v + 1] * t1[d][v]) % L;
t1[d][v + v] = (t1[d][v + v] * t1[d][v]) % L;
t1[d][v + v + 1] = (t1[d][v + v + 1] * t1[d][v]) % L;
t1[d][v] = 1;
}
void upd(int d,int v,int tl,int tr,int l,int r,ll x) {
if(q[d][tl] > r || q[d][tr] < l)return;
if(q[d][tl] >= l && q[d][tr] <= r) {
t1[d][v] = (t1[d][v] * x) % L;
t[d][v] = (t[d][v] * x) % L;
return;
}
push(d,v);
int tm = (tl + tr) / 2;
upd(d,v + v,tl,tm,l,r,x);
upd(d,v + v + 1,tm + 1,tr,l,r,x);
}
ll get(int d,int v,int tl,int tr,int pos) {
//cout << v << " " << tl << " " << tr << endl;
if(tl == tr)return t[d][v];
push(d,v);
int tm = (tl + tr) / 2;
if(q[d][tm] >= pos)return get(d,v + v,tl,tm,pos);
else return get(d,v + v + 1,tm + 1,tr,pos);
}
void build() {
for(int i = 1;i <= n;i++) {
if(q[i].empty())break;
k[i] = q[i].size();
q[i].pb(0);
sort(all(q[i]));
for(int j = 0;j <= 4 * k[i];j++) {
t[i].pb(1);
t1[i].pb(1);
}
mxh = i;
}
}
void add(int x,int dist,ll w) {
int v = x;
int tek = 0;
while(tek <= dist) {
int need = h[v] + dist - tek;
if(need <= mxh)upd(need,1,1,k[need],tin[v],tout[v],w);
if(x == 4) {
//cout << need << " ";
}
if(dist - tek > 0 && pr[v] != -1) {
need--;
//cout << need << " ";
if(need <= mxh)upd(need,1,1,k[need],tin[v],tout[v],w);
}
if(pr[v] != -1)v = pr[v];
tek++;
}
}
ll getans(int pos) {
//cout << h[pos] << " " << k[h[pos]] << endl;
ll val = get(h[pos],1,1,k[h[pos]],tin[pos]);
//cout << val << endl;
return (val * a[pos]) % L;
}
void solve() {
cin >> n >> L;
for(int i = 1;i <= n;i++) {
pr[i] = -1;
}
for(int i = 1;i < n;i++) {
int x,y;
cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
for(int i = 1;i <= n;i++)cin >> a[i];
h[1] = 1;
dfs_build(1,-1);
build();
int m;
cin >> m;
for(int i = 1;i <= m;i++) {
int typ;
cin >> typ;
if(typ == 1) {
int x,dist;
ll w;
cin >> x >> dist >> w;
add(x,dist,w);
}else {
int x;
cin >> x;
cout << getans(x) << en;
}
}
}
int main() {
boos();
int ttt;
ttt = 1;
while(ttt--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
37972 KB |
Output is correct |
2 |
Correct |
22 ms |
37844 KB |
Output is correct |
3 |
Correct |
19 ms |
37936 KB |
Output is correct |
4 |
Correct |
20 ms |
38128 KB |
Output is correct |
5 |
Correct |
20 ms |
38056 KB |
Output is correct |
6 |
Correct |
19 ms |
38096 KB |
Output is correct |
7 |
Correct |
19 ms |
38100 KB |
Output is correct |
8 |
Correct |
18 ms |
38100 KB |
Output is correct |
9 |
Correct |
18 ms |
37972 KB |
Output is correct |
10 |
Correct |
19 ms |
37888 KB |
Output is correct |
11 |
Correct |
20 ms |
37860 KB |
Output is correct |
12 |
Correct |
20 ms |
37972 KB |
Output is correct |
13 |
Correct |
20 ms |
37924 KB |
Output is correct |
14 |
Correct |
18 ms |
37924 KB |
Output is correct |
15 |
Correct |
19 ms |
37972 KB |
Output is correct |
16 |
Correct |
18 ms |
37912 KB |
Output is correct |
17 |
Correct |
18 ms |
37924 KB |
Output is correct |
18 |
Correct |
19 ms |
37932 KB |
Output is correct |
19 |
Correct |
19 ms |
37920 KB |
Output is correct |
20 |
Correct |
20 ms |
37944 KB |
Output is correct |
21 |
Correct |
23 ms |
37844 KB |
Output is correct |
22 |
Correct |
19 ms |
37836 KB |
Output is correct |
23 |
Correct |
19 ms |
37972 KB |
Output is correct |
24 |
Correct |
18 ms |
37972 KB |
Output is correct |
25 |
Correct |
19 ms |
37972 KB |
Output is correct |
26 |
Correct |
18 ms |
37924 KB |
Output is correct |
27 |
Correct |
18 ms |
37972 KB |
Output is correct |
28 |
Correct |
18 ms |
37944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
37908 KB |
Output is correct |
2 |
Correct |
478 ms |
79460 KB |
Output is correct |
3 |
Correct |
752 ms |
77388 KB |
Output is correct |
4 |
Correct |
483 ms |
102300 KB |
Output is correct |
5 |
Correct |
597 ms |
78988 KB |
Output is correct |
6 |
Correct |
604 ms |
76448 KB |
Output is correct |
7 |
Correct |
665 ms |
75240 KB |
Output is correct |
8 |
Correct |
481 ms |
75244 KB |
Output is correct |
9 |
Correct |
462 ms |
110800 KB |
Output is correct |
10 |
Correct |
609 ms |
107940 KB |
Output is correct |
11 |
Correct |
430 ms |
78996 KB |
Output is correct |
12 |
Correct |
788 ms |
78736 KB |
Output is correct |
13 |
Correct |
331 ms |
76276 KB |
Output is correct |
14 |
Correct |
701 ms |
75260 KB |
Output is correct |
15 |
Correct |
797 ms |
76080 KB |
Output is correct |
16 |
Correct |
746 ms |
77180 KB |
Output is correct |
17 |
Correct |
849 ms |
76300 KB |
Output is correct |
18 |
Correct |
24 ms |
37876 KB |
Output is correct |
19 |
Correct |
23 ms |
37880 KB |
Output is correct |
20 |
Correct |
18 ms |
37972 KB |
Output is correct |
21 |
Correct |
18 ms |
37964 KB |
Output is correct |
22 |
Correct |
20 ms |
37972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
37908 KB |
Output is correct |
2 |
Correct |
478 ms |
79460 KB |
Output is correct |
3 |
Correct |
752 ms |
77388 KB |
Output is correct |
4 |
Correct |
483 ms |
102300 KB |
Output is correct |
5 |
Correct |
597 ms |
78988 KB |
Output is correct |
6 |
Correct |
604 ms |
76448 KB |
Output is correct |
7 |
Correct |
665 ms |
75240 KB |
Output is correct |
8 |
Correct |
481 ms |
75244 KB |
Output is correct |
9 |
Correct |
462 ms |
110800 KB |
Output is correct |
10 |
Correct |
609 ms |
107940 KB |
Output is correct |
11 |
Correct |
430 ms |
78996 KB |
Output is correct |
12 |
Correct |
788 ms |
78736 KB |
Output is correct |
13 |
Correct |
331 ms |
76276 KB |
Output is correct |
14 |
Correct |
701 ms |
75260 KB |
Output is correct |
15 |
Correct |
797 ms |
76080 KB |
Output is correct |
16 |
Correct |
746 ms |
77180 KB |
Output is correct |
17 |
Correct |
849 ms |
76300 KB |
Output is correct |
18 |
Correct |
24 ms |
37876 KB |
Output is correct |
19 |
Correct |
23 ms |
37880 KB |
Output is correct |
20 |
Correct |
18 ms |
37972 KB |
Output is correct |
21 |
Correct |
18 ms |
37964 KB |
Output is correct |
22 |
Correct |
20 ms |
37972 KB |
Output is correct |
23 |
Correct |
18 ms |
37904 KB |
Output is correct |
24 |
Correct |
524 ms |
78784 KB |
Output is correct |
25 |
Correct |
1004 ms |
79692 KB |
Output is correct |
26 |
Correct |
531 ms |
109948 KB |
Output is correct |
27 |
Correct |
624 ms |
79608 KB |
Output is correct |
28 |
Correct |
720 ms |
76776 KB |
Output is correct |
29 |
Correct |
701 ms |
75452 KB |
Output is correct |
30 |
Correct |
556 ms |
75404 KB |
Output is correct |
31 |
Correct |
474 ms |
103156 KB |
Output is correct |
32 |
Correct |
623 ms |
109228 KB |
Output is correct |
33 |
Correct |
457 ms |
79276 KB |
Output is correct |
34 |
Correct |
1008 ms |
78964 KB |
Output is correct |
35 |
Correct |
18 ms |
37972 KB |
Output is correct |
36 |
Correct |
21 ms |
37972 KB |
Output is correct |
37 |
Correct |
18 ms |
37972 KB |
Output is correct |
38 |
Correct |
19 ms |
37888 KB |
Output is correct |
39 |
Correct |
19 ms |
37928 KB |
Output is correct |
40 |
Correct |
18 ms |
37844 KB |
Output is correct |
41 |
Correct |
19 ms |
37844 KB |
Output is correct |
42 |
Correct |
18 ms |
37912 KB |
Output is correct |
43 |
Correct |
19 ms |
37916 KB |
Output is correct |
44 |
Correct |
21 ms |
37844 KB |
Output is correct |
45 |
Correct |
18 ms |
37912 KB |
Output is correct |
46 |
Correct |
18 ms |
37864 KB |
Output is correct |
47 |
Correct |
20 ms |
37872 KB |
Output is correct |
48 |
Correct |
19 ms |
37972 KB |
Output is correct |
49 |
Correct |
18 ms |
37972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
37836 KB |
Output is correct |
2 |
Correct |
645 ms |
107400 KB |
Output is correct |
3 |
Correct |
2534 ms |
102124 KB |
Output is correct |
4 |
Correct |
1112 ms |
102456 KB |
Output is correct |
5 |
Correct |
3376 ms |
75368 KB |
Output is correct |
6 |
Correct |
1970 ms |
73184 KB |
Output is correct |
7 |
Correct |
1018 ms |
72056 KB |
Output is correct |
8 |
Correct |
360 ms |
71852 KB |
Output is correct |
9 |
Correct |
699 ms |
100368 KB |
Output is correct |
10 |
Correct |
2309 ms |
105260 KB |
Output is correct |
11 |
Correct |
1577 ms |
75152 KB |
Output is correct |
12 |
Execution timed out |
4067 ms |
73344 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
37844 KB |
Output is correct |
2 |
Correct |
769 ms |
101860 KB |
Output is correct |
3 |
Correct |
2788 ms |
96452 KB |
Output is correct |
4 |
Correct |
1162 ms |
99316 KB |
Output is correct |
5 |
Correct |
3376 ms |
78316 KB |
Output is correct |
6 |
Correct |
2017 ms |
73936 KB |
Output is correct |
7 |
Correct |
1432 ms |
73380 KB |
Output is correct |
8 |
Correct |
429 ms |
73232 KB |
Output is correct |
9 |
Correct |
728 ms |
108508 KB |
Output is correct |
10 |
Correct |
2271 ms |
107248 KB |
Output is correct |
11 |
Correct |
1623 ms |
78468 KB |
Output is correct |
12 |
Execution timed out |
4064 ms |
72888 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
37972 KB |
Output is correct |
2 |
Correct |
22 ms |
37844 KB |
Output is correct |
3 |
Correct |
19 ms |
37936 KB |
Output is correct |
4 |
Correct |
20 ms |
38128 KB |
Output is correct |
5 |
Correct |
20 ms |
38056 KB |
Output is correct |
6 |
Correct |
19 ms |
38096 KB |
Output is correct |
7 |
Correct |
19 ms |
38100 KB |
Output is correct |
8 |
Correct |
18 ms |
38100 KB |
Output is correct |
9 |
Correct |
18 ms |
37972 KB |
Output is correct |
10 |
Correct |
19 ms |
37888 KB |
Output is correct |
11 |
Correct |
20 ms |
37860 KB |
Output is correct |
12 |
Correct |
20 ms |
37972 KB |
Output is correct |
13 |
Correct |
20 ms |
37924 KB |
Output is correct |
14 |
Correct |
18 ms |
37924 KB |
Output is correct |
15 |
Correct |
19 ms |
37972 KB |
Output is correct |
16 |
Correct |
18 ms |
37912 KB |
Output is correct |
17 |
Correct |
18 ms |
37924 KB |
Output is correct |
18 |
Correct |
19 ms |
37932 KB |
Output is correct |
19 |
Correct |
19 ms |
37920 KB |
Output is correct |
20 |
Correct |
20 ms |
37944 KB |
Output is correct |
21 |
Correct |
23 ms |
37844 KB |
Output is correct |
22 |
Correct |
19 ms |
37836 KB |
Output is correct |
23 |
Correct |
19 ms |
37972 KB |
Output is correct |
24 |
Correct |
18 ms |
37972 KB |
Output is correct |
25 |
Correct |
19 ms |
37972 KB |
Output is correct |
26 |
Correct |
18 ms |
37924 KB |
Output is correct |
27 |
Correct |
18 ms |
37972 KB |
Output is correct |
28 |
Correct |
18 ms |
37944 KB |
Output is correct |
29 |
Correct |
18 ms |
37908 KB |
Output is correct |
30 |
Correct |
478 ms |
79460 KB |
Output is correct |
31 |
Correct |
752 ms |
77388 KB |
Output is correct |
32 |
Correct |
483 ms |
102300 KB |
Output is correct |
33 |
Correct |
597 ms |
78988 KB |
Output is correct |
34 |
Correct |
604 ms |
76448 KB |
Output is correct |
35 |
Correct |
665 ms |
75240 KB |
Output is correct |
36 |
Correct |
481 ms |
75244 KB |
Output is correct |
37 |
Correct |
462 ms |
110800 KB |
Output is correct |
38 |
Correct |
609 ms |
107940 KB |
Output is correct |
39 |
Correct |
430 ms |
78996 KB |
Output is correct |
40 |
Correct |
788 ms |
78736 KB |
Output is correct |
41 |
Correct |
331 ms |
76276 KB |
Output is correct |
42 |
Correct |
701 ms |
75260 KB |
Output is correct |
43 |
Correct |
797 ms |
76080 KB |
Output is correct |
44 |
Correct |
746 ms |
77180 KB |
Output is correct |
45 |
Correct |
849 ms |
76300 KB |
Output is correct |
46 |
Correct |
24 ms |
37876 KB |
Output is correct |
47 |
Correct |
23 ms |
37880 KB |
Output is correct |
48 |
Correct |
18 ms |
37972 KB |
Output is correct |
49 |
Correct |
18 ms |
37964 KB |
Output is correct |
50 |
Correct |
20 ms |
37972 KB |
Output is correct |
51 |
Correct |
18 ms |
37904 KB |
Output is correct |
52 |
Correct |
524 ms |
78784 KB |
Output is correct |
53 |
Correct |
1004 ms |
79692 KB |
Output is correct |
54 |
Correct |
531 ms |
109948 KB |
Output is correct |
55 |
Correct |
624 ms |
79608 KB |
Output is correct |
56 |
Correct |
720 ms |
76776 KB |
Output is correct |
57 |
Correct |
701 ms |
75452 KB |
Output is correct |
58 |
Correct |
556 ms |
75404 KB |
Output is correct |
59 |
Correct |
474 ms |
103156 KB |
Output is correct |
60 |
Correct |
623 ms |
109228 KB |
Output is correct |
61 |
Correct |
457 ms |
79276 KB |
Output is correct |
62 |
Correct |
1008 ms |
78964 KB |
Output is correct |
63 |
Correct |
18 ms |
37972 KB |
Output is correct |
64 |
Correct |
21 ms |
37972 KB |
Output is correct |
65 |
Correct |
18 ms |
37972 KB |
Output is correct |
66 |
Correct |
19 ms |
37888 KB |
Output is correct |
67 |
Correct |
19 ms |
37928 KB |
Output is correct |
68 |
Correct |
18 ms |
37844 KB |
Output is correct |
69 |
Correct |
19 ms |
37844 KB |
Output is correct |
70 |
Correct |
18 ms |
37912 KB |
Output is correct |
71 |
Correct |
19 ms |
37916 KB |
Output is correct |
72 |
Correct |
21 ms |
37844 KB |
Output is correct |
73 |
Correct |
18 ms |
37912 KB |
Output is correct |
74 |
Correct |
18 ms |
37864 KB |
Output is correct |
75 |
Correct |
20 ms |
37872 KB |
Output is correct |
76 |
Correct |
19 ms |
37972 KB |
Output is correct |
77 |
Correct |
18 ms |
37972 KB |
Output is correct |
78 |
Correct |
18 ms |
37836 KB |
Output is correct |
79 |
Correct |
645 ms |
107400 KB |
Output is correct |
80 |
Correct |
2534 ms |
102124 KB |
Output is correct |
81 |
Correct |
1112 ms |
102456 KB |
Output is correct |
82 |
Correct |
3376 ms |
75368 KB |
Output is correct |
83 |
Correct |
1970 ms |
73184 KB |
Output is correct |
84 |
Correct |
1018 ms |
72056 KB |
Output is correct |
85 |
Correct |
360 ms |
71852 KB |
Output is correct |
86 |
Correct |
699 ms |
100368 KB |
Output is correct |
87 |
Correct |
2309 ms |
105260 KB |
Output is correct |
88 |
Correct |
1577 ms |
75152 KB |
Output is correct |
89 |
Execution timed out |
4067 ms |
73344 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |