#include "circuit.h"
//#include "stub.cpp"
#include <bits/stdc++.h>
using namespace std;
const int MOD=1000002022, MAXN=(1<<17);
int n, m;
vector<int> parent, a;
vector<int> adj[MAXN];
long long subtree[2*MAXN], factor[2*MAXN];
long long ones[2*MAXN-1], zeros[2*MAXN-1];
int lazy[2*MAXN-1]={0};
void calcsize(int x)
{
subtree[x]=1;
if (x>=n)
return;
for (auto y : adj[x])
{
calcsize(y);
subtree[x]=subtree[x]*subtree[y]%MOD;
}
subtree[x]=subtree[x]*adj[x].size()%MOD;
return;
}
void calcfactor(int x, long long current)
{
if (x>=n)
return;
int n=adj[x].size();
long long prefix[n];
prefix[0]=subtree[adj[x][0]];
for (int i=1; i<n; i++)
prefix[i]=prefix[i-1]*subtree[adj[x][i]]%MOD;
long long suffix[n];
suffix[n-1]=subtree[adj[x][n-1]];
for (int i=n-2; i>=0; i--)
suffix[i]=suffix[i+1]*subtree[adj[x][i]]%MOD;
for (int i=0; i<n; i++)
{
int y=adj[x][i];
factor[y]=current;
if (i>0)
factor[y]=factor[y]*prefix[i-1]%MOD;
if (i+1<n)
factor[y]=factor[y]*suffix[i+1]%MOD;
calcfactor(y, factor[y]);
}
return;
}
void propagate(int x, int l, int r)
{
if (r-l==1)
{
lazy[x]=0;
return;
}
if (lazy[x]==0)
return;
swap(ones[2*x+1], zeros[2*x+1]);
swap(ones[2*x+2], zeros[2*x+2]);
lazy[2*x+1]=(lazy[2*x+1]+1)%2;
lazy[2*x+2]=(lazy[2*x+2]+1)%2;
lazy[x]=0;
return;
}
void update(int x, int l, int r, int lt, int rt)
{
if (l>=rt || r<=lt)
return;
if (l>=lt && r<=rt)
{
swap(ones[x], zeros[x]);
lazy[x]=(lazy[x]+1)%2;
return;
}
propagate(x, l, r);
update(2*x+1, l, (l+r)/2, lt, rt);
update(2*x+2, (l+r)/2, r, lt, rt);
ones[x]=(ones[2*x+1]+ones[2*x+2])%MOD;
zeros[x]=(zeros[2*x+1]+zeros[2*x+2])%MOD;
return;
}
void buildtree(int x, int l, int r)
{
if (r-l==1)
{
if (l>=m)
{
ones[x]=0;
zeros[x]=0;
return;
}
if (a[l]==1)
{
ones[x]=factor[l+n];
zeros[x]=0;
}
else
{
ones[x]=0;
zeros[x]=factor[l+n];
}
return;
}
buildtree(2*x+1, l, (l+r)/2);
buildtree(2*x+2, (l+r)/2, r);
ones[x]=(ones[2*x+1]+ones[2*x+2])%MOD;
zeros[x]=(zeros[2*x+1]+zeros[2*x+2])%MOD;
return;
}
void init(int N, int M, vector<int> P, vector<int> A)
{
n=N;
m=M;
parent=P;
a=A;
for (int i=1; i<n+m; i++)
adj[parent[i]].push_back(i);
calcsize(0);
calcfactor(0, 1);
buildtree(0, 0, MAXN);
return;
}
int count_ways(int l, int r)
{
l=l-n;
r=r-n;
update(0, 0, MAXN, l, r+1);
return ones[0];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12376 KB |
Output is correct |
2 |
Correct |
3 ms |
12376 KB |
Output is correct |
3 |
Correct |
3 ms |
12632 KB |
Output is correct |
4 |
Correct |
3 ms |
12608 KB |
Output is correct |
5 |
Correct |
3 ms |
12608 KB |
Output is correct |
6 |
Correct |
3 ms |
12632 KB |
Output is correct |
7 |
Correct |
3 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
12464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12376 KB |
Output is correct |
2 |
Correct |
3 ms |
12632 KB |
Output is correct |
3 |
Correct |
3 ms |
12376 KB |
Output is correct |
4 |
Correct |
3 ms |
12376 KB |
Output is correct |
5 |
Correct |
3 ms |
12376 KB |
Output is correct |
6 |
Correct |
3 ms |
12632 KB |
Output is correct |
7 |
Correct |
3 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
12632 KB |
Output is correct |
9 |
Correct |
3 ms |
12632 KB |
Output is correct |
10 |
Correct |
3 ms |
12632 KB |
Output is correct |
11 |
Correct |
3 ms |
12632 KB |
Output is correct |
12 |
Correct |
3 ms |
12632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12376 KB |
Output is correct |
2 |
Correct |
3 ms |
12376 KB |
Output is correct |
3 |
Correct |
3 ms |
12632 KB |
Output is correct |
4 |
Correct |
3 ms |
12608 KB |
Output is correct |
5 |
Correct |
3 ms |
12608 KB |
Output is correct |
6 |
Correct |
3 ms |
12632 KB |
Output is correct |
7 |
Correct |
3 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
12464 KB |
Output is correct |
9 |
Correct |
3 ms |
12376 KB |
Output is correct |
10 |
Correct |
3 ms |
12632 KB |
Output is correct |
11 |
Correct |
3 ms |
12376 KB |
Output is correct |
12 |
Correct |
3 ms |
12376 KB |
Output is correct |
13 |
Correct |
3 ms |
12376 KB |
Output is correct |
14 |
Correct |
3 ms |
12632 KB |
Output is correct |
15 |
Correct |
3 ms |
12632 KB |
Output is correct |
16 |
Correct |
3 ms |
12632 KB |
Output is correct |
17 |
Correct |
3 ms |
12632 KB |
Output is correct |
18 |
Correct |
3 ms |
12632 KB |
Output is correct |
19 |
Correct |
3 ms |
12632 KB |
Output is correct |
20 |
Correct |
3 ms |
12632 KB |
Output is correct |
21 |
Correct |
3 ms |
12632 KB |
Output is correct |
22 |
Correct |
3 ms |
12632 KB |
Output is correct |
23 |
Correct |
3 ms |
12632 KB |
Output is correct |
24 |
Correct |
3 ms |
12624 KB |
Output is correct |
25 |
Correct |
4 ms |
12636 KB |
Output is correct |
26 |
Correct |
4 ms |
12632 KB |
Output is correct |
27 |
Correct |
3 ms |
12632 KB |
Output is correct |
28 |
Correct |
3 ms |
12632 KB |
Output is correct |
29 |
Correct |
3 ms |
12632 KB |
Output is correct |
30 |
Correct |
3 ms |
12632 KB |
Output is correct |
31 |
Correct |
3 ms |
12632 KB |
Output is correct |
32 |
Correct |
3 ms |
12632 KB |
Output is correct |
33 |
Correct |
3 ms |
12632 KB |
Output is correct |
34 |
Correct |
3 ms |
12632 KB |
Output is correct |
35 |
Correct |
3 ms |
12632 KB |
Output is correct |
36 |
Correct |
3 ms |
12632 KB |
Output is correct |
37 |
Correct |
3 ms |
12632 KB |
Output is correct |
38 |
Correct |
3 ms |
12632 KB |
Output is correct |
39 |
Correct |
3 ms |
12632 KB |
Output is correct |
40 |
Correct |
3 ms |
12376 KB |
Output is correct |
41 |
Correct |
3 ms |
12604 KB |
Output is correct |
42 |
Correct |
3 ms |
12376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
381 ms |
14680 KB |
Output is correct |
2 |
Correct |
550 ms |
16728 KB |
Output is correct |
3 |
Correct |
547 ms |
16728 KB |
Output is correct |
4 |
Correct |
560 ms |
16932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
381 ms |
14680 KB |
Output is correct |
2 |
Correct |
550 ms |
16728 KB |
Output is correct |
3 |
Correct |
547 ms |
16728 KB |
Output is correct |
4 |
Correct |
560 ms |
16932 KB |
Output is correct |
5 |
Correct |
455 ms |
14680 KB |
Output is correct |
6 |
Correct |
596 ms |
16728 KB |
Output is correct |
7 |
Correct |
557 ms |
16920 KB |
Output is correct |
8 |
Correct |
604 ms |
16728 KB |
Output is correct |
9 |
Correct |
264 ms |
12632 KB |
Output is correct |
10 |
Correct |
518 ms |
12632 KB |
Output is correct |
11 |
Correct |
516 ms |
12632 KB |
Output is correct |
12 |
Correct |
565 ms |
12632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12376 KB |
Output is correct |
2 |
Correct |
3 ms |
12632 KB |
Output is correct |
3 |
Correct |
3 ms |
12376 KB |
Output is correct |
4 |
Correct |
3 ms |
12376 KB |
Output is correct |
5 |
Correct |
3 ms |
12376 KB |
Output is correct |
6 |
Correct |
3 ms |
12632 KB |
Output is correct |
7 |
Correct |
3 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
12632 KB |
Output is correct |
9 |
Correct |
3 ms |
12632 KB |
Output is correct |
10 |
Correct |
3 ms |
12632 KB |
Output is correct |
11 |
Correct |
3 ms |
12632 KB |
Output is correct |
12 |
Correct |
3 ms |
12632 KB |
Output is correct |
13 |
Correct |
381 ms |
14680 KB |
Output is correct |
14 |
Correct |
550 ms |
16728 KB |
Output is correct |
15 |
Correct |
547 ms |
16728 KB |
Output is correct |
16 |
Correct |
560 ms |
16932 KB |
Output is correct |
17 |
Correct |
455 ms |
14680 KB |
Output is correct |
18 |
Correct |
596 ms |
16728 KB |
Output is correct |
19 |
Correct |
557 ms |
16920 KB |
Output is correct |
20 |
Correct |
604 ms |
16728 KB |
Output is correct |
21 |
Correct |
264 ms |
12632 KB |
Output is correct |
22 |
Correct |
518 ms |
12632 KB |
Output is correct |
23 |
Correct |
516 ms |
12632 KB |
Output is correct |
24 |
Correct |
565 ms |
12632 KB |
Output is correct |
25 |
Correct |
598 ms |
19084 KB |
Output is correct |
26 |
Correct |
610 ms |
19032 KB |
Output is correct |
27 |
Correct |
604 ms |
19032 KB |
Output is correct |
28 |
Correct |
502 ms |
19024 KB |
Output is correct |
29 |
Correct |
605 ms |
34648 KB |
Output is correct |
30 |
Correct |
631 ms |
34640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12376 KB |
Output is correct |
2 |
Correct |
3 ms |
12376 KB |
Output is correct |
3 |
Correct |
3 ms |
12632 KB |
Output is correct |
4 |
Correct |
3 ms |
12608 KB |
Output is correct |
5 |
Correct |
3 ms |
12608 KB |
Output is correct |
6 |
Correct |
3 ms |
12632 KB |
Output is correct |
7 |
Correct |
3 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
12464 KB |
Output is correct |
9 |
Correct |
3 ms |
12376 KB |
Output is correct |
10 |
Correct |
3 ms |
12632 KB |
Output is correct |
11 |
Correct |
3 ms |
12376 KB |
Output is correct |
12 |
Correct |
3 ms |
12376 KB |
Output is correct |
13 |
Correct |
3 ms |
12376 KB |
Output is correct |
14 |
Correct |
3 ms |
12632 KB |
Output is correct |
15 |
Correct |
3 ms |
12632 KB |
Output is correct |
16 |
Correct |
3 ms |
12632 KB |
Output is correct |
17 |
Correct |
3 ms |
12632 KB |
Output is correct |
18 |
Correct |
3 ms |
12632 KB |
Output is correct |
19 |
Correct |
3 ms |
12632 KB |
Output is correct |
20 |
Correct |
3 ms |
12632 KB |
Output is correct |
21 |
Correct |
3 ms |
12632 KB |
Output is correct |
22 |
Correct |
3 ms |
12632 KB |
Output is correct |
23 |
Correct |
3 ms |
12632 KB |
Output is correct |
24 |
Correct |
3 ms |
12624 KB |
Output is correct |
25 |
Correct |
4 ms |
12636 KB |
Output is correct |
26 |
Correct |
4 ms |
12632 KB |
Output is correct |
27 |
Correct |
3 ms |
12632 KB |
Output is correct |
28 |
Correct |
3 ms |
12632 KB |
Output is correct |
29 |
Correct |
3 ms |
12632 KB |
Output is correct |
30 |
Correct |
3 ms |
12632 KB |
Output is correct |
31 |
Correct |
3 ms |
12632 KB |
Output is correct |
32 |
Correct |
3 ms |
12632 KB |
Output is correct |
33 |
Correct |
3 ms |
12632 KB |
Output is correct |
34 |
Correct |
3 ms |
12632 KB |
Output is correct |
35 |
Correct |
3 ms |
12632 KB |
Output is correct |
36 |
Correct |
3 ms |
12632 KB |
Output is correct |
37 |
Correct |
3 ms |
12632 KB |
Output is correct |
38 |
Correct |
3 ms |
12632 KB |
Output is correct |
39 |
Correct |
3 ms |
12632 KB |
Output is correct |
40 |
Correct |
3 ms |
12376 KB |
Output is correct |
41 |
Correct |
3 ms |
12604 KB |
Output is correct |
42 |
Correct |
3 ms |
12376 KB |
Output is correct |
43 |
Correct |
419 ms |
12632 KB |
Output is correct |
44 |
Correct |
526 ms |
12632 KB |
Output is correct |
45 |
Correct |
542 ms |
12632 KB |
Output is correct |
46 |
Correct |
539 ms |
12888 KB |
Output is correct |
47 |
Correct |
553 ms |
12888 KB |
Output is correct |
48 |
Correct |
573 ms |
12888 KB |
Output is correct |
49 |
Correct |
542 ms |
12888 KB |
Output is correct |
50 |
Correct |
579 ms |
12888 KB |
Output is correct |
51 |
Correct |
550 ms |
12632 KB |
Output is correct |
52 |
Correct |
543 ms |
12632 KB |
Output is correct |
53 |
Correct |
498 ms |
12888 KB |
Output is correct |
54 |
Correct |
530 ms |
12888 KB |
Output is correct |
55 |
Correct |
577 ms |
12632 KB |
Output is correct |
56 |
Correct |
561 ms |
12632 KB |
Output is correct |
57 |
Correct |
540 ms |
12632 KB |
Output is correct |
58 |
Correct |
528 ms |
13656 KB |
Output is correct |
59 |
Correct |
552 ms |
13144 KB |
Output is correct |
60 |
Correct |
501 ms |
13144 KB |
Output is correct |
61 |
Correct |
571 ms |
12888 KB |
Output is correct |
62 |
Correct |
546 ms |
12632 KB |
Output is correct |
63 |
Correct |
557 ms |
12884 KB |
Output is correct |
64 |
Correct |
563 ms |
12632 KB |
Output is correct |
65 |
Correct |
274 ms |
12632 KB |
Output is correct |
66 |
Correct |
540 ms |
12632 KB |
Output is correct |
67 |
Correct |
560 ms |
12632 KB |
Output is correct |
68 |
Correct |
549 ms |
12632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12376 KB |
Output is correct |
2 |
Correct |
3 ms |
12376 KB |
Output is correct |
3 |
Correct |
3 ms |
12632 KB |
Output is correct |
4 |
Correct |
3 ms |
12608 KB |
Output is correct |
5 |
Correct |
3 ms |
12608 KB |
Output is correct |
6 |
Correct |
3 ms |
12632 KB |
Output is correct |
7 |
Correct |
3 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
12464 KB |
Output is correct |
9 |
Correct |
3 ms |
12376 KB |
Output is correct |
10 |
Correct |
3 ms |
12632 KB |
Output is correct |
11 |
Correct |
3 ms |
12376 KB |
Output is correct |
12 |
Correct |
3 ms |
12376 KB |
Output is correct |
13 |
Correct |
3 ms |
12376 KB |
Output is correct |
14 |
Correct |
3 ms |
12632 KB |
Output is correct |
15 |
Correct |
3 ms |
12632 KB |
Output is correct |
16 |
Correct |
3 ms |
12632 KB |
Output is correct |
17 |
Correct |
3 ms |
12632 KB |
Output is correct |
18 |
Correct |
3 ms |
12632 KB |
Output is correct |
19 |
Correct |
3 ms |
12632 KB |
Output is correct |
20 |
Correct |
3 ms |
12632 KB |
Output is correct |
21 |
Correct |
3 ms |
12632 KB |
Output is correct |
22 |
Correct |
3 ms |
12632 KB |
Output is correct |
23 |
Correct |
3 ms |
12632 KB |
Output is correct |
24 |
Correct |
3 ms |
12624 KB |
Output is correct |
25 |
Correct |
4 ms |
12636 KB |
Output is correct |
26 |
Correct |
4 ms |
12632 KB |
Output is correct |
27 |
Correct |
3 ms |
12632 KB |
Output is correct |
28 |
Correct |
3 ms |
12632 KB |
Output is correct |
29 |
Correct |
3 ms |
12632 KB |
Output is correct |
30 |
Correct |
3 ms |
12632 KB |
Output is correct |
31 |
Correct |
3 ms |
12632 KB |
Output is correct |
32 |
Correct |
3 ms |
12632 KB |
Output is correct |
33 |
Correct |
3 ms |
12632 KB |
Output is correct |
34 |
Correct |
3 ms |
12632 KB |
Output is correct |
35 |
Correct |
3 ms |
12632 KB |
Output is correct |
36 |
Correct |
3 ms |
12632 KB |
Output is correct |
37 |
Correct |
3 ms |
12632 KB |
Output is correct |
38 |
Correct |
3 ms |
12632 KB |
Output is correct |
39 |
Correct |
3 ms |
12632 KB |
Output is correct |
40 |
Correct |
3 ms |
12376 KB |
Output is correct |
41 |
Correct |
3 ms |
12604 KB |
Output is correct |
42 |
Correct |
3 ms |
12376 KB |
Output is correct |
43 |
Correct |
381 ms |
14680 KB |
Output is correct |
44 |
Correct |
550 ms |
16728 KB |
Output is correct |
45 |
Correct |
547 ms |
16728 KB |
Output is correct |
46 |
Correct |
560 ms |
16932 KB |
Output is correct |
47 |
Correct |
455 ms |
14680 KB |
Output is correct |
48 |
Correct |
596 ms |
16728 KB |
Output is correct |
49 |
Correct |
557 ms |
16920 KB |
Output is correct |
50 |
Correct |
604 ms |
16728 KB |
Output is correct |
51 |
Correct |
264 ms |
12632 KB |
Output is correct |
52 |
Correct |
518 ms |
12632 KB |
Output is correct |
53 |
Correct |
516 ms |
12632 KB |
Output is correct |
54 |
Correct |
565 ms |
12632 KB |
Output is correct |
55 |
Correct |
598 ms |
19084 KB |
Output is correct |
56 |
Correct |
610 ms |
19032 KB |
Output is correct |
57 |
Correct |
604 ms |
19032 KB |
Output is correct |
58 |
Correct |
502 ms |
19024 KB |
Output is correct |
59 |
Correct |
605 ms |
34648 KB |
Output is correct |
60 |
Correct |
631 ms |
34640 KB |
Output is correct |
61 |
Correct |
419 ms |
12632 KB |
Output is correct |
62 |
Correct |
526 ms |
12632 KB |
Output is correct |
63 |
Correct |
542 ms |
12632 KB |
Output is correct |
64 |
Correct |
539 ms |
12888 KB |
Output is correct |
65 |
Correct |
553 ms |
12888 KB |
Output is correct |
66 |
Correct |
573 ms |
12888 KB |
Output is correct |
67 |
Correct |
542 ms |
12888 KB |
Output is correct |
68 |
Correct |
579 ms |
12888 KB |
Output is correct |
69 |
Correct |
550 ms |
12632 KB |
Output is correct |
70 |
Correct |
543 ms |
12632 KB |
Output is correct |
71 |
Correct |
498 ms |
12888 KB |
Output is correct |
72 |
Correct |
530 ms |
12888 KB |
Output is correct |
73 |
Correct |
577 ms |
12632 KB |
Output is correct |
74 |
Correct |
561 ms |
12632 KB |
Output is correct |
75 |
Correct |
540 ms |
12632 KB |
Output is correct |
76 |
Correct |
528 ms |
13656 KB |
Output is correct |
77 |
Correct |
552 ms |
13144 KB |
Output is correct |
78 |
Correct |
501 ms |
13144 KB |
Output is correct |
79 |
Correct |
571 ms |
12888 KB |
Output is correct |
80 |
Correct |
546 ms |
12632 KB |
Output is correct |
81 |
Correct |
557 ms |
12884 KB |
Output is correct |
82 |
Correct |
563 ms |
12632 KB |
Output is correct |
83 |
Correct |
274 ms |
12632 KB |
Output is correct |
84 |
Correct |
540 ms |
12632 KB |
Output is correct |
85 |
Correct |
560 ms |
12632 KB |
Output is correct |
86 |
Correct |
549 ms |
12632 KB |
Output is correct |
87 |
Correct |
3 ms |
12376 KB |
Output is correct |
88 |
Correct |
375 ms |
18552 KB |
Output is correct |
89 |
Correct |
573 ms |
17120 KB |
Output is correct |
90 |
Correct |
635 ms |
16728 KB |
Output is correct |
91 |
Correct |
586 ms |
19112 KB |
Output is correct |
92 |
Correct |
582 ms |
19288 KB |
Output is correct |
93 |
Correct |
508 ms |
19280 KB |
Output is correct |
94 |
Correct |
574 ms |
19280 KB |
Output is correct |
95 |
Correct |
522 ms |
19200 KB |
Output is correct |
96 |
Correct |
497 ms |
16848 KB |
Output is correct |
97 |
Correct |
573 ms |
16848 KB |
Output is correct |
98 |
Correct |
549 ms |
23128 KB |
Output is correct |
99 |
Correct |
652 ms |
19024 KB |
Output is correct |
100 |
Correct |
568 ms |
16984 KB |
Output is correct |
101 |
Correct |
655 ms |
16464 KB |
Output is correct |
102 |
Correct |
576 ms |
15448 KB |
Output is correct |
103 |
Correct |
632 ms |
34648 KB |
Output is correct |
104 |
Correct |
582 ms |
25816 KB |
Output is correct |
105 |
Correct |
584 ms |
25808 KB |
Output is correct |
106 |
Correct |
623 ms |
18776 KB |
Output is correct |
107 |
Correct |
587 ms |
15704 KB |
Output is correct |
108 |
Correct |
605 ms |
15704 KB |
Output is correct |
109 |
Correct |
603 ms |
15448 KB |
Output is correct |