// time_limit/solution-ayaze-dp.cpp
// O((N+M)^2 Q log (N + M))
#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
const int kMod = 1'000'002'022;
vector<pair<int, int>> dp;
vector<int> P;
vector<int> depth;
vector<vector<int>> children;
int N;
void calculate(int v) {
if (v >= N) return;
vector<int> temp_dp = {1};
for (int chld : children[v]) {
vector<int> next_dp(temp_dp.size()+1, 0);
for (int i = 0 ; i < static_cast<int>(temp_dp.size()) ; i++) {
next_dp[i] = (next_dp[i] + 1ll * temp_dp[i] * dp[chld].first) % kMod;
next_dp[i+1] = (next_dp[i+1] + 1ll * temp_dp[i] * dp[chld].second) % kMod;
}
temp_dp = next_dp;
}
int chld_count = children[v].size();
dp[v] = {0, 0};
for (int i = 0 ; i < static_cast<int>(temp_dp.size()) ; i++) {
dp[v].first = (dp[v].first + 1ll * (chld_count - i) * temp_dp[i]) % kMod;
dp[v].second = (dp[v].second + 1ll * i * temp_dp[i]) % kMod;
}
}
void init(int _N, int M, std::vector<int> _P, std::vector<int> A) {
N = _N;
P = _P;
depth.resize(N+M);
dp.resize(N+M);
children.resize(N+M);
for (int i = 1 ; i < static_cast<int>(P.size()) ; i++) {
children[P[i]].push_back(i);
depth[i] = 1 + depth[P[i]];
}
for (int i = N+M-1 ; i >= 0 ; i--) {
if (i >= N) {
dp[i] = {A[i-N] == 0, A[i-N] == 1};
} else {
calculate(i);
}
}
}
int count_ways(int L, int R) {
set<pair<int, int>> st;
for (int i = L ; i <= R ; i++) {
swap(dp[i].first, dp[i].second);
st.insert({depth[P[i]], P[i]});
}
while (!st.empty()) { // try to optimize, but this might be slower..
pair<int, int> cur = *st.rbegin();
st.erase(cur);
int v = cur.second;
calculate(v);
if (v > 0) {
st.insert({depth[P[v]], P[v]});
}
}
return dp[0].second;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
17 ms |
464 KB |
Output is correct |
4 |
Correct |
21 ms |
512 KB |
Output is correct |
5 |
Correct |
22 ms |
504 KB |
Output is correct |
6 |
Correct |
16 ms |
496 KB |
Output is correct |
7 |
Correct |
15 ms |
512 KB |
Output is correct |
8 |
Correct |
15 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
2 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
432 KB |
Output is correct |
9 |
Correct |
3 ms |
344 KB |
Output is correct |
10 |
Correct |
3 ms |
336 KB |
Output is correct |
11 |
Correct |
4 ms |
464 KB |
Output is correct |
12 |
Correct |
2 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
17 ms |
464 KB |
Output is correct |
4 |
Correct |
21 ms |
512 KB |
Output is correct |
5 |
Correct |
22 ms |
504 KB |
Output is correct |
6 |
Correct |
16 ms |
496 KB |
Output is correct |
7 |
Correct |
15 ms |
512 KB |
Output is correct |
8 |
Correct |
15 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
2 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
432 KB |
Output is correct |
17 |
Correct |
3 ms |
344 KB |
Output is correct |
18 |
Correct |
3 ms |
336 KB |
Output is correct |
19 |
Correct |
4 ms |
464 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
3 ms |
416 KB |
Output is correct |
22 |
Correct |
2 ms |
440 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
4 ms |
336 KB |
Output is correct |
27 |
Correct |
3 ms |
336 KB |
Output is correct |
28 |
Correct |
4 ms |
336 KB |
Output is correct |
29 |
Correct |
19 ms |
512 KB |
Output is correct |
30 |
Correct |
19 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
384 KB |
Output is correct |
33 |
Correct |
3 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
4 ms |
340 KB |
Output is correct |
36 |
Correct |
3 ms |
384 KB |
Output is correct |
37 |
Correct |
16 ms |
468 KB |
Output is correct |
38 |
Correct |
21 ms |
512 KB |
Output is correct |
39 |
Correct |
2 ms |
336 KB |
Output is correct |
40 |
Correct |
2 ms |
356 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1251 ms |
4648 KB |
Output is correct |
2 |
Correct |
1673 ms |
9012 KB |
Output is correct |
3 |
Correct |
1502 ms |
9036 KB |
Output is correct |
4 |
Correct |
1606 ms |
9036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1251 ms |
4648 KB |
Output is correct |
2 |
Correct |
1673 ms |
9012 KB |
Output is correct |
3 |
Correct |
1502 ms |
9036 KB |
Output is correct |
4 |
Correct |
1606 ms |
9036 KB |
Output is correct |
5 |
Execution timed out |
3009 ms |
5024 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
2 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
432 KB |
Output is correct |
9 |
Correct |
3 ms |
344 KB |
Output is correct |
10 |
Correct |
3 ms |
336 KB |
Output is correct |
11 |
Correct |
4 ms |
464 KB |
Output is correct |
12 |
Correct |
2 ms |
336 KB |
Output is correct |
13 |
Correct |
1251 ms |
4648 KB |
Output is correct |
14 |
Correct |
1673 ms |
9012 KB |
Output is correct |
15 |
Correct |
1502 ms |
9036 KB |
Output is correct |
16 |
Correct |
1606 ms |
9036 KB |
Output is correct |
17 |
Execution timed out |
3009 ms |
5024 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
17 ms |
464 KB |
Output is correct |
4 |
Correct |
21 ms |
512 KB |
Output is correct |
5 |
Correct |
22 ms |
504 KB |
Output is correct |
6 |
Correct |
16 ms |
496 KB |
Output is correct |
7 |
Correct |
15 ms |
512 KB |
Output is correct |
8 |
Correct |
15 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
2 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
432 KB |
Output is correct |
17 |
Correct |
3 ms |
344 KB |
Output is correct |
18 |
Correct |
3 ms |
336 KB |
Output is correct |
19 |
Correct |
4 ms |
464 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
3 ms |
416 KB |
Output is correct |
22 |
Correct |
2 ms |
440 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
4 ms |
336 KB |
Output is correct |
27 |
Correct |
3 ms |
336 KB |
Output is correct |
28 |
Correct |
4 ms |
336 KB |
Output is correct |
29 |
Correct |
19 ms |
512 KB |
Output is correct |
30 |
Correct |
19 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
384 KB |
Output is correct |
33 |
Correct |
3 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
4 ms |
340 KB |
Output is correct |
36 |
Correct |
3 ms |
384 KB |
Output is correct |
37 |
Correct |
16 ms |
468 KB |
Output is correct |
38 |
Correct |
21 ms |
512 KB |
Output is correct |
39 |
Correct |
2 ms |
336 KB |
Output is correct |
40 |
Correct |
2 ms |
356 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
368 KB |
Output is correct |
43 |
Execution timed out |
3066 ms |
740 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
17 ms |
464 KB |
Output is correct |
4 |
Correct |
21 ms |
512 KB |
Output is correct |
5 |
Correct |
22 ms |
504 KB |
Output is correct |
6 |
Correct |
16 ms |
496 KB |
Output is correct |
7 |
Correct |
15 ms |
512 KB |
Output is correct |
8 |
Correct |
15 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
2 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
432 KB |
Output is correct |
17 |
Correct |
3 ms |
344 KB |
Output is correct |
18 |
Correct |
3 ms |
336 KB |
Output is correct |
19 |
Correct |
4 ms |
464 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
3 ms |
416 KB |
Output is correct |
22 |
Correct |
2 ms |
440 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
4 ms |
336 KB |
Output is correct |
27 |
Correct |
3 ms |
336 KB |
Output is correct |
28 |
Correct |
4 ms |
336 KB |
Output is correct |
29 |
Correct |
19 ms |
512 KB |
Output is correct |
30 |
Correct |
19 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
384 KB |
Output is correct |
33 |
Correct |
3 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
4 ms |
340 KB |
Output is correct |
36 |
Correct |
3 ms |
384 KB |
Output is correct |
37 |
Correct |
16 ms |
468 KB |
Output is correct |
38 |
Correct |
21 ms |
512 KB |
Output is correct |
39 |
Correct |
2 ms |
336 KB |
Output is correct |
40 |
Correct |
2 ms |
356 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
368 KB |
Output is correct |
43 |
Correct |
1251 ms |
4648 KB |
Output is correct |
44 |
Correct |
1673 ms |
9012 KB |
Output is correct |
45 |
Correct |
1502 ms |
9036 KB |
Output is correct |
46 |
Correct |
1606 ms |
9036 KB |
Output is correct |
47 |
Execution timed out |
3009 ms |
5024 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |