#include<bits/stdc++.h>
#define X first
#define Y second
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, b, a) for (int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define MASK(x) (1LL << (x))
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
typedef pair<ll, pii> plii;
template<class A, class B>
bool maximize(A& x, B y) {
if (x < y) return x = y, true; else return false;
}
template<class A, class B>
bool minimize(A& x, B y) {
if (x > y) return x = y, true; else return false;
}
/* END OF TEMPLATE */
const int N = 2e5 + 7;
int n, mod, P[N], pos[N];
ll res[N][50], h[N], res2[N][50];
vector<int> v[N];
struct SegmentTree {
int n; // array size
int t[2 * N];
SegmentTree() {
n = 0;
REP(i, 0, 2 * N) t[i] = 1;
}
void build(int a[], int n) { // build the tree
FOR(i, 1, n) t[i - 1] = a[i];
this->n = n;
for (int i = n - 1; i > 0; --i) t[i] = 1LL * t[i<<1] * t[i<<1|1] % mod;
}
void update(int p, int value) { // set value at position p
p+=n;
for (t[p] = t[p] * value % mod; p > 1; p >>= 1) {
t[p>>1] = 1LL * t[p] * t[p^1] % mod;
}
}
ll Get(int l, int r) { // sum on interval [l, r)
if (l >= r) return 1;
ll res = 1;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l&1) res = 1LL * res * t[l++] % mod;
if (r&1) res = 1LL * res * t[--r] % mod;
}
return res;
}
} st[42];
void dfs(int x, int par) {
P[x] = par;
for (auto y : v[x])
if (y != par) dfs(y, x);
}
void firstQuery() {
int x, d, w;
cin>>x>>d>>w;
FOR(j, 0, d) res2[x][j] = res2[x][j] * w % mod;
int tmp = 0;
while (x != -1 && tmp <= d) {
st[d - tmp].update(pos[x] - 1, w);
res[x][d - tmp] = res[x][d - tmp] * w % mod;
tmp++;
x = P[x];
}
}
void secondQuery() {
int x;
cin>>x;
ll ans = h[x];
int tmp = 1;
int last_x = x;
// ans = ans * res2[x][0] % mod;
FOR(i, 0, 40) ans = ans * st[i].Get(pos[x] - 1, pos[x]) % mod;
x = P[x];
// cout<<res[2][1]<<"\n";
// cout <<x<<" "<<ans<<"\n";
while (x != -1 && tmp <= 40) {
ans = ans * res2[x][tmp] % mod;
// cout<<x<<" "<<ans<<"\n";
// ans = ans * res[x][0] % mod;
if (tmp < 40) {
int t = lower_bound(all(v[x]), last_x) - v[x].begin();
if (t < (int)v[x].size()) {
FOR(j, tmp + 1, 40) {
ans = ans * st[j].Get(pos[v[x][0]] - 1, pos[v[x][t]] - 1) % mod;
ans = ans * st[j].Get(pos[v[x][t]] + 1 - 1, pos[v[x].back()]) % mod;
}
}
// for (auto y : v[x])
// if (y != last_x && y != P[x]) {
// FOR(j, 0, 40)
// if (j >= tmp + 1) {
// ans = ans * res[y][j] % mod;
//// cout<<y<<" "<<j<<" "<<res[y][j]<<"\n";
// }
// }
}
// cout<<x<<" "<<ans<<"\n";
// break;
last_x = x;
x = P[x];
tmp++;
}
cout<<ans<<"\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>mod;
REP(i, 1, n) {
int x, y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
FOR(j, 0, 40) st[j].n = n;
FOR(i, 1, n)
FOR(j, 0, 40) res[i][j] = res2[i][j] = 1;
FOR(i, 1, n) cin>>h[i];
dfs(1, -1);
FOR(i, 1, n) {
vector<int> a;
for (auto x : v[i])
if (x != P[i]) a.push_back(x);
v[i] = a;
sort(all(v[i]));
}
int cnt = 0;
FOR(i, 1, n)
for (auto x : v[i]) pos[x] = ++cnt;
int Q;
cin>>Q;
while (Q--) {
int type;
cin>>type;
if (type == 1) firstQuery(); else secondQuery();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
70736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
70724 KB |
Output is correct |
2 |
Execution timed out |
4082 ms |
238224 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
70724 KB |
Output is correct |
2 |
Execution timed out |
4082 ms |
238224 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
70740 KB |
Output is correct |
2 |
Execution timed out |
4072 ms |
241780 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
70716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
70736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |