#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2")
#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 FenwickTree{
int n;
int mult;
vector<int> pf, sf;
void init(int _n){
n=_n;
mult=1;
pf.assign(n+1, 1);
sf.assign(n+1, 1);
}
void update(vector<int> &t, int pos, int val){
for (int i=pos; i<=n; i+=i&(-i)) t[i]=1ll*t[i]*val%mod;
}
void update_all(int val){
mult=1ll*mult*val%mod;
}
void update(int pos, int val){
if (pos-1) update(pf, pos-1, val);
if (n-pos) update(sf, n-pos, val);
}
int get(vector<int> &t, int pos){
int ans=1;
for (int i=pos; i; i-=i&(-i)) ans=1ll*ans*t[i]%mod;
return ans;
}
int get(int pos){
return 1ll*mult*get(pf, pos)%mod*get(sf, n-pos+1)%mod;
}
};
const int N=2e5+10;
int n, q, tin[N], tout[N], tdfs, par[N], idx[N], h[N];
FenwickTree 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_all(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(t+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]).first%mod;
for (int i=41-dist; i; i-=i&(-i)) val=1ll*val*mult[p][i].get(idx[lca]+1)%mod;
lca=par[lca];
}
return val;
}
void solve(){
#ifdef sus
freopen("cf.inp", "r", stdin);
freopen("cf.out", "w", stdout);
#endif
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 |
172 ms |
466444 KB |
Output is correct |
2 |
Correct |
136 ms |
466440 KB |
Output is correct |
3 |
Correct |
131 ms |
466348 KB |
Output is correct |
4 |
Incorrect |
147 ms |
469196 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
466444 KB |
Output is correct |
2 |
Execution timed out |
4100 ms |
1011724 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
466444 KB |
Output is correct |
2 |
Execution timed out |
4100 ms |
1011724 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
466256 KB |
Output is correct |
2 |
Execution timed out |
4075 ms |
1030176 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
142 ms |
466256 KB |
Output is correct |
2 |
Execution timed out |
4085 ms |
1024772 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
466444 KB |
Output is correct |
2 |
Correct |
136 ms |
466440 KB |
Output is correct |
3 |
Correct |
131 ms |
466348 KB |
Output is correct |
4 |
Incorrect |
147 ms |
469196 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |