#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;
class SegmentTree {
public:
void build(const vector<int>& b) {
int sz = 1;
while (sz < b.size()) {
sz *= 2;
}
x.resize(2 * sz - 1);
vn.resize(2 * sz - 1, 1);
size = 2 * sz - 1;
for (int i = sz - 1; i < 2 * sz - 1; i++) {
if (i - sz + 1 < b.size()) {
x[i] = b[i - sz + 1];
} else {
x[i] = 0;
}
}
for (int i = sz - 2; i >= 0; i--) {
//update(i);
x[i] = 0;
}
}
int get(int l, int r, int i = 0, int lx = 0, int rx = -1) {
if (rx == -1) {
rx = size / 2;
}
if (lx > r || rx < l) {
return 0;
}
if (lx >= l && rx <= r) {
return x[i];
}
updateVN(i);
return get(l, r, 2 * i + 1, lx, (rx + lx) / 2) + get(l, r, 2 * i + 2, (rx + lx) / 2 + 1, rx);
}
void change(int ind, int c, int i = 0, int lx = 0, int rx = -1) {
if (rx == -1) {
rx = size / 2;
}
if (lx == rx) {
x[i] = c;
return;
}
updateVN(i);
if ((rx + lx) / 2 >= ind) {
change(ind, c, 2 * i + 1, lx, (rx + lx) / 2);
} else {
change(ind, c, 2 * i + 2, (rx + lx) / 2 + 1, rx);
}
//update(i);
}
void plusOtr(int l, int r, ll p, int i = 0, int lx = 0, int rx = -1) {
if (r < l) {
return;
}
if (rx == -1) {
rx = size / 2;
}
if (lx >= l && rx <= r) {
x[i] = (x[i] * p) % mod;
vn[i] = (vn[i] * p) % mod;
return;
}
if (lx > r || rx < l) {
return;
}
updateVN(i);
plusOtr(l, r, p, 2 * i + 1, lx, (rx + lx) / 2);
plusOtr(l, r, p, 2 * i + 2, (rx + lx) / 2 + 1, rx);
//update(i);
}
private:
vector<ll> x, vn;
int size;
void updateVN(int i) {
if (2 * i + 2 < size) {
if (vn[i] != 1) {
if (x[2 * i + 1]) {
x[2 * i + 1] = (x[2 * i + 1] * vn[i]) % mod;
}
vn[2 * i + 1] = (vn[2 * i + 1] * vn[i]) % mod;
if (x[2 * i + 2]) {
x[2 * i + 2] = (x[2 * i + 2] * vn[i]) % mod;
}
vn[2 * i + 2] = (vn[2 * i + 2] * vn[i]) % mod;
vn[i] = 1;
}
}
}
void update(int i) {
if (2 * i + 2 < size) {
x[i] = x[2 * i + 1] + x[2 * i + 2];
}
}
};
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};
}
bool f = true;
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;
}
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++) {
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]];
}
}
SegmentTree 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--;
int nw = x, lev = 0, nlev = 0;
for (int l = -dk; l <= dk; l++) {
if (dist[x] - l < 0) {
break;
}
if (dist[x] - l >= n) {
continue;
}
int c = (dk + l) / 2;
while (c > nlev) {
nlev++;
if (p[nw] != -1) {
lev++;
nw = p[nw];
}
}
if (c - l >= 0 && c - l <= dk) {
int l1, r1;
l1 = seg[nw][lev - l].x;
r1 = seg[nw][lev - l].y;
dec[dist[x] - l].plusOtr(l1, r1, wk);
}
}
} else {
int x;
cin >> x; x--;
cout << (dec[dist[x]].get(seg[x][0].x, seg[x][0].y) % mod + mod) % mod << '\n';
}
}
}
Compilation message
sprinkler.cpp: In member function 'void SegmentTree::build(const std::vector<int>&)':
sprinkler.cpp:50:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | while (sz < b.size()) {
| ~~~^~~~~~~~~~
sprinkler.cpp:57:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | if (i - sz + 1 < b.size()) {
| ~~~~~~~~~~~^~~~~~~~~~
sprinkler.cpp: In function 'void dfs(int, int, std::vector<std::vector<int> >&, int*, std::vector<std::vector<int> >&, int*)':
sprinkler.cpp:160:10: warning: unused variable 'f' [-Wunused-variable]
160 | bool f = true;
| ^
sprinkler.cpp: In function 'void solve()':
sprinkler.cpp:202:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
202 | for (int j = 0; j < d[i].size(); j++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14428 KB |
Output is correct |
2 |
Correct |
8 ms |
14428 KB |
Output is correct |
3 |
Correct |
8 ms |
14428 KB |
Output is correct |
4 |
Correct |
10 ms |
16988 KB |
Output is correct |
5 |
Correct |
11 ms |
16732 KB |
Output is correct |
6 |
Correct |
10 ms |
16880 KB |
Output is correct |
7 |
Correct |
10 ms |
16728 KB |
Output is correct |
8 |
Correct |
10 ms |
16728 KB |
Output is correct |
9 |
Correct |
9 ms |
14428 KB |
Output is correct |
10 |
Correct |
9 ms |
14428 KB |
Output is correct |
11 |
Correct |
9 ms |
14428 KB |
Output is correct |
12 |
Correct |
9 ms |
14428 KB |
Output is correct |
13 |
Correct |
12 ms |
14752 KB |
Output is correct |
14 |
Correct |
9 ms |
14560 KB |
Output is correct |
15 |
Correct |
9 ms |
14428 KB |
Output is correct |
16 |
Correct |
9 ms |
14428 KB |
Output is correct |
17 |
Correct |
9 ms |
14428 KB |
Output is correct |
18 |
Correct |
9 ms |
14428 KB |
Output is correct |
19 |
Correct |
8 ms |
14428 KB |
Output is correct |
20 |
Correct |
9 ms |
14432 KB |
Output is correct |
21 |
Correct |
9 ms |
14428 KB |
Output is correct |
22 |
Correct |
10 ms |
14320 KB |
Output is correct |
23 |
Correct |
9 ms |
14428 KB |
Output is correct |
24 |
Correct |
8 ms |
14428 KB |
Output is correct |
25 |
Correct |
9 ms |
14428 KB |
Output is correct |
26 |
Correct |
9 ms |
14428 KB |
Output is correct |
27 |
Correct |
8 ms |
14396 KB |
Output is correct |
28 |
Correct |
9 ms |
14424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14428 KB |
Output is correct |
2 |
Correct |
351 ms |
120548 KB |
Output is correct |
3 |
Correct |
486 ms |
117644 KB |
Output is correct |
4 |
Correct |
474 ms |
140116 KB |
Output is correct |
5 |
Correct |
416 ms |
118884 KB |
Output is correct |
6 |
Correct |
434 ms |
119376 KB |
Output is correct |
7 |
Correct |
405 ms |
118792 KB |
Output is correct |
8 |
Correct |
361 ms |
118524 KB |
Output is correct |
9 |
Correct |
480 ms |
141652 KB |
Output is correct |
10 |
Correct |
455 ms |
138404 KB |
Output is correct |
11 |
Correct |
382 ms |
120368 KB |
Output is correct |
12 |
Correct |
562 ms |
118192 KB |
Output is correct |
13 |
Correct |
366 ms |
117988 KB |
Output is correct |
14 |
Correct |
442 ms |
119296 KB |
Output is correct |
15 |
Correct |
505 ms |
118104 KB |
Output is correct |
16 |
Correct |
508 ms |
118308 KB |
Output is correct |
17 |
Correct |
522 ms |
117904 KB |
Output is correct |
18 |
Correct |
9 ms |
14384 KB |
Output is correct |
19 |
Correct |
11 ms |
14396 KB |
Output is correct |
20 |
Correct |
12 ms |
14568 KB |
Output is correct |
21 |
Correct |
11 ms |
14804 KB |
Output is correct |
22 |
Correct |
11 ms |
14428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14428 KB |
Output is correct |
2 |
Correct |
351 ms |
120548 KB |
Output is correct |
3 |
Correct |
486 ms |
117644 KB |
Output is correct |
4 |
Correct |
474 ms |
140116 KB |
Output is correct |
5 |
Correct |
416 ms |
118884 KB |
Output is correct |
6 |
Correct |
434 ms |
119376 KB |
Output is correct |
7 |
Correct |
405 ms |
118792 KB |
Output is correct |
8 |
Correct |
361 ms |
118524 KB |
Output is correct |
9 |
Correct |
480 ms |
141652 KB |
Output is correct |
10 |
Correct |
455 ms |
138404 KB |
Output is correct |
11 |
Correct |
382 ms |
120368 KB |
Output is correct |
12 |
Correct |
562 ms |
118192 KB |
Output is correct |
13 |
Correct |
366 ms |
117988 KB |
Output is correct |
14 |
Correct |
442 ms |
119296 KB |
Output is correct |
15 |
Correct |
505 ms |
118104 KB |
Output is correct |
16 |
Correct |
508 ms |
118308 KB |
Output is correct |
17 |
Correct |
522 ms |
117904 KB |
Output is correct |
18 |
Correct |
9 ms |
14384 KB |
Output is correct |
19 |
Correct |
11 ms |
14396 KB |
Output is correct |
20 |
Correct |
12 ms |
14568 KB |
Output is correct |
21 |
Correct |
11 ms |
14804 KB |
Output is correct |
22 |
Correct |
11 ms |
14428 KB |
Output is correct |
23 |
Correct |
12 ms |
14428 KB |
Output is correct |
24 |
Correct |
469 ms |
120612 KB |
Output is correct |
25 |
Correct |
726 ms |
118044 KB |
Output is correct |
26 |
Correct |
507 ms |
139992 KB |
Output is correct |
27 |
Correct |
528 ms |
119068 KB |
Output is correct |
28 |
Correct |
533 ms |
119284 KB |
Output is correct |
29 |
Correct |
523 ms |
119808 KB |
Output is correct |
30 |
Correct |
458 ms |
118576 KB |
Output is correct |
31 |
Correct |
435 ms |
141884 KB |
Output is correct |
32 |
Correct |
637 ms |
138508 KB |
Output is correct |
33 |
Correct |
464 ms |
120836 KB |
Output is correct |
34 |
Correct |
728 ms |
117416 KB |
Output is correct |
35 |
Correct |
12 ms |
14380 KB |
Output is correct |
36 |
Correct |
9 ms |
14428 KB |
Output is correct |
37 |
Correct |
9 ms |
14428 KB |
Output is correct |
38 |
Correct |
9 ms |
14424 KB |
Output is correct |
39 |
Correct |
11 ms |
14424 KB |
Output is correct |
40 |
Correct |
11 ms |
14428 KB |
Output is correct |
41 |
Correct |
9 ms |
14428 KB |
Output is correct |
42 |
Correct |
10 ms |
14428 KB |
Output is correct |
43 |
Correct |
11 ms |
14428 KB |
Output is correct |
44 |
Correct |
12 ms |
14428 KB |
Output is correct |
45 |
Correct |
9 ms |
14428 KB |
Output is correct |
46 |
Correct |
10 ms |
14376 KB |
Output is correct |
47 |
Correct |
12 ms |
14428 KB |
Output is correct |
48 |
Correct |
9 ms |
14428 KB |
Output is correct |
49 |
Correct |
9 ms |
14428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14428 KB |
Output is correct |
2 |
Correct |
691 ms |
139044 KB |
Output is correct |
3 |
Correct |
2688 ms |
138508 KB |
Output is correct |
4 |
Correct |
1175 ms |
138808 KB |
Output is correct |
5 |
Correct |
1971 ms |
117412 KB |
Output is correct |
6 |
Correct |
1475 ms |
117928 KB |
Output is correct |
7 |
Correct |
961 ms |
118312 KB |
Output is correct |
8 |
Correct |
437 ms |
117064 KB |
Output is correct |
9 |
Correct |
700 ms |
138972 KB |
Output is correct |
10 |
Correct |
2731 ms |
138560 KB |
Output is correct |
11 |
Correct |
1014 ms |
118224 KB |
Output is correct |
12 |
Execution timed out |
4046 ms |
117164 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14384 KB |
Output is correct |
2 |
Correct |
800 ms |
141764 KB |
Output is correct |
3 |
Correct |
2671 ms |
138728 KB |
Output is correct |
4 |
Correct |
1116 ms |
139900 KB |
Output is correct |
5 |
Correct |
2263 ms |
119128 KB |
Output is correct |
6 |
Correct |
1714 ms |
119112 KB |
Output is correct |
7 |
Correct |
1129 ms |
118084 KB |
Output is correct |
8 |
Correct |
446 ms |
118396 KB |
Output is correct |
9 |
Correct |
726 ms |
141700 KB |
Output is correct |
10 |
Correct |
2631 ms |
138740 KB |
Output is correct |
11 |
Correct |
926 ms |
120592 KB |
Output is correct |
12 |
Execution timed out |
4051 ms |
117376 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14428 KB |
Output is correct |
2 |
Correct |
8 ms |
14428 KB |
Output is correct |
3 |
Correct |
8 ms |
14428 KB |
Output is correct |
4 |
Correct |
10 ms |
16988 KB |
Output is correct |
5 |
Correct |
11 ms |
16732 KB |
Output is correct |
6 |
Correct |
10 ms |
16880 KB |
Output is correct |
7 |
Correct |
10 ms |
16728 KB |
Output is correct |
8 |
Correct |
10 ms |
16728 KB |
Output is correct |
9 |
Correct |
9 ms |
14428 KB |
Output is correct |
10 |
Correct |
9 ms |
14428 KB |
Output is correct |
11 |
Correct |
9 ms |
14428 KB |
Output is correct |
12 |
Correct |
9 ms |
14428 KB |
Output is correct |
13 |
Correct |
12 ms |
14752 KB |
Output is correct |
14 |
Correct |
9 ms |
14560 KB |
Output is correct |
15 |
Correct |
9 ms |
14428 KB |
Output is correct |
16 |
Correct |
9 ms |
14428 KB |
Output is correct |
17 |
Correct |
9 ms |
14428 KB |
Output is correct |
18 |
Correct |
9 ms |
14428 KB |
Output is correct |
19 |
Correct |
8 ms |
14428 KB |
Output is correct |
20 |
Correct |
9 ms |
14432 KB |
Output is correct |
21 |
Correct |
9 ms |
14428 KB |
Output is correct |
22 |
Correct |
10 ms |
14320 KB |
Output is correct |
23 |
Correct |
9 ms |
14428 KB |
Output is correct |
24 |
Correct |
8 ms |
14428 KB |
Output is correct |
25 |
Correct |
9 ms |
14428 KB |
Output is correct |
26 |
Correct |
9 ms |
14428 KB |
Output is correct |
27 |
Correct |
8 ms |
14396 KB |
Output is correct |
28 |
Correct |
9 ms |
14424 KB |
Output is correct |
29 |
Correct |
8 ms |
14428 KB |
Output is correct |
30 |
Correct |
351 ms |
120548 KB |
Output is correct |
31 |
Correct |
486 ms |
117644 KB |
Output is correct |
32 |
Correct |
474 ms |
140116 KB |
Output is correct |
33 |
Correct |
416 ms |
118884 KB |
Output is correct |
34 |
Correct |
434 ms |
119376 KB |
Output is correct |
35 |
Correct |
405 ms |
118792 KB |
Output is correct |
36 |
Correct |
361 ms |
118524 KB |
Output is correct |
37 |
Correct |
480 ms |
141652 KB |
Output is correct |
38 |
Correct |
455 ms |
138404 KB |
Output is correct |
39 |
Correct |
382 ms |
120368 KB |
Output is correct |
40 |
Correct |
562 ms |
118192 KB |
Output is correct |
41 |
Correct |
366 ms |
117988 KB |
Output is correct |
42 |
Correct |
442 ms |
119296 KB |
Output is correct |
43 |
Correct |
505 ms |
118104 KB |
Output is correct |
44 |
Correct |
508 ms |
118308 KB |
Output is correct |
45 |
Correct |
522 ms |
117904 KB |
Output is correct |
46 |
Correct |
9 ms |
14384 KB |
Output is correct |
47 |
Correct |
11 ms |
14396 KB |
Output is correct |
48 |
Correct |
12 ms |
14568 KB |
Output is correct |
49 |
Correct |
11 ms |
14804 KB |
Output is correct |
50 |
Correct |
11 ms |
14428 KB |
Output is correct |
51 |
Correct |
12 ms |
14428 KB |
Output is correct |
52 |
Correct |
469 ms |
120612 KB |
Output is correct |
53 |
Correct |
726 ms |
118044 KB |
Output is correct |
54 |
Correct |
507 ms |
139992 KB |
Output is correct |
55 |
Correct |
528 ms |
119068 KB |
Output is correct |
56 |
Correct |
533 ms |
119284 KB |
Output is correct |
57 |
Correct |
523 ms |
119808 KB |
Output is correct |
58 |
Correct |
458 ms |
118576 KB |
Output is correct |
59 |
Correct |
435 ms |
141884 KB |
Output is correct |
60 |
Correct |
637 ms |
138508 KB |
Output is correct |
61 |
Correct |
464 ms |
120836 KB |
Output is correct |
62 |
Correct |
728 ms |
117416 KB |
Output is correct |
63 |
Correct |
12 ms |
14380 KB |
Output is correct |
64 |
Correct |
9 ms |
14428 KB |
Output is correct |
65 |
Correct |
9 ms |
14428 KB |
Output is correct |
66 |
Correct |
9 ms |
14424 KB |
Output is correct |
67 |
Correct |
11 ms |
14424 KB |
Output is correct |
68 |
Correct |
11 ms |
14428 KB |
Output is correct |
69 |
Correct |
9 ms |
14428 KB |
Output is correct |
70 |
Correct |
10 ms |
14428 KB |
Output is correct |
71 |
Correct |
11 ms |
14428 KB |
Output is correct |
72 |
Correct |
12 ms |
14428 KB |
Output is correct |
73 |
Correct |
9 ms |
14428 KB |
Output is correct |
74 |
Correct |
10 ms |
14376 KB |
Output is correct |
75 |
Correct |
12 ms |
14428 KB |
Output is correct |
76 |
Correct |
9 ms |
14428 KB |
Output is correct |
77 |
Correct |
9 ms |
14428 KB |
Output is correct |
78 |
Correct |
10 ms |
14428 KB |
Output is correct |
79 |
Correct |
691 ms |
139044 KB |
Output is correct |
80 |
Correct |
2688 ms |
138508 KB |
Output is correct |
81 |
Correct |
1175 ms |
138808 KB |
Output is correct |
82 |
Correct |
1971 ms |
117412 KB |
Output is correct |
83 |
Correct |
1475 ms |
117928 KB |
Output is correct |
84 |
Correct |
961 ms |
118312 KB |
Output is correct |
85 |
Correct |
437 ms |
117064 KB |
Output is correct |
86 |
Correct |
700 ms |
138972 KB |
Output is correct |
87 |
Correct |
2731 ms |
138560 KB |
Output is correct |
88 |
Correct |
1014 ms |
118224 KB |
Output is correct |
89 |
Execution timed out |
4046 ms |
117164 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |