#include "circuit.h"
#include <iostream>
#include <vector>
using namespace std;
const int MAXN=1e5+10;
const long long mod=1e9+2022;
vector <int> v[MAXN];
vector <long long> pref[MAXN], suf[MAXN];
long long mult[MAXN];
long long tree[4*MAXN][2], val[MAXN];
int lazy[4*MAXN];
int start[MAXN];
int n, m;
void precomp(int x) {
if (v[x].empty()) {
mult[x]=1;
return;
}
pref[x].resize(v[x].size());
suf[x].resize(v[x].size());
mult[x]=v[x].size();
for (int i=0;i<v[x].size();i++) {
precomp(v[x][i]);
if (i==0) pref[x][i]=mult[v[x][i]];
else pref[x][i]=(pref[x][i-1]*mult[v[x][i]])%mod;
mult[x]=(mult[x]*mult[v[x][i]])%mod;
}
for (int i=v[x].size()-1;i>=0;i--) {
if (i==v[x].size()-1) suf[x][i]=mult[v[x][i]];
else suf[x][i]=(suf[x][i+1]*mult[v[x][i]])%mod;
}
}
void dfs(int x, long long ways) {
if (x>=n) {
val[x-n]=ways;
//cout << x << ' ' << ways << endl;
return;
}
long long s;
for (int i=0;i<v[x].size();i++) {
s=1;
if (i>0) s*=pref[x][i-1];
if (i<v[x].size()-1) s*=suf[x][i+1];
s%=mod;
dfs(v[x][i],(ways*s)%mod);
}
}
void build_tree(int node, int l, int r) {
if (l==r) {
tree[node][0]=tree[node][1]=0;
tree[node][start[l]]=val[l];
return;
}
int mid=(l+r)/2;
build_tree(node*2,l,mid);
build_tree(node*2+1,mid+1,r);
tree[node][0]=(tree[node*2][0]+tree[node*2+1][0])%mod;
tree[node][1]=(tree[node*2][1]+tree[node*2+1][1])%mod;
}
void push_lazy(int node, int l, int r) {
if (l!=r && lazy[node]%2==1) {
lazy[node*2]++;
lazy[node*2+1]++;
swap(tree[node*2][0],tree[node*2][1]);
swap(tree[node*2+1][0],tree[node*2+1][1]);
}
lazy[node]=0;
}
void update(int node, int l, int r, int ql, int qr) {
if (ql>r || qr<l) return;
push_lazy(node,l,r);
if (l>=ql && r<=qr) {
swap(tree[node][0],tree[node][1]);
lazy[node]++;
return;
}
int mid=(l+r)/2;
update(node*2,l,mid,ql,qr);
update(node*2+1,mid+1,r,ql,qr);
tree[node][0]=(tree[node*2][0]+tree[node*2+1][0])%mod;
tree[node][1]=(tree[node*2][1]+tree[node*2+1][1])%mod;
}
void init(int N, int M, vector <int> p, vector <int> a) {
n=N;
m=M;
for (int i=1;i<n+m;i++) v[p[i]].push_back(i);
for (int i=0;i<m;i++) start[i]=a[i];
precomp(0);
dfs(0,1);
build_tree(1,0,m-1);
}
int count_ways(int l, int r) {
l-=n; r-=n;
update(1,0,m-1,l,r);
return tree[1][1];
}
Compilation message
circuit.cpp: In function 'void precomp(int)':
circuit.cpp:22:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i=0;i<v[x].size();i++) {
| ~^~~~~~~~~~~~
circuit.cpp:29:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | if (i==v[x].size()-1) suf[x][i]=mult[v[x][i]];
| ~^~~~~~~~~~~~~~~
circuit.cpp: In function 'void dfs(int, long long int)':
circuit.cpp:40:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i=0;i<v[x].size();i++) {
| ~^~~~~~~~~~~~
circuit.cpp:43:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | if (i<v[x].size()-1) s*=suf[x][i+1];
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12888 KB |
Output is correct |
3 |
Correct |
3 ms |
12888 KB |
Output is correct |
4 |
Correct |
2 ms |
13028 KB |
Output is correct |
5 |
Correct |
3 ms |
12888 KB |
Output is correct |
6 |
Correct |
2 ms |
12888 KB |
Output is correct |
7 |
Correct |
2 ms |
12888 KB |
Output is correct |
8 |
Correct |
3 ms |
12888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12888 KB |
Output is correct |
3 |
Correct |
2 ms |
12888 KB |
Output is correct |
4 |
Correct |
2 ms |
12884 KB |
Output is correct |
5 |
Correct |
2 ms |
12888 KB |
Output is correct |
6 |
Correct |
3 ms |
12888 KB |
Output is correct |
7 |
Correct |
3 ms |
12888 KB |
Output is correct |
8 |
Correct |
3 ms |
12888 KB |
Output is correct |
9 |
Correct |
3 ms |
12888 KB |
Output is correct |
10 |
Correct |
3 ms |
13140 KB |
Output is correct |
11 |
Correct |
3 ms |
13144 KB |
Output is correct |
12 |
Correct |
2 ms |
12888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12888 KB |
Output is correct |
3 |
Correct |
3 ms |
12888 KB |
Output is correct |
4 |
Correct |
2 ms |
13028 KB |
Output is correct |
5 |
Correct |
3 ms |
12888 KB |
Output is correct |
6 |
Correct |
2 ms |
12888 KB |
Output is correct |
7 |
Correct |
2 ms |
12888 KB |
Output is correct |
8 |
Correct |
3 ms |
12888 KB |
Output is correct |
9 |
Correct |
3 ms |
12888 KB |
Output is correct |
10 |
Correct |
2 ms |
12888 KB |
Output is correct |
11 |
Correct |
2 ms |
12888 KB |
Output is correct |
12 |
Correct |
2 ms |
12884 KB |
Output is correct |
13 |
Correct |
2 ms |
12888 KB |
Output is correct |
14 |
Correct |
3 ms |
12888 KB |
Output is correct |
15 |
Correct |
3 ms |
12888 KB |
Output is correct |
16 |
Correct |
3 ms |
12888 KB |
Output is correct |
17 |
Correct |
3 ms |
12888 KB |
Output is correct |
18 |
Correct |
3 ms |
13140 KB |
Output is correct |
19 |
Correct |
3 ms |
13144 KB |
Output is correct |
20 |
Correct |
2 ms |
12888 KB |
Output is correct |
21 |
Correct |
3 ms |
12888 KB |
Output is correct |
22 |
Correct |
4 ms |
12888 KB |
Output is correct |
23 |
Correct |
3 ms |
12888 KB |
Output is correct |
24 |
Correct |
3 ms |
12888 KB |
Output is correct |
25 |
Correct |
3 ms |
12888 KB |
Output is correct |
26 |
Correct |
3 ms |
12888 KB |
Output is correct |
27 |
Correct |
3 ms |
12888 KB |
Output is correct |
28 |
Correct |
3 ms |
12888 KB |
Output is correct |
29 |
Correct |
2 ms |
12888 KB |
Output is correct |
30 |
Correct |
2 ms |
12888 KB |
Output is correct |
31 |
Correct |
2 ms |
12996 KB |
Output is correct |
32 |
Correct |
2 ms |
12888 KB |
Output is correct |
33 |
Correct |
3 ms |
12888 KB |
Output is correct |
34 |
Correct |
2 ms |
12888 KB |
Output is correct |
35 |
Correct |
2 ms |
12888 KB |
Output is correct |
36 |
Correct |
3 ms |
13144 KB |
Output is correct |
37 |
Correct |
3 ms |
12976 KB |
Output is correct |
38 |
Correct |
3 ms |
12988 KB |
Output is correct |
39 |
Correct |
3 ms |
12888 KB |
Output is correct |
40 |
Correct |
3 ms |
12888 KB |
Output is correct |
41 |
Correct |
3 ms |
12888 KB |
Output is correct |
42 |
Correct |
2 ms |
12888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
443 ms |
16728 KB |
Output is correct |
2 |
Runtime error |
43 ms |
33868 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
443 ms |
16728 KB |
Output is correct |
2 |
Runtime error |
43 ms |
33868 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12888 KB |
Output is correct |
3 |
Correct |
2 ms |
12888 KB |
Output is correct |
4 |
Correct |
2 ms |
12884 KB |
Output is correct |
5 |
Correct |
2 ms |
12888 KB |
Output is correct |
6 |
Correct |
3 ms |
12888 KB |
Output is correct |
7 |
Correct |
3 ms |
12888 KB |
Output is correct |
8 |
Correct |
3 ms |
12888 KB |
Output is correct |
9 |
Correct |
3 ms |
12888 KB |
Output is correct |
10 |
Correct |
3 ms |
13140 KB |
Output is correct |
11 |
Correct |
3 ms |
13144 KB |
Output is correct |
12 |
Correct |
2 ms |
12888 KB |
Output is correct |
13 |
Correct |
443 ms |
16728 KB |
Output is correct |
14 |
Runtime error |
43 ms |
33868 KB |
Execution killed with signal 6 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12888 KB |
Output is correct |
3 |
Correct |
3 ms |
12888 KB |
Output is correct |
4 |
Correct |
2 ms |
13028 KB |
Output is correct |
5 |
Correct |
3 ms |
12888 KB |
Output is correct |
6 |
Correct |
2 ms |
12888 KB |
Output is correct |
7 |
Correct |
2 ms |
12888 KB |
Output is correct |
8 |
Correct |
3 ms |
12888 KB |
Output is correct |
9 |
Correct |
3 ms |
12888 KB |
Output is correct |
10 |
Correct |
2 ms |
12888 KB |
Output is correct |
11 |
Correct |
2 ms |
12888 KB |
Output is correct |
12 |
Correct |
2 ms |
12884 KB |
Output is correct |
13 |
Correct |
2 ms |
12888 KB |
Output is correct |
14 |
Correct |
3 ms |
12888 KB |
Output is correct |
15 |
Correct |
3 ms |
12888 KB |
Output is correct |
16 |
Correct |
3 ms |
12888 KB |
Output is correct |
17 |
Correct |
3 ms |
12888 KB |
Output is correct |
18 |
Correct |
3 ms |
13140 KB |
Output is correct |
19 |
Correct |
3 ms |
13144 KB |
Output is correct |
20 |
Correct |
2 ms |
12888 KB |
Output is correct |
21 |
Correct |
3 ms |
12888 KB |
Output is correct |
22 |
Correct |
4 ms |
12888 KB |
Output is correct |
23 |
Correct |
3 ms |
12888 KB |
Output is correct |
24 |
Correct |
3 ms |
12888 KB |
Output is correct |
25 |
Correct |
3 ms |
12888 KB |
Output is correct |
26 |
Correct |
3 ms |
12888 KB |
Output is correct |
27 |
Correct |
3 ms |
12888 KB |
Output is correct |
28 |
Correct |
3 ms |
12888 KB |
Output is correct |
29 |
Correct |
2 ms |
12888 KB |
Output is correct |
30 |
Correct |
2 ms |
12888 KB |
Output is correct |
31 |
Correct |
2 ms |
12996 KB |
Output is correct |
32 |
Correct |
2 ms |
12888 KB |
Output is correct |
33 |
Correct |
3 ms |
12888 KB |
Output is correct |
34 |
Correct |
2 ms |
12888 KB |
Output is correct |
35 |
Correct |
2 ms |
12888 KB |
Output is correct |
36 |
Correct |
3 ms |
13144 KB |
Output is correct |
37 |
Correct |
3 ms |
12976 KB |
Output is correct |
38 |
Correct |
3 ms |
12988 KB |
Output is correct |
39 |
Correct |
3 ms |
12888 KB |
Output is correct |
40 |
Correct |
3 ms |
12888 KB |
Output is correct |
41 |
Correct |
3 ms |
12888 KB |
Output is correct |
42 |
Correct |
2 ms |
12888 KB |
Output is correct |
43 |
Correct |
380 ms |
13144 KB |
Output is correct |
44 |
Correct |
580 ms |
13400 KB |
Output is correct |
45 |
Correct |
519 ms |
13144 KB |
Output is correct |
46 |
Correct |
507 ms |
13400 KB |
Output is correct |
47 |
Correct |
630 ms |
13400 KB |
Output is correct |
48 |
Correct |
522 ms |
13400 KB |
Output is correct |
49 |
Correct |
568 ms |
13652 KB |
Output is correct |
50 |
Correct |
549 ms |
13400 KB |
Output is correct |
51 |
Correct |
509 ms |
13192 KB |
Output is correct |
52 |
Correct |
537 ms |
13196 KB |
Output is correct |
53 |
Correct |
521 ms |
13852 KB |
Output is correct |
54 |
Correct |
607 ms |
13400 KB |
Output is correct |
55 |
Correct |
592 ms |
13144 KB |
Output is correct |
56 |
Correct |
561 ms |
13144 KB |
Output is correct |
57 |
Correct |
583 ms |
13144 KB |
Output is correct |
58 |
Correct |
559 ms |
13912 KB |
Output is correct |
59 |
Correct |
557 ms |
13912 KB |
Output is correct |
60 |
Correct |
543 ms |
13912 KB |
Output is correct |
61 |
Correct |
604 ms |
13400 KB |
Output is correct |
62 |
Correct |
576 ms |
13144 KB |
Output is correct |
63 |
Correct |
589 ms |
13144 KB |
Output is correct |
64 |
Correct |
595 ms |
13144 KB |
Output is correct |
65 |
Correct |
261 ms |
13144 KB |
Output is correct |
66 |
Correct |
577 ms |
13400 KB |
Output is correct |
67 |
Correct |
619 ms |
13400 KB |
Output is correct |
68 |
Correct |
524 ms |
13400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12888 KB |
Output is correct |
3 |
Correct |
3 ms |
12888 KB |
Output is correct |
4 |
Correct |
2 ms |
13028 KB |
Output is correct |
5 |
Correct |
3 ms |
12888 KB |
Output is correct |
6 |
Correct |
2 ms |
12888 KB |
Output is correct |
7 |
Correct |
2 ms |
12888 KB |
Output is correct |
8 |
Correct |
3 ms |
12888 KB |
Output is correct |
9 |
Correct |
3 ms |
12888 KB |
Output is correct |
10 |
Correct |
2 ms |
12888 KB |
Output is correct |
11 |
Correct |
2 ms |
12888 KB |
Output is correct |
12 |
Correct |
2 ms |
12884 KB |
Output is correct |
13 |
Correct |
2 ms |
12888 KB |
Output is correct |
14 |
Correct |
3 ms |
12888 KB |
Output is correct |
15 |
Correct |
3 ms |
12888 KB |
Output is correct |
16 |
Correct |
3 ms |
12888 KB |
Output is correct |
17 |
Correct |
3 ms |
12888 KB |
Output is correct |
18 |
Correct |
3 ms |
13140 KB |
Output is correct |
19 |
Correct |
3 ms |
13144 KB |
Output is correct |
20 |
Correct |
2 ms |
12888 KB |
Output is correct |
21 |
Correct |
3 ms |
12888 KB |
Output is correct |
22 |
Correct |
4 ms |
12888 KB |
Output is correct |
23 |
Correct |
3 ms |
12888 KB |
Output is correct |
24 |
Correct |
3 ms |
12888 KB |
Output is correct |
25 |
Correct |
3 ms |
12888 KB |
Output is correct |
26 |
Correct |
3 ms |
12888 KB |
Output is correct |
27 |
Correct |
3 ms |
12888 KB |
Output is correct |
28 |
Correct |
3 ms |
12888 KB |
Output is correct |
29 |
Correct |
2 ms |
12888 KB |
Output is correct |
30 |
Correct |
2 ms |
12888 KB |
Output is correct |
31 |
Correct |
2 ms |
12996 KB |
Output is correct |
32 |
Correct |
2 ms |
12888 KB |
Output is correct |
33 |
Correct |
3 ms |
12888 KB |
Output is correct |
34 |
Correct |
2 ms |
12888 KB |
Output is correct |
35 |
Correct |
2 ms |
12888 KB |
Output is correct |
36 |
Correct |
3 ms |
13144 KB |
Output is correct |
37 |
Correct |
3 ms |
12976 KB |
Output is correct |
38 |
Correct |
3 ms |
12988 KB |
Output is correct |
39 |
Correct |
3 ms |
12888 KB |
Output is correct |
40 |
Correct |
3 ms |
12888 KB |
Output is correct |
41 |
Correct |
3 ms |
12888 KB |
Output is correct |
42 |
Correct |
2 ms |
12888 KB |
Output is correct |
43 |
Correct |
443 ms |
16728 KB |
Output is correct |
44 |
Runtime error |
43 ms |
33868 KB |
Execution killed with signal 6 |
45 |
Halted |
0 ms |
0 KB |
- |