제출 #674593

#제출 시각아이디문제언어결과실행 시간메모리
674593someoneSprinkler (JOI22_sprinkler)C++14
3 / 100
4054 ms168952 KiB
#include <bits/stdc++.h> #define int long long using namespace std; struct Upd { int i, dist, prod; }; const int M = 2e5 + 42, N = 2 * M, T = 19, INF = 1e18 + 42, MOD = 1e9 + 7; vector<int> val, adj[M]; int n, l, q, id = 0, h[M], pere[M], prof[M], deb[M], fin[M], inv[M], ln2[N], mini[T][N]; void dfs(int i, int pre, int depth = 0) { int f = id++; inv[f] = i; deb[i] = (int)val.size(); val.push_back(f); pere[i] = pre; prof[i] = depth; for(int j : adj[i]) if(j != pre) { dfs(j, i, depth+1); val.push_back(f); } fin[i] = (int)val.size(); } inline int dist(int a, int b) { if(deb[a] > deb[b]) swap(a, b); int d = deb[a], f = deb[b]+1, len = ln2[f - d], lca = inv[min(mini[len][d], mini[len][f - (1 << len)])]; return prof[a] + prof[b] - 2 * prof[lca]; } int ch[M][42]; vector<Upd> upd; void update() { for(int i = 0; i < M; i++) for(int j = 0; j < 42; j++) ch[i][j] = 1; for(Upd u : upd) { int i = u.i, d = u.dist; do { if(i == 0 || d < 2) { for(int j = 0; j <= d; j++) ch[i][j] = ch[i][j] * u.prod % l; } else { ch[i][d] = ch[i][d] * u.prod % l; ch[i][d-1] = ch[i][d-1] * u.prod % l; } d--; i = pere[i]; } while(i != -1 && d >= 0); } for(int i = 0; i < n; i++) { int j = i, d = 0; while(j >= 0 && d < 41) { h[i] = h[i] * ch[j][d] % l; j = pere[j]; d++; } } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); for(int i = 0; (1 << i) < N; i++) ln2[1 << i] = i; for(int i = 1; i < N; i++) ln2[i] = max(ln2[i], ln2[i-1]); cin >> n >> l; for(int i = 0; i < n-1; i++) { int a, b; cin >> a >> b; a--, b--; adj[a].push_back(b); adj[b].push_back(a); } dfs(0, -1); int sz = (int)val.size(); for(int i = 0; i < sz; i++) mini[0][i] = val[i]; for(int i = 1; i < 19; i++) for(int d = 0, m = (1 << (i-1)); m < sz; d++, m++) mini[i][d] = min(mini[i-1][d], mini[i-1][m]); for(int i = 0; i < n; i++) cin >> h[i]; cin >> q; for(int i = 0; i < q; i++) { int typ; cin >> typ; if(typ == 1) { int x, dist, prod; cin >> x >> dist >> prod; upd.push_back({x-1, dist, prod}); if(1000 < (int)upd.size()) { update(); upd.clear(); } } else { int x; cin >> x; x--; int ans = h[x]; for(Upd u : upd) { if(dist(u.i, x) <= u.dist) ans = ans * u.prod % l; } cout << ans << '\n'; } } }
#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...