#include<bits/stdc++.h>
using namespace std;
// #define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define isz(x) ((int)x.size())
#define sumof(x) accumulate(all(x), 0ll)
int mod;
struct Node{
int val, lazy;
Node (int t=1){
val=t;
lazy=1;
}
Node operator+(Node b){
return Node(1ll*val*b.val%mod);
}
};
struct SegmentTree{
int n;
vector<Node> t;
void init(int _n){
n=_n;
t.assign(4*n+1, Node());
}
void apply(int k, int val){
t[k].val=1ll*t[k].val*val%mod;
t[k].lazy=1ll*t[k].lazy*val%mod;
}
void push(int k){
if (t[k].lazy!=1){
apply(k<<1, t[k].lazy);
apply(k<<1|1, t[k].lazy);
t[k].lazy=1;
}
}
void update(int k, int l, int r, int L, int R, int val){
if (r<L || R<l) return;
if (L<=l && r<=R){
apply(k, val);
return;
}
push(k);
int mid=(l+r)>>1;
update(k<<1, l, mid, L, R, val);
update(k<<1|1, mid+1, r, L, R, val);
t[k]=t[k<<1]+t[k<<1|1];
}
Node get(int k, int l, int r, int pos){
if (l==r) return t[k];
push(k);
int mid=(l+r)>>1;
if (pos<=mid) return get(k<<1, l, mid, pos);
return get(k<<1|1, mid+1, r, pos);
}
};
const int N=2e5+10;
int n, q, tin[N], tout[N], tdfs, par[N], idx[N], h[N];
SegmentTree mult[N][42];
vector<int> g[N];
void dfs(int u, int p){
par[u]=p;
tin[u]=++tdfs;
if (p) g[u].erase(find(all(g[u]), p));
for (int i=0; i<isz(g[u]); ++i){
idx[g[u][i]]=i;
dfs(g[u][i], u);
}
for (int i=0; i<=41; ++i) mult[u][i].init(g[u].size());
tout[u]=tdfs;
}
void update(int u, int d, int w){
int lca=u;
// for (int i=0; i<=d; ++i) mult[u][i].update(1, 0, isz(g[u])-1, 0, isz(g[u])-1, w);
for (int i=41-d; i<=41; i+=i&(-i)) mult[u][i].update(1, 0, isz(g[u])-1, 0, isz(g[u])-1, w);
h[u]=1ll*h[u]*w%mod;
while (par[lca] && (--d)>=0){
int p=par[lca];
h[p]=1ll*h[p]*w%mod;
int t=idx[lca];
// for (int i=0; i<=d; ++i){
// mult[p][i].update(1, 0, isz(g[p])-1, 0, t-1, w);
// mult[p][i].update(1, 0, isz(g[p])-1, t+1, isz(g[p])-1, w);
// }
for (int i=41-d; i<=41; i+=i&(-i)){
mult[p][i].update(1, 0, isz(g[p])-1, 0, t-1, w);
mult[p][i].update(1, 0, isz(g[p])-1, t+1, isz(g[p])-1, w);
}
lca=par[lca];
}
}
int query(int u){
int val=h[u];
int lca=u, dist=0;
while (par[lca] && (++dist)<=40){
int p=par[lca];
// val=1ll*val*mult[p][dist].get(1, 0, isz(g[p])-1, idx[lca]).val%mod;
for (int i=41-dist; i; i-=i&(-i)) val=1ll*val*mult[p][i].get(1, 0, isz(g[p])-1, idx[lca]).val%mod;
lca=par[lca];
}
return val;
}
void solve(){
cin >> n >> mod;
for (int i=0; i<n-1; ++i){
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
for (int i=1; i<=n; ++i) cin >> h[i];
cin >> q;
while (q--){
int o; cin >> o;
if (o==1){
int u, d, w; cin >> u >> d >> w;
update(u, d, w);
}else{
int u; cin >> u;
cout << query(u) << '\n';
}
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int ntests=1;
// cin >> ntests;
for (int i=1; i<=ntests; ++i) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
268116 KB |
Output is correct |
2 |
Correct |
122 ms |
268112 KB |
Output is correct |
3 |
Correct |
125 ms |
268036 KB |
Output is correct |
4 |
Correct |
127 ms |
270164 KB |
Output is correct |
5 |
Correct |
127 ms |
270420 KB |
Output is correct |
6 |
Correct |
126 ms |
270420 KB |
Output is correct |
7 |
Correct |
154 ms |
270640 KB |
Output is correct |
8 |
Correct |
128 ms |
270748 KB |
Output is correct |
9 |
Correct |
130 ms |
268120 KB |
Output is correct |
10 |
Correct |
125 ms |
268116 KB |
Output is correct |
11 |
Correct |
132 ms |
268216 KB |
Output is correct |
12 |
Correct |
123 ms |
268116 KB |
Output is correct |
13 |
Correct |
123 ms |
268152 KB |
Output is correct |
14 |
Correct |
122 ms |
268036 KB |
Output is correct |
15 |
Correct |
128 ms |
268120 KB |
Output is correct |
16 |
Correct |
124 ms |
268172 KB |
Output is correct |
17 |
Correct |
126 ms |
268116 KB |
Output is correct |
18 |
Correct |
124 ms |
268116 KB |
Output is correct |
19 |
Correct |
123 ms |
268112 KB |
Output is correct |
20 |
Correct |
124 ms |
268116 KB |
Output is correct |
21 |
Correct |
123 ms |
268116 KB |
Output is correct |
22 |
Correct |
126 ms |
267976 KB |
Output is correct |
23 |
Correct |
124 ms |
268112 KB |
Output is correct |
24 |
Correct |
127 ms |
267972 KB |
Output is correct |
25 |
Correct |
125 ms |
268128 KB |
Output is correct |
26 |
Correct |
124 ms |
267968 KB |
Output is correct |
27 |
Correct |
127 ms |
268072 KB |
Output is correct |
28 |
Correct |
122 ms |
268116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
268144 KB |
Output is correct |
2 |
Correct |
3825 ms |
733108 KB |
Output is correct |
3 |
Correct |
1467 ms |
733988 KB |
Output is correct |
4 |
Correct |
2314 ms |
694676 KB |
Output is correct |
5 |
Correct |
2727 ms |
733076 KB |
Output is correct |
6 |
Correct |
1417 ms |
750252 KB |
Output is correct |
7 |
Correct |
1505 ms |
763584 KB |
Output is correct |
8 |
Correct |
1100 ms |
817204 KB |
Output is correct |
9 |
Correct |
3268 ms |
699520 KB |
Output is correct |
10 |
Correct |
1289 ms |
698880 KB |
Output is correct |
11 |
Correct |
3718 ms |
733200 KB |
Output is correct |
12 |
Correct |
1447 ms |
733580 KB |
Output is correct |
13 |
Correct |
889 ms |
804180 KB |
Output is correct |
14 |
Correct |
1140 ms |
805300 KB |
Output is correct |
15 |
Correct |
1156 ms |
805456 KB |
Output is correct |
16 |
Correct |
1141 ms |
806980 KB |
Output is correct |
17 |
Correct |
1384 ms |
805224 KB |
Output is correct |
18 |
Correct |
116 ms |
268112 KB |
Output is correct |
19 |
Correct |
154 ms |
268372 KB |
Output is correct |
20 |
Correct |
119 ms |
268116 KB |
Output is correct |
21 |
Correct |
118 ms |
268216 KB |
Output is correct |
22 |
Correct |
116 ms |
268216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
268144 KB |
Output is correct |
2 |
Correct |
3825 ms |
733108 KB |
Output is correct |
3 |
Correct |
1467 ms |
733988 KB |
Output is correct |
4 |
Correct |
2314 ms |
694676 KB |
Output is correct |
5 |
Correct |
2727 ms |
733076 KB |
Output is correct |
6 |
Correct |
1417 ms |
750252 KB |
Output is correct |
7 |
Correct |
1505 ms |
763584 KB |
Output is correct |
8 |
Correct |
1100 ms |
817204 KB |
Output is correct |
9 |
Correct |
3268 ms |
699520 KB |
Output is correct |
10 |
Correct |
1289 ms |
698880 KB |
Output is correct |
11 |
Correct |
3718 ms |
733200 KB |
Output is correct |
12 |
Correct |
1447 ms |
733580 KB |
Output is correct |
13 |
Correct |
889 ms |
804180 KB |
Output is correct |
14 |
Correct |
1140 ms |
805300 KB |
Output is correct |
15 |
Correct |
1156 ms |
805456 KB |
Output is correct |
16 |
Correct |
1141 ms |
806980 KB |
Output is correct |
17 |
Correct |
1384 ms |
805224 KB |
Output is correct |
18 |
Correct |
116 ms |
268112 KB |
Output is correct |
19 |
Correct |
154 ms |
268372 KB |
Output is correct |
20 |
Correct |
119 ms |
268116 KB |
Output is correct |
21 |
Correct |
118 ms |
268216 KB |
Output is correct |
22 |
Correct |
116 ms |
268216 KB |
Output is correct |
23 |
Correct |
114 ms |
268132 KB |
Output is correct |
24 |
Correct |
3704 ms |
732908 KB |
Output is correct |
25 |
Correct |
1509 ms |
733788 KB |
Output is correct |
26 |
Correct |
2208 ms |
699184 KB |
Output is correct |
27 |
Correct |
2748 ms |
733300 KB |
Output is correct |
28 |
Correct |
1514 ms |
750944 KB |
Output is correct |
29 |
Correct |
1532 ms |
763528 KB |
Output is correct |
30 |
Correct |
1255 ms |
817332 KB |
Output is correct |
31 |
Correct |
3320 ms |
695368 KB |
Output is correct |
32 |
Correct |
1271 ms |
699060 KB |
Output is correct |
33 |
Correct |
3645 ms |
732832 KB |
Output is correct |
34 |
Correct |
1511 ms |
733840 KB |
Output is correct |
35 |
Correct |
115 ms |
268200 KB |
Output is correct |
36 |
Correct |
120 ms |
268168 KB |
Output is correct |
37 |
Correct |
119 ms |
268112 KB |
Output is correct |
38 |
Correct |
123 ms |
268112 KB |
Output is correct |
39 |
Correct |
116 ms |
268092 KB |
Output is correct |
40 |
Correct |
115 ms |
268116 KB |
Output is correct |
41 |
Correct |
120 ms |
268348 KB |
Output is correct |
42 |
Correct |
119 ms |
268164 KB |
Output is correct |
43 |
Correct |
117 ms |
268216 KB |
Output is correct |
44 |
Correct |
116 ms |
268064 KB |
Output is correct |
45 |
Correct |
117 ms |
268112 KB |
Output is correct |
46 |
Correct |
118 ms |
268112 KB |
Output is correct |
47 |
Correct |
117 ms |
268172 KB |
Output is correct |
48 |
Correct |
131 ms |
268220 KB |
Output is correct |
49 |
Correct |
119 ms |
268112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
268132 KB |
Output is correct |
2 |
Correct |
3415 ms |
696600 KB |
Output is correct |
3 |
Correct |
2154 ms |
693620 KB |
Output is correct |
4 |
Correct |
2585 ms |
693748 KB |
Output is correct |
5 |
Execution timed out |
4040 ms |
730552 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
268164 KB |
Output is correct |
2 |
Correct |
3329 ms |
694312 KB |
Output is correct |
3 |
Correct |
2035 ms |
690680 KB |
Output is correct |
4 |
Correct |
2649 ms |
692720 KB |
Output is correct |
5 |
Execution timed out |
4072 ms |
732088 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
268116 KB |
Output is correct |
2 |
Correct |
122 ms |
268112 KB |
Output is correct |
3 |
Correct |
125 ms |
268036 KB |
Output is correct |
4 |
Correct |
127 ms |
270164 KB |
Output is correct |
5 |
Correct |
127 ms |
270420 KB |
Output is correct |
6 |
Correct |
126 ms |
270420 KB |
Output is correct |
7 |
Correct |
154 ms |
270640 KB |
Output is correct |
8 |
Correct |
128 ms |
270748 KB |
Output is correct |
9 |
Correct |
130 ms |
268120 KB |
Output is correct |
10 |
Correct |
125 ms |
268116 KB |
Output is correct |
11 |
Correct |
132 ms |
268216 KB |
Output is correct |
12 |
Correct |
123 ms |
268116 KB |
Output is correct |
13 |
Correct |
123 ms |
268152 KB |
Output is correct |
14 |
Correct |
122 ms |
268036 KB |
Output is correct |
15 |
Correct |
128 ms |
268120 KB |
Output is correct |
16 |
Correct |
124 ms |
268172 KB |
Output is correct |
17 |
Correct |
126 ms |
268116 KB |
Output is correct |
18 |
Correct |
124 ms |
268116 KB |
Output is correct |
19 |
Correct |
123 ms |
268112 KB |
Output is correct |
20 |
Correct |
124 ms |
268116 KB |
Output is correct |
21 |
Correct |
123 ms |
268116 KB |
Output is correct |
22 |
Correct |
126 ms |
267976 KB |
Output is correct |
23 |
Correct |
124 ms |
268112 KB |
Output is correct |
24 |
Correct |
127 ms |
267972 KB |
Output is correct |
25 |
Correct |
125 ms |
268128 KB |
Output is correct |
26 |
Correct |
124 ms |
267968 KB |
Output is correct |
27 |
Correct |
127 ms |
268072 KB |
Output is correct |
28 |
Correct |
122 ms |
268116 KB |
Output is correct |
29 |
Correct |
126 ms |
268144 KB |
Output is correct |
30 |
Correct |
3825 ms |
733108 KB |
Output is correct |
31 |
Correct |
1467 ms |
733988 KB |
Output is correct |
32 |
Correct |
2314 ms |
694676 KB |
Output is correct |
33 |
Correct |
2727 ms |
733076 KB |
Output is correct |
34 |
Correct |
1417 ms |
750252 KB |
Output is correct |
35 |
Correct |
1505 ms |
763584 KB |
Output is correct |
36 |
Correct |
1100 ms |
817204 KB |
Output is correct |
37 |
Correct |
3268 ms |
699520 KB |
Output is correct |
38 |
Correct |
1289 ms |
698880 KB |
Output is correct |
39 |
Correct |
3718 ms |
733200 KB |
Output is correct |
40 |
Correct |
1447 ms |
733580 KB |
Output is correct |
41 |
Correct |
889 ms |
804180 KB |
Output is correct |
42 |
Correct |
1140 ms |
805300 KB |
Output is correct |
43 |
Correct |
1156 ms |
805456 KB |
Output is correct |
44 |
Correct |
1141 ms |
806980 KB |
Output is correct |
45 |
Correct |
1384 ms |
805224 KB |
Output is correct |
46 |
Correct |
116 ms |
268112 KB |
Output is correct |
47 |
Correct |
154 ms |
268372 KB |
Output is correct |
48 |
Correct |
119 ms |
268116 KB |
Output is correct |
49 |
Correct |
118 ms |
268216 KB |
Output is correct |
50 |
Correct |
116 ms |
268216 KB |
Output is correct |
51 |
Correct |
114 ms |
268132 KB |
Output is correct |
52 |
Correct |
3704 ms |
732908 KB |
Output is correct |
53 |
Correct |
1509 ms |
733788 KB |
Output is correct |
54 |
Correct |
2208 ms |
699184 KB |
Output is correct |
55 |
Correct |
2748 ms |
733300 KB |
Output is correct |
56 |
Correct |
1514 ms |
750944 KB |
Output is correct |
57 |
Correct |
1532 ms |
763528 KB |
Output is correct |
58 |
Correct |
1255 ms |
817332 KB |
Output is correct |
59 |
Correct |
3320 ms |
695368 KB |
Output is correct |
60 |
Correct |
1271 ms |
699060 KB |
Output is correct |
61 |
Correct |
3645 ms |
732832 KB |
Output is correct |
62 |
Correct |
1511 ms |
733840 KB |
Output is correct |
63 |
Correct |
115 ms |
268200 KB |
Output is correct |
64 |
Correct |
120 ms |
268168 KB |
Output is correct |
65 |
Correct |
119 ms |
268112 KB |
Output is correct |
66 |
Correct |
123 ms |
268112 KB |
Output is correct |
67 |
Correct |
116 ms |
268092 KB |
Output is correct |
68 |
Correct |
115 ms |
268116 KB |
Output is correct |
69 |
Correct |
120 ms |
268348 KB |
Output is correct |
70 |
Correct |
119 ms |
268164 KB |
Output is correct |
71 |
Correct |
117 ms |
268216 KB |
Output is correct |
72 |
Correct |
116 ms |
268064 KB |
Output is correct |
73 |
Correct |
117 ms |
268112 KB |
Output is correct |
74 |
Correct |
118 ms |
268112 KB |
Output is correct |
75 |
Correct |
117 ms |
268172 KB |
Output is correct |
76 |
Correct |
131 ms |
268220 KB |
Output is correct |
77 |
Correct |
119 ms |
268112 KB |
Output is correct |
78 |
Correct |
117 ms |
268132 KB |
Output is correct |
79 |
Correct |
3415 ms |
696600 KB |
Output is correct |
80 |
Correct |
2154 ms |
693620 KB |
Output is correct |
81 |
Correct |
2585 ms |
693748 KB |
Output is correct |
82 |
Execution timed out |
4040 ms |
730552 KB |
Time limit exceeded |
83 |
Halted |
0 ms |
0 KB |
- |