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 <bits/stdc++.h>
#include <random>
#ifndef LOCAL
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
typedef long long ll;
typedef double dd;
typedef long double ld;
typedef unsigned int uii;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void solve();
mt19937_64 mt(1);
int32_t main() {
#ifdef LOCAL
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#else
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
//freopen("amusing.in", "r", stdin);
//freopen("amusing.out", "w", stdout);
#endif
cout << fixed << setprecision(30);
int tests = 1;
//cin >> tests;
while (tests--) {
solve();
}
}
ll mod;
struct st_down {
vector<long long> tree;
int n;
void build(const vector<int>& arr) {
n = arr.size();
tree.assign(2 * n, 1);
for (int i = 0; i < n; i++) {
tree[n + i] = arr[i];
}
}
long long find_value(int pos) {
pos += n;
long long ans = 1;
while (pos > 0) {
ans = (ans * tree[pos]) % mod;
pos >>= 1;
}
return ans;
}
void segment_update(int l, int r, int addval) {
l += n;
r += n;
while (l < r) {
if (l & 1) {
tree[l++] *= addval;
tree[l - 1] %= mod;
}
if (r & 1) {
tree[--r] *= addval;
tree[r] %= mod;
}
l >>= 1;
r >>= 1;
}
}
};
pair<int, int> seg[200200][41];
void dfs(int s, int p, vector<vector<int>>& g, int dist[], vector<vector<int>>& d, int pr[]) {
if (p != -1) {
dist[s] = dist[p] + 1;
} else {
dist[s] = 0;
}
pr[s] = p;
d[dist[s]].push_back(s);
seg[s][0] = {d[dist[s]].size() - 1, d[dist[s]].size() - 1};
for (int i = 1; i < 41; i++) {
seg[s][i] = {INT32_MAX, INT32_MIN};
}
for (int i : g[s]) {
if (i != p) {
dfs(i, s, g, dist, d, pr);
for (int j = 1; j < 41; j++) {
seg[s][j].x = min(seg[s][j].x, seg[i][j - 1].x);
}
for (int j = 1; j < 41; j++) {
seg[s][j].y = max(seg[s][j].y, seg[i][j - 1].y);
}
}
}
}
void solve() {
int n;
cin >> n >> mod;
vector<vector<int>> g(n), d(n);
int deg[200200], dist[200200], h[200200], p[200200];
for (int i = 0; i < n; i++) {
deg[i] = 0;
}
int pr[200200][41], lev[200200][41];
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v; u--; v--;
g[u].push_back(v);
g[v].push_back(u);
deg[u]++;
deg[v]++;
}
int root = 0;
for (int i = 0; i < n; i++) {
if (deg[i] == 1) {
root = i;
break;
}
}
dfs(root, -1, g, dist, d, p);
for (int i = 0; i < n; i++) {
int nw = i, lv = 0, nlev = 0;
for (int c = 0; c < 41; c++) {
while (c > nlev) {
nlev++;
if (p[nw] != -1) {
lv++;
nw = p[nw];
}
}
pr[i][c] = nw;
lev[i][c] = lv;
}
}
for (int i = 0; i < n; i++) {
cin >> h[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < d[i].size(); j++) {
d[i][j] = h[d[i][j]];
}
}
st_down dec[200200];
for (int i = 0; i < n; i++) {
dec[i].build(d[i]);
}
int q;
cin >> q;
while (q--) {
int t;
cin >> t;
if (t == 1) {
int x, dk, wk;
cin >> x >> dk >> wk; x--;
for (int l = max(-dk, dist[x] - n + 1); l <= dk && l <= dist[x]; l++) {
int c = (dk + l) / 2;
if (c - l >= 0 && c - l <= dk) {
int l1, r1;
l1 = seg[pr[x][c]][lev[x][c] - l].x;
r1 = seg[pr[x][c]][lev[x][c] - l].y;
dec[dist[x] - l].segment_update(l1, r1 + 1, wk);
}
}
} else {
int x;
cin >> x; x--;
cout << (dec[dist[x]].find_value(seg[x][0].x) % mod + mod) % mod << '\n';
}
}
}
Compilation message (stderr)
sprinkler.cpp: In function 'void solve()':
sprinkler.cpp:156:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
156 | for (int j = 0; j < d[i].size(); j++) {
| ~~^~~~~~~~~~~~~
# | 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... |