#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
#define ll __int128
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
ll read() { int x; cin >> x; return (ll)x; }
long long bm(long long b, long long p) {
if(p==0) return 1 % MOD;
long long r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
long long inv(long long b) {
return bm(b, MOD-2);
}
long long f[MAXN];
long long nCr(int n, int r) {
long long ans = f[n]; ans *= inv(f[r]); ans %= MOD;
ans *= inv(f[n-r]); ans %= MOD; return ans;
}
long long fib[MAXN], lucas[MAXN];
void precomp() {
for(int i=0; i<MAXN; i++) f[i] = (i == 0 ? 1 % MOD : (f[i-1] * i) % MOD);
lucas[0] = 2;
lucas[1] = 1;
for(int i=2; i<MAXN; i++) lucas[i] = (lucas[i-2] + lucas[i-1]) % MOD;
fib[0] = 0;
fib[1] = 1;
for(int i=2; i<MAXN; i++) fib[i] = (fib[i-2] + fib[i-1]) % MOD;
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void gay(int i) { cout << "Case #" << i << ": "; }
int csb(int l, int r, int k) { // count number of [l, r] such that i & 2^k > 0
if(l > r) return 0;
if(l == 0) {
int s = r / (1ll << (k+1)); // number of complete cycles
int t = r % (1ll << (k+1));
int ans = s * (1ll << k);
ans += (t >= (1ll << k) ? t - (1ll << k) + 1 : 0);
return ans;
}
else return csb(0, r, k) - csb(0, l - 1, k);
}
int lis(vector<int> a) {
int n = a.size();
int bucket[n+1];
for(int i=1; i<=n; i++) bucket[i] = 1e18;
int ans = 1;
for(int x: a) {
auto it = lower_bound(bucket + 1, bucket +n +1, x);
int d = distance(bucket, it);
ans = max(ans, d);
bucket[d] = min(bucket[d], x);
}
return ans;
}
vector<int> adj[MAXN];
int par[MAXN];
int dep[MAXN];
void dfs(int node, int prv) {
par[node] = prv;
dep[node] = (prv== -1? 0: dep[prv]+1);
for(int x: adj[node]) if(x != prv) dfs(x, node);
}
void solve(int tc) {
int n, l;
cin >> n >> l;
for(int i=1; i<n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, -1);
int h[n+1];
for(int i=1; i<=n; i++) cin >> h[i];
int dlt[n+1][41];
for(int i=1; i<=n; i++) for(int j=0; j<41; j++) dlt[i][j] = 1;
int q;
cin >> q;
while(q--) {
int t;
cin >> t;
if(t == 1) {
int x, d, w;
cin >> x >> d >> w;
vector<int> v;
for(int i=0; i<=d; i++) {
v.push_back(x);
x = par[x];
if(x == -1) break;
}
int maxdepthdone = -1;
for(int i=v.size()-1; i>=0; i--) {
int allow = d - i + dep[v[i]];
int mini = dep[v[i]];
for(int j = max(maxdepthdone + 1, mini); j <= allow; j++) {
dlt[v[i]][j - dep[v[i]]] = (dlt[v[i]][j - dep[v[i]]] * w) % l;
}
maxdepthdone = allow;
}
}
else {
int x;
cin >> x;
int cur = h[x];
for(int i=0; i<=40; i++) {
cur = (cur * dlt[x][i]) % l;
x = par[x];
if(x == -1) break;
}
cout << cur << "\n";
}
}
}
int32_t main() {
precomp();
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14420 KB |
Output is correct |
2 |
Correct |
13 ms |
14428 KB |
Output is correct |
3 |
Correct |
12 ms |
14424 KB |
Output is correct |
4 |
Correct |
13 ms |
14784 KB |
Output is correct |
5 |
Correct |
14 ms |
14812 KB |
Output is correct |
6 |
Correct |
16 ms |
14788 KB |
Output is correct |
7 |
Correct |
14 ms |
14824 KB |
Output is correct |
8 |
Correct |
14 ms |
14804 KB |
Output is correct |
9 |
Correct |
13 ms |
14432 KB |
Output is correct |
10 |
Correct |
12 ms |
14420 KB |
Output is correct |
11 |
Correct |
11 ms |
14420 KB |
Output is correct |
12 |
Correct |
12 ms |
14420 KB |
Output is correct |
13 |
Correct |
12 ms |
14368 KB |
Output is correct |
14 |
Correct |
11 ms |
14420 KB |
Output is correct |
15 |
Correct |
13 ms |
14420 KB |
Output is correct |
16 |
Correct |
14 ms |
14360 KB |
Output is correct |
17 |
Correct |
18 ms |
14420 KB |
Output is correct |
18 |
Correct |
13 ms |
14420 KB |
Output is correct |
19 |
Correct |
15 ms |
14432 KB |
Output is correct |
20 |
Correct |
13 ms |
14444 KB |
Output is correct |
21 |
Correct |
12 ms |
14420 KB |
Output is correct |
22 |
Correct |
14 ms |
14320 KB |
Output is correct |
23 |
Correct |
12 ms |
14424 KB |
Output is correct |
24 |
Correct |
19 ms |
14424 KB |
Output is correct |
25 |
Correct |
12 ms |
14408 KB |
Output is correct |
26 |
Correct |
12 ms |
14420 KB |
Output is correct |
27 |
Correct |
13 ms |
14420 KB |
Output is correct |
28 |
Correct |
15 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14316 KB |
Output is correct |
2 |
Correct |
678 ms |
102312 KB |
Output is correct |
3 |
Correct |
463 ms |
102904 KB |
Output is correct |
4 |
Correct |
654 ms |
101360 KB |
Output is correct |
5 |
Correct |
549 ms |
102504 KB |
Output is correct |
6 |
Correct |
438 ms |
102240 KB |
Output is correct |
7 |
Correct |
382 ms |
102932 KB |
Output is correct |
8 |
Correct |
378 ms |
103244 KB |
Output is correct |
9 |
Correct |
945 ms |
101004 KB |
Output is correct |
10 |
Correct |
657 ms |
101756 KB |
Output is correct |
11 |
Correct |
798 ms |
102376 KB |
Output is correct |
12 |
Correct |
614 ms |
102840 KB |
Output is correct |
13 |
Correct |
355 ms |
102928 KB |
Output is correct |
14 |
Correct |
332 ms |
103360 KB |
Output is correct |
15 |
Correct |
336 ms |
102648 KB |
Output is correct |
16 |
Correct |
414 ms |
103240 KB |
Output is correct |
17 |
Correct |
367 ms |
103712 KB |
Output is correct |
18 |
Correct |
12 ms |
14420 KB |
Output is correct |
19 |
Correct |
12 ms |
14348 KB |
Output is correct |
20 |
Correct |
12 ms |
14424 KB |
Output is correct |
21 |
Correct |
11 ms |
14424 KB |
Output is correct |
22 |
Correct |
14 ms |
14424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14316 KB |
Output is correct |
2 |
Correct |
678 ms |
102312 KB |
Output is correct |
3 |
Correct |
463 ms |
102904 KB |
Output is correct |
4 |
Correct |
654 ms |
101360 KB |
Output is correct |
5 |
Correct |
549 ms |
102504 KB |
Output is correct |
6 |
Correct |
438 ms |
102240 KB |
Output is correct |
7 |
Correct |
382 ms |
102932 KB |
Output is correct |
8 |
Correct |
378 ms |
103244 KB |
Output is correct |
9 |
Correct |
945 ms |
101004 KB |
Output is correct |
10 |
Correct |
657 ms |
101756 KB |
Output is correct |
11 |
Correct |
798 ms |
102376 KB |
Output is correct |
12 |
Correct |
614 ms |
102840 KB |
Output is correct |
13 |
Correct |
355 ms |
102928 KB |
Output is correct |
14 |
Correct |
332 ms |
103360 KB |
Output is correct |
15 |
Correct |
336 ms |
102648 KB |
Output is correct |
16 |
Correct |
414 ms |
103240 KB |
Output is correct |
17 |
Correct |
367 ms |
103712 KB |
Output is correct |
18 |
Correct |
12 ms |
14420 KB |
Output is correct |
19 |
Correct |
12 ms |
14348 KB |
Output is correct |
20 |
Correct |
12 ms |
14424 KB |
Output is correct |
21 |
Correct |
11 ms |
14424 KB |
Output is correct |
22 |
Correct |
14 ms |
14424 KB |
Output is correct |
23 |
Correct |
14 ms |
14420 KB |
Output is correct |
24 |
Correct |
731 ms |
102328 KB |
Output is correct |
25 |
Correct |
595 ms |
102860 KB |
Output is correct |
26 |
Correct |
697 ms |
101544 KB |
Output is correct |
27 |
Correct |
629 ms |
102536 KB |
Output is correct |
28 |
Correct |
469 ms |
102724 KB |
Output is correct |
29 |
Correct |
433 ms |
102568 KB |
Output is correct |
30 |
Correct |
326 ms |
103224 KB |
Output is correct |
31 |
Correct |
713 ms |
101252 KB |
Output is correct |
32 |
Correct |
555 ms |
101740 KB |
Output is correct |
33 |
Correct |
654 ms |
102436 KB |
Output is correct |
34 |
Correct |
608 ms |
102980 KB |
Output is correct |
35 |
Correct |
12 ms |
14352 KB |
Output is correct |
36 |
Correct |
14 ms |
14420 KB |
Output is correct |
37 |
Correct |
12 ms |
14392 KB |
Output is correct |
38 |
Correct |
13 ms |
14420 KB |
Output is correct |
39 |
Correct |
12 ms |
14440 KB |
Output is correct |
40 |
Correct |
14 ms |
14412 KB |
Output is correct |
41 |
Correct |
16 ms |
14336 KB |
Output is correct |
42 |
Correct |
15 ms |
14424 KB |
Output is correct |
43 |
Correct |
15 ms |
14420 KB |
Output is correct |
44 |
Correct |
14 ms |
14396 KB |
Output is correct |
45 |
Correct |
13 ms |
14420 KB |
Output is correct |
46 |
Correct |
14 ms |
14400 KB |
Output is correct |
47 |
Correct |
13 ms |
14380 KB |
Output is correct |
48 |
Correct |
12 ms |
14420 KB |
Output is correct |
49 |
Correct |
13 ms |
14424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
14420 KB |
Output is correct |
2 |
Correct |
1095 ms |
98440 KB |
Output is correct |
3 |
Correct |
1553 ms |
99028 KB |
Output is correct |
4 |
Correct |
1362 ms |
98448 KB |
Output is correct |
5 |
Correct |
897 ms |
99644 KB |
Output is correct |
6 |
Correct |
628 ms |
99828 KB |
Output is correct |
7 |
Correct |
556 ms |
100016 KB |
Output is correct |
8 |
Correct |
400 ms |
100168 KB |
Output is correct |
9 |
Correct |
1031 ms |
98268 KB |
Output is correct |
10 |
Correct |
1798 ms |
98812 KB |
Output is correct |
11 |
Correct |
1006 ms |
99148 KB |
Output is correct |
12 |
Correct |
1807 ms |
100068 KB |
Output is correct |
13 |
Correct |
1078 ms |
100980 KB |
Output is correct |
14 |
Correct |
1040 ms |
101880 KB |
Output is correct |
15 |
Correct |
15 ms |
14328 KB |
Output is correct |
16 |
Correct |
15 ms |
14372 KB |
Output is correct |
17 |
Correct |
14 ms |
14420 KB |
Output is correct |
18 |
Correct |
13 ms |
14400 KB |
Output is correct |
19 |
Correct |
14 ms |
14428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14296 KB |
Output is correct |
2 |
Correct |
1034 ms |
100996 KB |
Output is correct |
3 |
Correct |
2279 ms |
99432 KB |
Output is correct |
4 |
Correct |
1295 ms |
99924 KB |
Output is correct |
5 |
Correct |
1248 ms |
101184 KB |
Output is correct |
6 |
Correct |
714 ms |
101340 KB |
Output is correct |
7 |
Correct |
547 ms |
101220 KB |
Output is correct |
8 |
Correct |
417 ms |
101272 KB |
Output is correct |
9 |
Correct |
1106 ms |
100920 KB |
Output is correct |
10 |
Correct |
1974 ms |
99316 KB |
Output is correct |
11 |
Correct |
864 ms |
101960 KB |
Output is correct |
12 |
Correct |
1434 ms |
100412 KB |
Output is correct |
13 |
Correct |
956 ms |
101332 KB |
Output is correct |
14 |
Correct |
971 ms |
102092 KB |
Output is correct |
15 |
Correct |
13 ms |
14420 KB |
Output is correct |
16 |
Correct |
14 ms |
14436 KB |
Output is correct |
17 |
Correct |
14 ms |
14400 KB |
Output is correct |
18 |
Correct |
14 ms |
14420 KB |
Output is correct |
19 |
Correct |
14 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14420 KB |
Output is correct |
2 |
Correct |
13 ms |
14428 KB |
Output is correct |
3 |
Correct |
12 ms |
14424 KB |
Output is correct |
4 |
Correct |
13 ms |
14784 KB |
Output is correct |
5 |
Correct |
14 ms |
14812 KB |
Output is correct |
6 |
Correct |
16 ms |
14788 KB |
Output is correct |
7 |
Correct |
14 ms |
14824 KB |
Output is correct |
8 |
Correct |
14 ms |
14804 KB |
Output is correct |
9 |
Correct |
13 ms |
14432 KB |
Output is correct |
10 |
Correct |
12 ms |
14420 KB |
Output is correct |
11 |
Correct |
11 ms |
14420 KB |
Output is correct |
12 |
Correct |
12 ms |
14420 KB |
Output is correct |
13 |
Correct |
12 ms |
14368 KB |
Output is correct |
14 |
Correct |
11 ms |
14420 KB |
Output is correct |
15 |
Correct |
13 ms |
14420 KB |
Output is correct |
16 |
Correct |
14 ms |
14360 KB |
Output is correct |
17 |
Correct |
18 ms |
14420 KB |
Output is correct |
18 |
Correct |
13 ms |
14420 KB |
Output is correct |
19 |
Correct |
15 ms |
14432 KB |
Output is correct |
20 |
Correct |
13 ms |
14444 KB |
Output is correct |
21 |
Correct |
12 ms |
14420 KB |
Output is correct |
22 |
Correct |
14 ms |
14320 KB |
Output is correct |
23 |
Correct |
12 ms |
14424 KB |
Output is correct |
24 |
Correct |
19 ms |
14424 KB |
Output is correct |
25 |
Correct |
12 ms |
14408 KB |
Output is correct |
26 |
Correct |
12 ms |
14420 KB |
Output is correct |
27 |
Correct |
13 ms |
14420 KB |
Output is correct |
28 |
Correct |
15 ms |
14420 KB |
Output is correct |
29 |
Correct |
15 ms |
14316 KB |
Output is correct |
30 |
Correct |
678 ms |
102312 KB |
Output is correct |
31 |
Correct |
463 ms |
102904 KB |
Output is correct |
32 |
Correct |
654 ms |
101360 KB |
Output is correct |
33 |
Correct |
549 ms |
102504 KB |
Output is correct |
34 |
Correct |
438 ms |
102240 KB |
Output is correct |
35 |
Correct |
382 ms |
102932 KB |
Output is correct |
36 |
Correct |
378 ms |
103244 KB |
Output is correct |
37 |
Correct |
945 ms |
101004 KB |
Output is correct |
38 |
Correct |
657 ms |
101756 KB |
Output is correct |
39 |
Correct |
798 ms |
102376 KB |
Output is correct |
40 |
Correct |
614 ms |
102840 KB |
Output is correct |
41 |
Correct |
355 ms |
102928 KB |
Output is correct |
42 |
Correct |
332 ms |
103360 KB |
Output is correct |
43 |
Correct |
336 ms |
102648 KB |
Output is correct |
44 |
Correct |
414 ms |
103240 KB |
Output is correct |
45 |
Correct |
367 ms |
103712 KB |
Output is correct |
46 |
Correct |
12 ms |
14420 KB |
Output is correct |
47 |
Correct |
12 ms |
14348 KB |
Output is correct |
48 |
Correct |
12 ms |
14424 KB |
Output is correct |
49 |
Correct |
11 ms |
14424 KB |
Output is correct |
50 |
Correct |
14 ms |
14424 KB |
Output is correct |
51 |
Correct |
14 ms |
14420 KB |
Output is correct |
52 |
Correct |
731 ms |
102328 KB |
Output is correct |
53 |
Correct |
595 ms |
102860 KB |
Output is correct |
54 |
Correct |
697 ms |
101544 KB |
Output is correct |
55 |
Correct |
629 ms |
102536 KB |
Output is correct |
56 |
Correct |
469 ms |
102724 KB |
Output is correct |
57 |
Correct |
433 ms |
102568 KB |
Output is correct |
58 |
Correct |
326 ms |
103224 KB |
Output is correct |
59 |
Correct |
713 ms |
101252 KB |
Output is correct |
60 |
Correct |
555 ms |
101740 KB |
Output is correct |
61 |
Correct |
654 ms |
102436 KB |
Output is correct |
62 |
Correct |
608 ms |
102980 KB |
Output is correct |
63 |
Correct |
12 ms |
14352 KB |
Output is correct |
64 |
Correct |
14 ms |
14420 KB |
Output is correct |
65 |
Correct |
12 ms |
14392 KB |
Output is correct |
66 |
Correct |
13 ms |
14420 KB |
Output is correct |
67 |
Correct |
12 ms |
14440 KB |
Output is correct |
68 |
Correct |
14 ms |
14412 KB |
Output is correct |
69 |
Correct |
16 ms |
14336 KB |
Output is correct |
70 |
Correct |
15 ms |
14424 KB |
Output is correct |
71 |
Correct |
15 ms |
14420 KB |
Output is correct |
72 |
Correct |
14 ms |
14396 KB |
Output is correct |
73 |
Correct |
13 ms |
14420 KB |
Output is correct |
74 |
Correct |
14 ms |
14400 KB |
Output is correct |
75 |
Correct |
13 ms |
14380 KB |
Output is correct |
76 |
Correct |
12 ms |
14420 KB |
Output is correct |
77 |
Correct |
13 ms |
14424 KB |
Output is correct |
78 |
Correct |
20 ms |
14420 KB |
Output is correct |
79 |
Correct |
1095 ms |
98440 KB |
Output is correct |
80 |
Correct |
1553 ms |
99028 KB |
Output is correct |
81 |
Correct |
1362 ms |
98448 KB |
Output is correct |
82 |
Correct |
897 ms |
99644 KB |
Output is correct |
83 |
Correct |
628 ms |
99828 KB |
Output is correct |
84 |
Correct |
556 ms |
100016 KB |
Output is correct |
85 |
Correct |
400 ms |
100168 KB |
Output is correct |
86 |
Correct |
1031 ms |
98268 KB |
Output is correct |
87 |
Correct |
1798 ms |
98812 KB |
Output is correct |
88 |
Correct |
1006 ms |
99148 KB |
Output is correct |
89 |
Correct |
1807 ms |
100068 KB |
Output is correct |
90 |
Correct |
1078 ms |
100980 KB |
Output is correct |
91 |
Correct |
1040 ms |
101880 KB |
Output is correct |
92 |
Correct |
15 ms |
14328 KB |
Output is correct |
93 |
Correct |
15 ms |
14372 KB |
Output is correct |
94 |
Correct |
14 ms |
14420 KB |
Output is correct |
95 |
Correct |
13 ms |
14400 KB |
Output is correct |
96 |
Correct |
14 ms |
14428 KB |
Output is correct |
97 |
Correct |
14 ms |
14296 KB |
Output is correct |
98 |
Correct |
1034 ms |
100996 KB |
Output is correct |
99 |
Correct |
2279 ms |
99432 KB |
Output is correct |
100 |
Correct |
1295 ms |
99924 KB |
Output is correct |
101 |
Correct |
1248 ms |
101184 KB |
Output is correct |
102 |
Correct |
714 ms |
101340 KB |
Output is correct |
103 |
Correct |
547 ms |
101220 KB |
Output is correct |
104 |
Correct |
417 ms |
101272 KB |
Output is correct |
105 |
Correct |
1106 ms |
100920 KB |
Output is correct |
106 |
Correct |
1974 ms |
99316 KB |
Output is correct |
107 |
Correct |
864 ms |
101960 KB |
Output is correct |
108 |
Correct |
1434 ms |
100412 KB |
Output is correct |
109 |
Correct |
956 ms |
101332 KB |
Output is correct |
110 |
Correct |
971 ms |
102092 KB |
Output is correct |
111 |
Correct |
13 ms |
14420 KB |
Output is correct |
112 |
Correct |
14 ms |
14436 KB |
Output is correct |
113 |
Correct |
14 ms |
14400 KB |
Output is correct |
114 |
Correct |
14 ms |
14420 KB |
Output is correct |
115 |
Correct |
14 ms |
14420 KB |
Output is correct |
116 |
Correct |
855 ms |
100320 KB |
Output is correct |
117 |
Correct |
1530 ms |
102924 KB |
Output is correct |
118 |
Correct |
1040 ms |
101432 KB |
Output is correct |
119 |
Correct |
1074 ms |
102580 KB |
Output is correct |
120 |
Correct |
613 ms |
102236 KB |
Output is correct |
121 |
Correct |
577 ms |
103192 KB |
Output is correct |
122 |
Correct |
395 ms |
103136 KB |
Output is correct |
123 |
Correct |
1004 ms |
101476 KB |
Output is correct |
124 |
Correct |
1911 ms |
101524 KB |
Output is correct |
125 |
Correct |
786 ms |
101468 KB |
Output is correct |
126 |
Correct |
1182 ms |
102788 KB |
Output is correct |
127 |
Correct |
1387 ms |
103240 KB |
Output is correct |
128 |
Correct |
898 ms |
103940 KB |
Output is correct |
129 |
Correct |
942 ms |
104780 KB |
Output is correct |