#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)x.size()
#define ar array
#include "circuit.h"
using namespace std;
const int mod = int(1e9) + 2022;
void fadd(int &a, int b) {
a += b;
if (a >= mod) a -= mod;
}
int add(int x, int y) {
return x+y >= mod ? x+y-mod : x+y;
}
struct seg {
int N; vector<int> tree, tot, lazy;
seg() {}
seg(int n, const vector<int>& v, const vector<int>& a) : N(1<<(__lg(n)+1)), tree(2*N), tot(2*N), lazy(2*N) {
for (int i = 0; i < n; i++) tot[i+N] = v[i], tree[i+N] = v[i] * a[i];
for (int i = N-1; i >= 1; i--) tot[i] = add(tot[i*2], tot[i*2+1]), tree[i] = add(tree[i*2], tree[i*2+1]);
}
void push(int node) {
if (!lazy[node]) return;
tree[node] = tot[node] - tree[node];
if (tree[node] < 0) tree[node] += mod;
if (node < N) {
lazy[node*2] ^= 1;
lazy[node*2+1] ^= 1;
}
lazy[node] = 0;
}
void update(int node, int nl, int nr, int ql, int qr) {
push(node);
if (ql > nr || qr < nl) return;
if (ql <= nl && nr <= qr) {
lazy[node] ^= 1; push(node); return;
}
int mid = (nl+nr)/2;
update(node*2, nl, mid, ql, qr);
update(node*2+1, mid+1, nr, ql, qr);
tree[node] = add(tree[node*2], tree[node*2+1]);
}
} st;
vector<int> p, a, sub, sub2, val;
vector<vector<int>> adj;
int n, m;
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
n = N, m = M;
p = P, a = A;
adj.resize(N), sub.resize(N+M, 1), sub2.resize(N+M, 1);
for (int i = 1; i < N + M; i++) adj[P[i]].emplace_back(i);
for (int i = N-1; i >= 0; i--) {
sub[i] = sz(adj[i]);
int cur = sz(adj[i]);
vector<int> pref(cur), suff(cur);
for (int j = 0; j < cur; j++) {
pref[j] = suff[j] = sub[adj[i][j]];
}
for (int j = 1; j < cur; j++) pref[j] = (ll)pref[j-1] * pref[j] % mod;
for (int j = cur-2; j >= 0; j--) suff[j] = (ll)suff[j+1] * suff[j] % mod;
for (int j = 0; j < cur; j++) {
int u = adj[i][j];
sub[i] = (ll)sub[i] * sub[u] % mod;
sub2[u] = (ll)(j ? pref[j-1] : 1) * (j+1 < cur ? suff[j+1] : 1) % mod;
}
}
val.resize(N+M);
val[0] = 1;
for (int i = 1; i < N + M; i++) val[i] = (ll)val[p[i]] * sub2[i] % mod;
vector<int> val2(M);
for (int i = N; i < N+M; i++) val2[i-N] = val[i];
st = seg(m, val2, A);
}
int count_ways(int L, int R) {
st.update(1, 0, st.N-1, L - n, R - n);
return st.tree[1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
504 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
502 ms |
5720 KB |
Output is correct |
2 |
Correct |
673 ms |
11200 KB |
Output is correct |
3 |
Correct |
648 ms |
11108 KB |
Output is correct |
4 |
Correct |
675 ms |
11096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
502 ms |
5720 KB |
Output is correct |
2 |
Correct |
673 ms |
11200 KB |
Output is correct |
3 |
Correct |
648 ms |
11108 KB |
Output is correct |
4 |
Correct |
675 ms |
11096 KB |
Output is correct |
5 |
Correct |
560 ms |
5720 KB |
Output is correct |
6 |
Correct |
733 ms |
11096 KB |
Output is correct |
7 |
Correct |
689 ms |
11096 KB |
Output is correct |
8 |
Correct |
697 ms |
11088 KB |
Output is correct |
9 |
Correct |
319 ms |
748 KB |
Output is correct |
10 |
Correct |
660 ms |
1084 KB |
Output is correct |
11 |
Correct |
688 ms |
1080 KB |
Output is correct |
12 |
Correct |
632 ms |
1076 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 |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
502 ms |
5720 KB |
Output is correct |
14 |
Correct |
673 ms |
11200 KB |
Output is correct |
15 |
Correct |
648 ms |
11108 KB |
Output is correct |
16 |
Correct |
675 ms |
11096 KB |
Output is correct |
17 |
Correct |
560 ms |
5720 KB |
Output is correct |
18 |
Correct |
733 ms |
11096 KB |
Output is correct |
19 |
Correct |
689 ms |
11096 KB |
Output is correct |
20 |
Correct |
697 ms |
11088 KB |
Output is correct |
21 |
Correct |
319 ms |
748 KB |
Output is correct |
22 |
Correct |
660 ms |
1084 KB |
Output is correct |
23 |
Correct |
688 ms |
1080 KB |
Output is correct |
24 |
Correct |
632 ms |
1076 KB |
Output is correct |
25 |
Correct |
635 ms |
14804 KB |
Output is correct |
26 |
Correct |
750 ms |
15184 KB |
Output is correct |
27 |
Correct |
711 ms |
15188 KB |
Output is correct |
28 |
Correct |
586 ms |
15192 KB |
Output is correct |
29 |
Correct |
714 ms |
15204 KB |
Output is correct |
30 |
Correct |
678 ms |
15184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
504 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
455 ms |
856 KB |
Output is correct |
44 |
Correct |
618 ms |
924 KB |
Output is correct |
45 |
Correct |
610 ms |
884 KB |
Output is correct |
46 |
Correct |
587 ms |
1192 KB |
Output is correct |
47 |
Correct |
642 ms |
1196 KB |
Output is correct |
48 |
Correct |
711 ms |
1188 KB |
Output is correct |
49 |
Correct |
701 ms |
1192 KB |
Output is correct |
50 |
Correct |
650 ms |
1192 KB |
Output is correct |
51 |
Correct |
677 ms |
832 KB |
Output is correct |
52 |
Correct |
677 ms |
856 KB |
Output is correct |
53 |
Correct |
603 ms |
600 KB |
Output is correct |
54 |
Correct |
696 ms |
1188 KB |
Output is correct |
55 |
Correct |
638 ms |
996 KB |
Output is correct |
56 |
Correct |
676 ms |
932 KB |
Output is correct |
57 |
Correct |
630 ms |
828 KB |
Output is correct |
58 |
Correct |
661 ms |
1188 KB |
Output is correct |
59 |
Correct |
667 ms |
1224 KB |
Output is correct |
60 |
Correct |
670 ms |
1232 KB |
Output is correct |
61 |
Correct |
658 ms |
916 KB |
Output is correct |
62 |
Correct |
692 ms |
808 KB |
Output is correct |
63 |
Correct |
666 ms |
768 KB |
Output is correct |
64 |
Correct |
698 ms |
856 KB |
Output is correct |
65 |
Correct |
307 ms |
744 KB |
Output is correct |
66 |
Correct |
637 ms |
1080 KB |
Output is correct |
67 |
Correct |
649 ms |
1084 KB |
Output is correct |
68 |
Correct |
591 ms |
1084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
504 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
502 ms |
5720 KB |
Output is correct |
44 |
Correct |
673 ms |
11200 KB |
Output is correct |
45 |
Correct |
648 ms |
11108 KB |
Output is correct |
46 |
Correct |
675 ms |
11096 KB |
Output is correct |
47 |
Correct |
560 ms |
5720 KB |
Output is correct |
48 |
Correct |
733 ms |
11096 KB |
Output is correct |
49 |
Correct |
689 ms |
11096 KB |
Output is correct |
50 |
Correct |
697 ms |
11088 KB |
Output is correct |
51 |
Correct |
319 ms |
748 KB |
Output is correct |
52 |
Correct |
660 ms |
1084 KB |
Output is correct |
53 |
Correct |
688 ms |
1080 KB |
Output is correct |
54 |
Correct |
632 ms |
1076 KB |
Output is correct |
55 |
Correct |
635 ms |
14804 KB |
Output is correct |
56 |
Correct |
750 ms |
15184 KB |
Output is correct |
57 |
Correct |
711 ms |
15188 KB |
Output is correct |
58 |
Correct |
586 ms |
15192 KB |
Output is correct |
59 |
Correct |
714 ms |
15204 KB |
Output is correct |
60 |
Correct |
678 ms |
15184 KB |
Output is correct |
61 |
Correct |
455 ms |
856 KB |
Output is correct |
62 |
Correct |
618 ms |
924 KB |
Output is correct |
63 |
Correct |
610 ms |
884 KB |
Output is correct |
64 |
Correct |
587 ms |
1192 KB |
Output is correct |
65 |
Correct |
642 ms |
1196 KB |
Output is correct |
66 |
Correct |
711 ms |
1188 KB |
Output is correct |
67 |
Correct |
701 ms |
1192 KB |
Output is correct |
68 |
Correct |
650 ms |
1192 KB |
Output is correct |
69 |
Correct |
677 ms |
832 KB |
Output is correct |
70 |
Correct |
677 ms |
856 KB |
Output is correct |
71 |
Correct |
603 ms |
600 KB |
Output is correct |
72 |
Correct |
696 ms |
1188 KB |
Output is correct |
73 |
Correct |
638 ms |
996 KB |
Output is correct |
74 |
Correct |
676 ms |
932 KB |
Output is correct |
75 |
Correct |
630 ms |
828 KB |
Output is correct |
76 |
Correct |
661 ms |
1188 KB |
Output is correct |
77 |
Correct |
667 ms |
1224 KB |
Output is correct |
78 |
Correct |
670 ms |
1232 KB |
Output is correct |
79 |
Correct |
658 ms |
916 KB |
Output is correct |
80 |
Correct |
692 ms |
808 KB |
Output is correct |
81 |
Correct |
666 ms |
768 KB |
Output is correct |
82 |
Correct |
698 ms |
856 KB |
Output is correct |
83 |
Correct |
307 ms |
744 KB |
Output is correct |
84 |
Correct |
637 ms |
1080 KB |
Output is correct |
85 |
Correct |
649 ms |
1084 KB |
Output is correct |
86 |
Correct |
591 ms |
1084 KB |
Output is correct |
87 |
Correct |
0 ms |
344 KB |
Output is correct |
88 |
Correct |
465 ms |
13976 KB |
Output is correct |
89 |
Correct |
760 ms |
10072 KB |
Output is correct |
90 |
Correct |
695 ms |
9296 KB |
Output is correct |
91 |
Correct |
687 ms |
15184 KB |
Output is correct |
92 |
Correct |
667 ms |
15348 KB |
Output is correct |
93 |
Correct |
705 ms |
15184 KB |
Output is correct |
94 |
Correct |
741 ms |
15184 KB |
Output is correct |
95 |
Correct |
678 ms |
15184 KB |
Output is correct |
96 |
Correct |
648 ms |
7616 KB |
Output is correct |
97 |
Correct |
692 ms |
7616 KB |
Output is correct |
98 |
Correct |
652 ms |
8024 KB |
Output is correct |
99 |
Correct |
761 ms |
15184 KB |
Output is correct |
100 |
Correct |
777 ms |
11344 KB |
Output is correct |
101 |
Correct |
782 ms |
9804 KB |
Output is correct |
102 |
Correct |
719 ms |
7768 KB |
Output is correct |
103 |
Correct |
738 ms |
15192 KB |
Output is correct |
104 |
Correct |
690 ms |
15560 KB |
Output is correct |
105 |
Correct |
695 ms |
15548 KB |
Output is correct |
106 |
Correct |
692 ms |
9644 KB |
Output is correct |
107 |
Correct |
741 ms |
9296 KB |
Output is correct |
108 |
Correct |
759 ms |
9048 KB |
Output is correct |
109 |
Correct |
717 ms |
8024 KB |
Output is correct |