#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll NP=2e5+100,mod=1000002022;
ll n,m,p[NP],a[NP];
vector<ll> ma[NP];
const int KP=100;
vector<ll> knapsack[NP];
ll dp[NP][2],lazy[NP],dp1[NP][2],lc[NP],rc[NP],mi[NP],mx[NP];
void compute(int x=0,bool continue_=1)
{
dp[x][0]=dp[x][1]=0;
dp1[x][0]=dp1[x][1]=0;
if(n<=x){
dp[x][a[x-n]]=1;
dp1[x][1-a[x-n]]=1;
mx[x]=x-n;
mi[x]=x-n;
return;
}
int sz=ma[x].size();
if(knapsack[x].size()==0)knapsack[x].resize(sz+2,0);
for(int j=0;j<=sz;j++)knapsack[x][j]=0;
knapsack[x][0]=1;
mx[x]=-1e9;
mi[x]=1e9;
for(auto y:ma[x])
{
if(continue_)
compute(y);
mx[x]=max(mx[x],mx[y]);
mi[x]=min(mi[x],mi[y]);
for(int j=sz;j>=0;j--)
{
if(j==0)
{
knapsack[x][j]=(knapsack[x][j]*dp[y][0])%mod;
}
else
{
knapsack[x][j]=(knapsack[x][j]*dp[y][0])%mod;
knapsack[x][j]=(knapsack[x][j] + ((knapsack[x][j-1]*dp[y][1])%mod))%mod;
}
}
}
int sm=0;
for(int j=sz;j>=1;j--)
{
sm = ( sm + knapsack[x][j] ) % mod;
dp[x][1] = ( dp[x][1] + sm ) % mod;
}
sm=knapsack[x][0];
for(int j=1;j<=sz;j++)
{
dp[x][0] = ( dp[x][0] + sm ) % mod;
sm = ( sm + knapsack[x][j] ) % mod;
}
// Dp 1
for(int j=0;j<=sz;j++)knapsack[x][j]=0;
knapsack[x][0]=1;
for(auto y:ma[x])
{
for(int j=sz;j>=0;j--)
{
if(j==0)
{
knapsack[x][j]=(knapsack[x][j]*dp1[y][0])%mod;
}
else
{
knapsack[x][j]=(knapsack[x][j]*dp1[y][0])%mod;
knapsack[x][j]=(knapsack[x][j] + ((knapsack[x][j-1]*dp1[y][1])%mod))%mod;
}
}
}
sm=0;
for(int j=sz;j>=1;j--)
{
sm = ( sm + knapsack[x][j] ) % mod;
dp1[x][1] = ( dp1[x][1] + sm ) % mod;
}
sm=knapsack[x][0];
for(int j=1;j<=sz;j++)
{
dp1[x][0] = ( dp1[x][0] + sm ) % mod;
sm = ( sm + knapsack[x][j] ) % mod;
}
}
void push(int&v)
{
if(!lazy[v])return;
for(ll&y:ma[v])
lazy[y]^=lazy[v];
swap(dp[v][0],dp1[v][0]);
swap(dp[v][1],dp1[v][1]);
lazy[v]=0;
}
void update(int v,int&ql,int&qr)
{
// // cout<<"WAnt to update "<<ql<<' '<<qr<<" At "<<v<<' '<<mi[v]<<' '<<mx[v]<<endl;
push(v);
if(qr<mi[v] or mx[v]<ql) // nothing changes here
return;
if(ql<=mi[v] and mx[v]<=qr) // whole subtree to be updated // just swap the values else
{
lazy[v]^=1;
push(v);
return;
}
int x=v;
dp[x][0]=dp[x][1]=0;
dp1[x][0]=dp1[x][1]=0;
int sz=ma[x].size();
for(int j=0;j<=sz;j++)knapsack[x][j]=0;
knapsack[x][0]=1;
for(ll&y:ma[x])
{
update(y,ql,qr);
for(int j=sz;j>=0;j--)
{
if(j==0)
{
knapsack[x][j]=(knapsack[x][j]*dp[y][0])%mod;
}
else
{
knapsack[x][j]=(knapsack[x][j]*dp[y][0])%mod;
knapsack[x][j]=(knapsack[x][j] + ((knapsack[x][j-1]*dp[y][1])%mod))%mod;
}
}
}
int sm=0;
for(int j=sz;j>=1;j--)
{
sm = ( sm + knapsack[x][j] ) % mod;
dp[x][1] = ( dp[x][1] + sm ) % mod;
}
sm=knapsack[x][0];
for(int j=1;j<=sz;j++)
{
dp[x][0] = ( dp[x][0] + sm ) % mod;
sm = ( sm + knapsack[x][j] ) % mod;
}
// Dp 1
for(int j=0;j<=sz;j++)knapsack[x][j]=0;
knapsack[x][0]=1;
for(auto y:ma[x])
{
for(int j=sz;j>=0;j--)
{
if(j==0)
{
knapsack[x][j]=(knapsack[x][j]*dp1[y][0])%mod;
}
else
{
knapsack[x][j]=(knapsack[x][j]*dp1[y][0])%mod;
knapsack[x][j]=(knapsack[x][j] + ((knapsack[x][j-1]*dp1[y][1])%mod))%mod;
}
}
}
sm=0;
for(int j=sz;j>=1;j--)
{
sm = ( sm + knapsack[x][j] ) % mod;
dp1[x][1] = ( dp1[x][1] + sm ) % mod;
}
sm=knapsack[x][0];
for(int j=1;j<=sz;j++)
{
dp1[x][0] = ( dp1[x][0] + sm ) % mod;
sm = ( sm + knapsack[x][j] ) % mod;
}
}
void init(int N, int M, std::vector<int> P, std::vector<int> A)
{
n=N,m=M;
for(ll i=0;i<n+m;i++)
{
p[i]=P[i];
if(p[i]!=-1)ma[p[i]].push_back(i);
}
for(ll j=0;j<m;j++)a[j]=A[j];
compute();
}
int count_ways(int L, int R)
{
L-=n;
R-=n;
update(0,L,R);
return (int)dp[0][1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12632 KB |
Output is correct |
2 |
Correct |
4 ms |
9816 KB |
Output is correct |
3 |
Correct |
46 ms |
9816 KB |
Output is correct |
4 |
Correct |
48 ms |
9816 KB |
Output is correct |
5 |
Correct |
48 ms |
9816 KB |
Output is correct |
6 |
Correct |
49 ms |
9816 KB |
Output is correct |
7 |
Correct |
46 ms |
12892 KB |
Output is correct |
8 |
Correct |
48 ms |
18776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
6 ms |
9816 KB |
Output is correct |
3 |
Correct |
4 ms |
18776 KB |
Output is correct |
4 |
Correct |
4 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
9976 KB |
Output is correct |
6 |
Correct |
5 ms |
9816 KB |
Output is correct |
7 |
Correct |
8 ms |
10072 KB |
Output is correct |
8 |
Correct |
3 ms |
19032 KB |
Output is correct |
9 |
Correct |
3 ms |
19032 KB |
Output is correct |
10 |
Correct |
5 ms |
10072 KB |
Output is correct |
11 |
Correct |
6 ms |
10072 KB |
Output is correct |
12 |
Correct |
7 ms |
10072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12632 KB |
Output is correct |
2 |
Correct |
4 ms |
9816 KB |
Output is correct |
3 |
Correct |
46 ms |
9816 KB |
Output is correct |
4 |
Correct |
48 ms |
9816 KB |
Output is correct |
5 |
Correct |
48 ms |
9816 KB |
Output is correct |
6 |
Correct |
49 ms |
9816 KB |
Output is correct |
7 |
Correct |
46 ms |
12892 KB |
Output is correct |
8 |
Correct |
48 ms |
18776 KB |
Output is correct |
9 |
Correct |
5 ms |
9816 KB |
Output is correct |
10 |
Correct |
6 ms |
9816 KB |
Output is correct |
11 |
Correct |
4 ms |
18776 KB |
Output is correct |
12 |
Correct |
4 ms |
9816 KB |
Output is correct |
13 |
Correct |
4 ms |
9976 KB |
Output is correct |
14 |
Correct |
5 ms |
9816 KB |
Output is correct |
15 |
Correct |
8 ms |
10072 KB |
Output is correct |
16 |
Correct |
3 ms |
19032 KB |
Output is correct |
17 |
Correct |
3 ms |
19032 KB |
Output is correct |
18 |
Correct |
5 ms |
10072 KB |
Output is correct |
19 |
Correct |
6 ms |
10072 KB |
Output is correct |
20 |
Correct |
7 ms |
10072 KB |
Output is correct |
21 |
Correct |
4 ms |
14936 KB |
Output is correct |
22 |
Correct |
4 ms |
11096 KB |
Output is correct |
23 |
Correct |
4 ms |
11096 KB |
Output is correct |
24 |
Correct |
4 ms |
11352 KB |
Output is correct |
25 |
Correct |
3 ms |
19032 KB |
Output is correct |
26 |
Correct |
3 ms |
12888 KB |
Output is correct |
27 |
Correct |
4 ms |
11352 KB |
Output is correct |
28 |
Correct |
5 ms |
11348 KB |
Output is correct |
29 |
Correct |
47 ms |
14676 KB |
Output is correct |
30 |
Correct |
47 ms |
12888 KB |
Output is correct |
31 |
Correct |
3 ms |
19032 KB |
Output is correct |
32 |
Correct |
4 ms |
14936 KB |
Output is correct |
33 |
Correct |
5 ms |
9816 KB |
Output is correct |
34 |
Correct |
3 ms |
18776 KB |
Output is correct |
35 |
Correct |
10 ms |
18776 KB |
Output is correct |
36 |
Correct |
6 ms |
10192 KB |
Output is correct |
37 |
Correct |
50 ms |
10072 KB |
Output is correct |
38 |
Correct |
67 ms |
13140 KB |
Output is correct |
39 |
Correct |
3 ms |
18776 KB |
Output is correct |
40 |
Correct |
4 ms |
12888 KB |
Output is correct |
41 |
Correct |
4 ms |
9816 KB |
Output is correct |
42 |
Correct |
5 ms |
9984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
672 ms |
24860 KB |
Output is correct |
2 |
Correct |
1012 ms |
26960 KB |
Output is correct |
3 |
Correct |
936 ms |
30808 KB |
Output is correct |
4 |
Correct |
995 ms |
25940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
672 ms |
24860 KB |
Output is correct |
2 |
Correct |
1012 ms |
26960 KB |
Output is correct |
3 |
Correct |
936 ms |
30808 KB |
Output is correct |
4 |
Correct |
995 ms |
25940 KB |
Output is correct |
5 |
Correct |
879 ms |
25120 KB |
Output is correct |
6 |
Correct |
1149 ms |
31144 KB |
Output is correct |
7 |
Correct |
1235 ms |
24488 KB |
Output is correct |
8 |
Correct |
912 ms |
30664 KB |
Output is correct |
9 |
Correct |
425 ms |
19032 KB |
Output is correct |
10 |
Correct |
857 ms |
21336 KB |
Output is correct |
11 |
Correct |
893 ms |
21336 KB |
Output is correct |
12 |
Correct |
819 ms |
21336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
6 ms |
9816 KB |
Output is correct |
3 |
Correct |
4 ms |
18776 KB |
Output is correct |
4 |
Correct |
4 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
9976 KB |
Output is correct |
6 |
Correct |
5 ms |
9816 KB |
Output is correct |
7 |
Correct |
8 ms |
10072 KB |
Output is correct |
8 |
Correct |
3 ms |
19032 KB |
Output is correct |
9 |
Correct |
3 ms |
19032 KB |
Output is correct |
10 |
Correct |
5 ms |
10072 KB |
Output is correct |
11 |
Correct |
6 ms |
10072 KB |
Output is correct |
12 |
Correct |
7 ms |
10072 KB |
Output is correct |
13 |
Correct |
672 ms |
24860 KB |
Output is correct |
14 |
Correct |
1012 ms |
26960 KB |
Output is correct |
15 |
Correct |
936 ms |
30808 KB |
Output is correct |
16 |
Correct |
995 ms |
25940 KB |
Output is correct |
17 |
Correct |
879 ms |
25120 KB |
Output is correct |
18 |
Correct |
1149 ms |
31144 KB |
Output is correct |
19 |
Correct |
1235 ms |
24488 KB |
Output is correct |
20 |
Correct |
912 ms |
30664 KB |
Output is correct |
21 |
Correct |
425 ms |
19032 KB |
Output is correct |
22 |
Correct |
857 ms |
21336 KB |
Output is correct |
23 |
Correct |
893 ms |
21336 KB |
Output is correct |
24 |
Correct |
819 ms |
21336 KB |
Output is correct |
25 |
Execution timed out |
3096 ms |
36580 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12632 KB |
Output is correct |
2 |
Correct |
4 ms |
9816 KB |
Output is correct |
3 |
Correct |
46 ms |
9816 KB |
Output is correct |
4 |
Correct |
48 ms |
9816 KB |
Output is correct |
5 |
Correct |
48 ms |
9816 KB |
Output is correct |
6 |
Correct |
49 ms |
9816 KB |
Output is correct |
7 |
Correct |
46 ms |
12892 KB |
Output is correct |
8 |
Correct |
48 ms |
18776 KB |
Output is correct |
9 |
Correct |
5 ms |
9816 KB |
Output is correct |
10 |
Correct |
6 ms |
9816 KB |
Output is correct |
11 |
Correct |
4 ms |
18776 KB |
Output is correct |
12 |
Correct |
4 ms |
9816 KB |
Output is correct |
13 |
Correct |
4 ms |
9976 KB |
Output is correct |
14 |
Correct |
5 ms |
9816 KB |
Output is correct |
15 |
Correct |
8 ms |
10072 KB |
Output is correct |
16 |
Correct |
3 ms |
19032 KB |
Output is correct |
17 |
Correct |
3 ms |
19032 KB |
Output is correct |
18 |
Correct |
5 ms |
10072 KB |
Output is correct |
19 |
Correct |
6 ms |
10072 KB |
Output is correct |
20 |
Correct |
7 ms |
10072 KB |
Output is correct |
21 |
Correct |
4 ms |
14936 KB |
Output is correct |
22 |
Correct |
4 ms |
11096 KB |
Output is correct |
23 |
Correct |
4 ms |
11096 KB |
Output is correct |
24 |
Correct |
4 ms |
11352 KB |
Output is correct |
25 |
Correct |
3 ms |
19032 KB |
Output is correct |
26 |
Correct |
3 ms |
12888 KB |
Output is correct |
27 |
Correct |
4 ms |
11352 KB |
Output is correct |
28 |
Correct |
5 ms |
11348 KB |
Output is correct |
29 |
Correct |
47 ms |
14676 KB |
Output is correct |
30 |
Correct |
47 ms |
12888 KB |
Output is correct |
31 |
Correct |
3 ms |
19032 KB |
Output is correct |
32 |
Correct |
4 ms |
14936 KB |
Output is correct |
33 |
Correct |
5 ms |
9816 KB |
Output is correct |
34 |
Correct |
3 ms |
18776 KB |
Output is correct |
35 |
Correct |
10 ms |
18776 KB |
Output is correct |
36 |
Correct |
6 ms |
10192 KB |
Output is correct |
37 |
Correct |
50 ms |
10072 KB |
Output is correct |
38 |
Correct |
67 ms |
13140 KB |
Output is correct |
39 |
Correct |
3 ms |
18776 KB |
Output is correct |
40 |
Correct |
4 ms |
12888 KB |
Output is correct |
41 |
Correct |
4 ms |
9816 KB |
Output is correct |
42 |
Correct |
5 ms |
9984 KB |
Output is correct |
43 |
Execution timed out |
3070 ms |
21336 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12632 KB |
Output is correct |
2 |
Correct |
4 ms |
9816 KB |
Output is correct |
3 |
Correct |
46 ms |
9816 KB |
Output is correct |
4 |
Correct |
48 ms |
9816 KB |
Output is correct |
5 |
Correct |
48 ms |
9816 KB |
Output is correct |
6 |
Correct |
49 ms |
9816 KB |
Output is correct |
7 |
Correct |
46 ms |
12892 KB |
Output is correct |
8 |
Correct |
48 ms |
18776 KB |
Output is correct |
9 |
Correct |
5 ms |
9816 KB |
Output is correct |
10 |
Correct |
6 ms |
9816 KB |
Output is correct |
11 |
Correct |
4 ms |
18776 KB |
Output is correct |
12 |
Correct |
4 ms |
9816 KB |
Output is correct |
13 |
Correct |
4 ms |
9976 KB |
Output is correct |
14 |
Correct |
5 ms |
9816 KB |
Output is correct |
15 |
Correct |
8 ms |
10072 KB |
Output is correct |
16 |
Correct |
3 ms |
19032 KB |
Output is correct |
17 |
Correct |
3 ms |
19032 KB |
Output is correct |
18 |
Correct |
5 ms |
10072 KB |
Output is correct |
19 |
Correct |
6 ms |
10072 KB |
Output is correct |
20 |
Correct |
7 ms |
10072 KB |
Output is correct |
21 |
Correct |
4 ms |
14936 KB |
Output is correct |
22 |
Correct |
4 ms |
11096 KB |
Output is correct |
23 |
Correct |
4 ms |
11096 KB |
Output is correct |
24 |
Correct |
4 ms |
11352 KB |
Output is correct |
25 |
Correct |
3 ms |
19032 KB |
Output is correct |
26 |
Correct |
3 ms |
12888 KB |
Output is correct |
27 |
Correct |
4 ms |
11352 KB |
Output is correct |
28 |
Correct |
5 ms |
11348 KB |
Output is correct |
29 |
Correct |
47 ms |
14676 KB |
Output is correct |
30 |
Correct |
47 ms |
12888 KB |
Output is correct |
31 |
Correct |
3 ms |
19032 KB |
Output is correct |
32 |
Correct |
4 ms |
14936 KB |
Output is correct |
33 |
Correct |
5 ms |
9816 KB |
Output is correct |
34 |
Correct |
3 ms |
18776 KB |
Output is correct |
35 |
Correct |
10 ms |
18776 KB |
Output is correct |
36 |
Correct |
6 ms |
10192 KB |
Output is correct |
37 |
Correct |
50 ms |
10072 KB |
Output is correct |
38 |
Correct |
67 ms |
13140 KB |
Output is correct |
39 |
Correct |
3 ms |
18776 KB |
Output is correct |
40 |
Correct |
4 ms |
12888 KB |
Output is correct |
41 |
Correct |
4 ms |
9816 KB |
Output is correct |
42 |
Correct |
5 ms |
9984 KB |
Output is correct |
43 |
Correct |
672 ms |
24860 KB |
Output is correct |
44 |
Correct |
1012 ms |
26960 KB |
Output is correct |
45 |
Correct |
936 ms |
30808 KB |
Output is correct |
46 |
Correct |
995 ms |
25940 KB |
Output is correct |
47 |
Correct |
879 ms |
25120 KB |
Output is correct |
48 |
Correct |
1149 ms |
31144 KB |
Output is correct |
49 |
Correct |
1235 ms |
24488 KB |
Output is correct |
50 |
Correct |
912 ms |
30664 KB |
Output is correct |
51 |
Correct |
425 ms |
19032 KB |
Output is correct |
52 |
Correct |
857 ms |
21336 KB |
Output is correct |
53 |
Correct |
893 ms |
21336 KB |
Output is correct |
54 |
Correct |
819 ms |
21336 KB |
Output is correct |
55 |
Execution timed out |
3096 ms |
36580 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |