#include <bits/stdc++.h>
#include "circuit.h"
using namespace std;
using ll = long long;
const int mxN = 100005;
const int MOD = 1000002022;
ll coef[mxN];
vector<int> state;
struct Seg {
ll val;
ll sum = 0;
bool flip = false;
int l, r, mid;
Seg *ls, *rs;
Seg(int l, int r): l(l), r(r), mid((l+r)/2) {
if(l + 1 < r) {
ls = new Seg(l,mid);
rs = new Seg(mid,r);
sum = (ls->sum + rs->sum)%MOD;
val = (ls->val + rs->val)%MOD;
} else {
sum = coef[l];
val = (coef[l]*state[l]);
}
}
void push() {
if(!flip) { return; }
if(l + 1 < r) {
ls->flip ^= true;
rs->flip ^= true;
}
flip = false;
val = (MOD + sum - val)%MOD;
}
void update(int a, int b) {
push();
if(a >= r || b <= l) { return; }
if(a <= l && b >= r) {
flip ^= true;
push();
return;
}
ls->update(a,b);
rs->update(a,b);
val = (ls->val + rs->val)%MOD;
}
};
ll sub_mul[mxN];
ll ex_mul[mxN];
ll deg[mxN];
vector<int> cs[mxN];
void get_mul(int node) {
ll mul = deg[node];
for(int ne : cs[node]) {
get_mul(ne);
mul *= sub_mul[ne];
mul %= MOD;
}
sub_mul[node] = mul;
}
void dfs(int node, ll val = 1) {
vector<ll> muls;
ll cur_res = val;
for(int ne: cs[node]) {
muls.push_back(sub_mul[ne]);
cur_res *= sub_mul[ne];
cur_res %= MOD;
}
ex_mul[node] = cur_res;
int sz = muls.size();
vector<ll> pre(sz+1);
vector<ll> suf(sz+1);
pre[0] = 1;
suf[sz] = 1;
for(int i = 1; i <= sz; i ++) {
pre[i] = pre[i-1] * muls[i-1];
pre[i] %= MOD;
}
for(int i = sz - 1; i >= 0; i --) {
suf[i] = suf[i+1] * muls[i];
suf[i] %= MOD;
}
int idx = 0;
for(int ne : cs[node]) {
ll cur_mul = (pre[idx] * suf[idx+1])%MOD;
dfs(ne,(val*cur_mul)%MOD);
idx ++;
}
}
Seg *seg;
int n;
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
n = N;
state = A;
for(int i = 1; i < N; i ++) {
cs[P[i]].push_back(i);
}
for(int i = 1; i < N + M; i ++) {
deg[P[i]] ++;
}
get_mul(0);
dfs(0);
for(int i = 0; i < M; i ++) {
coef[i] = ex_mul[P[N+i]];
}
seg = new Seg(0,M);
}
int count_ways(int L, int R) {
L -= n;
R -= n;
seg->update(L,R+1);
return seg->val;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2768 KB |
Output is correct |
4 |
Correct |
2 ms |
2768 KB |
Output is correct |
5 |
Correct |
2 ms |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2768 KB |
Output is correct |
4 |
Correct |
2 ms |
2768 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
3 ms |
2768 KB |
Output is correct |
9 |
Correct |
2 ms |
2768 KB |
Output is correct |
10 |
Correct |
2 ms |
3024 KB |
Output is correct |
11 |
Correct |
2 ms |
3024 KB |
Output is correct |
12 |
Correct |
2 ms |
2768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2768 KB |
Output is correct |
4 |
Correct |
2 ms |
2768 KB |
Output is correct |
5 |
Correct |
2 ms |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2768 KB |
Output is correct |
12 |
Correct |
2 ms |
2768 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2768 KB |
Output is correct |
15 |
Correct |
2 ms |
2768 KB |
Output is correct |
16 |
Correct |
3 ms |
2768 KB |
Output is correct |
17 |
Correct |
2 ms |
2768 KB |
Output is correct |
18 |
Correct |
2 ms |
3024 KB |
Output is correct |
19 |
Correct |
2 ms |
3024 KB |
Output is correct |
20 |
Correct |
2 ms |
2768 KB |
Output is correct |
21 |
Correct |
2 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2768 KB |
Output is correct |
23 |
Correct |
2 ms |
2768 KB |
Output is correct |
24 |
Correct |
2 ms |
2768 KB |
Output is correct |
25 |
Correct |
4 ms |
2768 KB |
Output is correct |
26 |
Correct |
3 ms |
2768 KB |
Output is correct |
27 |
Correct |
2 ms |
2768 KB |
Output is correct |
28 |
Correct |
3 ms |
2768 KB |
Output is correct |
29 |
Correct |
2 ms |
2768 KB |
Output is correct |
30 |
Correct |
2 ms |
2768 KB |
Output is correct |
31 |
Correct |
3 ms |
2896 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2768 KB |
Output is correct |
34 |
Correct |
2 ms |
2768 KB |
Output is correct |
35 |
Correct |
2 ms |
2768 KB |
Output is correct |
36 |
Correct |
2 ms |
3024 KB |
Output is correct |
37 |
Correct |
2 ms |
3152 KB |
Output is correct |
38 |
Correct |
3 ms |
3024 KB |
Output is correct |
39 |
Correct |
2 ms |
2768 KB |
Output is correct |
40 |
Correct |
2 ms |
2768 KB |
Output is correct |
41 |
Correct |
2 ms |
2768 KB |
Output is correct |
42 |
Correct |
2 ms |
2768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
645 ms |
9196 KB |
Output is correct |
2 |
Correct |
911 ms |
15688 KB |
Output is correct |
3 |
Correct |
1054 ms |
15664 KB |
Output is correct |
4 |
Correct |
973 ms |
15708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
645 ms |
9196 KB |
Output is correct |
2 |
Correct |
911 ms |
15688 KB |
Output is correct |
3 |
Correct |
1054 ms |
15664 KB |
Output is correct |
4 |
Correct |
973 ms |
15708 KB |
Output is correct |
5 |
Correct |
752 ms |
9160 KB |
Output is correct |
6 |
Correct |
1000 ms |
15704 KB |
Output is correct |
7 |
Correct |
1035 ms |
15704 KB |
Output is correct |
8 |
Correct |
872 ms |
15680 KB |
Output is correct |
9 |
Correct |
502 ms |
3176 KB |
Output is correct |
10 |
Correct |
855 ms |
3408 KB |
Output is correct |
11 |
Correct |
1027 ms |
3424 KB |
Output is correct |
12 |
Correct |
846 ms |
3408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2768 KB |
Output is correct |
4 |
Correct |
2 ms |
2768 KB |
Output is correct |
5 |
Correct |
2 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
3 ms |
2768 KB |
Output is correct |
9 |
Correct |
2 ms |
2768 KB |
Output is correct |
10 |
Correct |
2 ms |
3024 KB |
Output is correct |
11 |
Correct |
2 ms |
3024 KB |
Output is correct |
12 |
Correct |
2 ms |
2768 KB |
Output is correct |
13 |
Correct |
645 ms |
9196 KB |
Output is correct |
14 |
Correct |
911 ms |
15688 KB |
Output is correct |
15 |
Correct |
1054 ms |
15664 KB |
Output is correct |
16 |
Correct |
973 ms |
15708 KB |
Output is correct |
17 |
Correct |
752 ms |
9160 KB |
Output is correct |
18 |
Correct |
1000 ms |
15704 KB |
Output is correct |
19 |
Correct |
1035 ms |
15704 KB |
Output is correct |
20 |
Correct |
872 ms |
15680 KB |
Output is correct |
21 |
Correct |
502 ms |
3176 KB |
Output is correct |
22 |
Correct |
855 ms |
3408 KB |
Output is correct |
23 |
Correct |
1027 ms |
3424 KB |
Output is correct |
24 |
Correct |
846 ms |
3408 KB |
Output is correct |
25 |
Correct |
1041 ms |
22616 KB |
Output is correct |
26 |
Correct |
1133 ms |
22872 KB |
Output is correct |
27 |
Correct |
1191 ms |
22860 KB |
Output is correct |
28 |
Correct |
755 ms |
22900 KB |
Output is correct |
29 |
Correct |
945 ms |
42836 KB |
Output is correct |
30 |
Correct |
1086 ms |
42884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2768 KB |
Output is correct |
4 |
Correct |
2 ms |
2768 KB |
Output is correct |
5 |
Correct |
2 ms |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2768 KB |
Output is correct |
12 |
Correct |
2 ms |
2768 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2768 KB |
Output is correct |
15 |
Correct |
2 ms |
2768 KB |
Output is correct |
16 |
Correct |
3 ms |
2768 KB |
Output is correct |
17 |
Correct |
2 ms |
2768 KB |
Output is correct |
18 |
Correct |
2 ms |
3024 KB |
Output is correct |
19 |
Correct |
2 ms |
3024 KB |
Output is correct |
20 |
Correct |
2 ms |
2768 KB |
Output is correct |
21 |
Correct |
2 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2768 KB |
Output is correct |
23 |
Correct |
2 ms |
2768 KB |
Output is correct |
24 |
Correct |
2 ms |
2768 KB |
Output is correct |
25 |
Correct |
4 ms |
2768 KB |
Output is correct |
26 |
Correct |
3 ms |
2768 KB |
Output is correct |
27 |
Correct |
2 ms |
2768 KB |
Output is correct |
28 |
Correct |
3 ms |
2768 KB |
Output is correct |
29 |
Correct |
2 ms |
2768 KB |
Output is correct |
30 |
Correct |
2 ms |
2768 KB |
Output is correct |
31 |
Correct |
3 ms |
2896 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2768 KB |
Output is correct |
34 |
Correct |
2 ms |
2768 KB |
Output is correct |
35 |
Correct |
2 ms |
2768 KB |
Output is correct |
36 |
Correct |
2 ms |
3024 KB |
Output is correct |
37 |
Correct |
2 ms |
3152 KB |
Output is correct |
38 |
Correct |
3 ms |
3024 KB |
Output is correct |
39 |
Correct |
2 ms |
2768 KB |
Output is correct |
40 |
Correct |
2 ms |
2768 KB |
Output is correct |
41 |
Correct |
2 ms |
2768 KB |
Output is correct |
42 |
Correct |
2 ms |
2768 KB |
Output is correct |
43 |
Correct |
520 ms |
3152 KB |
Output is correct |
44 |
Correct |
837 ms |
3152 KB |
Output is correct |
45 |
Correct |
931 ms |
3152 KB |
Output is correct |
46 |
Correct |
840 ms |
3664 KB |
Output is correct |
47 |
Correct |
779 ms |
3664 KB |
Output is correct |
48 |
Correct |
867 ms |
3664 KB |
Output is correct |
49 |
Correct |
742 ms |
3664 KB |
Output is correct |
50 |
Correct |
957 ms |
3664 KB |
Output is correct |
51 |
Correct |
949 ms |
3408 KB |
Output is correct |
52 |
Correct |
1034 ms |
3408 KB |
Output is correct |
53 |
Correct |
880 ms |
4304 KB |
Output is correct |
54 |
Correct |
1016 ms |
3664 KB |
Output is correct |
55 |
Correct |
944 ms |
3420 KB |
Output is correct |
56 |
Correct |
949 ms |
3408 KB |
Output is correct |
57 |
Correct |
917 ms |
3280 KB |
Output is correct |
58 |
Correct |
915 ms |
4688 KB |
Output is correct |
59 |
Correct |
895 ms |
4560 KB |
Output is correct |
60 |
Correct |
922 ms |
4560 KB |
Output is correct |
61 |
Correct |
891 ms |
3404 KB |
Output is correct |
62 |
Correct |
928 ms |
3300 KB |
Output is correct |
63 |
Correct |
996 ms |
3300 KB |
Output is correct |
64 |
Correct |
1043 ms |
3408 KB |
Output is correct |
65 |
Correct |
382 ms |
3024 KB |
Output is correct |
66 |
Correct |
1133 ms |
3408 KB |
Output is correct |
67 |
Correct |
998 ms |
3476 KB |
Output is correct |
68 |
Correct |
902 ms |
3408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2768 KB |
Output is correct |
4 |
Correct |
2 ms |
2768 KB |
Output is correct |
5 |
Correct |
2 ms |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2768 KB |
Output is correct |
7 |
Correct |
2 ms |
2768 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2768 KB |
Output is correct |
12 |
Correct |
2 ms |
2768 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2768 KB |
Output is correct |
15 |
Correct |
2 ms |
2768 KB |
Output is correct |
16 |
Correct |
3 ms |
2768 KB |
Output is correct |
17 |
Correct |
2 ms |
2768 KB |
Output is correct |
18 |
Correct |
2 ms |
3024 KB |
Output is correct |
19 |
Correct |
2 ms |
3024 KB |
Output is correct |
20 |
Correct |
2 ms |
2768 KB |
Output is correct |
21 |
Correct |
2 ms |
2768 KB |
Output is correct |
22 |
Correct |
2 ms |
2768 KB |
Output is correct |
23 |
Correct |
2 ms |
2768 KB |
Output is correct |
24 |
Correct |
2 ms |
2768 KB |
Output is correct |
25 |
Correct |
4 ms |
2768 KB |
Output is correct |
26 |
Correct |
3 ms |
2768 KB |
Output is correct |
27 |
Correct |
2 ms |
2768 KB |
Output is correct |
28 |
Correct |
3 ms |
2768 KB |
Output is correct |
29 |
Correct |
2 ms |
2768 KB |
Output is correct |
30 |
Correct |
2 ms |
2768 KB |
Output is correct |
31 |
Correct |
3 ms |
2896 KB |
Output is correct |
32 |
Correct |
2 ms |
2768 KB |
Output is correct |
33 |
Correct |
2 ms |
2768 KB |
Output is correct |
34 |
Correct |
2 ms |
2768 KB |
Output is correct |
35 |
Correct |
2 ms |
2768 KB |
Output is correct |
36 |
Correct |
2 ms |
3024 KB |
Output is correct |
37 |
Correct |
2 ms |
3152 KB |
Output is correct |
38 |
Correct |
3 ms |
3024 KB |
Output is correct |
39 |
Correct |
2 ms |
2768 KB |
Output is correct |
40 |
Correct |
2 ms |
2768 KB |
Output is correct |
41 |
Correct |
2 ms |
2768 KB |
Output is correct |
42 |
Correct |
2 ms |
2768 KB |
Output is correct |
43 |
Correct |
645 ms |
9196 KB |
Output is correct |
44 |
Correct |
911 ms |
15688 KB |
Output is correct |
45 |
Correct |
1054 ms |
15664 KB |
Output is correct |
46 |
Correct |
973 ms |
15708 KB |
Output is correct |
47 |
Correct |
752 ms |
9160 KB |
Output is correct |
48 |
Correct |
1000 ms |
15704 KB |
Output is correct |
49 |
Correct |
1035 ms |
15704 KB |
Output is correct |
50 |
Correct |
872 ms |
15680 KB |
Output is correct |
51 |
Correct |
502 ms |
3176 KB |
Output is correct |
52 |
Correct |
855 ms |
3408 KB |
Output is correct |
53 |
Correct |
1027 ms |
3424 KB |
Output is correct |
54 |
Correct |
846 ms |
3408 KB |
Output is correct |
55 |
Correct |
1041 ms |
22616 KB |
Output is correct |
56 |
Correct |
1133 ms |
22872 KB |
Output is correct |
57 |
Correct |
1191 ms |
22860 KB |
Output is correct |
58 |
Correct |
755 ms |
22900 KB |
Output is correct |
59 |
Correct |
945 ms |
42836 KB |
Output is correct |
60 |
Correct |
1086 ms |
42884 KB |
Output is correct |
61 |
Correct |
520 ms |
3152 KB |
Output is correct |
62 |
Correct |
837 ms |
3152 KB |
Output is correct |
63 |
Correct |
931 ms |
3152 KB |
Output is correct |
64 |
Correct |
840 ms |
3664 KB |
Output is correct |
65 |
Correct |
779 ms |
3664 KB |
Output is correct |
66 |
Correct |
867 ms |
3664 KB |
Output is correct |
67 |
Correct |
742 ms |
3664 KB |
Output is correct |
68 |
Correct |
957 ms |
3664 KB |
Output is correct |
69 |
Correct |
949 ms |
3408 KB |
Output is correct |
70 |
Correct |
1034 ms |
3408 KB |
Output is correct |
71 |
Correct |
880 ms |
4304 KB |
Output is correct |
72 |
Correct |
1016 ms |
3664 KB |
Output is correct |
73 |
Correct |
944 ms |
3420 KB |
Output is correct |
74 |
Correct |
949 ms |
3408 KB |
Output is correct |
75 |
Correct |
917 ms |
3280 KB |
Output is correct |
76 |
Correct |
915 ms |
4688 KB |
Output is correct |
77 |
Correct |
895 ms |
4560 KB |
Output is correct |
78 |
Correct |
922 ms |
4560 KB |
Output is correct |
79 |
Correct |
891 ms |
3404 KB |
Output is correct |
80 |
Correct |
928 ms |
3300 KB |
Output is correct |
81 |
Correct |
996 ms |
3300 KB |
Output is correct |
82 |
Correct |
1043 ms |
3408 KB |
Output is correct |
83 |
Correct |
382 ms |
3024 KB |
Output is correct |
84 |
Correct |
1133 ms |
3408 KB |
Output is correct |
85 |
Correct |
998 ms |
3476 KB |
Output is correct |
86 |
Correct |
902 ms |
3408 KB |
Output is correct |
87 |
Correct |
2 ms |
2640 KB |
Output is correct |
88 |
Correct |
605 ms |
19804 KB |
Output is correct |
89 |
Correct |
1030 ms |
14232 KB |
Output is correct |
90 |
Correct |
905 ms |
14756 KB |
Output is correct |
91 |
Correct |
1104 ms |
22772 KB |
Output is correct |
92 |
Correct |
1043 ms |
22772 KB |
Output is correct |
93 |
Correct |
920 ms |
22744 KB |
Output is correct |
94 |
Correct |
1053 ms |
22772 KB |
Output is correct |
95 |
Correct |
978 ms |
22728 KB |
Output is correct |
96 |
Correct |
915 ms |
17864 KB |
Output is correct |
97 |
Correct |
1083 ms |
17876 KB |
Output is correct |
98 |
Correct |
816 ms |
37068 KB |
Output is correct |
99 |
Correct |
1143 ms |
22864 KB |
Output is correct |
100 |
Correct |
1043 ms |
20312 KB |
Output is correct |
101 |
Correct |
1144 ms |
19120 KB |
Output is correct |
102 |
Correct |
818 ms |
17864 KB |
Output is correct |
103 |
Correct |
1108 ms |
42952 KB |
Output is correct |
104 |
Correct |
948 ms |
42840 KB |
Output is correct |
105 |
Correct |
758 ms |
42932 KB |
Output is correct |
106 |
Correct |
970 ms |
17228 KB |
Output is correct |
107 |
Correct |
1059 ms |
14808 KB |
Output is correct |
108 |
Correct |
998 ms |
15960 KB |
Output is correct |
109 |
Correct |
1031 ms |
17864 KB |
Output is correct |