This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <queue>
#include <array>
#define ll long long
using namespace std;
queue <ll> Q;
array <ll, 2> R[200000][41];
ll n, q, t, M, a, b, c, f, cnt, H[200000], P[200000], X[200000], D[200000], dpt[200000];
struct SegTree{
vector <ll> st, lazy;
ll stsz = 0;
void init() {
st.resize(4*stsz);
lazy.resize(4*stsz);
fill(st.begin(), st.end(), 1);
fill(lazy.begin(), lazy.end(), 1);
}
void push(ll id) {
st[id*2] = st[id*2] * lazy[id] % M;
st[id*2+1] = st[id*2+1] * lazy[id] % M;
lazy[id*2] = lazy[id*2] * lazy[id] % M;
lazy[id*2+1] = lazy[id*2+1] * lazy[id] % M;
lazy[id] = 1;
}
void update(ll id, ll l, ll r, ll ql, ll qr, ll w) {
if (qr < l || r < ql || ql > qr) return;
else if (ql <= l && r <= qr) {
st[id] = st[id] * w % M;
lazy[id] = lazy[id] * w % M;
return;
}
push(id);
ll mid = (l+r)/2;
update(id*2, l, mid, ql, qr, w);
update(id*2+1, mid+1, r, ql, qr, w);
st[id] = st[id*2] * st[id*2+1] % M;
}
ll query(ll id, ll l, ll r, ll q) {
if (l == r) return st[id];
push(id);
ll mid = (l+r)/2;
if (q <= mid) return query(id*2, l, mid, q);
else return query(id*2+1, mid+1, r, q);
}
}ST[200000];
vector <ll> adj[200000];
void dfs(ll u, ll p) {
for (auto v : adj[u]) {
if (v != p) {
D[v] = D[u] + 1;
P[v] = u;
dfs(v, u);
}
}
}
void solve(ll u, ll p) {
R[u][0] = {X[u], X[u]};
for (int i=1; i<=40; ++i) {
R[u][i] = {(ll)1e18, (ll)-1e18};
}
for (auto v : adj[u]) {
if (v != p) {
solve(v, u);
for (int i=1; i<=40; ++i) {
R[u][i][0] = min(R[u][i][0], R[v][i-1][0]);
R[u][i][1] = max(R[u][i][1], R[v][i-1][1]);
}
}
}
}
void bfs() {
Q.push(0);
while (!Q.empty()) {
auto u = Q.front();
Q.pop();
X[u] = dpt[D[u]]++;
for (auto v : adj[u]) {
if (D[v] > D[u]) Q.push(v);
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cin >> n >> M;
for (int i=0; i<n-1; ++i) {
cin >> a >> b;
--a, --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i=0; i<n; ++i) {
cin >> H[i];
}
dfs(0, -1);
P[0] = -1;
bfs();
solve(0, -1);
for (int i=0; i<n; ++i) {
ST[i].stsz = dpt[i];
ST[i].init();
}
cin >> q;
while (q--) {
cin >> t;
if (t == 1) {
cin >> a >> b >> c;
--a;
ll mnd = 1e18;
for (int i=0; i<=2*b && a != -1; ++i) {
mnd = min(mnd, b-(i/2)-(i&1));
if (D[a]+b-(i/2)-(i&1) < n) ST[D[a]+b-(i/2)-(i&1)].update(1, 0, ST[D[a]+b-(i/2)-(i&1)].stsz-1, R[a][b-(i/2)-(i&1)][0], R[a][b-(i/2)-(i&1)][1], c);
if (i & 1) a = P[a];
}
if (a == -1) a = 0;
for (int i=mnd-1; i>=0; --i) {
if (D[a]+i < n) ST[D[a]+i].update(1, 0, ST[D[a]+i].stsz-1, R[a][i][0], R[a][i][1], c);
}
}
else {
cin >> a;
--a;
cout << H[a] * ST[D[a]].query(1, 0, ST[D[a]].stsz-1, X[a]) % M << '\n';
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |