#include <bits/stdc++.h>
using namespace std;
#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
#define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
typedef pair<int, int> pii;
const int MXN = 400005, MXM = 45;
int n, q, h[MXN], mod;
vector<int> edge[MXN];
pii itv[MXN];
int p[MXN], rdfn[MXN], sdfn[MXN];
struct SMG {
int n, val[MXN * 2];
void init(int _n) {
n = _n;
fill(val, val + 2 * n, 1);
}
void modify(int l, int r, int v) {
for (l += n, r += n; l != r; (l >>= 1), (r >>= 1)) {
if (l & 1) val[l] = val[l] * v % mod, l++;
if (r & 1) val[r - 1] = val[r - 1] * v % mod, r--;
}
// val[l] = val[l] * v % mod;
}
int query(int p) {
int ans = 1;
for (p += n; p > 1; p >>= 1) ans = ans * val[p] % mod;
return ans;
}
// void modify(int id, int l, int r, int _l, int _r, int v) {
// if (_r <= l || r <= _l) return;
// if (_l <= l && r <= _r) {
// val[id] = val[id] * v % mod;
// return;
// }
// int mid = (l + r) >> 1;
// modify(id * 2 + 1, l, mid, _l, _r, v);
// modify(id * 2 + 2, mid, r, _l, _r, v);
// }
// int query(int id, int l, int r, int p) {
// if (l + 1 == r) return val[id];
// int mid = (l + r) >> 1;
// if (p < mid) return val[id] * query(id * 2 + 1, l, mid, p) % mod;
// return val[id] * query(id * 2 + 2, mid, r, p) % mod;
// }
// void modify(int l, int r, int v) {
// modify(0, 0, n, l, r, v);
// }
// int query(int p) {
// return query(0, 0, n, p);
// }
};
struct BIT {
int n;
SMG smg[MXM];
void init(int _n, int _m) {
n = _n;
FOR(i, 0, n + 1) smg[i].init(_m);
}
void modify(int lyr, int l, int r, int v) {
debug(lyr, l, r, v);
if (l == r) return;
for (; lyr <= n; lyr += (lyr & -lyr)) smg[lyr].modify(l, r, v);
}
int query(int lyr, int id) {
int ans = 1;
for (; lyr > 0; lyr -= (lyr & -lyr)) ans = ans * smg[lyr].query(id) % mod;
return ans;
}
} B;
void PRE() {
fill(rdfn + 1, rdfn + n + 1, -1);
sdfn[0] = 1;
rdfn[1] = 0;
p[1] = 0;
int ptr = 1;
FOR(i, 0, n) {
int id = sdfn[i];
itv[id].fs = ptr;
for (auto &i : edge[id]) {
if (rdfn[i] != -1) continue;
p[i] = id;
sdfn[ptr] = i;
rdfn[i] = ptr;
ptr++;
}
itv[id].sc = ptr;
}
}
void MODIFY_ITV(int d, int l, int r, int w) {
debug(d, l, r, w);
if (d < 0) return;
if (l == r) return;
if (d == 0) {
B.smg[0].modify(l, r, w);
return;
}
B.modify(41 - d, l, r, w);
}
void MODIFY(int x, int d, int w) {
MODIFY_ITV(d, rdfn[x], rdfn[x] + 1, w);
int pre = x, cur = p[x];
for (int dd = d - 1; dd >= 0; dd--) {
debug(dd, pre, cur);
if (cur == 0) return;
MODIFY_ITV(0, rdfn[cur], rdfn[cur] + 1, w);
MODIFY_ITV(dd - 1, itv[cur].fs, rdfn[pre], w);
MODIFY_ITV(dd - 1, rdfn[pre] + 1, itv[cur].sc, w);
pre = cur;
cur = p[cur];
}
}
int QUERY(int id) {
int now = id;
int ans = B.query(40, rdfn[now]) * B.smg[0].query(rdfn[now]) % mod;
ans = ans * h[id] % mod;
for (int d = 40; d >= 0; d--) {
now = p[now];
if (now == 0) break;
ans = ans * B.query(d, rdfn[now]) % mod;
}
return ans;
}
void miku() {
int x, y;
cin >> n >> mod;
FOR(i, 1, n) {
cin >> x >> y;
edge[x].push_back(y);
edge[y].push_back(x);
}
FOR(i, 1, n + 1) cin >> h[i];
PRE();
B.init(40, n);
// FOR(i, 1, n + 1) cout << QUERY(i) << ' ';
// cout << endl;
cin >> q;
while (q--) {
int t, x, d, w;
cin >> t;
if (t == 1) {
cin >> x >> d >> w;
MODIFY(x, d, w);
} else {
cin >> x;
cout << QUERY(x) << '\n';
}
// FOR(i, 1, n + 1) cout << QUERY(i) << ' ';
// cout << endl;
}
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit);
miku();
return 0;
}
Compilation message
sprinkler.cpp: In member function 'void BIT::modify(long long int, long long int, long long int, long long int)':
sprinkler.cpp:11:20: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 39
| ^~
sprinkler.cpp:87:9: note: in expansion of macro 'debug'
87 | debug(lyr, l, r, v);
| ^~~~~
sprinkler.cpp: In function 'void MODIFY_ITV(long long int, long long int, long long int, long long int)':
sprinkler.cpp:11:20: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 39
| ^~
sprinkler.cpp:120:5: note: in expansion of macro 'debug'
120 | debug(d, l, r, w);
| ^~~~~
sprinkler.cpp: In function 'void MODIFY(long long int, long long int, long long int)':
sprinkler.cpp:11:20: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 39
| ^~
sprinkler.cpp:134:9: note: in expansion of macro 'debug'
134 | debug(dd, pre, cur);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
84572 KB |
Output is correct |
2 |
Correct |
11 ms |
86524 KB |
Output is correct |
3 |
Correct |
11 ms |
84568 KB |
Output is correct |
4 |
Correct |
14 ms |
84824 KB |
Output is correct |
5 |
Correct |
15 ms |
84828 KB |
Output is correct |
6 |
Correct |
12 ms |
84612 KB |
Output is correct |
7 |
Correct |
14 ms |
84824 KB |
Output is correct |
8 |
Correct |
12 ms |
84828 KB |
Output is correct |
9 |
Correct |
11 ms |
84572 KB |
Output is correct |
10 |
Correct |
11 ms |
84572 KB |
Output is correct |
11 |
Correct |
11 ms |
84572 KB |
Output is correct |
12 |
Correct |
11 ms |
84572 KB |
Output is correct |
13 |
Correct |
12 ms |
84572 KB |
Output is correct |
14 |
Correct |
11 ms |
84572 KB |
Output is correct |
15 |
Correct |
11 ms |
84568 KB |
Output is correct |
16 |
Correct |
11 ms |
86616 KB |
Output is correct |
17 |
Correct |
11 ms |
84572 KB |
Output is correct |
18 |
Correct |
11 ms |
84824 KB |
Output is correct |
19 |
Correct |
10 ms |
84572 KB |
Output is correct |
20 |
Correct |
11 ms |
84544 KB |
Output is correct |
21 |
Correct |
11 ms |
84824 KB |
Output is correct |
22 |
Correct |
11 ms |
84584 KB |
Output is correct |
23 |
Correct |
11 ms |
84536 KB |
Output is correct |
24 |
Correct |
11 ms |
86576 KB |
Output is correct |
25 |
Correct |
11 ms |
84572 KB |
Output is correct |
26 |
Correct |
11 ms |
84532 KB |
Output is correct |
27 |
Correct |
11 ms |
84572 KB |
Output is correct |
28 |
Correct |
11 ms |
84572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
84572 KB |
Output is correct |
2 |
Execution timed out |
4072 ms |
182944 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
84572 KB |
Output is correct |
2 |
Execution timed out |
4072 ms |
182944 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
84568 KB |
Output is correct |
2 |
Execution timed out |
4073 ms |
180424 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
84568 KB |
Output is correct |
2 |
Execution timed out |
4038 ms |
181680 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
84572 KB |
Output is correct |
2 |
Correct |
11 ms |
86524 KB |
Output is correct |
3 |
Correct |
11 ms |
84568 KB |
Output is correct |
4 |
Correct |
14 ms |
84824 KB |
Output is correct |
5 |
Correct |
15 ms |
84828 KB |
Output is correct |
6 |
Correct |
12 ms |
84612 KB |
Output is correct |
7 |
Correct |
14 ms |
84824 KB |
Output is correct |
8 |
Correct |
12 ms |
84828 KB |
Output is correct |
9 |
Correct |
11 ms |
84572 KB |
Output is correct |
10 |
Correct |
11 ms |
84572 KB |
Output is correct |
11 |
Correct |
11 ms |
84572 KB |
Output is correct |
12 |
Correct |
11 ms |
84572 KB |
Output is correct |
13 |
Correct |
12 ms |
84572 KB |
Output is correct |
14 |
Correct |
11 ms |
84572 KB |
Output is correct |
15 |
Correct |
11 ms |
84568 KB |
Output is correct |
16 |
Correct |
11 ms |
86616 KB |
Output is correct |
17 |
Correct |
11 ms |
84572 KB |
Output is correct |
18 |
Correct |
11 ms |
84824 KB |
Output is correct |
19 |
Correct |
10 ms |
84572 KB |
Output is correct |
20 |
Correct |
11 ms |
84544 KB |
Output is correct |
21 |
Correct |
11 ms |
84824 KB |
Output is correct |
22 |
Correct |
11 ms |
84584 KB |
Output is correct |
23 |
Correct |
11 ms |
84536 KB |
Output is correct |
24 |
Correct |
11 ms |
86576 KB |
Output is correct |
25 |
Correct |
11 ms |
84572 KB |
Output is correct |
26 |
Correct |
11 ms |
84532 KB |
Output is correct |
27 |
Correct |
11 ms |
84572 KB |
Output is correct |
28 |
Correct |
11 ms |
84572 KB |
Output is correct |
29 |
Correct |
11 ms |
84572 KB |
Output is correct |
30 |
Execution timed out |
4072 ms |
182944 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |