#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1'000'002'022;
struct segment{
struct Node{
ll sum=0, act=0, lazy=0;
};
vector<Node> tr;
void push(int v){
if(!tr[v].lazy) return;
tr[v].lazy = 0;
if(2*v+1 < (int)tr.size()){
tr[2*v].lazy ^= 1;
tr[2*v+1].lazy ^= 1;
}
tr[v].act = (MOD + tr[v].sum - tr[v].act)%MOD;
}
void merge(int v){
tr[v].sum = (tr[2*v].sum + tr[2*v+1].sum)%MOD;
tr[v].act = (tr[2*v].act + tr[2*v+1].act)%MOD;
}
void build(int v, int l, int r, vector<ll> &a){
if(r-l == 1){
tr[v] = {a[l], a[l], 0};
return;
}
int m = (l+r)/2;
build(2*v, l, m, a);
build(2*v+1, m, r, a);
merge(v);
}
segment(int n, vector<ll> &v){
tr.resize(4*(n+1));
build(1, 0, n, v);
}
void upd(int v, int l, int r, int ql, int qr){
push(v);
if(l >= qr or r <= ql) return;
if(l >= ql and r <= qr){
tr[v].lazy = 1;
push(v);
return;
}
int m = (l+r)/2;
upd(2*v, l, m, ql, qr);
upd(2*v+1, m, r, ql, qr);
merge(v);
}
ll query(){
return tr[1].act;
}
};
segment *st;
int m, n;
void init(int N, int M, std::vector<int> P, std::vector<int> A){
vector<vector<int>> ad(N+M);
for(int i=1; i<N+M; i++){
ad[P[i]].push_back(i);
}
vector<ll> prod(N+M);
vector<ll> ways(M);
function<void(int)> dfs = [&](int v){
prod[v] = max(1, (int)ad[v].size());
for(int u: ad[v]){
dfs(u);
prod[v] = prod[v]*prod[u]%MOD;
}
};
function<void(int,ll)> dfs2 = [&](int v, ll up){
if(v >= N){
ways[v-N] = up;
return;
}
int len = ad[v].size();
vector<ll> pref(len+1, 1), suff(len+1, 1);
for(int i=0; i<len; i++) pref[i+1] = pref[i]*prod[ad[v][i]]%MOD;
for(int i=len-1; i>=0; i--) suff[i] = suff[i+1]*prod[ad[v][i]]%MOD;
for(int i=0; i<len; i++){
dfs2(ad[v][i], pref[i]*suff[i+1]%MOD*up%MOD);
}
};
dfs(0); dfs2(0, 1);
m = M; n = N;
st = new segment(m, ways);
for(int i=0; i<M; i++){
if(!A[i]) st->upd(1, 0, m, i, i+1);
}
}
int count_ways(int L, int R) {
st->upd(1, 0, m, L-n, R+1-n);
return st->query();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
768 KB |
Output is correct |
35 |
Correct |
1 ms |
544 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
852 KB |
Output is correct |
38 |
Correct |
1 ms |
856 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
391 ms |
7512 KB |
Output is correct |
2 |
Correct |
590 ms |
14792 KB |
Output is correct |
3 |
Correct |
597 ms |
14780 KB |
Output is correct |
4 |
Correct |
617 ms |
14772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
391 ms |
7512 KB |
Output is correct |
2 |
Correct |
590 ms |
14792 KB |
Output is correct |
3 |
Correct |
597 ms |
14780 KB |
Output is correct |
4 |
Correct |
617 ms |
14772 KB |
Output is correct |
5 |
Correct |
566 ms |
7488 KB |
Output is correct |
6 |
Correct |
668 ms |
14760 KB |
Output is correct |
7 |
Correct |
665 ms |
14784 KB |
Output is correct |
8 |
Correct |
653 ms |
14764 KB |
Output is correct |
9 |
Correct |
305 ms |
856 KB |
Output is correct |
10 |
Correct |
633 ms |
1112 KB |
Output is correct |
11 |
Correct |
590 ms |
1112 KB |
Output is correct |
12 |
Correct |
562 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
391 ms |
7512 KB |
Output is correct |
14 |
Correct |
590 ms |
14792 KB |
Output is correct |
15 |
Correct |
597 ms |
14780 KB |
Output is correct |
16 |
Correct |
617 ms |
14772 KB |
Output is correct |
17 |
Correct |
566 ms |
7488 KB |
Output is correct |
18 |
Correct |
668 ms |
14760 KB |
Output is correct |
19 |
Correct |
665 ms |
14784 KB |
Output is correct |
20 |
Correct |
653 ms |
14764 KB |
Output is correct |
21 |
Correct |
305 ms |
856 KB |
Output is correct |
22 |
Correct |
633 ms |
1112 KB |
Output is correct |
23 |
Correct |
590 ms |
1112 KB |
Output is correct |
24 |
Correct |
562 ms |
1112 KB |
Output is correct |
25 |
Correct |
700 ms |
21920 KB |
Output is correct |
26 |
Correct |
706 ms |
22372 KB |
Output is correct |
27 |
Correct |
683 ms |
22104 KB |
Output is correct |
28 |
Correct |
507 ms |
22348 KB |
Output is correct |
29 |
Correct |
696 ms |
42592 KB |
Output is correct |
30 |
Correct |
614 ms |
42700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
768 KB |
Output is correct |
35 |
Correct |
1 ms |
544 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
852 KB |
Output is correct |
38 |
Correct |
1 ms |
856 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
425 ms |
864 KB |
Output is correct |
44 |
Correct |
553 ms |
856 KB |
Output is correct |
45 |
Correct |
538 ms |
856 KB |
Output is correct |
46 |
Correct |
514 ms |
1368 KB |
Output is correct |
47 |
Correct |
519 ms |
1368 KB |
Output is correct |
48 |
Correct |
540 ms |
1368 KB |
Output is correct |
49 |
Correct |
526 ms |
1368 KB |
Output is correct |
50 |
Correct |
506 ms |
1368 KB |
Output is correct |
51 |
Correct |
506 ms |
1112 KB |
Output is correct |
52 |
Correct |
534 ms |
1112 KB |
Output is correct |
53 |
Correct |
479 ms |
1624 KB |
Output is correct |
54 |
Correct |
536 ms |
1368 KB |
Output is correct |
55 |
Correct |
525 ms |
1112 KB |
Output is correct |
56 |
Correct |
536 ms |
1112 KB |
Output is correct |
57 |
Correct |
512 ms |
1112 KB |
Output is correct |
58 |
Correct |
521 ms |
2392 KB |
Output is correct |
59 |
Correct |
529 ms |
2648 KB |
Output is correct |
60 |
Correct |
478 ms |
2648 KB |
Output is correct |
61 |
Correct |
530 ms |
1112 KB |
Output is correct |
62 |
Correct |
544 ms |
1112 KB |
Output is correct |
63 |
Correct |
518 ms |
856 KB |
Output is correct |
64 |
Correct |
547 ms |
1112 KB |
Output is correct |
65 |
Correct |
250 ms |
856 KB |
Output is correct |
66 |
Correct |
519 ms |
1112 KB |
Output is correct |
67 |
Correct |
510 ms |
1112 KB |
Output is correct |
68 |
Correct |
520 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
768 KB |
Output is correct |
35 |
Correct |
1 ms |
544 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
852 KB |
Output is correct |
38 |
Correct |
1 ms |
856 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
391 ms |
7512 KB |
Output is correct |
44 |
Correct |
590 ms |
14792 KB |
Output is correct |
45 |
Correct |
597 ms |
14780 KB |
Output is correct |
46 |
Correct |
617 ms |
14772 KB |
Output is correct |
47 |
Correct |
566 ms |
7488 KB |
Output is correct |
48 |
Correct |
668 ms |
14760 KB |
Output is correct |
49 |
Correct |
665 ms |
14784 KB |
Output is correct |
50 |
Correct |
653 ms |
14764 KB |
Output is correct |
51 |
Correct |
305 ms |
856 KB |
Output is correct |
52 |
Correct |
633 ms |
1112 KB |
Output is correct |
53 |
Correct |
590 ms |
1112 KB |
Output is correct |
54 |
Correct |
562 ms |
1112 KB |
Output is correct |
55 |
Correct |
700 ms |
21920 KB |
Output is correct |
56 |
Correct |
706 ms |
22372 KB |
Output is correct |
57 |
Correct |
683 ms |
22104 KB |
Output is correct |
58 |
Correct |
507 ms |
22348 KB |
Output is correct |
59 |
Correct |
696 ms |
42592 KB |
Output is correct |
60 |
Correct |
614 ms |
42700 KB |
Output is correct |
61 |
Correct |
425 ms |
864 KB |
Output is correct |
62 |
Correct |
553 ms |
856 KB |
Output is correct |
63 |
Correct |
538 ms |
856 KB |
Output is correct |
64 |
Correct |
514 ms |
1368 KB |
Output is correct |
65 |
Correct |
519 ms |
1368 KB |
Output is correct |
66 |
Correct |
540 ms |
1368 KB |
Output is correct |
67 |
Correct |
526 ms |
1368 KB |
Output is correct |
68 |
Correct |
506 ms |
1368 KB |
Output is correct |
69 |
Correct |
506 ms |
1112 KB |
Output is correct |
70 |
Correct |
534 ms |
1112 KB |
Output is correct |
71 |
Correct |
479 ms |
1624 KB |
Output is correct |
72 |
Correct |
536 ms |
1368 KB |
Output is correct |
73 |
Correct |
525 ms |
1112 KB |
Output is correct |
74 |
Correct |
536 ms |
1112 KB |
Output is correct |
75 |
Correct |
512 ms |
1112 KB |
Output is correct |
76 |
Correct |
521 ms |
2392 KB |
Output is correct |
77 |
Correct |
529 ms |
2648 KB |
Output is correct |
78 |
Correct |
478 ms |
2648 KB |
Output is correct |
79 |
Correct |
530 ms |
1112 KB |
Output is correct |
80 |
Correct |
544 ms |
1112 KB |
Output is correct |
81 |
Correct |
518 ms |
856 KB |
Output is correct |
82 |
Correct |
547 ms |
1112 KB |
Output is correct |
83 |
Correct |
250 ms |
856 KB |
Output is correct |
84 |
Correct |
519 ms |
1112 KB |
Output is correct |
85 |
Correct |
510 ms |
1112 KB |
Output is correct |
86 |
Correct |
520 ms |
1112 KB |
Output is correct |
87 |
Correct |
0 ms |
344 KB |
Output is correct |
88 |
Correct |
384 ms |
19108 KB |
Output is correct |
89 |
Correct |
596 ms |
13168 KB |
Output is correct |
90 |
Correct |
617 ms |
13428 KB |
Output is correct |
91 |
Correct |
604 ms |
22524 KB |
Output is correct |
92 |
Correct |
662 ms |
22348 KB |
Output is correct |
93 |
Correct |
649 ms |
22352 KB |
Output is correct |
94 |
Correct |
711 ms |
22524 KB |
Output is correct |
95 |
Correct |
665 ms |
22292 KB |
Output is correct |
96 |
Correct |
534 ms |
15792 KB |
Output is correct |
97 |
Correct |
633 ms |
15792 KB |
Output is correct |
98 |
Correct |
541 ms |
27704 KB |
Output is correct |
99 |
Correct |
697 ms |
22348 KB |
Output is correct |
100 |
Correct |
592 ms |
18660 KB |
Output is correct |
101 |
Correct |
614 ms |
17624 KB |
Output is correct |
102 |
Correct |
657 ms |
15824 KB |
Output is correct |
103 |
Correct |
688 ms |
42572 KB |
Output is correct |
104 |
Correct |
669 ms |
42840 KB |
Output is correct |
105 |
Correct |
602 ms |
42900 KB |
Output is correct |
106 |
Correct |
635 ms |
16428 KB |
Output is correct |
107 |
Correct |
681 ms |
13124 KB |
Output is correct |
108 |
Correct |
665 ms |
14200 KB |
Output is correct |
109 |
Correct |
644 ms |
15820 KB |
Output is correct |