#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000002022;
struct node {
ll sum, sum2;
};
node operator+(const node& a, const node& b) {
node ret;
ret.sum = (a.sum + b.sum) % MOD;
ret.sum2 = (a.sum2 + b.sum2) % MOD;
return ret;
}
struct segmentTree {
vector<node> tree;
vector<int> lazy;
void init(int n) {
int sz = (1 << (int)ceil(log2(n)) + 1);
tree.resize(sz);
lazy.resize(sz);
}
node updateVal(int x, node v, int l, int r, int n) {
if (r < x || x < l)
return tree[n];
if (l == r)
return tree[n] = v;
int m = (l + r) >> 1;
return tree[n] = updateVal(x, v, l, m, n << 1) + updateVal(x, v, m + 1, r, n << 1 | 1);
}
void propagate(int l, int r, int n) {
if (!lazy[n])
return;
swap(tree[n].sum, tree[n].sum2);
if (l != r) {
lazy[n << 1] = !lazy[n << 1];
lazy[n << 1 | 1] = !lazy[n << 1 | 1];
}
lazy[n] = 0;
}
node update(int lx, int rx, int l, int r, int n) {
propagate(l, r, n);
if (r < lx || rx < l)
return tree[n];
if (lx <= l && r <= rx) {
lazy[n] = !lazy[n];
propagate(l, r, n);
return tree[n];
}
int m = (l + r) >> 1;
return tree[n] = update(lx, rx, l, m, n << 1) + update(lx, rx, m + 1, r, n << 1 | 1);
}
};
int n, m;
vector<int> child[200000];
ll degs[200000];
ll dp[200000];
segmentTree st;
ll dfs(int v) {
int sz = (int)child[v].size();
ll& ret = degs[v] = max(sz, 1);
for (int i : child[v])
ret = ret * dfs(i) % MOD;
return ret;
}
void dfs2(int v, ll tmp) {
dp[v] = tmp;
int sz = (int)child[v].size();
vector<ll> pre(sz + 1), suf(sz + 1);
pre[0] = 1;
for (int i = 1; i <= sz; i++)
pre[i] = pre[i - 1] * degs[child[v][i - 1]] % MOD;
suf[sz] = 1;
for (int i = sz - 1; i >= 0; i--)
suf[i] = suf[i + 1] * degs[child[v][i]] % MOD;
for (int i = 0; i < sz; i++)
dfs2(child[v][i], tmp * pre[i] % MOD * suf[i + 1] % MOD);
}
void init(int _n, int _m, vector<int> p, vector<int> a) {
n = _n;
m = _m;
for (int i = 0; i < n + m; i++)
child[p[i]].push_back(i);
dfs(0);
dfs2(0, 1);
st.init(m);
for (int i = 0; i < m; i++) {
node cur = { dp[i + n],0 };
if (!a[i])
swap(cur.sum, cur.sum2);
st.updateVal(i, cur, 0, m - 1, 1);
}
}
int count_ways(int l, int r) {
st.update(l - n, r - n, 0, m - 1, 1);
st.propagate(0, m - 1, 1);
return st.tree[1].sum;
}
Compilation message
circuit.cpp: In member function 'void segmentTree::init(int)':
circuit.cpp:19:37: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
19 | int sz = (1 << (int)ceil(log2(n)) + 1);
| ~~~~~~~~~~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4908 KB |
Output is correct |
3 |
Correct |
4 ms |
5004 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5000 KB |
Output is correct |
7 |
Correct |
4 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
3 ms |
4944 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
4 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5200 KB |
Output is correct |
11 |
Correct |
5 ms |
5200 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4908 KB |
Output is correct |
3 |
Correct |
4 ms |
5004 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5000 KB |
Output is correct |
7 |
Correct |
4 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
3 ms |
4944 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
4 ms |
5072 KB |
Output is correct |
17 |
Correct |
4 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
5 ms |
5200 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5012 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
5 ms |
5088 KB |
Output is correct |
28 |
Correct |
4 ms |
5072 KB |
Output is correct |
29 |
Correct |
4 ms |
5072 KB |
Output is correct |
30 |
Correct |
4 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
4 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
4 ms |
5200 KB |
Output is correct |
37 |
Correct |
3 ms |
5200 KB |
Output is correct |
38 |
Correct |
4 ms |
5200 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5032 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
649 ms |
9088 KB |
Output is correct |
2 |
Correct |
884 ms |
13192 KB |
Output is correct |
3 |
Correct |
755 ms |
13188 KB |
Output is correct |
4 |
Correct |
890 ms |
13176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
649 ms |
9088 KB |
Output is correct |
2 |
Correct |
884 ms |
13192 KB |
Output is correct |
3 |
Correct |
755 ms |
13188 KB |
Output is correct |
4 |
Correct |
890 ms |
13176 KB |
Output is correct |
5 |
Correct |
654 ms |
9060 KB |
Output is correct |
6 |
Correct |
856 ms |
13128 KB |
Output is correct |
7 |
Correct |
727 ms |
13192 KB |
Output is correct |
8 |
Correct |
846 ms |
13196 KB |
Output is correct |
9 |
Correct |
470 ms |
5240 KB |
Output is correct |
10 |
Correct |
936 ms |
5500 KB |
Output is correct |
11 |
Correct |
907 ms |
5508 KB |
Output is correct |
12 |
Correct |
822 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
3 ms |
4944 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
4 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5200 KB |
Output is correct |
11 |
Correct |
5 ms |
5200 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
649 ms |
9088 KB |
Output is correct |
14 |
Correct |
884 ms |
13192 KB |
Output is correct |
15 |
Correct |
755 ms |
13188 KB |
Output is correct |
16 |
Correct |
890 ms |
13176 KB |
Output is correct |
17 |
Correct |
654 ms |
9060 KB |
Output is correct |
18 |
Correct |
856 ms |
13128 KB |
Output is correct |
19 |
Correct |
727 ms |
13192 KB |
Output is correct |
20 |
Correct |
846 ms |
13196 KB |
Output is correct |
21 |
Correct |
470 ms |
5240 KB |
Output is correct |
22 |
Correct |
936 ms |
5500 KB |
Output is correct |
23 |
Correct |
907 ms |
5508 KB |
Output is correct |
24 |
Correct |
822 ms |
5456 KB |
Output is correct |
25 |
Correct |
1058 ms |
18560 KB |
Output is correct |
26 |
Correct |
934 ms |
18720 KB |
Output is correct |
27 |
Correct |
1311 ms |
18808 KB |
Output is correct |
28 |
Correct |
865 ms |
18688 KB |
Output is correct |
29 |
Correct |
1115 ms |
33940 KB |
Output is correct |
30 |
Correct |
916 ms |
33948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4908 KB |
Output is correct |
3 |
Correct |
4 ms |
5004 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5000 KB |
Output is correct |
7 |
Correct |
4 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
3 ms |
4944 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
4 ms |
5072 KB |
Output is correct |
17 |
Correct |
4 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
5 ms |
5200 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5012 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
5 ms |
5088 KB |
Output is correct |
28 |
Correct |
4 ms |
5072 KB |
Output is correct |
29 |
Correct |
4 ms |
5072 KB |
Output is correct |
30 |
Correct |
4 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
4 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
4 ms |
5200 KB |
Output is correct |
37 |
Correct |
3 ms |
5200 KB |
Output is correct |
38 |
Correct |
4 ms |
5200 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5032 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
43 |
Correct |
364 ms |
5416 KB |
Output is correct |
44 |
Correct |
852 ms |
5464 KB |
Output is correct |
45 |
Correct |
959 ms |
5440 KB |
Output is correct |
46 |
Correct |
1018 ms |
5764 KB |
Output is correct |
47 |
Correct |
996 ms |
5760 KB |
Output is correct |
48 |
Correct |
951 ms |
5756 KB |
Output is correct |
49 |
Correct |
974 ms |
5768 KB |
Output is correct |
50 |
Correct |
1018 ms |
5760 KB |
Output is correct |
51 |
Correct |
727 ms |
5456 KB |
Output is correct |
52 |
Correct |
912 ms |
5456 KB |
Output is correct |
53 |
Correct |
660 ms |
6292 KB |
Output is correct |
54 |
Correct |
1043 ms |
5764 KB |
Output is correct |
55 |
Correct |
712 ms |
5628 KB |
Output is correct |
56 |
Correct |
747 ms |
5616 KB |
Output is correct |
57 |
Correct |
880 ms |
5504 KB |
Output is correct |
58 |
Correct |
762 ms |
6480 KB |
Output is correct |
59 |
Correct |
763 ms |
6548 KB |
Output is correct |
60 |
Correct |
896 ms |
6480 KB |
Output is correct |
61 |
Correct |
945 ms |
5636 KB |
Output is correct |
62 |
Correct |
912 ms |
5380 KB |
Output is correct |
63 |
Correct |
923 ms |
5380 KB |
Output is correct |
64 |
Correct |
832 ms |
5556 KB |
Output is correct |
65 |
Correct |
392 ms |
5252 KB |
Output is correct |
66 |
Correct |
706 ms |
5456 KB |
Output is correct |
67 |
Correct |
897 ms |
5508 KB |
Output is correct |
68 |
Correct |
860 ms |
5512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4908 KB |
Output is correct |
3 |
Correct |
4 ms |
5004 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5000 KB |
Output is correct |
7 |
Correct |
4 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
3 ms |
4944 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
4 ms |
5072 KB |
Output is correct |
17 |
Correct |
4 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
5 ms |
5200 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5012 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
5 ms |
5088 KB |
Output is correct |
28 |
Correct |
4 ms |
5072 KB |
Output is correct |
29 |
Correct |
4 ms |
5072 KB |
Output is correct |
30 |
Correct |
4 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
4 ms |
5072 KB |
Output is correct |
34 |
Correct |
3 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
4 ms |
5200 KB |
Output is correct |
37 |
Correct |
3 ms |
5200 KB |
Output is correct |
38 |
Correct |
4 ms |
5200 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5032 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
43 |
Correct |
649 ms |
9088 KB |
Output is correct |
44 |
Correct |
884 ms |
13192 KB |
Output is correct |
45 |
Correct |
755 ms |
13188 KB |
Output is correct |
46 |
Correct |
890 ms |
13176 KB |
Output is correct |
47 |
Correct |
654 ms |
9060 KB |
Output is correct |
48 |
Correct |
856 ms |
13128 KB |
Output is correct |
49 |
Correct |
727 ms |
13192 KB |
Output is correct |
50 |
Correct |
846 ms |
13196 KB |
Output is correct |
51 |
Correct |
470 ms |
5240 KB |
Output is correct |
52 |
Correct |
936 ms |
5500 KB |
Output is correct |
53 |
Correct |
907 ms |
5508 KB |
Output is correct |
54 |
Correct |
822 ms |
5456 KB |
Output is correct |
55 |
Correct |
1058 ms |
18560 KB |
Output is correct |
56 |
Correct |
934 ms |
18720 KB |
Output is correct |
57 |
Correct |
1311 ms |
18808 KB |
Output is correct |
58 |
Correct |
865 ms |
18688 KB |
Output is correct |
59 |
Correct |
1115 ms |
33940 KB |
Output is correct |
60 |
Correct |
916 ms |
33948 KB |
Output is correct |
61 |
Correct |
364 ms |
5416 KB |
Output is correct |
62 |
Correct |
852 ms |
5464 KB |
Output is correct |
63 |
Correct |
959 ms |
5440 KB |
Output is correct |
64 |
Correct |
1018 ms |
5764 KB |
Output is correct |
65 |
Correct |
996 ms |
5760 KB |
Output is correct |
66 |
Correct |
951 ms |
5756 KB |
Output is correct |
67 |
Correct |
974 ms |
5768 KB |
Output is correct |
68 |
Correct |
1018 ms |
5760 KB |
Output is correct |
69 |
Correct |
727 ms |
5456 KB |
Output is correct |
70 |
Correct |
912 ms |
5456 KB |
Output is correct |
71 |
Correct |
660 ms |
6292 KB |
Output is correct |
72 |
Correct |
1043 ms |
5764 KB |
Output is correct |
73 |
Correct |
712 ms |
5628 KB |
Output is correct |
74 |
Correct |
747 ms |
5616 KB |
Output is correct |
75 |
Correct |
880 ms |
5504 KB |
Output is correct |
76 |
Correct |
762 ms |
6480 KB |
Output is correct |
77 |
Correct |
763 ms |
6548 KB |
Output is correct |
78 |
Correct |
896 ms |
6480 KB |
Output is correct |
79 |
Correct |
945 ms |
5636 KB |
Output is correct |
80 |
Correct |
912 ms |
5380 KB |
Output is correct |
81 |
Correct |
923 ms |
5380 KB |
Output is correct |
82 |
Correct |
832 ms |
5556 KB |
Output is correct |
83 |
Correct |
392 ms |
5252 KB |
Output is correct |
84 |
Correct |
706 ms |
5456 KB |
Output is correct |
85 |
Correct |
897 ms |
5508 KB |
Output is correct |
86 |
Correct |
860 ms |
5512 KB |
Output is correct |
87 |
Correct |
3 ms |
4944 KB |
Output is correct |
88 |
Correct |
595 ms |
17780 KB |
Output is correct |
89 |
Correct |
940 ms |
13308 KB |
Output is correct |
90 |
Correct |
1022 ms |
13080 KB |
Output is correct |
91 |
Correct |
1057 ms |
18812 KB |
Output is correct |
92 |
Correct |
889 ms |
18856 KB |
Output is correct |
93 |
Correct |
939 ms |
18808 KB |
Output is correct |
94 |
Correct |
969 ms |
18804 KB |
Output is correct |
95 |
Correct |
1025 ms |
18812 KB |
Output is correct |
96 |
Correct |
868 ms |
13780 KB |
Output is correct |
97 |
Correct |
887 ms |
13728 KB |
Output is correct |
98 |
Correct |
797 ms |
30712 KB |
Output is correct |
99 |
Correct |
907 ms |
18804 KB |
Output is correct |
100 |
Correct |
884 ms |
15988 KB |
Output is correct |
101 |
Correct |
946 ms |
15048 KB |
Output is correct |
102 |
Correct |
825 ms |
13800 KB |
Output is correct |
103 |
Correct |
785 ms |
33932 KB |
Output is correct |
104 |
Correct |
1081 ms |
35912 KB |
Output is correct |
105 |
Correct |
927 ms |
35784 KB |
Output is correct |
106 |
Correct |
967 ms |
17092 KB |
Output is correct |
107 |
Correct |
1004 ms |
14408 KB |
Output is correct |
108 |
Correct |
1018 ms |
14320 KB |
Output is correct |
109 |
Correct |
1155 ms |
13940 KB |
Output is correct |