#include "circuit.h"
#include<bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 2e5 + 5;
const int MOD = 1000002022;
int mult(ll x, ll y) {return x * y % MOD;}
int add(int x, int y) {x += y;if(x >= MOD) x -= MOD;return x;}
int n, m;
int c[maxn];
int prod[maxn];
vector<int> adj[maxn];
vector<int> pref[maxn], suff[maxn];
struct Seg {
int pot;
vector<ii> T;
vector<int> L;
void init(int sz) {
pot = 1;
while(pot < sz) pot <<= 1;
for(int i = 0;i < (pot << 1);i++) T.pb({0, 0}), L.pb(0);
for(int i = 0;i < m;i++)
T[i + pot] = {0, c[i]};
for(int i = pot - 1;i > 0;i--) {
T[i].x = add(T[i * 2].x, T[i * 2 + 1].x);
T[i].y = add(T[i * 2].y, T[i * 2 + 1].y);
}
}
void prop(int x) {
if(L[x] == 0) return;
swap(T[x].x, T[x].y);
if(x < pot)
L[x * 2] ^= 1, L[x * 2 + 1] ^= 1;
L[x] = 0;
}
void update(int x, int lo, int hi, int a, int b) {
prop(x);
if(hi < a || b < lo) return;
if(a <= lo && hi <= b) {
L[x] ^= 1;
prop(x);
return;
}
int mid = (lo + hi) / 2;
update(x * 2, lo, mid, a, b);
update(x * 2 + 1, mid + 1, hi, a, b);
T[x].x = add(T[x * 2].x, T[x * 2 + 1].x);
T[x].y = add(T[x * 2].y, T[x * 2 + 1].y);
}
void toggle(int lo, int hi) {update(1, 0, pot - 1, lo, hi);}
int get() {return T[1].x;}
} S;
void dfs(int x) {
prod[x] = (int)adj[x].size();
for(int y : adj[x]) dfs(y);
for(int y : adj[x]) {
int p = 1;
if(pref[x].size()) p = pref[x].back();
pref[x].pb(mult(p, prod[y]));
}
reverse(all(adj[x]));
for(int y : adj[x]) {
int p = 1;
if(suff[x].size()) p = suff[x].back();
suff[x].pb(mult(p, prod[y]));
}
reverse(all(suff[x]));
reverse(all(adj[x]));
if(pref[x].size())
prod[x] = mult(prod[x], pref[x].back());
if(adj[x].size() == 0)
prod[x] = 1;
}
void solve(int x, int sol) {
if(x >= n) c[x - n] = sol;
int i = 0;
for(int y : adj[x]) {
int l = 1;
if(i) l = pref[x][i - 1];
int r = 1;
if(i < (int)adj[x].size() - 1) r = suff[x][i + 1];
solve(y, mult(sol, mult(l, r)));
i++;
}
}
void init(int N, int M, vector<int> P, vector<int> A) {
n = N, m = M;
for(int i = 1;i < n + m;i++)
adj[P[i]].pb(i);
dfs(0);
solve(0, 1);
S.init(m);
for(int i = 0;i < m;i++)
if(A[i]) S.toggle(i, i);
}
int count_ways(int L, int R) {
L -= n, R -= n;
S.toggle(L, R);
return S.get();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14376 KB |
Output is correct |
2 |
Correct |
8 ms |
14288 KB |
Output is correct |
3 |
Correct |
6 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14416 KB |
Output is correct |
5 |
Correct |
6 ms |
14416 KB |
Output is correct |
6 |
Correct |
7 ms |
14440 KB |
Output is correct |
7 |
Correct |
9 ms |
14412 KB |
Output is correct |
8 |
Correct |
9 ms |
14472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14288 KB |
Output is correct |
2 |
Correct |
7 ms |
14416 KB |
Output is correct |
3 |
Correct |
7 ms |
14424 KB |
Output is correct |
4 |
Correct |
7 ms |
14416 KB |
Output is correct |
5 |
Correct |
7 ms |
14416 KB |
Output is correct |
6 |
Correct |
7 ms |
14416 KB |
Output is correct |
7 |
Correct |
7 ms |
14528 KB |
Output is correct |
8 |
Correct |
8 ms |
14544 KB |
Output is correct |
9 |
Correct |
7 ms |
14544 KB |
Output is correct |
10 |
Correct |
7 ms |
14544 KB |
Output is correct |
11 |
Correct |
7 ms |
14664 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14376 KB |
Output is correct |
2 |
Correct |
8 ms |
14288 KB |
Output is correct |
3 |
Correct |
6 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14416 KB |
Output is correct |
5 |
Correct |
6 ms |
14416 KB |
Output is correct |
6 |
Correct |
7 ms |
14440 KB |
Output is correct |
7 |
Correct |
9 ms |
14412 KB |
Output is correct |
8 |
Correct |
9 ms |
14472 KB |
Output is correct |
9 |
Correct |
6 ms |
14288 KB |
Output is correct |
10 |
Correct |
7 ms |
14416 KB |
Output is correct |
11 |
Correct |
7 ms |
14424 KB |
Output is correct |
12 |
Correct |
7 ms |
14416 KB |
Output is correct |
13 |
Correct |
7 ms |
14416 KB |
Output is correct |
14 |
Correct |
7 ms |
14416 KB |
Output is correct |
15 |
Correct |
7 ms |
14528 KB |
Output is correct |
16 |
Correct |
8 ms |
14544 KB |
Output is correct |
17 |
Correct |
7 ms |
14544 KB |
Output is correct |
18 |
Correct |
7 ms |
14544 KB |
Output is correct |
19 |
Correct |
7 ms |
14664 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14416 KB |
Output is correct |
22 |
Correct |
9 ms |
14424 KB |
Output is correct |
23 |
Correct |
10 ms |
14416 KB |
Output is correct |
24 |
Correct |
8 ms |
14548 KB |
Output is correct |
25 |
Correct |
8 ms |
14492 KB |
Output is correct |
26 |
Correct |
7 ms |
14544 KB |
Output is correct |
27 |
Correct |
7 ms |
14544 KB |
Output is correct |
28 |
Correct |
6 ms |
14544 KB |
Output is correct |
29 |
Correct |
8 ms |
14468 KB |
Output is correct |
30 |
Correct |
7 ms |
14416 KB |
Output is correct |
31 |
Correct |
7 ms |
14616 KB |
Output is correct |
32 |
Correct |
6 ms |
14480 KB |
Output is correct |
33 |
Correct |
7 ms |
14416 KB |
Output is correct |
34 |
Correct |
7 ms |
14416 KB |
Output is correct |
35 |
Correct |
7 ms |
14416 KB |
Output is correct |
36 |
Correct |
7 ms |
14672 KB |
Output is correct |
37 |
Correct |
8 ms |
14580 KB |
Output is correct |
38 |
Correct |
8 ms |
14672 KB |
Output is correct |
39 |
Correct |
7 ms |
14416 KB |
Output is correct |
40 |
Correct |
7 ms |
14464 KB |
Output is correct |
41 |
Correct |
9 ms |
14416 KB |
Output is correct |
42 |
Correct |
9 ms |
14416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
569 ms |
19592 KB |
Output is correct |
2 |
Correct |
737 ms |
24876 KB |
Output is correct |
3 |
Correct |
814 ms |
24832 KB |
Output is correct |
4 |
Correct |
803 ms |
24936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
569 ms |
19592 KB |
Output is correct |
2 |
Correct |
737 ms |
24876 KB |
Output is correct |
3 |
Correct |
814 ms |
24832 KB |
Output is correct |
4 |
Correct |
803 ms |
24936 KB |
Output is correct |
5 |
Correct |
517 ms |
19648 KB |
Output is correct |
6 |
Correct |
794 ms |
24876 KB |
Output is correct |
7 |
Correct |
765 ms |
24896 KB |
Output is correct |
8 |
Correct |
773 ms |
24840 KB |
Output is correct |
9 |
Correct |
315 ms |
14672 KB |
Output is correct |
10 |
Correct |
587 ms |
15056 KB |
Output is correct |
11 |
Correct |
666 ms |
15044 KB |
Output is correct |
12 |
Correct |
722 ms |
15056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14288 KB |
Output is correct |
2 |
Correct |
7 ms |
14416 KB |
Output is correct |
3 |
Correct |
7 ms |
14424 KB |
Output is correct |
4 |
Correct |
7 ms |
14416 KB |
Output is correct |
5 |
Correct |
7 ms |
14416 KB |
Output is correct |
6 |
Correct |
7 ms |
14416 KB |
Output is correct |
7 |
Correct |
7 ms |
14528 KB |
Output is correct |
8 |
Correct |
8 ms |
14544 KB |
Output is correct |
9 |
Correct |
7 ms |
14544 KB |
Output is correct |
10 |
Correct |
7 ms |
14544 KB |
Output is correct |
11 |
Correct |
7 ms |
14664 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
569 ms |
19592 KB |
Output is correct |
14 |
Correct |
737 ms |
24876 KB |
Output is correct |
15 |
Correct |
814 ms |
24832 KB |
Output is correct |
16 |
Correct |
803 ms |
24936 KB |
Output is correct |
17 |
Correct |
517 ms |
19648 KB |
Output is correct |
18 |
Correct |
794 ms |
24876 KB |
Output is correct |
19 |
Correct |
765 ms |
24896 KB |
Output is correct |
20 |
Correct |
773 ms |
24840 KB |
Output is correct |
21 |
Correct |
315 ms |
14672 KB |
Output is correct |
22 |
Correct |
587 ms |
15056 KB |
Output is correct |
23 |
Correct |
666 ms |
15044 KB |
Output is correct |
24 |
Correct |
722 ms |
15056 KB |
Output is correct |
25 |
Correct |
858 ms |
31188 KB |
Output is correct |
26 |
Correct |
765 ms |
31336 KB |
Output is correct |
27 |
Correct |
764 ms |
31348 KB |
Output is correct |
28 |
Correct |
678 ms |
31304 KB |
Output is correct |
29 |
Correct |
824 ms |
42280 KB |
Output is correct |
30 |
Correct |
740 ms |
42304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14376 KB |
Output is correct |
2 |
Correct |
8 ms |
14288 KB |
Output is correct |
3 |
Correct |
6 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14416 KB |
Output is correct |
5 |
Correct |
6 ms |
14416 KB |
Output is correct |
6 |
Correct |
7 ms |
14440 KB |
Output is correct |
7 |
Correct |
9 ms |
14412 KB |
Output is correct |
8 |
Correct |
9 ms |
14472 KB |
Output is correct |
9 |
Correct |
6 ms |
14288 KB |
Output is correct |
10 |
Correct |
7 ms |
14416 KB |
Output is correct |
11 |
Correct |
7 ms |
14424 KB |
Output is correct |
12 |
Correct |
7 ms |
14416 KB |
Output is correct |
13 |
Correct |
7 ms |
14416 KB |
Output is correct |
14 |
Correct |
7 ms |
14416 KB |
Output is correct |
15 |
Correct |
7 ms |
14528 KB |
Output is correct |
16 |
Correct |
8 ms |
14544 KB |
Output is correct |
17 |
Correct |
7 ms |
14544 KB |
Output is correct |
18 |
Correct |
7 ms |
14544 KB |
Output is correct |
19 |
Correct |
7 ms |
14664 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14416 KB |
Output is correct |
22 |
Correct |
9 ms |
14424 KB |
Output is correct |
23 |
Correct |
10 ms |
14416 KB |
Output is correct |
24 |
Correct |
8 ms |
14548 KB |
Output is correct |
25 |
Correct |
8 ms |
14492 KB |
Output is correct |
26 |
Correct |
7 ms |
14544 KB |
Output is correct |
27 |
Correct |
7 ms |
14544 KB |
Output is correct |
28 |
Correct |
6 ms |
14544 KB |
Output is correct |
29 |
Correct |
8 ms |
14468 KB |
Output is correct |
30 |
Correct |
7 ms |
14416 KB |
Output is correct |
31 |
Correct |
7 ms |
14616 KB |
Output is correct |
32 |
Correct |
6 ms |
14480 KB |
Output is correct |
33 |
Correct |
7 ms |
14416 KB |
Output is correct |
34 |
Correct |
7 ms |
14416 KB |
Output is correct |
35 |
Correct |
7 ms |
14416 KB |
Output is correct |
36 |
Correct |
7 ms |
14672 KB |
Output is correct |
37 |
Correct |
8 ms |
14580 KB |
Output is correct |
38 |
Correct |
8 ms |
14672 KB |
Output is correct |
39 |
Correct |
7 ms |
14416 KB |
Output is correct |
40 |
Correct |
7 ms |
14464 KB |
Output is correct |
41 |
Correct |
9 ms |
14416 KB |
Output is correct |
42 |
Correct |
9 ms |
14416 KB |
Output is correct |
43 |
Correct |
369 ms |
14896 KB |
Output is correct |
44 |
Correct |
752 ms |
14996 KB |
Output is correct |
45 |
Correct |
783 ms |
14944 KB |
Output is correct |
46 |
Correct |
724 ms |
15312 KB |
Output is correct |
47 |
Correct |
720 ms |
15312 KB |
Output is correct |
48 |
Correct |
642 ms |
15312 KB |
Output is correct |
49 |
Correct |
736 ms |
15300 KB |
Output is correct |
50 |
Correct |
757 ms |
15300 KB |
Output is correct |
51 |
Correct |
664 ms |
14816 KB |
Output is correct |
52 |
Correct |
595 ms |
14868 KB |
Output is correct |
53 |
Correct |
627 ms |
15420 KB |
Output is correct |
54 |
Correct |
756 ms |
15224 KB |
Output is correct |
55 |
Correct |
679 ms |
15056 KB |
Output is correct |
56 |
Correct |
829 ms |
14928 KB |
Output is correct |
57 |
Correct |
571 ms |
14840 KB |
Output is correct |
58 |
Correct |
797 ms |
15824 KB |
Output is correct |
59 |
Correct |
719 ms |
15932 KB |
Output is correct |
60 |
Correct |
823 ms |
15928 KB |
Output is correct |
61 |
Correct |
704 ms |
15016 KB |
Output is correct |
62 |
Correct |
561 ms |
14824 KB |
Output is correct |
63 |
Correct |
811 ms |
14764 KB |
Output is correct |
64 |
Correct |
658 ms |
14868 KB |
Output is correct |
65 |
Correct |
410 ms |
14672 KB |
Output is correct |
66 |
Correct |
699 ms |
15056 KB |
Output is correct |
67 |
Correct |
598 ms |
15076 KB |
Output is correct |
68 |
Correct |
701 ms |
15056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14376 KB |
Output is correct |
2 |
Correct |
8 ms |
14288 KB |
Output is correct |
3 |
Correct |
6 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14416 KB |
Output is correct |
5 |
Correct |
6 ms |
14416 KB |
Output is correct |
6 |
Correct |
7 ms |
14440 KB |
Output is correct |
7 |
Correct |
9 ms |
14412 KB |
Output is correct |
8 |
Correct |
9 ms |
14472 KB |
Output is correct |
9 |
Correct |
6 ms |
14288 KB |
Output is correct |
10 |
Correct |
7 ms |
14416 KB |
Output is correct |
11 |
Correct |
7 ms |
14424 KB |
Output is correct |
12 |
Correct |
7 ms |
14416 KB |
Output is correct |
13 |
Correct |
7 ms |
14416 KB |
Output is correct |
14 |
Correct |
7 ms |
14416 KB |
Output is correct |
15 |
Correct |
7 ms |
14528 KB |
Output is correct |
16 |
Correct |
8 ms |
14544 KB |
Output is correct |
17 |
Correct |
7 ms |
14544 KB |
Output is correct |
18 |
Correct |
7 ms |
14544 KB |
Output is correct |
19 |
Correct |
7 ms |
14664 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14416 KB |
Output is correct |
22 |
Correct |
9 ms |
14424 KB |
Output is correct |
23 |
Correct |
10 ms |
14416 KB |
Output is correct |
24 |
Correct |
8 ms |
14548 KB |
Output is correct |
25 |
Correct |
8 ms |
14492 KB |
Output is correct |
26 |
Correct |
7 ms |
14544 KB |
Output is correct |
27 |
Correct |
7 ms |
14544 KB |
Output is correct |
28 |
Correct |
6 ms |
14544 KB |
Output is correct |
29 |
Correct |
8 ms |
14468 KB |
Output is correct |
30 |
Correct |
7 ms |
14416 KB |
Output is correct |
31 |
Correct |
7 ms |
14616 KB |
Output is correct |
32 |
Correct |
6 ms |
14480 KB |
Output is correct |
33 |
Correct |
7 ms |
14416 KB |
Output is correct |
34 |
Correct |
7 ms |
14416 KB |
Output is correct |
35 |
Correct |
7 ms |
14416 KB |
Output is correct |
36 |
Correct |
7 ms |
14672 KB |
Output is correct |
37 |
Correct |
8 ms |
14580 KB |
Output is correct |
38 |
Correct |
8 ms |
14672 KB |
Output is correct |
39 |
Correct |
7 ms |
14416 KB |
Output is correct |
40 |
Correct |
7 ms |
14464 KB |
Output is correct |
41 |
Correct |
9 ms |
14416 KB |
Output is correct |
42 |
Correct |
9 ms |
14416 KB |
Output is correct |
43 |
Correct |
569 ms |
19592 KB |
Output is correct |
44 |
Correct |
737 ms |
24876 KB |
Output is correct |
45 |
Correct |
814 ms |
24832 KB |
Output is correct |
46 |
Correct |
803 ms |
24936 KB |
Output is correct |
47 |
Correct |
517 ms |
19648 KB |
Output is correct |
48 |
Correct |
794 ms |
24876 KB |
Output is correct |
49 |
Correct |
765 ms |
24896 KB |
Output is correct |
50 |
Correct |
773 ms |
24840 KB |
Output is correct |
51 |
Correct |
315 ms |
14672 KB |
Output is correct |
52 |
Correct |
587 ms |
15056 KB |
Output is correct |
53 |
Correct |
666 ms |
15044 KB |
Output is correct |
54 |
Correct |
722 ms |
15056 KB |
Output is correct |
55 |
Correct |
858 ms |
31188 KB |
Output is correct |
56 |
Correct |
765 ms |
31336 KB |
Output is correct |
57 |
Correct |
764 ms |
31348 KB |
Output is correct |
58 |
Correct |
678 ms |
31304 KB |
Output is correct |
59 |
Correct |
824 ms |
42280 KB |
Output is correct |
60 |
Correct |
740 ms |
42304 KB |
Output is correct |
61 |
Correct |
369 ms |
14896 KB |
Output is correct |
62 |
Correct |
752 ms |
14996 KB |
Output is correct |
63 |
Correct |
783 ms |
14944 KB |
Output is correct |
64 |
Correct |
724 ms |
15312 KB |
Output is correct |
65 |
Correct |
720 ms |
15312 KB |
Output is correct |
66 |
Correct |
642 ms |
15312 KB |
Output is correct |
67 |
Correct |
736 ms |
15300 KB |
Output is correct |
68 |
Correct |
757 ms |
15300 KB |
Output is correct |
69 |
Correct |
664 ms |
14816 KB |
Output is correct |
70 |
Correct |
595 ms |
14868 KB |
Output is correct |
71 |
Correct |
627 ms |
15420 KB |
Output is correct |
72 |
Correct |
756 ms |
15224 KB |
Output is correct |
73 |
Correct |
679 ms |
15056 KB |
Output is correct |
74 |
Correct |
829 ms |
14928 KB |
Output is correct |
75 |
Correct |
571 ms |
14840 KB |
Output is correct |
76 |
Correct |
797 ms |
15824 KB |
Output is correct |
77 |
Correct |
719 ms |
15932 KB |
Output is correct |
78 |
Correct |
823 ms |
15928 KB |
Output is correct |
79 |
Correct |
704 ms |
15016 KB |
Output is correct |
80 |
Correct |
561 ms |
14824 KB |
Output is correct |
81 |
Correct |
811 ms |
14764 KB |
Output is correct |
82 |
Correct |
658 ms |
14868 KB |
Output is correct |
83 |
Correct |
410 ms |
14672 KB |
Output is correct |
84 |
Correct |
699 ms |
15056 KB |
Output is correct |
85 |
Correct |
598 ms |
15076 KB |
Output is correct |
86 |
Correct |
701 ms |
15056 KB |
Output is correct |
87 |
Correct |
7 ms |
14384 KB |
Output is correct |
88 |
Correct |
507 ms |
30516 KB |
Output is correct |
89 |
Correct |
789 ms |
25912 KB |
Output is correct |
90 |
Correct |
744 ms |
24892 KB |
Output is correct |
91 |
Correct |
840 ms |
31696 KB |
Output is correct |
92 |
Correct |
807 ms |
31688 KB |
Output is correct |
93 |
Correct |
726 ms |
31736 KB |
Output is correct |
94 |
Correct |
897 ms |
31692 KB |
Output is correct |
95 |
Correct |
858 ms |
31728 KB |
Output is correct |
96 |
Correct |
689 ms |
21936 KB |
Output is correct |
97 |
Correct |
656 ms |
21952 KB |
Output is correct |
98 |
Correct |
763 ms |
35888 KB |
Output is correct |
99 |
Correct |
834 ms |
31396 KB |
Output is correct |
100 |
Correct |
805 ms |
26028 KB |
Output is correct |
101 |
Correct |
707 ms |
24752 KB |
Output is correct |
102 |
Correct |
735 ms |
22180 KB |
Output is correct |
103 |
Correct |
953 ms |
42296 KB |
Output is correct |
104 |
Correct |
742 ms |
43464 KB |
Output is correct |
105 |
Correct |
597 ms |
43464 KB |
Output is correct |
106 |
Correct |
832 ms |
27972 KB |
Output is correct |
107 |
Correct |
793 ms |
24484 KB |
Output is correct |
108 |
Correct |
816 ms |
23700 KB |
Output is correct |
109 |
Correct |
875 ms |
22544 KB |
Output is correct |