#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int mod=1e9+2022;
const int N=2e5;
int n, m, q;
vector<int> adj[N];
int p[N];
int a[N];
int sum[4*N], con[4*N], c[N];
ll prod[N];
int add(int a, int b){
return ((a+mod)%mod+(b+mod)%mod)%mod;
}
void add2(int &a, int b){
a=add(a,b);
}
int mult(ll a, ll b){
return (a*b)%mod;
}
void mult2(ll &a, ll b){
a=mult(a,b);
}
void dfs(int x){
prod[x]=x<n?adj[x].size():1;
for(int k:adj[x]){
dfs(k);
mult2(prod[x], prod[k]);
}
}
void dfs2(int x){
int p=adj[x].size();
vector<ll> pre(p), suf(p);
for(int i=0; i<p; i++){
int k=adj[x][i];
pre[i]=suf[i]=prod[k];
}
for(int i=1; i<p; i++){
mult2(pre[i], pre[i-1]);
}
for(int i=p-2; i>=0; i--){
mult2(suf[i], suf[i+1]);
}
for(int i=0; i<p; i++){
int k=adj[x][i];
c[k]=mult(c[x], mult(i?pre[i-1]:1, i<p-1?suf[i+1]:1));
dfs2(k);
}
}
short lazy[4*N];
void build(int x, int lx, int rx){
if(rx-lx==1){
con[x]=a[lx]?c[lx+n]:0;
// cout << "c[" << lx+n << "] " << c[lx+n] << "\n";
sum[x]=c[lx+n];
return;
}
int m=(lx+rx)/2;
build(2*x+1, lx, m);
build(2*x+2, m, rx);
con[x]=add(con[2*x+1], con[2*x+2]);
sum[x]=add(sum[2*x+1], sum[2*x+2]);
}
void push(int x, int lx, int rx){
if(rx-lx==1 || !lazy[x]) return;
for(int y=2*x+1; y<=2*x+2; y++){
con[y]=add(sum[y], -con[y]);
lazy[y]^=1;
}
lazy[x]=0;
}
void tog(int l, int r, int x=0, int lx=0, int rx=m){
push(x, lx, rx);
if(lx>=r || rx<=l) return;
if(lx>=l && rx<=r){
con[x]=add(sum[x], -con[x]);
lazy[x]=1;
return;
}
int m=(lx+rx)/2;
tog(l, r, 2*x+1, lx, m);
tog(l, r, 2*x+2, m, rx);
con[x]=add(con[2*x+1], con[2*x+2]);
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
n=N, m=M;
for(int i=0; i<n+m; i++){
p[i]=P[i];
if(i) adj[p[i]].push_back(i);
}
for(int i=0; i<m; i++) a[i]=A[i];
dfs(0);
c[0]=1;
dfs2(0);
build(0, 0, m);
}
int count_ways(int l, int r) {
l-=n; r-=n;
tog(l, r+1);
return con[0];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
4 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5124 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5328 KB |
Output is correct |
11 |
Correct |
4 ms |
5328 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
4 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 ms |
5124 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
4 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5328 KB |
Output is correct |
19 |
Correct |
4 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
4 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
4 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5072 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
4 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5140 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
4 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
4 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
3 ms |
5328 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
409 ms |
8464 KB |
Output is correct |
2 |
Correct |
779 ms |
11848 KB |
Output is correct |
3 |
Correct |
967 ms |
11848 KB |
Output is correct |
4 |
Correct |
922 ms |
11900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
409 ms |
8464 KB |
Output is correct |
2 |
Correct |
779 ms |
11848 KB |
Output is correct |
3 |
Correct |
967 ms |
11848 KB |
Output is correct |
4 |
Correct |
922 ms |
11900 KB |
Output is correct |
5 |
Correct |
834 ms |
8432 KB |
Output is correct |
6 |
Correct |
1111 ms |
11924 KB |
Output is correct |
7 |
Correct |
1035 ms |
11924 KB |
Output is correct |
8 |
Correct |
764 ms |
11876 KB |
Output is correct |
9 |
Correct |
452 ms |
5236 KB |
Output is correct |
10 |
Correct |
827 ms |
5420 KB |
Output is correct |
11 |
Correct |
890 ms |
5456 KB |
Output is correct |
12 |
Correct |
968 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
3 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5124 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
4 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5328 KB |
Output is correct |
11 |
Correct |
4 ms |
5328 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
409 ms |
8464 KB |
Output is correct |
14 |
Correct |
779 ms |
11848 KB |
Output is correct |
15 |
Correct |
967 ms |
11848 KB |
Output is correct |
16 |
Correct |
922 ms |
11900 KB |
Output is correct |
17 |
Correct |
834 ms |
8432 KB |
Output is correct |
18 |
Correct |
1111 ms |
11924 KB |
Output is correct |
19 |
Correct |
1035 ms |
11924 KB |
Output is correct |
20 |
Correct |
764 ms |
11876 KB |
Output is correct |
21 |
Correct |
452 ms |
5236 KB |
Output is correct |
22 |
Correct |
827 ms |
5420 KB |
Output is correct |
23 |
Correct |
890 ms |
5456 KB |
Output is correct |
24 |
Correct |
968 ms |
5456 KB |
Output is correct |
25 |
Correct |
969 ms |
15816 KB |
Output is correct |
26 |
Correct |
1152 ms |
16036 KB |
Output is correct |
27 |
Correct |
1184 ms |
16052 KB |
Output is correct |
28 |
Correct |
808 ms |
16044 KB |
Output is correct |
29 |
Correct |
988 ms |
36344 KB |
Output is correct |
30 |
Correct |
1030 ms |
36376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
4 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 ms |
5124 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
4 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5328 KB |
Output is correct |
19 |
Correct |
4 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
4 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
4 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5072 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
4 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5140 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
4 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
4 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
3 ms |
5328 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5104 KB |
Output is correct |
43 |
Correct |
538 ms |
5328 KB |
Output is correct |
44 |
Correct |
846 ms |
5328 KB |
Output is correct |
45 |
Correct |
871 ms |
5328 KB |
Output is correct |
46 |
Correct |
938 ms |
5584 KB |
Output is correct |
47 |
Correct |
863 ms |
5584 KB |
Output is correct |
48 |
Correct |
957 ms |
5584 KB |
Output is correct |
49 |
Correct |
868 ms |
5584 KB |
Output is correct |
50 |
Correct |
690 ms |
5616 KB |
Output is correct |
51 |
Correct |
755 ms |
5476 KB |
Output is correct |
52 |
Correct |
864 ms |
5496 KB |
Output is correct |
53 |
Correct |
761 ms |
6352 KB |
Output is correct |
54 |
Correct |
764 ms |
5636 KB |
Output is correct |
55 |
Correct |
899 ms |
5456 KB |
Output is correct |
56 |
Correct |
856 ms |
5456 KB |
Output is correct |
57 |
Correct |
1012 ms |
5328 KB |
Output is correct |
58 |
Correct |
878 ms |
6608 KB |
Output is correct |
59 |
Correct |
955 ms |
6736 KB |
Output is correct |
60 |
Correct |
812 ms |
6756 KB |
Output is correct |
61 |
Correct |
808 ms |
5584 KB |
Output is correct |
62 |
Correct |
904 ms |
5360 KB |
Output is correct |
63 |
Correct |
735 ms |
5328 KB |
Output is correct |
64 |
Correct |
883 ms |
5428 KB |
Output is correct |
65 |
Correct |
472 ms |
5200 KB |
Output is correct |
66 |
Correct |
881 ms |
5480 KB |
Output is correct |
67 |
Correct |
704 ms |
5456 KB |
Output is correct |
68 |
Correct |
757 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
5072 KB |
Output is correct |
4 |
Correct |
4 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
5072 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
3 ms |
5072 KB |
Output is correct |
14 |
Correct |
3 ms |
5124 KB |
Output is correct |
15 |
Correct |
3 ms |
5072 KB |
Output is correct |
16 |
Correct |
3 ms |
5072 KB |
Output is correct |
17 |
Correct |
4 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5328 KB |
Output is correct |
19 |
Correct |
4 ms |
5328 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
4 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
5072 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
3 ms |
5072 KB |
Output is correct |
27 |
Correct |
3 ms |
5072 KB |
Output is correct |
28 |
Correct |
4 ms |
5072 KB |
Output is correct |
29 |
Correct |
3 ms |
5072 KB |
Output is correct |
30 |
Correct |
3 ms |
5072 KB |
Output is correct |
31 |
Correct |
4 ms |
5200 KB |
Output is correct |
32 |
Correct |
3 ms |
5140 KB |
Output is correct |
33 |
Correct |
3 ms |
5072 KB |
Output is correct |
34 |
Correct |
4 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5072 KB |
Output is correct |
36 |
Correct |
4 ms |
5328 KB |
Output is correct |
37 |
Correct |
3 ms |
5328 KB |
Output is correct |
38 |
Correct |
3 ms |
5328 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
3 ms |
5072 KB |
Output is correct |
42 |
Correct |
3 ms |
5104 KB |
Output is correct |
43 |
Correct |
409 ms |
8464 KB |
Output is correct |
44 |
Correct |
779 ms |
11848 KB |
Output is correct |
45 |
Correct |
967 ms |
11848 KB |
Output is correct |
46 |
Correct |
922 ms |
11900 KB |
Output is correct |
47 |
Correct |
834 ms |
8432 KB |
Output is correct |
48 |
Correct |
1111 ms |
11924 KB |
Output is correct |
49 |
Correct |
1035 ms |
11924 KB |
Output is correct |
50 |
Correct |
764 ms |
11876 KB |
Output is correct |
51 |
Correct |
452 ms |
5236 KB |
Output is correct |
52 |
Correct |
827 ms |
5420 KB |
Output is correct |
53 |
Correct |
890 ms |
5456 KB |
Output is correct |
54 |
Correct |
968 ms |
5456 KB |
Output is correct |
55 |
Correct |
969 ms |
15816 KB |
Output is correct |
56 |
Correct |
1152 ms |
16036 KB |
Output is correct |
57 |
Correct |
1184 ms |
16052 KB |
Output is correct |
58 |
Correct |
808 ms |
16044 KB |
Output is correct |
59 |
Correct |
988 ms |
36344 KB |
Output is correct |
60 |
Correct |
1030 ms |
36376 KB |
Output is correct |
61 |
Correct |
538 ms |
5328 KB |
Output is correct |
62 |
Correct |
846 ms |
5328 KB |
Output is correct |
63 |
Correct |
871 ms |
5328 KB |
Output is correct |
64 |
Correct |
938 ms |
5584 KB |
Output is correct |
65 |
Correct |
863 ms |
5584 KB |
Output is correct |
66 |
Correct |
957 ms |
5584 KB |
Output is correct |
67 |
Correct |
868 ms |
5584 KB |
Output is correct |
68 |
Correct |
690 ms |
5616 KB |
Output is correct |
69 |
Correct |
755 ms |
5476 KB |
Output is correct |
70 |
Correct |
864 ms |
5496 KB |
Output is correct |
71 |
Correct |
761 ms |
6352 KB |
Output is correct |
72 |
Correct |
764 ms |
5636 KB |
Output is correct |
73 |
Correct |
899 ms |
5456 KB |
Output is correct |
74 |
Correct |
856 ms |
5456 KB |
Output is correct |
75 |
Correct |
1012 ms |
5328 KB |
Output is correct |
76 |
Correct |
878 ms |
6608 KB |
Output is correct |
77 |
Correct |
955 ms |
6736 KB |
Output is correct |
78 |
Correct |
812 ms |
6756 KB |
Output is correct |
79 |
Correct |
808 ms |
5584 KB |
Output is correct |
80 |
Correct |
904 ms |
5360 KB |
Output is correct |
81 |
Correct |
735 ms |
5328 KB |
Output is correct |
82 |
Correct |
883 ms |
5428 KB |
Output is correct |
83 |
Correct |
472 ms |
5200 KB |
Output is correct |
84 |
Correct |
881 ms |
5480 KB |
Output is correct |
85 |
Correct |
704 ms |
5456 KB |
Output is correct |
86 |
Correct |
757 ms |
5456 KB |
Output is correct |
87 |
Correct |
3 ms |
4944 KB |
Output is correct |
88 |
Correct |
521 ms |
15092 KB |
Output is correct |
89 |
Correct |
1167 ms |
12008 KB |
Output is correct |
90 |
Correct |
1122 ms |
11772 KB |
Output is correct |
91 |
Correct |
901 ms |
16104 KB |
Output is correct |
92 |
Correct |
1115 ms |
16180 KB |
Output is correct |
93 |
Correct |
1072 ms |
16156 KB |
Output is correct |
94 |
Correct |
1097 ms |
16164 KB |
Output is correct |
95 |
Correct |
1109 ms |
16180 KB |
Output is correct |
96 |
Correct |
888 ms |
11116 KB |
Output is correct |
97 |
Correct |
1162 ms |
11120 KB |
Output is correct |
98 |
Correct |
922 ms |
30812 KB |
Output is correct |
99 |
Correct |
1211 ms |
16072 KB |
Output is correct |
100 |
Correct |
1124 ms |
13308 KB |
Output is correct |
101 |
Correct |
907 ms |
12364 KB |
Output is correct |
102 |
Correct |
1022 ms |
11132 KB |
Output is correct |
103 |
Correct |
1037 ms |
36300 KB |
Output is correct |
104 |
Correct |
1087 ms |
36728 KB |
Output is correct |
105 |
Correct |
1045 ms |
36856 KB |
Output is correct |
106 |
Correct |
1070 ms |
16120 KB |
Output is correct |
107 |
Correct |
997 ms |
11692 KB |
Output is correct |
108 |
Correct |
1088 ms |
11592 KB |
Output is correct |
109 |
Correct |
1022 ms |
11280 KB |
Output is correct |