#include "circuit.h"
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll,ll>;
constexpr ll mod = 1'000'002'022;
#define rep(i,j,k) for(ll i=ll(j); i<ll(k); i++)
#define REP(i,j,k) for(ll i=ll(j); i<=ll(k); i++)
#define per(i,j,k) for(ll i=ll(j); i>=ll(k); i--)
#define all(a) a.begin(),a.end()
#define pb emplace_back
#define ln "\n"
struct lazy_segment_tree{
ll N;
vi node;
vector<bool> flag;
vi sum;
lazy_segment_tree(ll n, vi s, vi a){
N = 1;
while(N <= n) N *= 2;
node.resize(2*N);
flag.resize(2*N);
sum.resize(2*N);
rep(i,0,n) sum[i+N] = s[i];
per(i,N-1,1) sum[i] = (sum[i*2]+sum[i*2+1])%mod;
rep(i,0,n) node[i+N] = s[i]*a[i];
per(i,N-1,1) node[i] = (node[i*2]+node[i*2+1])%mod;
};
void eval(ll now){
if(flag[now]){
node[now] = sum[now]-node[now];
if(now < N){
flag[now*2] = !flag[now*2];
flag[now*2+1] = !flag[now*2+1];
}
flag[now] = false;
}
}
void update(ll a, ll b, ll now = 1, ll l = 0, ll r = -1){
if(r == -1) r = N;
eval(now);
if(a <= l && r <= b){
flag[now] = !flag[now];
eval(now);
return;
}
if(r <= a || b <= l) return;
update(a,b,now*2,l,(l+r)/2);
update(a,b,now*2+1,(l+r)/2,r);
node[now] = (node[now*2]+node[now*2+1])%mod;
}
ll calc(ll a, ll b, ll now = 1, ll l = 0, ll r = -1){
if(r == -1) r = N;
eval(now);
if(a <= l && r <= b) return node[now];
if(r <= a || b <= l) return 0;
return (calc(a,b,now*2,l,(l+r)/2)+calc(a,b,now*2+1,(l+r)/2,r))%mod;
}
};
vi sum;
vii edge;
vi A;
ll N,M;
lazy_segment_tree seg(0,vi(),vi());
void init(int n, int m, std::vector<int> P, std::vector<int> a) {
N = n, M = m;
sum.resize(N+M,1);
edge.resize(N+M);
A.resize(N+M);
rep(i,0,M) A[i+N] = a[i];
rep(i,1,N+M){
edge[P[i]].pb(i);
}
vi sum2(N+M);
std::function<void(ll)> dfs1 = [&](ll now){
sum2[now] = edge[now].size();
sum2[now] = std::max(sum2[now], 1ll);
for(auto next: edge[now]){
dfs1(next);
sum2[now] *= sum2[next];
sum2[now] %= mod;
}
};
dfs1(0);
std::function<void(ll)> dfs2 = [&](ll now){
ll len = edge[now].size();
vi l(len+1,1), r(len+1,1);
rep(i,0,len){
l[i+1] = l[i]*sum2[edge[now][i]]%mod;
}
per(i,len-1,0){
r[i] = r[i+1]*sum2[edge[now][i]]%mod;
}
rep(i,0,len){
ll next = edge[now][i];
sum[next] = sum[now]*l[i]%mod*r[i+1]%mod;
dfs2(next);
}
};
dfs2(0);
vi sum_(M);
rep(i,0,M) sum_[i] = sum[i+N];
vi A_(M);
rep(i,0,M) A_[i] = a[i];
lazy_segment_tree seg2(M,sum_,A_);
seg = seg2;
}
int count_ways(int L, int R) {
L -= N;
R -= N;
seg.update(L,R+1);
ll ans = seg.calc(0,M);
if(ans < 0) ans += mod;
ans %= mod;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
216 KB |
Output is correct |
2 |
Correct |
0 ms |
376 KB |
Output is correct |
3 |
Correct |
1 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
416 KB |
Output is correct |
9 |
Correct |
0 ms |
216 KB |
Output is correct |
10 |
Correct |
0 ms |
376 KB |
Output is correct |
11 |
Correct |
1 ms |
424 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
464 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
544 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
592 KB |
Output is correct |
37 |
Correct |
1 ms |
592 KB |
Output is correct |
38 |
Correct |
2 ms |
592 KB |
Output is correct |
39 |
Correct |
1 ms |
464 KB |
Output is correct |
40 |
Correct |
1 ms |
464 KB |
Output is correct |
41 |
Correct |
1 ms |
464 KB |
Output is correct |
42 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
478 ms |
9860 KB |
Output is correct |
2 |
Correct |
800 ms |
19344 KB |
Output is correct |
3 |
Correct |
792 ms |
19360 KB |
Output is correct |
4 |
Correct |
804 ms |
19352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
478 ms |
9860 KB |
Output is correct |
2 |
Correct |
800 ms |
19344 KB |
Output is correct |
3 |
Correct |
792 ms |
19360 KB |
Output is correct |
4 |
Correct |
804 ms |
19352 KB |
Output is correct |
5 |
Correct |
641 ms |
9772 KB |
Output is correct |
6 |
Correct |
717 ms |
19352 KB |
Output is correct |
7 |
Correct |
835 ms |
19348 KB |
Output is correct |
8 |
Correct |
802 ms |
19344 KB |
Output is correct |
9 |
Correct |
250 ms |
904 KB |
Output is correct |
10 |
Correct |
631 ms |
1488 KB |
Output is correct |
11 |
Correct |
772 ms |
1488 KB |
Output is correct |
12 |
Correct |
720 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
216 KB |
Output is correct |
2 |
Correct |
0 ms |
376 KB |
Output is correct |
3 |
Correct |
1 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
478 ms |
9860 KB |
Output is correct |
14 |
Correct |
800 ms |
19344 KB |
Output is correct |
15 |
Correct |
792 ms |
19360 KB |
Output is correct |
16 |
Correct |
804 ms |
19352 KB |
Output is correct |
17 |
Correct |
641 ms |
9772 KB |
Output is correct |
18 |
Correct |
717 ms |
19352 KB |
Output is correct |
19 |
Correct |
835 ms |
19348 KB |
Output is correct |
20 |
Correct |
802 ms |
19344 KB |
Output is correct |
21 |
Correct |
250 ms |
904 KB |
Output is correct |
22 |
Correct |
631 ms |
1488 KB |
Output is correct |
23 |
Correct |
772 ms |
1488 KB |
Output is correct |
24 |
Correct |
720 ms |
1492 KB |
Output is correct |
25 |
Correct |
789 ms |
24732 KB |
Output is correct |
26 |
Correct |
784 ms |
25000 KB |
Output is correct |
27 |
Correct |
808 ms |
25000 KB |
Output is correct |
28 |
Correct |
551 ms |
24980 KB |
Output is correct |
29 |
Correct |
900 ms |
43192 KB |
Output is correct |
30 |
Correct |
640 ms |
43188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
416 KB |
Output is correct |
9 |
Correct |
0 ms |
216 KB |
Output is correct |
10 |
Correct |
0 ms |
376 KB |
Output is correct |
11 |
Correct |
1 ms |
424 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
464 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
544 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
592 KB |
Output is correct |
37 |
Correct |
1 ms |
592 KB |
Output is correct |
38 |
Correct |
2 ms |
592 KB |
Output is correct |
39 |
Correct |
1 ms |
464 KB |
Output is correct |
40 |
Correct |
1 ms |
464 KB |
Output is correct |
41 |
Correct |
1 ms |
464 KB |
Output is correct |
42 |
Correct |
1 ms |
464 KB |
Output is correct |
43 |
Correct |
482 ms |
1080 KB |
Output is correct |
44 |
Correct |
750 ms |
1140 KB |
Output is correct |
45 |
Correct |
571 ms |
1120 KB |
Output is correct |
46 |
Correct |
742 ms |
1716 KB |
Output is correct |
47 |
Correct |
671 ms |
1744 KB |
Output is correct |
48 |
Correct |
827 ms |
1696 KB |
Output is correct |
49 |
Correct |
644 ms |
1700 KB |
Output is correct |
50 |
Correct |
805 ms |
1704 KB |
Output is correct |
51 |
Correct |
566 ms |
1320 KB |
Output is correct |
52 |
Correct |
761 ms |
1316 KB |
Output is correct |
53 |
Correct |
651 ms |
1744 KB |
Output is correct |
54 |
Correct |
640 ms |
1608 KB |
Output is correct |
55 |
Correct |
665 ms |
1488 KB |
Output is correct |
56 |
Correct |
652 ms |
1488 KB |
Output is correct |
57 |
Correct |
853 ms |
1232 KB |
Output is correct |
58 |
Correct |
743 ms |
2456 KB |
Output is correct |
59 |
Correct |
701 ms |
2580 KB |
Output is correct |
60 |
Correct |
699 ms |
2640 KB |
Output is correct |
61 |
Correct |
661 ms |
1356 KB |
Output is correct |
62 |
Correct |
690 ms |
1076 KB |
Output is correct |
63 |
Correct |
745 ms |
1056 KB |
Output is correct |
64 |
Correct |
846 ms |
1360 KB |
Output is correct |
65 |
Correct |
330 ms |
904 KB |
Output is correct |
66 |
Correct |
669 ms |
1508 KB |
Output is correct |
67 |
Correct |
720 ms |
1444 KB |
Output is correct |
68 |
Correct |
801 ms |
1444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
416 KB |
Output is correct |
9 |
Correct |
0 ms |
216 KB |
Output is correct |
10 |
Correct |
0 ms |
376 KB |
Output is correct |
11 |
Correct |
1 ms |
424 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
464 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
544 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
592 KB |
Output is correct |
37 |
Correct |
1 ms |
592 KB |
Output is correct |
38 |
Correct |
2 ms |
592 KB |
Output is correct |
39 |
Correct |
1 ms |
464 KB |
Output is correct |
40 |
Correct |
1 ms |
464 KB |
Output is correct |
41 |
Correct |
1 ms |
464 KB |
Output is correct |
42 |
Correct |
1 ms |
464 KB |
Output is correct |
43 |
Correct |
478 ms |
9860 KB |
Output is correct |
44 |
Correct |
800 ms |
19344 KB |
Output is correct |
45 |
Correct |
792 ms |
19360 KB |
Output is correct |
46 |
Correct |
804 ms |
19352 KB |
Output is correct |
47 |
Correct |
641 ms |
9772 KB |
Output is correct |
48 |
Correct |
717 ms |
19352 KB |
Output is correct |
49 |
Correct |
835 ms |
19348 KB |
Output is correct |
50 |
Correct |
802 ms |
19344 KB |
Output is correct |
51 |
Correct |
250 ms |
904 KB |
Output is correct |
52 |
Correct |
631 ms |
1488 KB |
Output is correct |
53 |
Correct |
772 ms |
1488 KB |
Output is correct |
54 |
Correct |
720 ms |
1492 KB |
Output is correct |
55 |
Correct |
789 ms |
24732 KB |
Output is correct |
56 |
Correct |
784 ms |
25000 KB |
Output is correct |
57 |
Correct |
808 ms |
25000 KB |
Output is correct |
58 |
Correct |
551 ms |
24980 KB |
Output is correct |
59 |
Correct |
900 ms |
43192 KB |
Output is correct |
60 |
Correct |
640 ms |
43188 KB |
Output is correct |
61 |
Correct |
482 ms |
1080 KB |
Output is correct |
62 |
Correct |
750 ms |
1140 KB |
Output is correct |
63 |
Correct |
571 ms |
1120 KB |
Output is correct |
64 |
Correct |
742 ms |
1716 KB |
Output is correct |
65 |
Correct |
671 ms |
1744 KB |
Output is correct |
66 |
Correct |
827 ms |
1696 KB |
Output is correct |
67 |
Correct |
644 ms |
1700 KB |
Output is correct |
68 |
Correct |
805 ms |
1704 KB |
Output is correct |
69 |
Correct |
566 ms |
1320 KB |
Output is correct |
70 |
Correct |
761 ms |
1316 KB |
Output is correct |
71 |
Correct |
651 ms |
1744 KB |
Output is correct |
72 |
Correct |
640 ms |
1608 KB |
Output is correct |
73 |
Correct |
665 ms |
1488 KB |
Output is correct |
74 |
Correct |
652 ms |
1488 KB |
Output is correct |
75 |
Correct |
853 ms |
1232 KB |
Output is correct |
76 |
Correct |
743 ms |
2456 KB |
Output is correct |
77 |
Correct |
701 ms |
2580 KB |
Output is correct |
78 |
Correct |
699 ms |
2640 KB |
Output is correct |
79 |
Correct |
661 ms |
1356 KB |
Output is correct |
80 |
Correct |
690 ms |
1076 KB |
Output is correct |
81 |
Correct |
745 ms |
1056 KB |
Output is correct |
82 |
Correct |
846 ms |
1360 KB |
Output is correct |
83 |
Correct |
330 ms |
904 KB |
Output is correct |
84 |
Correct |
669 ms |
1508 KB |
Output is correct |
85 |
Correct |
720 ms |
1444 KB |
Output is correct |
86 |
Correct |
801 ms |
1444 KB |
Output is correct |
87 |
Correct |
0 ms |
208 KB |
Output is correct |
88 |
Correct |
398 ms |
23528 KB |
Output is correct |
89 |
Correct |
759 ms |
15008 KB |
Output is correct |
90 |
Correct |
757 ms |
15048 KB |
Output is correct |
91 |
Correct |
745 ms |
25596 KB |
Output is correct |
92 |
Correct |
697 ms |
25616 KB |
Output is correct |
93 |
Correct |
823 ms |
25600 KB |
Output is correct |
94 |
Correct |
862 ms |
25644 KB |
Output is correct |
95 |
Correct |
820 ms |
25596 KB |
Output is correct |
96 |
Correct |
634 ms |
18672 KB |
Output is correct |
97 |
Correct |
887 ms |
18724 KB |
Output is correct |
98 |
Correct |
672 ms |
30744 KB |
Output is correct |
99 |
Correct |
791 ms |
25000 KB |
Output is correct |
100 |
Correct |
819 ms |
21712 KB |
Output is correct |
101 |
Correct |
687 ms |
19880 KB |
Output is correct |
102 |
Correct |
727 ms |
17448 KB |
Output is correct |
103 |
Correct |
662 ms |
43168 KB |
Output is correct |
104 |
Correct |
708 ms |
43920 KB |
Output is correct |
105 |
Correct |
830 ms |
44000 KB |
Output is correct |
106 |
Correct |
929 ms |
18704 KB |
Output is correct |
107 |
Correct |
802 ms |
17592 KB |
Output is correct |
108 |
Correct |
878 ms |
17996 KB |
Output is correct |
109 |
Correct |
713 ms |
17584 KB |
Output is correct |