#include "circuit.h"
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N_MAX = 2e5 + 11;
const int MOD = 1000002022;
vector<int> G[N_MAX];
int f[N_MAX], g[N_MAX];
void dfsU(int v){
f[v] = G[v].empty() ? 1 : G[v].size();
for(auto u : G[v]){
dfsU(u);
f[v] = (f[v] * f[u]) % MOD;
}
}
int a[N_MAX << 2];
void upd(int pos, int val, int v, int l, int r){
if(l == r) a[v] = val;
else {
int m = (l + r) / 2;
if(pos <= m){
upd(pos, val, v * 2 + 1, l, m);
}else{
upd(pos, val, v * 2 + 2, m + 1, r);
}
a[v] = a[v * 2 + 1] * a[v * 2 + 2] % MOD;
}
}
int qry(int ql, int qr, int v, int l, int r){
if(qr < l || r < ql) return 1;
if(ql <= l && r <= qr) return a[v];
int m = (l + r) / 2;
int qx = qry(ql, qr, v * 2 + 1, l, m);
int qy = qry(ql, qr, v * 2 + 2, m + 1, r);
return qx * qy % MOD;
}
int qry_except(int x, int V){
int qx = qry(0, x - 1, 0, 0, V - 1);
int qy = qry(x + 1, V - 1, 0, 0, V - 1);
return qx * qy % MOD;
}
void dfsD(int v){
int V = G[v].size();
for(int i = 0; i < V; i++){
int u = G[v][i];
upd(i, f[u], 0, 0, V - 1);
}
for(int i = 0; i < V; i++){
int u = G[v][i];
g[u] = g[v] * qry_except(i, V) % MOD;
}
for(int i = 0; i < V; i++){
dfsD(G[v][i]);
}
}
int ac[N_MAX];
int N, M;
int norm(int x){
return (x % MOD + MOD) % MOD;
}
namespace SegTree{
int s[N_MAX << 2], t[N_MAX << 2], lz[N_MAX << 2];
void push(int v){
if(lz[v]){
t[v * 2 + 1] = norm(s[v * 2 + 1] - t[v * 2 + 1]);
t[v * 2 + 2] = norm(s[v * 2 + 2] - t[v * 2 + 2]);
lz[v * 2 + 1] ^= 1;
lz[v * 2 + 2] ^= 1;
lz[v] = 0;
}
}
void build(int v, int l, int r){
if(l == r){
s[v] = g[l];
t[v] = g[l] * ac[l];
}else{
int m = (l + r) / 2;
build(v * 2 + 1, l, m);
build(v * 2 + 2, m + 1, r);
s[v] = norm(s[v * 2 + 1] + s[v * 2 + 2]);
t[v] = norm(t[v * 2 + 1] + t[v * 2 + 2]);
}
}
void toggle(int ql, int qr, int v, int l, int r){
if(qr < l || r < ql) return;
if(ql <= l && r <= qr){
t[v] = norm(s[v] - t[v]);
lz[v] ^= 1;
return;
}
push(v);
int m = (l + r) / 2;
toggle(ql, qr, v * 2 + 1, l, m);
toggle(ql, qr, v * 2 + 2, m + 1, r);
t[v] = norm(t[v * 2 + 1] + t[v * 2 + 2]);
}
int query(){
return t[0];
}
};
void init(int32_t N, int32_t M, vector<int32_t> P, vector<int32_t> A) {
::N = N; ::M = M;
for(int i = 1; i < N + M; i++){
G[P[i]].push_back(i);
}
dfsU(0);
g[0] = 1; dfsD(0);
for(int i = N; i < N + M; i++){
ac[i] = A[i - N];
}
SegTree::build(0, N, N + M - 1);
}
int32_t count_ways(int32_t L, int32_t R) {
SegTree::toggle(L, R, 0, N, N + M - 1);
return SegTree::query();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5044 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5148 KB |
Output is correct |
6 |
Correct |
3 ms |
5040 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
5072 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5200 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5200 KB |
Output is correct |
11 |
Correct |
3 ms |
5200 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5044 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5148 KB |
Output is correct |
6 |
Correct |
3 ms |
5040 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5200 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
3 ms |
5200 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5172 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 |
5072 KB |
Output is correct |
25 |
Correct |
4 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5200 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5072 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
4 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 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 |
3 ms |
5200 KB |
Output is correct |
37 |
Correct |
3 ms |
5200 KB |
Output is correct |
38 |
Correct |
3 ms |
5268 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5024 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
737 ms |
9032 KB |
Output is correct |
2 |
Correct |
1020 ms |
13168 KB |
Output is correct |
3 |
Correct |
846 ms |
13180 KB |
Output is correct |
4 |
Correct |
810 ms |
13180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
737 ms |
9032 KB |
Output is correct |
2 |
Correct |
1020 ms |
13168 KB |
Output is correct |
3 |
Correct |
846 ms |
13180 KB |
Output is correct |
4 |
Correct |
810 ms |
13180 KB |
Output is correct |
5 |
Correct |
810 ms |
9284 KB |
Output is correct |
6 |
Correct |
888 ms |
13512 KB |
Output is correct |
7 |
Correct |
732 ms |
13568 KB |
Output is correct |
8 |
Correct |
1075 ms |
13176 KB |
Output is correct |
9 |
Correct |
488 ms |
5328 KB |
Output is correct |
10 |
Correct |
949 ms |
5584 KB |
Output is correct |
11 |
Correct |
795 ms |
5504 KB |
Output is correct |
12 |
Correct |
710 ms |
5572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
5072 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5200 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5200 KB |
Output is correct |
11 |
Correct |
3 ms |
5200 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
737 ms |
9032 KB |
Output is correct |
14 |
Correct |
1020 ms |
13168 KB |
Output is correct |
15 |
Correct |
846 ms |
13180 KB |
Output is correct |
16 |
Correct |
810 ms |
13180 KB |
Output is correct |
17 |
Correct |
810 ms |
9284 KB |
Output is correct |
18 |
Correct |
888 ms |
13512 KB |
Output is correct |
19 |
Correct |
732 ms |
13568 KB |
Output is correct |
20 |
Correct |
1075 ms |
13176 KB |
Output is correct |
21 |
Correct |
488 ms |
5328 KB |
Output is correct |
22 |
Correct |
949 ms |
5584 KB |
Output is correct |
23 |
Correct |
795 ms |
5504 KB |
Output is correct |
24 |
Correct |
710 ms |
5572 KB |
Output is correct |
25 |
Correct |
986 ms |
19272 KB |
Output is correct |
26 |
Correct |
718 ms |
19396 KB |
Output is correct |
27 |
Correct |
718 ms |
19464 KB |
Output is correct |
28 |
Correct |
786 ms |
18504 KB |
Output is correct |
29 |
Correct |
681 ms |
25656 KB |
Output is correct |
30 |
Correct |
840 ms |
24776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5044 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5148 KB |
Output is correct |
6 |
Correct |
3 ms |
5040 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5200 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
3 ms |
5200 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5172 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 |
5072 KB |
Output is correct |
25 |
Correct |
4 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5200 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5072 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
4 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 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 |
3 ms |
5200 KB |
Output is correct |
37 |
Correct |
3 ms |
5200 KB |
Output is correct |
38 |
Correct |
3 ms |
5268 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5024 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
43 |
Correct |
577 ms |
5456 KB |
Output is correct |
44 |
Correct |
722 ms |
5456 KB |
Output is correct |
45 |
Correct |
619 ms |
5456 KB |
Output is correct |
46 |
Correct |
841 ms |
5840 KB |
Output is correct |
47 |
Correct |
890 ms |
5816 KB |
Output is correct |
48 |
Correct |
827 ms |
5892 KB |
Output is correct |
49 |
Correct |
944 ms |
5840 KB |
Output is correct |
50 |
Correct |
795 ms |
5764 KB |
Output is correct |
51 |
Correct |
788 ms |
5732 KB |
Output is correct |
52 |
Correct |
777 ms |
5840 KB |
Output is correct |
53 |
Correct |
739 ms |
5504 KB |
Output is correct |
54 |
Correct |
847 ms |
5840 KB |
Output is correct |
55 |
Correct |
750 ms |
5768 KB |
Output is correct |
56 |
Correct |
986 ms |
5712 KB |
Output is correct |
57 |
Correct |
859 ms |
5584 KB |
Output is correct |
58 |
Correct |
793 ms |
6096 KB |
Output is correct |
59 |
Correct |
811 ms |
6456 KB |
Output is correct |
60 |
Correct |
761 ms |
6324 KB |
Output is correct |
61 |
Correct |
794 ms |
5496 KB |
Output is correct |
62 |
Correct |
924 ms |
5448 KB |
Output is correct |
63 |
Correct |
871 ms |
5456 KB |
Output is correct |
64 |
Correct |
881 ms |
5632 KB |
Output is correct |
65 |
Correct |
502 ms |
5248 KB |
Output is correct |
66 |
Correct |
829 ms |
5584 KB |
Output is correct |
67 |
Correct |
867 ms |
5584 KB |
Output is correct |
68 |
Correct |
777 ms |
5504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5044 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5148 KB |
Output is correct |
6 |
Correct |
3 ms |
5040 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5200 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
3 ms |
5200 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
3 ms |
5172 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 |
5072 KB |
Output is correct |
25 |
Correct |
4 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5200 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5072 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
3 ms |
5072 KB |
Output is correct |
32 |
Correct |
4 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 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 |
3 ms |
5200 KB |
Output is correct |
37 |
Correct |
3 ms |
5200 KB |
Output is correct |
38 |
Correct |
3 ms |
5268 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5024 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
43 |
Correct |
737 ms |
9032 KB |
Output is correct |
44 |
Correct |
1020 ms |
13168 KB |
Output is correct |
45 |
Correct |
846 ms |
13180 KB |
Output is correct |
46 |
Correct |
810 ms |
13180 KB |
Output is correct |
47 |
Correct |
810 ms |
9284 KB |
Output is correct |
48 |
Correct |
888 ms |
13512 KB |
Output is correct |
49 |
Correct |
732 ms |
13568 KB |
Output is correct |
50 |
Correct |
1075 ms |
13176 KB |
Output is correct |
51 |
Correct |
488 ms |
5328 KB |
Output is correct |
52 |
Correct |
949 ms |
5584 KB |
Output is correct |
53 |
Correct |
795 ms |
5504 KB |
Output is correct |
54 |
Correct |
710 ms |
5572 KB |
Output is correct |
55 |
Correct |
986 ms |
19272 KB |
Output is correct |
56 |
Correct |
718 ms |
19396 KB |
Output is correct |
57 |
Correct |
718 ms |
19464 KB |
Output is correct |
58 |
Correct |
786 ms |
18504 KB |
Output is correct |
59 |
Correct |
681 ms |
25656 KB |
Output is correct |
60 |
Correct |
840 ms |
24776 KB |
Output is correct |
61 |
Correct |
577 ms |
5456 KB |
Output is correct |
62 |
Correct |
722 ms |
5456 KB |
Output is correct |
63 |
Correct |
619 ms |
5456 KB |
Output is correct |
64 |
Correct |
841 ms |
5840 KB |
Output is correct |
65 |
Correct |
890 ms |
5816 KB |
Output is correct |
66 |
Correct |
827 ms |
5892 KB |
Output is correct |
67 |
Correct |
944 ms |
5840 KB |
Output is correct |
68 |
Correct |
795 ms |
5764 KB |
Output is correct |
69 |
Correct |
788 ms |
5732 KB |
Output is correct |
70 |
Correct |
777 ms |
5840 KB |
Output is correct |
71 |
Correct |
739 ms |
5504 KB |
Output is correct |
72 |
Correct |
847 ms |
5840 KB |
Output is correct |
73 |
Correct |
750 ms |
5768 KB |
Output is correct |
74 |
Correct |
986 ms |
5712 KB |
Output is correct |
75 |
Correct |
859 ms |
5584 KB |
Output is correct |
76 |
Correct |
793 ms |
6096 KB |
Output is correct |
77 |
Correct |
811 ms |
6456 KB |
Output is correct |
78 |
Correct |
761 ms |
6324 KB |
Output is correct |
79 |
Correct |
794 ms |
5496 KB |
Output is correct |
80 |
Correct |
924 ms |
5448 KB |
Output is correct |
81 |
Correct |
871 ms |
5456 KB |
Output is correct |
82 |
Correct |
881 ms |
5632 KB |
Output is correct |
83 |
Correct |
502 ms |
5248 KB |
Output is correct |
84 |
Correct |
829 ms |
5584 KB |
Output is correct |
85 |
Correct |
867 ms |
5584 KB |
Output is correct |
86 |
Correct |
777 ms |
5504 KB |
Output is correct |
87 |
Correct |
2 ms |
4944 KB |
Output is correct |
88 |
Correct |
498 ms |
19000 KB |
Output is correct |
89 |
Correct |
1038 ms |
13660 KB |
Output is correct |
90 |
Correct |
832 ms |
13668 KB |
Output is correct |
91 |
Correct |
948 ms |
19584 KB |
Output is correct |
92 |
Correct |
865 ms |
20092 KB |
Output is correct |
93 |
Correct |
904 ms |
20016 KB |
Output is correct |
94 |
Correct |
1098 ms |
20008 KB |
Output is correct |
95 |
Correct |
842 ms |
19064 KB |
Output is correct |
96 |
Correct |
983 ms |
16060 KB |
Output is correct |
97 |
Correct |
956 ms |
17284 KB |
Output is correct |
98 |
Correct |
672 ms |
16648 KB |
Output is correct |
99 |
Correct |
1063 ms |
19400 KB |
Output is correct |
100 |
Correct |
1028 ms |
18080 KB |
Output is correct |
101 |
Correct |
953 ms |
16928 KB |
Output is correct |
102 |
Correct |
1098 ms |
15292 KB |
Output is correct |
103 |
Correct |
898 ms |
25660 KB |
Output is correct |
104 |
Correct |
966 ms |
28628 KB |
Output is correct |
105 |
Correct |
1104 ms |
27724 KB |
Output is correct |
106 |
Correct |
995 ms |
14848 KB |
Output is correct |
107 |
Correct |
1020 ms |
15796 KB |
Output is correct |
108 |
Correct |
888 ms |
15980 KB |
Output is correct |
109 |
Correct |
875 ms |
15468 KB |
Output is correct |