#include<iostream>
#include<vector>
using namespace std;
using ll = long long;
const int N = 5e5 + 123;
const int LOG = 20;
const int D = 41;
ll h[N], q[N][D];
int n, L;
void mult(ll &a, ll b) {
a += b;
return;
a *= b;
a %= L;
}
void dvdv(ll &a, ll b) {
a -= b;
}
vector<int>g[N];
int pr[N];
void dfs(int v) {
for (int to : g[v]) if (to != pr[v]) {
pr[to] = v;
dfs(to);
}
}
ll binpow(ll a, ll b) {
ll r = 1;
while (b) {
if (b & 1) r = r * a % L;
a = a * a % L;
b >>= 1;
}
return r;
}
ll ih[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> L;
for (int i = 1 ; i < n ; ++ i) {
int a, b;
cin >> a >> b;
g[a].emplace_back(b);
g[b].emplace_back(a);
}
dfs(1);
for (int i = 1 ; i <= n ; ++ i) {
cin >> ih[i];
h[i] = 0;
for (int j = 0 ; j <= 40 ; ++ j) {
//q[i][j] = 1;
q[i][j] = 0;
}
}
int qq;
cin >> qq;
for (int i = 0 ; i < qq ; ++ i) {
int t, x, d, w;
cin >> t;
if (t == 1) {
cin >> x >> d >> w;
w = 1;
for (int i = 0 ; i <= d ; ++ i) {
for (int j = d-i ; j >= 0 ; -- j) {
mult(q[x][j], w);
}
mult(h[x], w);
if (x == 1) break;
x = pr[x];
}
} else {
cin >> x;
ll ret = ih[x];
ll ans = h[x];
for (int i = 1 ; i <= 40 ; ++ i) {
if (x == 1) break;
int y = x;
x = pr[x];
mult(ans, q[x][i]);
if (i > 1) dvdv(ans, q[y][i]);
}
if (ans > 0) {
ans = 0;
} else {
ans = ret;
}
cout << ans << "\n";
//cout << ret*binpow(2, ans) % L << "\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
11972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
11988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
11988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11988 KB |
Output is correct |
2 |
Incorrect |
781 ms |
93252 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
11988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
11972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |