// In the name of Allah
#include <bits/stdc++.h>
#include "circuit.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define endl '\n'
#define sep ' '
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
const int maxn = (1 << 18) + 4;
const int mod = 1e9 + 2022;
struct node {
int lazy; ll ans, ansx;
};
int n, m;
vector<int> adj[maxn]; int D[maxn];
ll valx[maxn], valr[maxn];
ll sm1[maxn], sm2[maxn];
ll val[maxn]; int A[maxn];
node t[maxn];
void pre_dfs(int v) {
if (len(adj[v]) == 0) valx[v] = 1;
else valx[v] = D[v];
for (int u : adj[v]) {
pre_dfs(u);
valx[v] *= valx[u]; valx[v] %= mod;
}
}
void res_dfs(int v) {
for (int j = 0; j <= len(adj[v]); j++) {
if (j == 0) sm1[j] = 1;
else {
int u = adj[v][j - 1];
sm1[j] = sm1[j - 1] * valx[u] % mod;
}
}
for (int j = 0; j <= len(adj[v]); j++) {
if (j == 0) sm2[j] = 1;
else {
int u = adj[v][len(adj[v]) - j];
sm2[j] = sm2[j - 1] * valx[u] % mod;
}
}
val[v] = valr[v] * sm1[len(adj[v])] % mod;
for (int j = 0; j < len(adj[v]); j++) {
int u = adj[v][j];
valr[u] = valr[v] * sm1[j] % mod * sm2[len(adj[v]) - j - 1] % mod;
}
for (int u : adj[v]) {
res_dfs(u);
}
}
node f(node a, node b, int lazy) {
node res; res.lazy = 0;
res.ans = (a.ans + b.ans) % mod;
res.ansx = (a.ansx + b.ansx) % mod;
if (lazy) {
res.lazy ^= 1;
swap(res.ans, res.ansx);
}
return res;
}
void build(int v, int tl, int tr) {
t[v].lazy = 0;
if (tr - tl == 1) {
t[v].ans = val[tl]; t[v].ansx = 0;
if (!A[tl]) swap(t[v].ans, t[v].ansx);
return ;
}
int mid = (tl + tr) / 2;
build(2 * v + 1, tl, mid); build(2 * v + 2, mid, tr);
t[v] = f(t[2 * v + 1], t[2 * v + 2], t[v].lazy);
}
void rev_val(int v, int tl, int tr, int l, int r) {
l = max(l, tl); r = min(r, tr);
if (l >= tr || r <= tl) return ;
if (l == tl && r == tr) {
t[v].lazy ^= 1;
swap(t[v].ans, t[v].ansx);
return ;
}
int mid = (tl + tr) / 2;
rev_val(2 * v + 1, tl, mid, l, r); rev_val(2 * v + 2, mid, tr, l, r);
t[v] = f(t[2 * v + 1], t[2 * v + 2], t[v].lazy);
}
void init(int N, int M, vector<int> Px, vector<int> Ax) {
n = N; m = M;
for (int i = 1; i < n + m; i++) {
adj[Px[i]].pb(i); D[Px[i]]++;
}
pre_dfs(0);
valr[0] = 1; res_dfs(0);
for (int i = 0; i < m; i++) {
val[i] = val[i + n]; A[i] = Ax[i];
}
build(0, 0, m);
}
int count_ways(int L, int R) {
rev_val(0, 0, m, L - n, R - n + 1);
return t[0].ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
3 ms |
6572 KB |
Output is correct |
4 |
Correct |
3 ms |
6744 KB |
Output is correct |
5 |
Correct |
3 ms |
6744 KB |
Output is correct |
6 |
Correct |
3 ms |
6744 KB |
Output is correct |
7 |
Correct |
3 ms |
6740 KB |
Output is correct |
8 |
Correct |
3 ms |
6744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
3 ms |
6488 KB |
Output is correct |
3 |
Correct |
3 ms |
6488 KB |
Output is correct |
4 |
Correct |
3 ms |
6488 KB |
Output is correct |
5 |
Correct |
3 ms |
6488 KB |
Output is correct |
6 |
Correct |
3 ms |
6744 KB |
Output is correct |
7 |
Correct |
4 ms |
6744 KB |
Output is correct |
8 |
Correct |
3 ms |
6744 KB |
Output is correct |
9 |
Correct |
3 ms |
6664 KB |
Output is correct |
10 |
Correct |
3 ms |
6744 KB |
Output is correct |
11 |
Correct |
3 ms |
6744 KB |
Output is correct |
12 |
Correct |
4 ms |
6776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
3 ms |
6572 KB |
Output is correct |
4 |
Correct |
3 ms |
6744 KB |
Output is correct |
5 |
Correct |
3 ms |
6744 KB |
Output is correct |
6 |
Correct |
3 ms |
6744 KB |
Output is correct |
7 |
Correct |
3 ms |
6740 KB |
Output is correct |
8 |
Correct |
3 ms |
6744 KB |
Output is correct |
9 |
Correct |
3 ms |
6488 KB |
Output is correct |
10 |
Correct |
3 ms |
6488 KB |
Output is correct |
11 |
Correct |
3 ms |
6488 KB |
Output is correct |
12 |
Correct |
3 ms |
6488 KB |
Output is correct |
13 |
Correct |
3 ms |
6488 KB |
Output is correct |
14 |
Correct |
3 ms |
6744 KB |
Output is correct |
15 |
Correct |
4 ms |
6744 KB |
Output is correct |
16 |
Correct |
3 ms |
6744 KB |
Output is correct |
17 |
Correct |
3 ms |
6664 KB |
Output is correct |
18 |
Correct |
3 ms |
6744 KB |
Output is correct |
19 |
Correct |
3 ms |
6744 KB |
Output is correct |
20 |
Correct |
4 ms |
6776 KB |
Output is correct |
21 |
Correct |
4 ms |
6744 KB |
Output is correct |
22 |
Correct |
3 ms |
6488 KB |
Output is correct |
23 |
Correct |
3 ms |
6744 KB |
Output is correct |
24 |
Correct |
5 ms |
6744 KB |
Output is correct |
25 |
Correct |
3 ms |
6744 KB |
Output is correct |
26 |
Correct |
3 ms |
6744 KB |
Output is correct |
27 |
Correct |
3 ms |
6996 KB |
Output is correct |
28 |
Correct |
3 ms |
6744 KB |
Output is correct |
29 |
Correct |
3 ms |
6744 KB |
Output is correct |
30 |
Correct |
3 ms |
6744 KB |
Output is correct |
31 |
Correct |
3 ms |
6744 KB |
Output is correct |
32 |
Correct |
3 ms |
6744 KB |
Output is correct |
33 |
Correct |
3 ms |
6744 KB |
Output is correct |
34 |
Correct |
3 ms |
6744 KB |
Output is correct |
35 |
Correct |
2 ms |
6488 KB |
Output is correct |
36 |
Correct |
3 ms |
6744 KB |
Output is correct |
37 |
Correct |
3 ms |
6744 KB |
Output is correct |
38 |
Correct |
3 ms |
6744 KB |
Output is correct |
39 |
Correct |
3 ms |
6488 KB |
Output is correct |
40 |
Correct |
3 ms |
6744 KB |
Output is correct |
41 |
Correct |
3 ms |
6744 KB |
Output is correct |
42 |
Correct |
3 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
442 ms |
11608 KB |
Output is correct |
2 |
Correct |
648 ms |
16856 KB |
Output is correct |
3 |
Correct |
595 ms |
16728 KB |
Output is correct |
4 |
Correct |
636 ms |
16728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
442 ms |
11608 KB |
Output is correct |
2 |
Correct |
648 ms |
16856 KB |
Output is correct |
3 |
Correct |
595 ms |
16728 KB |
Output is correct |
4 |
Correct |
636 ms |
16728 KB |
Output is correct |
5 |
Correct |
542 ms |
11608 KB |
Output is correct |
6 |
Correct |
667 ms |
16728 KB |
Output is correct |
7 |
Correct |
713 ms |
16720 KB |
Output is correct |
8 |
Correct |
643 ms |
16728 KB |
Output is correct |
9 |
Correct |
361 ms |
6744 KB |
Output is correct |
10 |
Correct |
635 ms |
7256 KB |
Output is correct |
11 |
Correct |
631 ms |
7256 KB |
Output is correct |
12 |
Correct |
561 ms |
7256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
3 ms |
6488 KB |
Output is correct |
3 |
Correct |
3 ms |
6488 KB |
Output is correct |
4 |
Correct |
3 ms |
6488 KB |
Output is correct |
5 |
Correct |
3 ms |
6488 KB |
Output is correct |
6 |
Correct |
3 ms |
6744 KB |
Output is correct |
7 |
Correct |
4 ms |
6744 KB |
Output is correct |
8 |
Correct |
3 ms |
6744 KB |
Output is correct |
9 |
Correct |
3 ms |
6664 KB |
Output is correct |
10 |
Correct |
3 ms |
6744 KB |
Output is correct |
11 |
Correct |
3 ms |
6744 KB |
Output is correct |
12 |
Correct |
4 ms |
6776 KB |
Output is correct |
13 |
Correct |
442 ms |
11608 KB |
Output is correct |
14 |
Correct |
648 ms |
16856 KB |
Output is correct |
15 |
Correct |
595 ms |
16728 KB |
Output is correct |
16 |
Correct |
636 ms |
16728 KB |
Output is correct |
17 |
Correct |
542 ms |
11608 KB |
Output is correct |
18 |
Correct |
667 ms |
16728 KB |
Output is correct |
19 |
Correct |
713 ms |
16720 KB |
Output is correct |
20 |
Correct |
643 ms |
16728 KB |
Output is correct |
21 |
Correct |
361 ms |
6744 KB |
Output is correct |
22 |
Correct |
635 ms |
7256 KB |
Output is correct |
23 |
Correct |
631 ms |
7256 KB |
Output is correct |
24 |
Correct |
561 ms |
7256 KB |
Output is correct |
25 |
Correct |
651 ms |
23372 KB |
Output is correct |
26 |
Correct |
703 ms |
23620 KB |
Output is correct |
27 |
Correct |
757 ms |
23632 KB |
Output is correct |
28 |
Correct |
565 ms |
23640 KB |
Output is correct |
29 |
Correct |
747 ms |
29776 KB |
Output is correct |
30 |
Correct |
687 ms |
29776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
3 ms |
6572 KB |
Output is correct |
4 |
Correct |
3 ms |
6744 KB |
Output is correct |
5 |
Correct |
3 ms |
6744 KB |
Output is correct |
6 |
Correct |
3 ms |
6744 KB |
Output is correct |
7 |
Correct |
3 ms |
6740 KB |
Output is correct |
8 |
Correct |
3 ms |
6744 KB |
Output is correct |
9 |
Correct |
3 ms |
6488 KB |
Output is correct |
10 |
Correct |
3 ms |
6488 KB |
Output is correct |
11 |
Correct |
3 ms |
6488 KB |
Output is correct |
12 |
Correct |
3 ms |
6488 KB |
Output is correct |
13 |
Correct |
3 ms |
6488 KB |
Output is correct |
14 |
Correct |
3 ms |
6744 KB |
Output is correct |
15 |
Correct |
4 ms |
6744 KB |
Output is correct |
16 |
Correct |
3 ms |
6744 KB |
Output is correct |
17 |
Correct |
3 ms |
6664 KB |
Output is correct |
18 |
Correct |
3 ms |
6744 KB |
Output is correct |
19 |
Correct |
3 ms |
6744 KB |
Output is correct |
20 |
Correct |
4 ms |
6776 KB |
Output is correct |
21 |
Correct |
4 ms |
6744 KB |
Output is correct |
22 |
Correct |
3 ms |
6488 KB |
Output is correct |
23 |
Correct |
3 ms |
6744 KB |
Output is correct |
24 |
Correct |
5 ms |
6744 KB |
Output is correct |
25 |
Correct |
3 ms |
6744 KB |
Output is correct |
26 |
Correct |
3 ms |
6744 KB |
Output is correct |
27 |
Correct |
3 ms |
6996 KB |
Output is correct |
28 |
Correct |
3 ms |
6744 KB |
Output is correct |
29 |
Correct |
3 ms |
6744 KB |
Output is correct |
30 |
Correct |
3 ms |
6744 KB |
Output is correct |
31 |
Correct |
3 ms |
6744 KB |
Output is correct |
32 |
Correct |
3 ms |
6744 KB |
Output is correct |
33 |
Correct |
3 ms |
6744 KB |
Output is correct |
34 |
Correct |
3 ms |
6744 KB |
Output is correct |
35 |
Correct |
2 ms |
6488 KB |
Output is correct |
36 |
Correct |
3 ms |
6744 KB |
Output is correct |
37 |
Correct |
3 ms |
6744 KB |
Output is correct |
38 |
Correct |
3 ms |
6744 KB |
Output is correct |
39 |
Correct |
3 ms |
6488 KB |
Output is correct |
40 |
Correct |
3 ms |
6744 KB |
Output is correct |
41 |
Correct |
3 ms |
6744 KB |
Output is correct |
42 |
Correct |
3 ms |
6488 KB |
Output is correct |
43 |
Correct |
476 ms |
7000 KB |
Output is correct |
44 |
Correct |
633 ms |
7000 KB |
Output is correct |
45 |
Correct |
584 ms |
7000 KB |
Output is correct |
46 |
Correct |
611 ms |
7512 KB |
Output is correct |
47 |
Correct |
628 ms |
7512 KB |
Output is correct |
48 |
Correct |
599 ms |
7512 KB |
Output is correct |
49 |
Correct |
507 ms |
7512 KB |
Output is correct |
50 |
Correct |
467 ms |
7360 KB |
Output is correct |
51 |
Correct |
472 ms |
7256 KB |
Output is correct |
52 |
Correct |
533 ms |
7144 KB |
Output is correct |
53 |
Correct |
488 ms |
7256 KB |
Output is correct |
54 |
Correct |
626 ms |
7512 KB |
Output is correct |
55 |
Correct |
600 ms |
7256 KB |
Output is correct |
56 |
Correct |
663 ms |
7256 KB |
Output is correct |
57 |
Correct |
665 ms |
7256 KB |
Output is correct |
58 |
Correct |
644 ms |
7768 KB |
Output is correct |
59 |
Correct |
621 ms |
7996 KB |
Output is correct |
60 |
Correct |
645 ms |
7992 KB |
Output is correct |
61 |
Correct |
681 ms |
7256 KB |
Output is correct |
62 |
Correct |
639 ms |
7000 KB |
Output is correct |
63 |
Correct |
579 ms |
7000 KB |
Output is correct |
64 |
Correct |
629 ms |
7256 KB |
Output is correct |
65 |
Correct |
330 ms |
6744 KB |
Output is correct |
66 |
Correct |
637 ms |
7256 KB |
Output is correct |
67 |
Correct |
628 ms |
7256 KB |
Output is correct |
68 |
Correct |
636 ms |
7256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
3 ms |
6572 KB |
Output is correct |
4 |
Correct |
3 ms |
6744 KB |
Output is correct |
5 |
Correct |
3 ms |
6744 KB |
Output is correct |
6 |
Correct |
3 ms |
6744 KB |
Output is correct |
7 |
Correct |
3 ms |
6740 KB |
Output is correct |
8 |
Correct |
3 ms |
6744 KB |
Output is correct |
9 |
Correct |
3 ms |
6488 KB |
Output is correct |
10 |
Correct |
3 ms |
6488 KB |
Output is correct |
11 |
Correct |
3 ms |
6488 KB |
Output is correct |
12 |
Correct |
3 ms |
6488 KB |
Output is correct |
13 |
Correct |
3 ms |
6488 KB |
Output is correct |
14 |
Correct |
3 ms |
6744 KB |
Output is correct |
15 |
Correct |
4 ms |
6744 KB |
Output is correct |
16 |
Correct |
3 ms |
6744 KB |
Output is correct |
17 |
Correct |
3 ms |
6664 KB |
Output is correct |
18 |
Correct |
3 ms |
6744 KB |
Output is correct |
19 |
Correct |
3 ms |
6744 KB |
Output is correct |
20 |
Correct |
4 ms |
6776 KB |
Output is correct |
21 |
Correct |
4 ms |
6744 KB |
Output is correct |
22 |
Correct |
3 ms |
6488 KB |
Output is correct |
23 |
Correct |
3 ms |
6744 KB |
Output is correct |
24 |
Correct |
5 ms |
6744 KB |
Output is correct |
25 |
Correct |
3 ms |
6744 KB |
Output is correct |
26 |
Correct |
3 ms |
6744 KB |
Output is correct |
27 |
Correct |
3 ms |
6996 KB |
Output is correct |
28 |
Correct |
3 ms |
6744 KB |
Output is correct |
29 |
Correct |
3 ms |
6744 KB |
Output is correct |
30 |
Correct |
3 ms |
6744 KB |
Output is correct |
31 |
Correct |
3 ms |
6744 KB |
Output is correct |
32 |
Correct |
3 ms |
6744 KB |
Output is correct |
33 |
Correct |
3 ms |
6744 KB |
Output is correct |
34 |
Correct |
3 ms |
6744 KB |
Output is correct |
35 |
Correct |
2 ms |
6488 KB |
Output is correct |
36 |
Correct |
3 ms |
6744 KB |
Output is correct |
37 |
Correct |
3 ms |
6744 KB |
Output is correct |
38 |
Correct |
3 ms |
6744 KB |
Output is correct |
39 |
Correct |
3 ms |
6488 KB |
Output is correct |
40 |
Correct |
3 ms |
6744 KB |
Output is correct |
41 |
Correct |
3 ms |
6744 KB |
Output is correct |
42 |
Correct |
3 ms |
6488 KB |
Output is correct |
43 |
Correct |
442 ms |
11608 KB |
Output is correct |
44 |
Correct |
648 ms |
16856 KB |
Output is correct |
45 |
Correct |
595 ms |
16728 KB |
Output is correct |
46 |
Correct |
636 ms |
16728 KB |
Output is correct |
47 |
Correct |
542 ms |
11608 KB |
Output is correct |
48 |
Correct |
667 ms |
16728 KB |
Output is correct |
49 |
Correct |
713 ms |
16720 KB |
Output is correct |
50 |
Correct |
643 ms |
16728 KB |
Output is correct |
51 |
Correct |
361 ms |
6744 KB |
Output is correct |
52 |
Correct |
635 ms |
7256 KB |
Output is correct |
53 |
Correct |
631 ms |
7256 KB |
Output is correct |
54 |
Correct |
561 ms |
7256 KB |
Output is correct |
55 |
Correct |
651 ms |
23372 KB |
Output is correct |
56 |
Correct |
703 ms |
23620 KB |
Output is correct |
57 |
Correct |
757 ms |
23632 KB |
Output is correct |
58 |
Correct |
565 ms |
23640 KB |
Output is correct |
59 |
Correct |
747 ms |
29776 KB |
Output is correct |
60 |
Correct |
687 ms |
29776 KB |
Output is correct |
61 |
Correct |
476 ms |
7000 KB |
Output is correct |
62 |
Correct |
633 ms |
7000 KB |
Output is correct |
63 |
Correct |
584 ms |
7000 KB |
Output is correct |
64 |
Correct |
611 ms |
7512 KB |
Output is correct |
65 |
Correct |
628 ms |
7512 KB |
Output is correct |
66 |
Correct |
599 ms |
7512 KB |
Output is correct |
67 |
Correct |
507 ms |
7512 KB |
Output is correct |
68 |
Correct |
467 ms |
7360 KB |
Output is correct |
69 |
Correct |
472 ms |
7256 KB |
Output is correct |
70 |
Correct |
533 ms |
7144 KB |
Output is correct |
71 |
Correct |
488 ms |
7256 KB |
Output is correct |
72 |
Correct |
626 ms |
7512 KB |
Output is correct |
73 |
Correct |
600 ms |
7256 KB |
Output is correct |
74 |
Correct |
663 ms |
7256 KB |
Output is correct |
75 |
Correct |
665 ms |
7256 KB |
Output is correct |
76 |
Correct |
644 ms |
7768 KB |
Output is correct |
77 |
Correct |
621 ms |
7996 KB |
Output is correct |
78 |
Correct |
645 ms |
7992 KB |
Output is correct |
79 |
Correct |
681 ms |
7256 KB |
Output is correct |
80 |
Correct |
639 ms |
7000 KB |
Output is correct |
81 |
Correct |
579 ms |
7000 KB |
Output is correct |
82 |
Correct |
629 ms |
7256 KB |
Output is correct |
83 |
Correct |
330 ms |
6744 KB |
Output is correct |
84 |
Correct |
637 ms |
7256 KB |
Output is correct |
85 |
Correct |
628 ms |
7256 KB |
Output is correct |
86 |
Correct |
636 ms |
7256 KB |
Output is correct |
87 |
Correct |
3 ms |
6488 KB |
Output is correct |
88 |
Correct |
442 ms |
22456 KB |
Output is correct |
89 |
Correct |
672 ms |
16984 KB |
Output is correct |
90 |
Correct |
721 ms |
16472 KB |
Output is correct |
91 |
Correct |
700 ms |
23636 KB |
Output is correct |
92 |
Correct |
725 ms |
23632 KB |
Output is correct |
93 |
Correct |
724 ms |
23632 KB |
Output is correct |
94 |
Correct |
734 ms |
23640 KB |
Output is correct |
95 |
Correct |
689 ms |
23632 KB |
Output is correct |
96 |
Correct |
656 ms |
19148 KB |
Output is correct |
97 |
Correct |
702 ms |
19144 KB |
Output is correct |
98 |
Correct |
572 ms |
19544 KB |
Output is correct |
99 |
Correct |
692 ms |
23632 KB |
Output is correct |
100 |
Correct |
705 ms |
20300 KB |
Output is correct |
101 |
Correct |
776 ms |
19024 KB |
Output is correct |
102 |
Correct |
690 ms |
17380 KB |
Output is correct |
103 |
Correct |
750 ms |
29772 KB |
Output is correct |
104 |
Correct |
724 ms |
32020 KB |
Output is correct |
105 |
Correct |
673 ms |
31908 KB |
Output is correct |
106 |
Correct |
696 ms |
18008 KB |
Output is correct |
107 |
Correct |
637 ms |
18256 KB |
Output is correct |
108 |
Correct |
591 ms |
18256 KB |
Output is correct |
109 |
Correct |
599 ms |
17628 KB |
Output is correct |