#define ll long long
#define pll pair<ll, ll>
#define MOD 1000002022
#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
bool subFirst = false;
ll n = 0;
vector<ll> a(0);
vector<vector<ll>> adjlist(0);
pll dfsSubFirst(int i) //On, off
{
if(adjlist[i].empty())
{
return {a[i], 1-a[i]};
}
ll c = adjlist[i].size();
ll all = c;
vector<ll> dp(c+1, 0); //Possibilities for being on
dp[0] = 1;
for(ll j : adjlist[i])
{
pll p = dfsSubFirst(j);
all = (all * ((p.first+p.second)%MOD))%MOD;
vector<ll> postdp(c+1, 0);
for(ll k = 0; k <= c; k++)
{
postdp[k] = (dp[k]*p.second)%MOD;
if(k > 0)
{
postdp[k] = (postdp[k] + (dp[k-1]*p.first)%MOD)%MOD;
}
}
dp = postdp;
}
ll on = 0;
for(ll k = c; k > 0; k--)
{
on = (on+((dp[k]*k)%MOD))%MOD;
}
return {on, (all+MOD-on)%MOD};
}
struct node
{
ll on = 0;
ll off = 0;
bool flip = false;
ll left = -1;
ll right = -1;
};
vector<node> s(0);
void update(ll i)
{
if(s[i].flip)
{
swap(s[i].on, s[i].off);
if(s[i].left != -1)
{
s[s[i].left].flip = !s[s[i].left].flip;
s[s[i].right].flip = !s[s[i].right].flip;
}
s[i].flip = false;
}
}
void apply(ll i, ll L, ll R, ll a, ll b)
{
if(L <= a && b <= R)
{
s[i].flip = !s[i].flip;
}
update(i);
if(!(L <= a && b <= R) && !(R < a || b < L))
{
ll mid = (a+b)/2;
apply(s[i].left, L, R, a, mid);
apply(s[i].left, L, R, a, mid);
}
ll l = s[i].left;
ll r = s[i].right;
s[i].on = (2*s[l].on*s[r].on)%MOD;
s[i].on += (s[l].on*s[r].off)%MOD;
s[i].on %= MOD;
s[i].on += (s[l].off*s[r].off)%MOD;
s[i].on %= MOD;
s[i].off = (2*s[l].off*s[r].off)%MOD;
s[i].off += (s[l].on*s[r].off)%MOD;
s[i].off %= MOD;
s[i].off += (s[l].off*s[r].off)%MOD;
s[i].off %= MOD;
}
void init(int N, int M, std::vector<int> P, std::vector<int> A)
{
n = N+M;
subFirst = (N <= 1000 && M <= 1000);
if(subFirst)
{
adjlist.assign(n, vector<ll> (0));
for(ll i = 1; i < n; i++)
{
adjlist[P[i]].push_back(i);
}
a.assign(n, 0);
for(ll i = N; i < n; i++)
{
a[i] = A[i-N];
}
return;
}
ll segnodes = n;
node def;
s.assign(segnodes, def);
for(ll i = 1; i < segnodes; i++)
{
if((i-1)%2 == 0)
{
s[(i-1)/2].left = i;
}
else
{
s[(i-1)/2].right = i;
}
}
for(ll i = 0; i < M; i++)
{
s[i+N].on = A[i];
s[i+N].off = 1-s[i+N].on;
}
for(ll i = N-1; i >= 0; i--)
{
ll l = s[i].left;
ll r = s[i].right;
s[i].on = (2*s[l].on*s[r].on)%MOD;
s[i].on += (s[l].on*s[r].off)%MOD;
s[i].on %= MOD;
s[i].on += (s[l].off*s[r].off)%MOD;
s[i].on %= MOD;
s[i].off = (2*s[l].off*s[r].off)%MOD;
s[i].off += (s[l].on*s[r].off)%MOD;
s[i].off %= MOD;
s[i].off += (s[l].off*s[r].off)%MOD;
s[i].off %= MOD;
}
}
int count_ways(int L, int R)
{
if(subFirst)
{
for(ll i = L; i <= R; i++)
{
a[i] = 1-a[i];
}
return dfsSubFirst(0).first;
}
ll segnodes = s.size();
ll M = (segnodes+1)/2;
apply(0, L+1-M, R+1-M, 0, M-1);
return s[0].on;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
17 ms |
364 KB |
Output is correct |
4 |
Correct |
18 ms |
336 KB |
Output is correct |
5 |
Correct |
18 ms |
364 KB |
Output is correct |
6 |
Correct |
18 ms |
336 KB |
Output is correct |
7 |
Correct |
18 ms |
364 KB |
Output is correct |
8 |
Correct |
18 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
17 ms |
364 KB |
Output is correct |
4 |
Correct |
18 ms |
336 KB |
Output is correct |
5 |
Correct |
18 ms |
364 KB |
Output is correct |
6 |
Correct |
18 ms |
336 KB |
Output is correct |
7 |
Correct |
18 ms |
364 KB |
Output is correct |
8 |
Correct |
18 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
18 ms |
336 KB |
Output is correct |
30 |
Correct |
19 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
4 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
592 KB |
Output is correct |
37 |
Correct |
18 ms |
592 KB |
Output is correct |
38 |
Correct |
18 ms |
592 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
2 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
79 ms |
6324 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
79 ms |
6324 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Runtime error |
79 ms |
6324 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
17 ms |
364 KB |
Output is correct |
4 |
Correct |
18 ms |
336 KB |
Output is correct |
5 |
Correct |
18 ms |
364 KB |
Output is correct |
6 |
Correct |
18 ms |
336 KB |
Output is correct |
7 |
Correct |
18 ms |
364 KB |
Output is correct |
8 |
Correct |
18 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
18 ms |
336 KB |
Output is correct |
30 |
Correct |
19 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
4 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
592 KB |
Output is correct |
37 |
Correct |
18 ms |
592 KB |
Output is correct |
38 |
Correct |
18 ms |
592 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
2 ms |
336 KB |
Output is correct |
43 |
Runtime error |
2 ms |
976 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
17 ms |
364 KB |
Output is correct |
4 |
Correct |
18 ms |
336 KB |
Output is correct |
5 |
Correct |
18 ms |
364 KB |
Output is correct |
6 |
Correct |
18 ms |
336 KB |
Output is correct |
7 |
Correct |
18 ms |
364 KB |
Output is correct |
8 |
Correct |
18 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
18 ms |
336 KB |
Output is correct |
30 |
Correct |
19 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
4 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
592 KB |
Output is correct |
37 |
Correct |
18 ms |
592 KB |
Output is correct |
38 |
Correct |
18 ms |
592 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
2 ms |
336 KB |
Output is correct |
43 |
Runtime error |
79 ms |
6324 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |