#include "circuit.h"
#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;
int n, m;
vector<int>child[200001];
bool state[200001];
ll encen[200001][2]; // encendido y apagado
const ll MOD = 1000002022;
ll past[2002][2002];
void generate(int node);
int parent[200002];
bool casito = true;
ll aporte[200002];
ll prefix[200002];
ll T[200002];
ll ans = 0;
#define isCase4 false
// seg tree
ll sumaUno[200005*4];
ll sumaCero[200005*4];
bool lazy[200005*4];
ll aux1[200005];
ll aux0[200005];
int mid[200005*4];
void upd(int node);
void cambiar(int node, int dd, int ht, int l, int r);
void generate(int node, int dd, int ht){
mid[node] = (dd + ht)/2;
if(dd == ht){
sumaUno[node] = aux1[dd];
sumaCero[node] = aux0[dd];
}else{
generate(node*2, dd, mid[node]);
generate(node * 2 + 1, mid[node] + 1, ht );
upd(node);
}
}
ll get1(int node){
if(lazy[node]){
return sumaCero[node];
}else return sumaUno[node];
}
ll get0(int node){
if(lazy[node]){
return sumaUno[node];
}else return sumaCero[node];
}
void upd(int node){
sumaUno[node] = (get1(node*2) + get1(node*2+1))%MOD;
sumaCero[node] = (get0(node*2) + get0(node*2+1))%MOD;
}
void prop(int node, int dd, int ht){
if(lazy[node] ){
//cout << "propagating from " << dd << " " << ht << endl;
if(dd == ht)swap(sumaCero[node], sumaUno[node]);
else{
cambiar(node*2, dd, mid[node], dd, mid[node]);
cambiar(node*2+1, mid[node] + 1, ht, mid[node] + 1, ht);
}
}
}
void cambiar(int node, int dd, int ht, int l, int r){
if(dd == l and ht == r){
lazy[node] = !lazy[node];
//cout << "node " << dd << " " << ht << " exc " << lazy[node] << endl;
}
else{
prop(node, dd, ht);
lazy[node] = 0;
if(r <= mid[node])cambiar(node * 2, dd, mid[node], l, r);
else if(mid[node] < l)cambiar(node*2+1, mid[node]+1, ht, l, r);
else{
cambiar(node*2, dd, mid[node], l, mid[node]);
cambiar(node*2+1, mid[node]+1, ht, mid[node] + 1, r);
}
upd(node);
//cout << "node " << dd << " " << ht << " updated ";
//cout << get1(node) << " " << get0(node) << endl;
}
}
void precalc(int node){
T[node] = 1;
if(node >= n)return ;
for(int i = 0 ; i < child[node].size() ; i ++){
precalc(child[node][i]);
T[node] = T[node] * T[child[node][i]] % MOD;
}
T[node] = T[node] * (ll)child[node].size() % MOD;
}
void calcPrefix(int node, ll acum){
if(node >= n){
aporte[node] = acum;
//cout << "aporte de " << node << ": " << acum << endl;
sumaUno[node] = 0;
sumaCero[node] = 0;
if(state[node])aux1[node] = aporte[node];
else aux0[node] = aporte[node];
return ;
}
int hijo1 = child[node][0];
int hijo2 = child[node][1];
calcPrefix(hijo1, (acum * T[hijo2]) % MOD);
calcPrefix(hijo2, (acum * T[hijo1]) % MOD);
}
//
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
n = N;
m = M;
for(int i = 0 ; i < n+m ; i ++){
if(P[i] != -1){
child[P[i]].pb(i);
}
parent[i] = P[i];
}
for(int i = 0 ; i < n+m ; i ++){
if(child[i].size() != 0 and child[i].size() != 2)casito = false;
}
//assert(casito);
for(int i = N ; i <= N+M ; i ++){
state[i] = A[i-N];
}
memset(past, -1, sizeof past);
memset(lazy, false, sizeof lazy);
generate(0);
ans = encen[0][0];
if(isCase4 or ( casito and (n > 1000 or m > 1000) ) ){
precalc(0);
calcPrefix(0, 1);
generate(1, n, n+m-1);
}
}
ll mem[2002][2002];
ll currentIt = -1 ;
void newDp(){
currentIt++;
}
ll dp(int parent, ll i, int k){
if(k < 0)return 0;
if(i == child[parent].size() and k > 0)return 0;
else if(i == child[parent].size()) return 1;
if(past[i][k] == currentIt)return mem[i][k];
past[i][k] = currentIt;
return mem[i][k] = ((dp(parent, i+1, k-1) * encen[child[parent][i]][0] % MOD) + ((dp(parent, i+1, k) * encen[child[parent][i]][1])%MOD))%MOD;
}
void generate(int node){
if(node >= n){
if(state[node]){
encen[node][0] = 1;
encen[node][1] = 0;
}else{
encen[node][0] = 0;
encen[node][1] = 1;
}
return ;
}
for(int i = 0 ; i < child[node].size() ; i ++){
generate(child[node][i]);
}
newDp();
ll enc = 0, inv = 0;
for(ll i = 1 ; i <= child[node].size() ; i ++){
enc += dp(node, 0, i) * i % MOD;
enc %= MOD;
}
ll allWays = 1;
for(int i = 0 ; i < child[node].size() ; i ++){
allWays *= (encen[child[node][i]][0] + encen[child[node][i]][1])%MOD;
allWays %= MOD;
}
allWays *= (ll)child[node].size();
allWays %= MOD;
encen[node][0] = enc;
encen[node][1] = ((allWays - enc) + MOD) % MOD;
//cout << "for node " << node << " there are " << encen[node][0] << ", " << encen[node][1] << "\n";
}
int count_ways(int L, int R) {
if(isCase4 or ( casito and (n > 1000 or m > 1000) ) ){
cambiar(1, n, n+m-1, L, R);
return get1(1);
}
else {
for(int i = L ; i <= R ; i ++){
state[i] = !state[i];
}
generate(0);
}
return encen[0][0];
}
Compilation message
circuit.cpp: In function 'void precalc(int)':
circuit.cpp:94:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for(int i = 0 ; i < child[node].size() ; i ++){
| ~~^~~~~~~~~~~~~~~~~~~~
circuit.cpp: In function 'long long int dp(int, long long int, int)':
circuit.cpp:154:7: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
154 | if(i == child[parent].size() and k > 0)return 0;
| ~~^~~~~~~~~~~~~~~~~~~~~~~
circuit.cpp:155:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
155 | else if(i == child[parent].size()) return 1;
| ~~^~~~~~~~~~~~~~~~~~~~~~~
circuit.cpp: In function 'void generate(int)':
circuit.cpp:173:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
173 | for(int i = 0 ; i < child[node].size() ; i ++){
| ~~^~~~~~~~~~~~~~~~~~~~
circuit.cpp:178:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
178 | for(ll i = 1 ; i <= child[node].size() ; i ++){
| ~~^~~~~~~~~~~~~~~~~~~~~
circuit.cpp:183:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
183 | for(int i = 0 ; i < child[node].size() ; i ++){
| ~~^~~~~~~~~~~~~~~~~~~~
circuit.cpp:177:14: warning: unused variable 'inv' [-Wunused-variable]
177 | ll enc = 0, inv = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
37264 KB |
Output is correct |
2 |
Correct |
15 ms |
37072 KB |
Output is correct |
3 |
Correct |
146 ms |
48572 KB |
Output is correct |
4 |
Correct |
161 ms |
49120 KB |
Output is correct |
5 |
Correct |
154 ms |
49000 KB |
Output is correct |
6 |
Correct |
154 ms |
49024 KB |
Output is correct |
7 |
Correct |
177 ms |
49084 KB |
Output is correct |
8 |
Correct |
161 ms |
49124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
37176 KB |
Output is correct |
2 |
Correct |
16 ms |
37120 KB |
Output is correct |
3 |
Correct |
13 ms |
37180 KB |
Output is correct |
4 |
Correct |
13 ms |
37180 KB |
Output is correct |
5 |
Correct |
12 ms |
37200 KB |
Output is correct |
6 |
Correct |
17 ms |
37124 KB |
Output is correct |
7 |
Correct |
13 ms |
37224 KB |
Output is correct |
8 |
Correct |
13 ms |
37200 KB |
Output is correct |
9 |
Correct |
13 ms |
37212 KB |
Output is correct |
10 |
Correct |
13 ms |
37200 KB |
Output is correct |
11 |
Correct |
17 ms |
37328 KB |
Output is correct |
12 |
Correct |
15 ms |
37200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
37264 KB |
Output is correct |
2 |
Correct |
15 ms |
37072 KB |
Output is correct |
3 |
Correct |
146 ms |
48572 KB |
Output is correct |
4 |
Correct |
161 ms |
49120 KB |
Output is correct |
5 |
Correct |
154 ms |
49000 KB |
Output is correct |
6 |
Correct |
154 ms |
49024 KB |
Output is correct |
7 |
Correct |
177 ms |
49084 KB |
Output is correct |
8 |
Correct |
161 ms |
49124 KB |
Output is correct |
9 |
Correct |
13 ms |
37176 KB |
Output is correct |
10 |
Correct |
16 ms |
37120 KB |
Output is correct |
11 |
Correct |
13 ms |
37180 KB |
Output is correct |
12 |
Correct |
13 ms |
37180 KB |
Output is correct |
13 |
Correct |
12 ms |
37200 KB |
Output is correct |
14 |
Correct |
17 ms |
37124 KB |
Output is correct |
15 |
Correct |
13 ms |
37224 KB |
Output is correct |
16 |
Correct |
13 ms |
37200 KB |
Output is correct |
17 |
Correct |
13 ms |
37212 KB |
Output is correct |
18 |
Correct |
13 ms |
37200 KB |
Output is correct |
19 |
Correct |
17 ms |
37328 KB |
Output is correct |
20 |
Correct |
15 ms |
37200 KB |
Output is correct |
21 |
Correct |
15 ms |
37180 KB |
Output is correct |
22 |
Correct |
15 ms |
37160 KB |
Output is correct |
23 |
Correct |
15 ms |
37248 KB |
Output is correct |
24 |
Correct |
16 ms |
37232 KB |
Output is correct |
25 |
Correct |
16 ms |
37200 KB |
Output is correct |
26 |
Correct |
14 ms |
37200 KB |
Output is correct |
27 |
Correct |
14 ms |
37292 KB |
Output is correct |
28 |
Correct |
16 ms |
37364 KB |
Output is correct |
29 |
Correct |
156 ms |
49052 KB |
Output is correct |
30 |
Correct |
188 ms |
49108 KB |
Output is correct |
31 |
Correct |
15 ms |
37184 KB |
Output is correct |
32 |
Correct |
15 ms |
37200 KB |
Output is correct |
33 |
Correct |
17 ms |
37200 KB |
Output is correct |
34 |
Correct |
16 ms |
37228 KB |
Output is correct |
35 |
Correct |
34 ms |
38480 KB |
Output is correct |
36 |
Correct |
17 ms |
37328 KB |
Output is correct |
37 |
Correct |
193 ms |
49224 KB |
Output is correct |
38 |
Correct |
149 ms |
49232 KB |
Output is correct |
39 |
Correct |
15 ms |
37200 KB |
Output is correct |
40 |
Correct |
15 ms |
37200 KB |
Output is correct |
41 |
Correct |
16 ms |
37176 KB |
Output is correct |
42 |
Correct |
18 ms |
37384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
421 ms |
42896 KB |
Output is correct |
2 |
Correct |
818 ms |
48564 KB |
Output is correct |
3 |
Correct |
637 ms |
48008 KB |
Output is correct |
4 |
Correct |
658 ms |
48052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
421 ms |
42896 KB |
Output is correct |
2 |
Correct |
818 ms |
48564 KB |
Output is correct |
3 |
Correct |
637 ms |
48008 KB |
Output is correct |
4 |
Correct |
658 ms |
48052 KB |
Output is correct |
5 |
Correct |
622 ms |
42836 KB |
Output is correct |
6 |
Correct |
849 ms |
48564 KB |
Output is correct |
7 |
Correct |
753 ms |
48508 KB |
Output is correct |
8 |
Correct |
750 ms |
48584 KB |
Output is correct |
9 |
Correct |
297 ms |
37508 KB |
Output is correct |
10 |
Correct |
605 ms |
37832 KB |
Output is correct |
11 |
Correct |
691 ms |
37840 KB |
Output is correct |
12 |
Correct |
612 ms |
37840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
37176 KB |
Output is correct |
2 |
Correct |
16 ms |
37120 KB |
Output is correct |
3 |
Correct |
13 ms |
37180 KB |
Output is correct |
4 |
Correct |
13 ms |
37180 KB |
Output is correct |
5 |
Correct |
12 ms |
37200 KB |
Output is correct |
6 |
Correct |
17 ms |
37124 KB |
Output is correct |
7 |
Correct |
13 ms |
37224 KB |
Output is correct |
8 |
Correct |
13 ms |
37200 KB |
Output is correct |
9 |
Correct |
13 ms |
37212 KB |
Output is correct |
10 |
Correct |
13 ms |
37200 KB |
Output is correct |
11 |
Correct |
17 ms |
37328 KB |
Output is correct |
12 |
Correct |
15 ms |
37200 KB |
Output is correct |
13 |
Correct |
421 ms |
42896 KB |
Output is correct |
14 |
Correct |
818 ms |
48564 KB |
Output is correct |
15 |
Correct |
637 ms |
48008 KB |
Output is correct |
16 |
Correct |
658 ms |
48052 KB |
Output is correct |
17 |
Correct |
622 ms |
42836 KB |
Output is correct |
18 |
Correct |
849 ms |
48564 KB |
Output is correct |
19 |
Correct |
753 ms |
48508 KB |
Output is correct |
20 |
Correct |
750 ms |
48584 KB |
Output is correct |
21 |
Correct |
297 ms |
37508 KB |
Output is correct |
22 |
Correct |
605 ms |
37832 KB |
Output is correct |
23 |
Correct |
691 ms |
37840 KB |
Output is correct |
24 |
Correct |
612 ms |
37840 KB |
Output is correct |
25 |
Correct |
711 ms |
55420 KB |
Output is correct |
26 |
Correct |
794 ms |
55724 KB |
Output is correct |
27 |
Correct |
795 ms |
55620 KB |
Output is correct |
28 |
Correct |
645 ms |
55712 KB |
Output is correct |
29 |
Correct |
902 ms |
61944 KB |
Output is correct |
30 |
Correct |
781 ms |
61976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
37264 KB |
Output is correct |
2 |
Correct |
15 ms |
37072 KB |
Output is correct |
3 |
Correct |
146 ms |
48572 KB |
Output is correct |
4 |
Correct |
161 ms |
49120 KB |
Output is correct |
5 |
Correct |
154 ms |
49000 KB |
Output is correct |
6 |
Correct |
154 ms |
49024 KB |
Output is correct |
7 |
Correct |
177 ms |
49084 KB |
Output is correct |
8 |
Correct |
161 ms |
49124 KB |
Output is correct |
9 |
Correct |
13 ms |
37176 KB |
Output is correct |
10 |
Correct |
16 ms |
37120 KB |
Output is correct |
11 |
Correct |
13 ms |
37180 KB |
Output is correct |
12 |
Correct |
13 ms |
37180 KB |
Output is correct |
13 |
Correct |
12 ms |
37200 KB |
Output is correct |
14 |
Correct |
17 ms |
37124 KB |
Output is correct |
15 |
Correct |
13 ms |
37224 KB |
Output is correct |
16 |
Correct |
13 ms |
37200 KB |
Output is correct |
17 |
Correct |
13 ms |
37212 KB |
Output is correct |
18 |
Correct |
13 ms |
37200 KB |
Output is correct |
19 |
Correct |
17 ms |
37328 KB |
Output is correct |
20 |
Correct |
15 ms |
37200 KB |
Output is correct |
21 |
Correct |
15 ms |
37180 KB |
Output is correct |
22 |
Correct |
15 ms |
37160 KB |
Output is correct |
23 |
Correct |
15 ms |
37248 KB |
Output is correct |
24 |
Correct |
16 ms |
37232 KB |
Output is correct |
25 |
Correct |
16 ms |
37200 KB |
Output is correct |
26 |
Correct |
14 ms |
37200 KB |
Output is correct |
27 |
Correct |
14 ms |
37292 KB |
Output is correct |
28 |
Correct |
16 ms |
37364 KB |
Output is correct |
29 |
Correct |
156 ms |
49052 KB |
Output is correct |
30 |
Correct |
188 ms |
49108 KB |
Output is correct |
31 |
Correct |
15 ms |
37184 KB |
Output is correct |
32 |
Correct |
15 ms |
37200 KB |
Output is correct |
33 |
Correct |
17 ms |
37200 KB |
Output is correct |
34 |
Correct |
16 ms |
37228 KB |
Output is correct |
35 |
Correct |
34 ms |
38480 KB |
Output is correct |
36 |
Correct |
17 ms |
37328 KB |
Output is correct |
37 |
Correct |
193 ms |
49224 KB |
Output is correct |
38 |
Correct |
149 ms |
49232 KB |
Output is correct |
39 |
Correct |
15 ms |
37200 KB |
Output is correct |
40 |
Correct |
15 ms |
37200 KB |
Output is correct |
41 |
Correct |
16 ms |
37176 KB |
Output is correct |
42 |
Correct |
18 ms |
37384 KB |
Output is correct |
43 |
Execution timed out |
3054 ms |
37456 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
37264 KB |
Output is correct |
2 |
Correct |
15 ms |
37072 KB |
Output is correct |
3 |
Correct |
146 ms |
48572 KB |
Output is correct |
4 |
Correct |
161 ms |
49120 KB |
Output is correct |
5 |
Correct |
154 ms |
49000 KB |
Output is correct |
6 |
Correct |
154 ms |
49024 KB |
Output is correct |
7 |
Correct |
177 ms |
49084 KB |
Output is correct |
8 |
Correct |
161 ms |
49124 KB |
Output is correct |
9 |
Correct |
13 ms |
37176 KB |
Output is correct |
10 |
Correct |
16 ms |
37120 KB |
Output is correct |
11 |
Correct |
13 ms |
37180 KB |
Output is correct |
12 |
Correct |
13 ms |
37180 KB |
Output is correct |
13 |
Correct |
12 ms |
37200 KB |
Output is correct |
14 |
Correct |
17 ms |
37124 KB |
Output is correct |
15 |
Correct |
13 ms |
37224 KB |
Output is correct |
16 |
Correct |
13 ms |
37200 KB |
Output is correct |
17 |
Correct |
13 ms |
37212 KB |
Output is correct |
18 |
Correct |
13 ms |
37200 KB |
Output is correct |
19 |
Correct |
17 ms |
37328 KB |
Output is correct |
20 |
Correct |
15 ms |
37200 KB |
Output is correct |
21 |
Correct |
15 ms |
37180 KB |
Output is correct |
22 |
Correct |
15 ms |
37160 KB |
Output is correct |
23 |
Correct |
15 ms |
37248 KB |
Output is correct |
24 |
Correct |
16 ms |
37232 KB |
Output is correct |
25 |
Correct |
16 ms |
37200 KB |
Output is correct |
26 |
Correct |
14 ms |
37200 KB |
Output is correct |
27 |
Correct |
14 ms |
37292 KB |
Output is correct |
28 |
Correct |
16 ms |
37364 KB |
Output is correct |
29 |
Correct |
156 ms |
49052 KB |
Output is correct |
30 |
Correct |
188 ms |
49108 KB |
Output is correct |
31 |
Correct |
15 ms |
37184 KB |
Output is correct |
32 |
Correct |
15 ms |
37200 KB |
Output is correct |
33 |
Correct |
17 ms |
37200 KB |
Output is correct |
34 |
Correct |
16 ms |
37228 KB |
Output is correct |
35 |
Correct |
34 ms |
38480 KB |
Output is correct |
36 |
Correct |
17 ms |
37328 KB |
Output is correct |
37 |
Correct |
193 ms |
49224 KB |
Output is correct |
38 |
Correct |
149 ms |
49232 KB |
Output is correct |
39 |
Correct |
15 ms |
37200 KB |
Output is correct |
40 |
Correct |
15 ms |
37200 KB |
Output is correct |
41 |
Correct |
16 ms |
37176 KB |
Output is correct |
42 |
Correct |
18 ms |
37384 KB |
Output is correct |
43 |
Correct |
421 ms |
42896 KB |
Output is correct |
44 |
Correct |
818 ms |
48564 KB |
Output is correct |
45 |
Correct |
637 ms |
48008 KB |
Output is correct |
46 |
Correct |
658 ms |
48052 KB |
Output is correct |
47 |
Correct |
622 ms |
42836 KB |
Output is correct |
48 |
Correct |
849 ms |
48564 KB |
Output is correct |
49 |
Correct |
753 ms |
48508 KB |
Output is correct |
50 |
Correct |
750 ms |
48584 KB |
Output is correct |
51 |
Correct |
297 ms |
37508 KB |
Output is correct |
52 |
Correct |
605 ms |
37832 KB |
Output is correct |
53 |
Correct |
691 ms |
37840 KB |
Output is correct |
54 |
Correct |
612 ms |
37840 KB |
Output is correct |
55 |
Correct |
711 ms |
55420 KB |
Output is correct |
56 |
Correct |
794 ms |
55724 KB |
Output is correct |
57 |
Correct |
795 ms |
55620 KB |
Output is correct |
58 |
Correct |
645 ms |
55712 KB |
Output is correct |
59 |
Correct |
902 ms |
61944 KB |
Output is correct |
60 |
Correct |
781 ms |
61976 KB |
Output is correct |
61 |
Execution timed out |
3054 ms |
37456 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |