#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
ll MOD = 1e9 + 2022;
int n, m;
vi g[400000];
vector<ll> prod(200000);
vector<ll> b(200000);
vi a;
void get_prod(int u){
prod[u]=max(1,(int)g[u].size());
for (int v : g[u]){
get_prod(v);
prod[u] *= prod[v]; prod[u] %= MOD;
}
}
void dfs(int u, ll val){
int len = g[u].size();
if (len==0){
b[u-n] = val; return;
}
vector<ll> p(len);
FOR(i,0,len){
p[i] = prod[g[u][i]];
}
vector<ll> pre(len+1), suf(len+1);
pre[0]=1; FOR(i,1,len+1){
pre[i]=(pre[i-1]*p[i-1]) % MOD;
}
suf[len]=1; ROF(i,len-1,0){
suf[i]=(suf[i+1]*p[i]) % MOD;
}
FOR(i,0,len){
int v = g[u][i];
ll Val = (pre[i]*suf[i+1]) % MOD;
Val *= val; Val %= MOD;
dfs(v,Val);
}
}
ll ans=0;
struct segtree{
int sz; vi neg; vector<ll> sm;
void build(int v, int tl, int tr){
if (tl==tr){
if (a[tl]==0) sm[v]=MOD-b[tl];
else sm[v]=b[tl];
return;
}
int tm = (tl+tr)/2;
build(2*v,tl,tm); build(2*v+1,tm+1,tr);
sm[v]=(sm[2*v]+sm[2*v+1]) % MOD;
}
void init(int n){
sz=n; neg.assign(4*n,1); sm.assign(4*n,0);
build(1,0,sz-1);
}
void update(int v, int tl, int tr, int l, int r){
if (tl != tr && neg[v]==-1){
neg[2*v]=-neg[2*v]; sm[2*v]=MOD-sm[2*v];
neg[2*v+1]=-neg[2*v+1]; sm[2*v+1]=MOD-sm[2*v+1];
neg[v]=1;
}
if (tl==l && tr==r){
neg[v]=-neg[v]; sm[v]=MOD-sm[v]; return;
}
if (l>r) return;
int tm = (tl+tr)/2;
update(2*v,tl,tm,l,min(r,tm)); update(2*v+1,tm+1,tr,max(l,tm+1),r);
sm[v]=(sm[2*v]+sm[2*v+1]) % MOD;
}
void update(int l, int r){update(1,0,sz-1,l,r);}
ll get(int v, int tl, int tr, int l, int r){
if (tl != tr && neg[v]==-1){
neg[2*v]=-neg[2*v]; sm[2*v]=MOD-sm[2*v];
neg[2*v+1]=-neg[2*v+1]; sm[2*v+1]=MOD-sm[2*v+1];
neg[v]=1;
}
if (tl==l && tr==r) return sm[v];
if (l>r) return 0;
int tm = (tl+tr)/2;
ll val = (get(2*v,tl,tm,l,min(r,tm))+get(2*v+1,tm+1,tr,max(l,tm+1),r)) % MOD;
sm[v]=(sm[2*v]+sm[2*v+1])%MOD;
return val;
}
ll get(int l, int r){return get(1,0,sz-1,l,r);}
};
segtree st;
void init(int N, int M, vi P, vi A) {
n=N, m=M; a=A;
FOR(i,1,n+m){
g[P[i]].pb(i);
}
get_prod(0); dfs(0,1);
//FOR(i,0,m) cout << a[i] << " ";
//cout << '\n';
//FOR(i,0,m) cout << b[i] << " ";
//cout << '\n';
st.init(m);
FOR(i,0,m){
if (a[i]){ans += b[i]; ans %= MOD;}
}
}
int count_ways(int L, int R) {
st.update(L-n,R-n);
//FOR(i,0,m) cout << st.get(i,i) << " ";
//cout << '\n';
ans += st.get(L-n,R-n);
ans %= MOD;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12752 KB |
Output is correct |
2 |
Correct |
6 ms |
12752 KB |
Output is correct |
3 |
Correct |
7 ms |
12880 KB |
Output is correct |
4 |
Correct |
5 ms |
12872 KB |
Output is correct |
5 |
Correct |
5 ms |
12916 KB |
Output is correct |
6 |
Correct |
6 ms |
12920 KB |
Output is correct |
7 |
Correct |
8 ms |
12788 KB |
Output is correct |
8 |
Correct |
5 ms |
12880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12972 KB |
Output is correct |
2 |
Correct |
5 ms |
12752 KB |
Output is correct |
3 |
Correct |
5 ms |
12880 KB |
Output is correct |
4 |
Correct |
6 ms |
12844 KB |
Output is correct |
5 |
Correct |
6 ms |
12892 KB |
Output is correct |
6 |
Correct |
6 ms |
12880 KB |
Output is correct |
7 |
Correct |
6 ms |
12880 KB |
Output is correct |
8 |
Correct |
7 ms |
12832 KB |
Output is correct |
9 |
Correct |
6 ms |
12880 KB |
Output is correct |
10 |
Correct |
6 ms |
13092 KB |
Output is correct |
11 |
Correct |
6 ms |
13148 KB |
Output is correct |
12 |
Correct |
6 ms |
12904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12752 KB |
Output is correct |
2 |
Correct |
6 ms |
12752 KB |
Output is correct |
3 |
Correct |
7 ms |
12880 KB |
Output is correct |
4 |
Correct |
5 ms |
12872 KB |
Output is correct |
5 |
Correct |
5 ms |
12916 KB |
Output is correct |
6 |
Correct |
6 ms |
12920 KB |
Output is correct |
7 |
Correct |
8 ms |
12788 KB |
Output is correct |
8 |
Correct |
5 ms |
12880 KB |
Output is correct |
9 |
Correct |
7 ms |
12972 KB |
Output is correct |
10 |
Correct |
5 ms |
12752 KB |
Output is correct |
11 |
Correct |
5 ms |
12880 KB |
Output is correct |
12 |
Correct |
6 ms |
12844 KB |
Output is correct |
13 |
Correct |
6 ms |
12892 KB |
Output is correct |
14 |
Correct |
6 ms |
12880 KB |
Output is correct |
15 |
Correct |
6 ms |
12880 KB |
Output is correct |
16 |
Correct |
7 ms |
12832 KB |
Output is correct |
17 |
Correct |
6 ms |
12880 KB |
Output is correct |
18 |
Correct |
6 ms |
13092 KB |
Output is correct |
19 |
Correct |
6 ms |
13148 KB |
Output is correct |
20 |
Correct |
6 ms |
12904 KB |
Output is correct |
21 |
Correct |
6 ms |
12880 KB |
Output is correct |
22 |
Correct |
6 ms |
12776 KB |
Output is correct |
23 |
Correct |
6 ms |
12880 KB |
Output is correct |
24 |
Correct |
6 ms |
12948 KB |
Output is correct |
25 |
Correct |
6 ms |
12896 KB |
Output is correct |
26 |
Correct |
6 ms |
12880 KB |
Output is correct |
27 |
Correct |
7 ms |
12880 KB |
Output is correct |
28 |
Correct |
6 ms |
12928 KB |
Output is correct |
29 |
Correct |
5 ms |
12880 KB |
Output is correct |
30 |
Correct |
5 ms |
12808 KB |
Output is correct |
31 |
Correct |
6 ms |
13136 KB |
Output is correct |
32 |
Correct |
6 ms |
12880 KB |
Output is correct |
33 |
Correct |
6 ms |
12880 KB |
Output is correct |
34 |
Correct |
6 ms |
12880 KB |
Output is correct |
35 |
Correct |
7 ms |
12880 KB |
Output is correct |
36 |
Correct |
6 ms |
13136 KB |
Output is correct |
37 |
Correct |
6 ms |
13136 KB |
Output is correct |
38 |
Correct |
6 ms |
13136 KB |
Output is correct |
39 |
Correct |
6 ms |
12880 KB |
Output is correct |
40 |
Correct |
7 ms |
12880 KB |
Output is correct |
41 |
Correct |
6 ms |
12788 KB |
Output is correct |
42 |
Correct |
6 ms |
12880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
473 ms |
16208 KB |
Output is correct |
2 |
Correct |
888 ms |
19712 KB |
Output is correct |
3 |
Correct |
648 ms |
19744 KB |
Output is correct |
4 |
Correct |
679 ms |
19764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
473 ms |
16208 KB |
Output is correct |
2 |
Correct |
888 ms |
19712 KB |
Output is correct |
3 |
Correct |
648 ms |
19744 KB |
Output is correct |
4 |
Correct |
679 ms |
19764 KB |
Output is correct |
5 |
Correct |
659 ms |
16208 KB |
Output is correct |
6 |
Correct |
969 ms |
19672 KB |
Output is correct |
7 |
Correct |
846 ms |
19656 KB |
Output is correct |
8 |
Correct |
790 ms |
19656 KB |
Output is correct |
9 |
Correct |
372 ms |
13036 KB |
Output is correct |
10 |
Correct |
790 ms |
13264 KB |
Output is correct |
11 |
Correct |
790 ms |
13264 KB |
Output is correct |
12 |
Correct |
864 ms |
13264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12972 KB |
Output is correct |
2 |
Correct |
5 ms |
12752 KB |
Output is correct |
3 |
Correct |
5 ms |
12880 KB |
Output is correct |
4 |
Correct |
6 ms |
12844 KB |
Output is correct |
5 |
Correct |
6 ms |
12892 KB |
Output is correct |
6 |
Correct |
6 ms |
12880 KB |
Output is correct |
7 |
Correct |
6 ms |
12880 KB |
Output is correct |
8 |
Correct |
7 ms |
12832 KB |
Output is correct |
9 |
Correct |
6 ms |
12880 KB |
Output is correct |
10 |
Correct |
6 ms |
13092 KB |
Output is correct |
11 |
Correct |
6 ms |
13148 KB |
Output is correct |
12 |
Correct |
6 ms |
12904 KB |
Output is correct |
13 |
Correct |
473 ms |
16208 KB |
Output is correct |
14 |
Correct |
888 ms |
19712 KB |
Output is correct |
15 |
Correct |
648 ms |
19744 KB |
Output is correct |
16 |
Correct |
679 ms |
19764 KB |
Output is correct |
17 |
Correct |
659 ms |
16208 KB |
Output is correct |
18 |
Correct |
969 ms |
19672 KB |
Output is correct |
19 |
Correct |
846 ms |
19656 KB |
Output is correct |
20 |
Correct |
790 ms |
19656 KB |
Output is correct |
21 |
Correct |
372 ms |
13036 KB |
Output is correct |
22 |
Correct |
790 ms |
13264 KB |
Output is correct |
23 |
Correct |
790 ms |
13264 KB |
Output is correct |
24 |
Correct |
864 ms |
13264 KB |
Output is correct |
25 |
Correct |
781 ms |
23192 KB |
Output is correct |
26 |
Correct |
971 ms |
23320 KB |
Output is correct |
27 |
Correct |
916 ms |
23336 KB |
Output is correct |
28 |
Correct |
616 ms |
23336 KB |
Output is correct |
29 |
Correct |
965 ms |
46760 KB |
Output is correct |
30 |
Correct |
812 ms |
46760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12752 KB |
Output is correct |
2 |
Correct |
6 ms |
12752 KB |
Output is correct |
3 |
Correct |
7 ms |
12880 KB |
Output is correct |
4 |
Correct |
5 ms |
12872 KB |
Output is correct |
5 |
Correct |
5 ms |
12916 KB |
Output is correct |
6 |
Correct |
6 ms |
12920 KB |
Output is correct |
7 |
Correct |
8 ms |
12788 KB |
Output is correct |
8 |
Correct |
5 ms |
12880 KB |
Output is correct |
9 |
Correct |
7 ms |
12972 KB |
Output is correct |
10 |
Correct |
5 ms |
12752 KB |
Output is correct |
11 |
Correct |
5 ms |
12880 KB |
Output is correct |
12 |
Correct |
6 ms |
12844 KB |
Output is correct |
13 |
Correct |
6 ms |
12892 KB |
Output is correct |
14 |
Correct |
6 ms |
12880 KB |
Output is correct |
15 |
Correct |
6 ms |
12880 KB |
Output is correct |
16 |
Correct |
7 ms |
12832 KB |
Output is correct |
17 |
Correct |
6 ms |
12880 KB |
Output is correct |
18 |
Correct |
6 ms |
13092 KB |
Output is correct |
19 |
Correct |
6 ms |
13148 KB |
Output is correct |
20 |
Correct |
6 ms |
12904 KB |
Output is correct |
21 |
Correct |
6 ms |
12880 KB |
Output is correct |
22 |
Correct |
6 ms |
12776 KB |
Output is correct |
23 |
Correct |
6 ms |
12880 KB |
Output is correct |
24 |
Correct |
6 ms |
12948 KB |
Output is correct |
25 |
Correct |
6 ms |
12896 KB |
Output is correct |
26 |
Correct |
6 ms |
12880 KB |
Output is correct |
27 |
Correct |
7 ms |
12880 KB |
Output is correct |
28 |
Correct |
6 ms |
12928 KB |
Output is correct |
29 |
Correct |
5 ms |
12880 KB |
Output is correct |
30 |
Correct |
5 ms |
12808 KB |
Output is correct |
31 |
Correct |
6 ms |
13136 KB |
Output is correct |
32 |
Correct |
6 ms |
12880 KB |
Output is correct |
33 |
Correct |
6 ms |
12880 KB |
Output is correct |
34 |
Correct |
6 ms |
12880 KB |
Output is correct |
35 |
Correct |
7 ms |
12880 KB |
Output is correct |
36 |
Correct |
6 ms |
13136 KB |
Output is correct |
37 |
Correct |
6 ms |
13136 KB |
Output is correct |
38 |
Correct |
6 ms |
13136 KB |
Output is correct |
39 |
Correct |
6 ms |
12880 KB |
Output is correct |
40 |
Correct |
7 ms |
12880 KB |
Output is correct |
41 |
Correct |
6 ms |
12788 KB |
Output is correct |
42 |
Correct |
6 ms |
12880 KB |
Output is correct |
43 |
Correct |
562 ms |
13136 KB |
Output is correct |
44 |
Correct |
764 ms |
13256 KB |
Output is correct |
45 |
Correct |
863 ms |
13148 KB |
Output is correct |
46 |
Correct |
683 ms |
13360 KB |
Output is correct |
47 |
Correct |
831 ms |
13360 KB |
Output is correct |
48 |
Correct |
842 ms |
13364 KB |
Output is correct |
49 |
Correct |
735 ms |
13356 KB |
Output is correct |
50 |
Correct |
749 ms |
13368 KB |
Output is correct |
51 |
Correct |
719 ms |
13248 KB |
Output is correct |
52 |
Correct |
659 ms |
13244 KB |
Output is correct |
53 |
Correct |
526 ms |
14432 KB |
Output is correct |
54 |
Correct |
779 ms |
13356 KB |
Output is correct |
55 |
Correct |
703 ms |
13264 KB |
Output is correct |
56 |
Correct |
773 ms |
13244 KB |
Output is correct |
57 |
Correct |
644 ms |
13172 KB |
Output is correct |
58 |
Correct |
767 ms |
14512 KB |
Output is correct |
59 |
Correct |
690 ms |
14672 KB |
Output is correct |
60 |
Correct |
614 ms |
14660 KB |
Output is correct |
61 |
Correct |
714 ms |
13416 KB |
Output is correct |
62 |
Correct |
925 ms |
13140 KB |
Output is correct |
63 |
Correct |
813 ms |
13156 KB |
Output is correct |
64 |
Correct |
799 ms |
13264 KB |
Output is correct |
65 |
Correct |
349 ms |
13024 KB |
Output is correct |
66 |
Correct |
912 ms |
13252 KB |
Output is correct |
67 |
Correct |
685 ms |
13264 KB |
Output is correct |
68 |
Correct |
668 ms |
13264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12752 KB |
Output is correct |
2 |
Correct |
6 ms |
12752 KB |
Output is correct |
3 |
Correct |
7 ms |
12880 KB |
Output is correct |
4 |
Correct |
5 ms |
12872 KB |
Output is correct |
5 |
Correct |
5 ms |
12916 KB |
Output is correct |
6 |
Correct |
6 ms |
12920 KB |
Output is correct |
7 |
Correct |
8 ms |
12788 KB |
Output is correct |
8 |
Correct |
5 ms |
12880 KB |
Output is correct |
9 |
Correct |
7 ms |
12972 KB |
Output is correct |
10 |
Correct |
5 ms |
12752 KB |
Output is correct |
11 |
Correct |
5 ms |
12880 KB |
Output is correct |
12 |
Correct |
6 ms |
12844 KB |
Output is correct |
13 |
Correct |
6 ms |
12892 KB |
Output is correct |
14 |
Correct |
6 ms |
12880 KB |
Output is correct |
15 |
Correct |
6 ms |
12880 KB |
Output is correct |
16 |
Correct |
7 ms |
12832 KB |
Output is correct |
17 |
Correct |
6 ms |
12880 KB |
Output is correct |
18 |
Correct |
6 ms |
13092 KB |
Output is correct |
19 |
Correct |
6 ms |
13148 KB |
Output is correct |
20 |
Correct |
6 ms |
12904 KB |
Output is correct |
21 |
Correct |
6 ms |
12880 KB |
Output is correct |
22 |
Correct |
6 ms |
12776 KB |
Output is correct |
23 |
Correct |
6 ms |
12880 KB |
Output is correct |
24 |
Correct |
6 ms |
12948 KB |
Output is correct |
25 |
Correct |
6 ms |
12896 KB |
Output is correct |
26 |
Correct |
6 ms |
12880 KB |
Output is correct |
27 |
Correct |
7 ms |
12880 KB |
Output is correct |
28 |
Correct |
6 ms |
12928 KB |
Output is correct |
29 |
Correct |
5 ms |
12880 KB |
Output is correct |
30 |
Correct |
5 ms |
12808 KB |
Output is correct |
31 |
Correct |
6 ms |
13136 KB |
Output is correct |
32 |
Correct |
6 ms |
12880 KB |
Output is correct |
33 |
Correct |
6 ms |
12880 KB |
Output is correct |
34 |
Correct |
6 ms |
12880 KB |
Output is correct |
35 |
Correct |
7 ms |
12880 KB |
Output is correct |
36 |
Correct |
6 ms |
13136 KB |
Output is correct |
37 |
Correct |
6 ms |
13136 KB |
Output is correct |
38 |
Correct |
6 ms |
13136 KB |
Output is correct |
39 |
Correct |
6 ms |
12880 KB |
Output is correct |
40 |
Correct |
7 ms |
12880 KB |
Output is correct |
41 |
Correct |
6 ms |
12788 KB |
Output is correct |
42 |
Correct |
6 ms |
12880 KB |
Output is correct |
43 |
Correct |
473 ms |
16208 KB |
Output is correct |
44 |
Correct |
888 ms |
19712 KB |
Output is correct |
45 |
Correct |
648 ms |
19744 KB |
Output is correct |
46 |
Correct |
679 ms |
19764 KB |
Output is correct |
47 |
Correct |
659 ms |
16208 KB |
Output is correct |
48 |
Correct |
969 ms |
19672 KB |
Output is correct |
49 |
Correct |
846 ms |
19656 KB |
Output is correct |
50 |
Correct |
790 ms |
19656 KB |
Output is correct |
51 |
Correct |
372 ms |
13036 KB |
Output is correct |
52 |
Correct |
790 ms |
13264 KB |
Output is correct |
53 |
Correct |
790 ms |
13264 KB |
Output is correct |
54 |
Correct |
864 ms |
13264 KB |
Output is correct |
55 |
Correct |
781 ms |
23192 KB |
Output is correct |
56 |
Correct |
971 ms |
23320 KB |
Output is correct |
57 |
Correct |
916 ms |
23336 KB |
Output is correct |
58 |
Correct |
616 ms |
23336 KB |
Output is correct |
59 |
Correct |
965 ms |
46760 KB |
Output is correct |
60 |
Correct |
812 ms |
46760 KB |
Output is correct |
61 |
Correct |
562 ms |
13136 KB |
Output is correct |
62 |
Correct |
764 ms |
13256 KB |
Output is correct |
63 |
Correct |
863 ms |
13148 KB |
Output is correct |
64 |
Correct |
683 ms |
13360 KB |
Output is correct |
65 |
Correct |
831 ms |
13360 KB |
Output is correct |
66 |
Correct |
842 ms |
13364 KB |
Output is correct |
67 |
Correct |
735 ms |
13356 KB |
Output is correct |
68 |
Correct |
749 ms |
13368 KB |
Output is correct |
69 |
Correct |
719 ms |
13248 KB |
Output is correct |
70 |
Correct |
659 ms |
13244 KB |
Output is correct |
71 |
Correct |
526 ms |
14432 KB |
Output is correct |
72 |
Correct |
779 ms |
13356 KB |
Output is correct |
73 |
Correct |
703 ms |
13264 KB |
Output is correct |
74 |
Correct |
773 ms |
13244 KB |
Output is correct |
75 |
Correct |
644 ms |
13172 KB |
Output is correct |
76 |
Correct |
767 ms |
14512 KB |
Output is correct |
77 |
Correct |
690 ms |
14672 KB |
Output is correct |
78 |
Correct |
614 ms |
14660 KB |
Output is correct |
79 |
Correct |
714 ms |
13416 KB |
Output is correct |
80 |
Correct |
925 ms |
13140 KB |
Output is correct |
81 |
Correct |
813 ms |
13156 KB |
Output is correct |
82 |
Correct |
799 ms |
13264 KB |
Output is correct |
83 |
Correct |
349 ms |
13024 KB |
Output is correct |
84 |
Correct |
912 ms |
13252 KB |
Output is correct |
85 |
Correct |
685 ms |
13264 KB |
Output is correct |
86 |
Correct |
668 ms |
13264 KB |
Output is correct |
87 |
Correct |
6 ms |
12820 KB |
Output is correct |
88 |
Correct |
514 ms |
22012 KB |
Output is correct |
89 |
Correct |
799 ms |
19144 KB |
Output is correct |
90 |
Correct |
853 ms |
19224 KB |
Output is correct |
91 |
Correct |
696 ms |
23448 KB |
Output is correct |
92 |
Correct |
629 ms |
23464 KB |
Output is correct |
93 |
Correct |
1076 ms |
23464 KB |
Output is correct |
94 |
Correct |
850 ms |
23460 KB |
Output is correct |
95 |
Correct |
780 ms |
23464 KB |
Output is correct |
96 |
Correct |
782 ms |
19960 KB |
Output is correct |
97 |
Correct |
756 ms |
19992 KB |
Output is correct |
98 |
Correct |
643 ms |
44872 KB |
Output is correct |
99 |
Correct |
852 ms |
23300 KB |
Output is correct |
100 |
Correct |
781 ms |
21456 KB |
Output is correct |
101 |
Correct |
715 ms |
20916 KB |
Output is correct |
102 |
Correct |
848 ms |
19928 KB |
Output is correct |
103 |
Correct |
812 ms |
46868 KB |
Output is correct |
104 |
Correct |
856 ms |
49572 KB |
Output is correct |
105 |
Correct |
683 ms |
49504 KB |
Output is correct |
106 |
Correct |
756 ms |
22856 KB |
Output is correct |
107 |
Correct |
764 ms |
18888 KB |
Output is correct |
108 |
Correct |
723 ms |
19192 KB |
Output is correct |
109 |
Correct |
819 ms |
20176 KB |
Output is correct |