// time_limit/solution-prabowo-no-segtree.cpp
// O(N + QM)
// Note: this solution can pass quadratic subtask because of low constant
#pragma GCC optimize("Ofast,unroll-loops")
#include "circuit.h"
#include <functional>
#include <utility>
#include <vector>
const int kMod = 1000002022;
int N;
std::vector<int> A;
std::vector<int> contribution;
long long total;
void init(int _N, int M, std::vector<int> P, std::vector<int> _A) {
N = _N; A = _A;
std::vector<std::vector<int>> adj(N + M);
for (int i = 1; i < N + M; ++i) {
adj[P[i]].push_back(i);
}
std::vector<int> prod(N + M);
std::function<int(int)> dfs_prod = [&](int u) {
if (u >= N) {
return prod[u] = 1;
}
prod[u] = static_cast<int>(adj[u].size());
for (int v : adj[u]) {
prod[u] = 1LL * prod[u] * dfs_prod(v) % kMod;
}
return prod[u];
};
dfs_prod(0);
contribution.resize(M);
std::function<void(int, int)> dfs_contrib = [&](int u, int product) {
if (u >= N) {
contribution[u - N] = product;
return;
}
std::vector<int> prefix(adj[u].size()), suffix(adj[u].size());
for (int i = 0; i < static_cast<int>(adj[u].size()); ++i) {
prefix[i] = prod[adj[u][i]];
if (i > 0) {
prefix[i] = 1LL * prefix[i] * prefix[i - 1] % kMod;
}
}
for (int i = static_cast<int>(adj[u].size()) - 1; i >= 0; --i) {
suffix[i] = prod[adj[u][i]];
if (i + 1 < static_cast<int>(adj[u].size())) {
suffix[i] = 1LL * suffix[i] * suffix[i + 1] % kMod;
}
}
for (int i = 0; i < static_cast<int>(adj[u].size()); ++i) {
int next_product = product;
if (i > 0) {
next_product = 1LL * next_product * prefix[i - 1] % kMod;
}
if (i + 1 < static_cast<int>(adj[u].size())) {
next_product = 1LL * next_product * suffix[i + 1] % kMod;
}
dfs_contrib(adj[u][i], next_product);
}
};
dfs_contrib(0, 1);
total = 0;
for (int j = 0; j < M; ++j) {
total += contribution[j] * A[j];
}
}
int count_ways(int L, int R) {
L -= N; R -= N;
for (int j = L; j <= R; ++j) {
total += (2 * (A[j] ^= 1) - 1) * contribution[j];
}
return total % kMod;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
552 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
552 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
400 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
476 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
360 KB |
Output is correct |
35 |
Correct |
1 ms |
472 KB |
Output is correct |
36 |
Correct |
2 ms |
472 KB |
Output is correct |
37 |
Correct |
1 ms |
472 KB |
Output is correct |
38 |
Correct |
1 ms |
476 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
1 ms |
368 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
827 ms |
4148 KB |
Output is correct |
2 |
Correct |
754 ms |
8000 KB |
Output is correct |
3 |
Correct |
1101 ms |
7964 KB |
Output is correct |
4 |
Correct |
1162 ms |
7960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
827 ms |
4148 KB |
Output is correct |
2 |
Correct |
754 ms |
8000 KB |
Output is correct |
3 |
Correct |
1101 ms |
7964 KB |
Output is correct |
4 |
Correct |
1162 ms |
7960 KB |
Output is correct |
5 |
Correct |
1305 ms |
4116 KB |
Output is correct |
6 |
Execution timed out |
2776 ms |
7964 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
552 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
827 ms |
4148 KB |
Output is correct |
14 |
Correct |
754 ms |
8000 KB |
Output is correct |
15 |
Correct |
1101 ms |
7964 KB |
Output is correct |
16 |
Correct |
1162 ms |
7960 KB |
Output is correct |
17 |
Correct |
1305 ms |
4116 KB |
Output is correct |
18 |
Execution timed out |
2776 ms |
7964 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
552 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
400 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
476 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
360 KB |
Output is correct |
35 |
Correct |
1 ms |
472 KB |
Output is correct |
36 |
Correct |
2 ms |
472 KB |
Output is correct |
37 |
Correct |
1 ms |
472 KB |
Output is correct |
38 |
Correct |
1 ms |
476 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
1 ms |
368 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
669 ms |
624 KB |
Output is correct |
44 |
Correct |
1193 ms |
640 KB |
Output is correct |
45 |
Correct |
955 ms |
592 KB |
Output is correct |
46 |
Correct |
646 ms |
788 KB |
Output is correct |
47 |
Correct |
1029 ms |
848 KB |
Output is correct |
48 |
Correct |
982 ms |
848 KB |
Output is correct |
49 |
Correct |
1261 ms |
848 KB |
Output is correct |
50 |
Correct |
1522 ms |
848 KB |
Output is correct |
51 |
Correct |
1294 ms |
592 KB |
Output is correct |
52 |
Correct |
1196 ms |
592 KB |
Output is correct |
53 |
Correct |
1044 ms |
1360 KB |
Output is correct |
54 |
Correct |
1132 ms |
848 KB |
Output is correct |
55 |
Correct |
1069 ms |
592 KB |
Output is correct |
56 |
Correct |
1220 ms |
592 KB |
Output is correct |
57 |
Correct |
999 ms |
464 KB |
Output is correct |
58 |
Correct |
1105 ms |
1616 KB |
Output is correct |
59 |
Correct |
1436 ms |
1744 KB |
Output is correct |
60 |
Correct |
1109 ms |
1688 KB |
Output is correct |
61 |
Correct |
1139 ms |
848 KB |
Output is correct |
62 |
Correct |
1267 ms |
592 KB |
Output is correct |
63 |
Correct |
1293 ms |
628 KB |
Output is correct |
64 |
Correct |
1092 ms |
464 KB |
Output is correct |
65 |
Correct |
423 ms |
464 KB |
Output is correct |
66 |
Correct |
889 ms |
720 KB |
Output is correct |
67 |
Correct |
1006 ms |
720 KB |
Output is correct |
68 |
Correct |
1056 ms |
720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
552 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
400 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
476 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
360 KB |
Output is correct |
35 |
Correct |
1 ms |
472 KB |
Output is correct |
36 |
Correct |
2 ms |
472 KB |
Output is correct |
37 |
Correct |
1 ms |
472 KB |
Output is correct |
38 |
Correct |
1 ms |
476 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
1 ms |
368 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
827 ms |
4148 KB |
Output is correct |
44 |
Correct |
754 ms |
8000 KB |
Output is correct |
45 |
Correct |
1101 ms |
7964 KB |
Output is correct |
46 |
Correct |
1162 ms |
7960 KB |
Output is correct |
47 |
Correct |
1305 ms |
4116 KB |
Output is correct |
48 |
Execution timed out |
2776 ms |
7964 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |