#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];
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();
vector<int> tmp(sz+1);
// DP 1
tmp[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)
{
tmp[j]=(tmp[j]*dp[y][0])%mod;
}
else
{
tmp[j]=(tmp[j]*dp[y][0])%mod;
tmp[j]=(tmp[j] + ((tmp[j-1]*dp[y][1])%mod))%mod;
}
}
}
int sm=0;
for(int j=sz;j>=1;j--)
{
sm = ( sm + tmp[j] ) % mod;
dp[x][1] = ( dp[x][1] + sm ) % mod;
}
sm=tmp[0];
for(int j=1;j<=sz;j++)
{
dp[x][0] = ( dp[x][0] + sm ) % mod;
sm = ( sm + tmp[j] ) % mod;
}
// Dp 1
for(int j=0;j<=sz;j++)tmp[j]=0;
tmp[0]=1;
for(auto y:ma[x])
{
for(int j=sz;j>=0;j--)
{
if(j==0)
{
tmp[j]=(tmp[j]*dp1[y][0])%mod;
}
else
{
tmp[j]=(tmp[j]*dp1[y][0])%mod;
tmp[j]=(tmp[j] + ((tmp[j-1]*dp1[y][1])%mod))%mod;
}
}
}
sm=0;
for(int j=sz;j>=1;j--)
{
sm = ( sm + tmp[j] ) % mod;
dp1[x][1] = ( dp1[x][1] + sm ) % mod;
}
sm=tmp[0];
for(int j=1;j<=sz;j++)
{
dp1[x][0] = ( dp1[x][0] + sm ) % mod;
sm = ( sm + tmp[j] ) % mod;
}
}
void push(int v)
{
if(!lazy[v])return;
for(auto 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();
vector<int> tmp(sz+1);
tmp[0]=1;
for(auto y:ma[x])
{
update(y,ql,qr);
for(int j=sz;j>=0;j--)
{
if(j==0)
{
tmp[j]=(tmp[j]*dp[y][0])%mod;
}
else
{
tmp[j]=(tmp[j]*dp[y][0])%mod;
tmp[j]=(tmp[j] + ((tmp[j-1]*dp[y][1])%mod))%mod;
}
}
}
int sm=0;
for(int j=sz;j>=1;j--)
{
sm = ( sm + tmp[j] ) % mod;
dp[x][1] = ( dp[x][1] + sm ) % mod;
}
sm=tmp[0];
for(int j=1;j<=sz;j++)
{
dp[x][0] = ( dp[x][0] + sm ) % mod;
sm = ( sm + tmp[j] ) % mod;
}
// Dp 1
for(int j=0;j<=sz;j++)tmp[j]=0;
tmp[0]=1;
for(auto y:ma[x])
{
for(int j=sz;j>=0;j--)
{
if(j==0)
{
tmp[j]=(tmp[j]*dp1[y][0])%mod;
}
else
{
tmp[j]=(tmp[j]*dp1[y][0])%mod;
tmp[j]=(tmp[j] + ((tmp[j-1]*dp1[y][1])%mod))%mod;
}
}
}
sm=0;
for(int j=sz;j>=1;j--)
{
sm = ( sm + tmp[j] ) % mod;
dp1[x][1] = ( dp1[x][1] + sm ) % mod;
}
sm=tmp[0];
for(int j=1;j<=sz;j++)
{
dp1[x][0] = ( dp1[x][0] + sm ) % mod;
sm = ( sm + tmp[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];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
45 ms |
5272 KB |
Output is correct |
4 |
Correct |
48 ms |
5292 KB |
Output is correct |
5 |
Correct |
47 ms |
5208 KB |
Output is correct |
6 |
Correct |
48 ms |
5208 KB |
Output is correct |
7 |
Correct |
47 ms |
5208 KB |
Output is correct |
8 |
Correct |
48 ms |
5268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5208 KB |
Output is correct |
5 |
Correct |
3 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
5220 KB |
Output is correct |
7 |
Correct |
3 ms |
5208 KB |
Output is correct |
8 |
Correct |
3 ms |
5208 KB |
Output is correct |
9 |
Correct |
4 ms |
5208 KB |
Output is correct |
10 |
Correct |
3 ms |
5464 KB |
Output is correct |
11 |
Correct |
3 ms |
5464 KB |
Output is correct |
12 |
Correct |
3 ms |
5208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
45 ms |
5272 KB |
Output is correct |
4 |
Correct |
48 ms |
5292 KB |
Output is correct |
5 |
Correct |
47 ms |
5208 KB |
Output is correct |
6 |
Correct |
48 ms |
5208 KB |
Output is correct |
7 |
Correct |
47 ms |
5208 KB |
Output is correct |
8 |
Correct |
48 ms |
5268 KB |
Output is correct |
9 |
Correct |
3 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
5208 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5220 KB |
Output is correct |
15 |
Correct |
3 ms |
5208 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
4 ms |
5208 KB |
Output is correct |
18 |
Correct |
3 ms |
5464 KB |
Output is correct |
19 |
Correct |
3 ms |
5464 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5276 KB |
Output is correct |
22 |
Correct |
3 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5208 KB |
Output is correct |
24 |
Correct |
3 ms |
5208 KB |
Output is correct |
25 |
Correct |
4 ms |
5208 KB |
Output is correct |
26 |
Correct |
4 ms |
5208 KB |
Output is correct |
27 |
Correct |
5 ms |
5208 KB |
Output is correct |
28 |
Correct |
3 ms |
5208 KB |
Output is correct |
29 |
Correct |
47 ms |
5288 KB |
Output is correct |
30 |
Correct |
48 ms |
5280 KB |
Output is correct |
31 |
Correct |
3 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
3 ms |
5208 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
11 ms |
5208 KB |
Output is correct |
36 |
Correct |
4 ms |
5464 KB |
Output is correct |
37 |
Correct |
49 ms |
5464 KB |
Output is correct |
38 |
Correct |
48 ms |
5464 KB |
Output is correct |
39 |
Correct |
2 ms |
5208 KB |
Output is correct |
40 |
Correct |
5 ms |
5300 KB |
Output is correct |
41 |
Correct |
3 ms |
5208 KB |
Output is correct |
42 |
Correct |
3 ms |
5232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
681 ms |
10840 KB |
Output is correct |
2 |
Correct |
964 ms |
16384 KB |
Output is correct |
3 |
Correct |
969 ms |
16460 KB |
Output is correct |
4 |
Correct |
1027 ms |
16456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
681 ms |
10840 KB |
Output is correct |
2 |
Correct |
964 ms |
16384 KB |
Output is correct |
3 |
Correct |
969 ms |
16460 KB |
Output is correct |
4 |
Correct |
1027 ms |
16456 KB |
Output is correct |
5 |
Correct |
936 ms |
10960 KB |
Output is correct |
6 |
Correct |
1168 ms |
16724 KB |
Output is correct |
7 |
Correct |
1136 ms |
16720 KB |
Output is correct |
8 |
Correct |
1012 ms |
16472 KB |
Output is correct |
9 |
Correct |
429 ms |
5464 KB |
Output is correct |
10 |
Correct |
887 ms |
5720 KB |
Output is correct |
11 |
Correct |
876 ms |
5720 KB |
Output is correct |
12 |
Correct |
843 ms |
5720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5208 KB |
Output is correct |
5 |
Correct |
3 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
5220 KB |
Output is correct |
7 |
Correct |
3 ms |
5208 KB |
Output is correct |
8 |
Correct |
3 ms |
5208 KB |
Output is correct |
9 |
Correct |
4 ms |
5208 KB |
Output is correct |
10 |
Correct |
3 ms |
5464 KB |
Output is correct |
11 |
Correct |
3 ms |
5464 KB |
Output is correct |
12 |
Correct |
3 ms |
5208 KB |
Output is correct |
13 |
Correct |
681 ms |
10840 KB |
Output is correct |
14 |
Correct |
964 ms |
16384 KB |
Output is correct |
15 |
Correct |
969 ms |
16460 KB |
Output is correct |
16 |
Correct |
1027 ms |
16456 KB |
Output is correct |
17 |
Correct |
936 ms |
10960 KB |
Output is correct |
18 |
Correct |
1168 ms |
16724 KB |
Output is correct |
19 |
Correct |
1136 ms |
16720 KB |
Output is correct |
20 |
Correct |
1012 ms |
16472 KB |
Output is correct |
21 |
Correct |
429 ms |
5464 KB |
Output is correct |
22 |
Correct |
887 ms |
5720 KB |
Output is correct |
23 |
Correct |
876 ms |
5720 KB |
Output is correct |
24 |
Correct |
843 ms |
5720 KB |
Output is correct |
25 |
Execution timed out |
3097 ms |
22496 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
45 ms |
5272 KB |
Output is correct |
4 |
Correct |
48 ms |
5292 KB |
Output is correct |
5 |
Correct |
47 ms |
5208 KB |
Output is correct |
6 |
Correct |
48 ms |
5208 KB |
Output is correct |
7 |
Correct |
47 ms |
5208 KB |
Output is correct |
8 |
Correct |
48 ms |
5268 KB |
Output is correct |
9 |
Correct |
3 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
5208 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5220 KB |
Output is correct |
15 |
Correct |
3 ms |
5208 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
4 ms |
5208 KB |
Output is correct |
18 |
Correct |
3 ms |
5464 KB |
Output is correct |
19 |
Correct |
3 ms |
5464 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5276 KB |
Output is correct |
22 |
Correct |
3 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5208 KB |
Output is correct |
24 |
Correct |
3 ms |
5208 KB |
Output is correct |
25 |
Correct |
4 ms |
5208 KB |
Output is correct |
26 |
Correct |
4 ms |
5208 KB |
Output is correct |
27 |
Correct |
5 ms |
5208 KB |
Output is correct |
28 |
Correct |
3 ms |
5208 KB |
Output is correct |
29 |
Correct |
47 ms |
5288 KB |
Output is correct |
30 |
Correct |
48 ms |
5280 KB |
Output is correct |
31 |
Correct |
3 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
3 ms |
5208 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
11 ms |
5208 KB |
Output is correct |
36 |
Correct |
4 ms |
5464 KB |
Output is correct |
37 |
Correct |
49 ms |
5464 KB |
Output is correct |
38 |
Correct |
48 ms |
5464 KB |
Output is correct |
39 |
Correct |
2 ms |
5208 KB |
Output is correct |
40 |
Correct |
5 ms |
5300 KB |
Output is correct |
41 |
Correct |
3 ms |
5208 KB |
Output is correct |
42 |
Correct |
3 ms |
5232 KB |
Output is correct |
43 |
Execution timed out |
3052 ms |
5720 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
45 ms |
5272 KB |
Output is correct |
4 |
Correct |
48 ms |
5292 KB |
Output is correct |
5 |
Correct |
47 ms |
5208 KB |
Output is correct |
6 |
Correct |
48 ms |
5208 KB |
Output is correct |
7 |
Correct |
47 ms |
5208 KB |
Output is correct |
8 |
Correct |
48 ms |
5268 KB |
Output is correct |
9 |
Correct |
3 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
5208 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5220 KB |
Output is correct |
15 |
Correct |
3 ms |
5208 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
4 ms |
5208 KB |
Output is correct |
18 |
Correct |
3 ms |
5464 KB |
Output is correct |
19 |
Correct |
3 ms |
5464 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5276 KB |
Output is correct |
22 |
Correct |
3 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5208 KB |
Output is correct |
24 |
Correct |
3 ms |
5208 KB |
Output is correct |
25 |
Correct |
4 ms |
5208 KB |
Output is correct |
26 |
Correct |
4 ms |
5208 KB |
Output is correct |
27 |
Correct |
5 ms |
5208 KB |
Output is correct |
28 |
Correct |
3 ms |
5208 KB |
Output is correct |
29 |
Correct |
47 ms |
5288 KB |
Output is correct |
30 |
Correct |
48 ms |
5280 KB |
Output is correct |
31 |
Correct |
3 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
3 ms |
5208 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
11 ms |
5208 KB |
Output is correct |
36 |
Correct |
4 ms |
5464 KB |
Output is correct |
37 |
Correct |
49 ms |
5464 KB |
Output is correct |
38 |
Correct |
48 ms |
5464 KB |
Output is correct |
39 |
Correct |
2 ms |
5208 KB |
Output is correct |
40 |
Correct |
5 ms |
5300 KB |
Output is correct |
41 |
Correct |
3 ms |
5208 KB |
Output is correct |
42 |
Correct |
3 ms |
5232 KB |
Output is correct |
43 |
Correct |
681 ms |
10840 KB |
Output is correct |
44 |
Correct |
964 ms |
16384 KB |
Output is correct |
45 |
Correct |
969 ms |
16460 KB |
Output is correct |
46 |
Correct |
1027 ms |
16456 KB |
Output is correct |
47 |
Correct |
936 ms |
10960 KB |
Output is correct |
48 |
Correct |
1168 ms |
16724 KB |
Output is correct |
49 |
Correct |
1136 ms |
16720 KB |
Output is correct |
50 |
Correct |
1012 ms |
16472 KB |
Output is correct |
51 |
Correct |
429 ms |
5464 KB |
Output is correct |
52 |
Correct |
887 ms |
5720 KB |
Output is correct |
53 |
Correct |
876 ms |
5720 KB |
Output is correct |
54 |
Correct |
843 ms |
5720 KB |
Output is correct |
55 |
Execution timed out |
3097 ms |
22496 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |