제출 #922584

#제출 시각아이디문제언어결과실행 시간메모리
922584huutuanSprinkler (JOI22_sprinkler)C++14
41 / 100
4051 ms356968 KiB
#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) const int MAX_DIST=40; int mod; int memory[30000000]; int sz; struct FenwickTree{ int n; int mult; int *pf, *sf; void init(int _n){ n=_n; mult=1; pf=memory+sz; sz+=n; sf=memory+sz; sz+=n; } void update(int *t, int pos, int val){ if (pos<1 || pos>n) return; 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){ update(pf, n-(pos-1)+1, val); update(sf, pos+1, val); } int get(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, n-pos+1)%mod*get(sf, pos)%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<=MAX_DIST+1; ++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=MAX_DIST+1-d; i<=MAX_DIST+1; 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=MAX_DIST+1-d; i<=MAX_DIST+1; 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)<=MAX_DIST){ int p=par[lca]; for (int i=MAX_DIST+1-dist; i; i-=i&(-i)) val=1ll*val*mult[p][i].get(idx[lca]+1)%mod; lca=par[lca]; } return val; } void solve(){ fill(memory, memory+30000000, 1); 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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...