#include "circuit.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define pb push_back
#define ln '\n'
const int Mod = 1000002022;
vector <int> a;
vector <vector<int>> adj;
vector <int> dp;
int n, m;
struct SegTree{
vector <ar<int,2>> T;
vector <int> lazy;
int n;
SegTree() {}
void modify(int _n, vector <int> val, vector <int> state){
n = _n;
T.resize(n * 4 + 5);
lazy.resize(n * 4 + 5);
auto build = [&](auto build, int v, int l, int r) -> void{
if ( l == r ){
T[v][state[l]] = val[l];
return;
}
int m = (l + r) / 2;
build(build, v * 2, l, m);
build(build, v * 2 + 1, m + 1, r);
for ( auto j: {0, 1} ){
T[v][j] = (T[v * 2][j] + T[v * 2 + 1][j]) % Mod;
}
};
build(build, 1, 0, n - 1);
}
void push(int v, int l, int r){
if ( !lazy[v] ) return;
if ( l != r ){
lazy[v * 2] ^= lazy[v];
lazy[v * 2 + 1] ^= lazy[v];
}
swap(T[v][1], T[v][0]);
lazy[v] = 0;
}
void upd(int v, int l, int r, int tl, int tr){
push(v, l, r);
if ( l > tr or r < tl ) return;
if ( tl <= l && tr >= r ){
lazy[v] = 1;
push(v, l, r);
return;
}
int m = (l + r) / 2;
upd(v * 2, l, m, tl, tr);
upd(v * 2 + 1, m + 1, r, tl, tr);
for ( auto j: {0, 1} ){
T[v][j] = (T[v * 2][j] + T[v * 2 + 1][j]) % Mod;
}
}
void upd(int l, int r){
upd(1, 0, m - 1, l, r);
}
int qry(){
push(1, 0, m - 1);
return T[1][1];
}
};
template <class F, class S>
void add(F &x, const S &y){
x = (x + 0LL + y) % Mod;
if ( x < 0 ) x += Mod;
}
SegTree seg;
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
a = A, n = N, m = M;
adj.resize(n + m);
for ( int i = 1; i < n + m; i++ ){
adj[P[i]].pb(i);
}
vector <int> s(n + m);
auto trav = [&](auto trav, int u) -> void{
if ( u >= n ){
s[u] = 1; return;
}
s[u] = adj[u].size();
for ( auto &v: adj[u] ){
trav(trav, v);
s[u] = s[u] * 1LL * s[v] % Mod;
}
};
trav(trav, 0);
dp.resize(m);
auto dfs = [&](auto dfs, int u, int cnt) -> void{
if ( u >= n ){
dp[u - n] = cnt;
return;
}
int sz = adj[u].size();
vector <int> child, vl;
for ( auto &v: adj[u] ){
child.pb(v); vl.pb(s[v]);
}
vector <int> pf(sz + 1, 1), sf(sz + 2, 1);
for ( int i = 0; i < sz; i++ ){
pf[i + 1] = pf[i] * 1LL * vl[i] % Mod;
}
for ( int i = sz; i > 0; i-- ){
sf[i] = sf[i + 1] * 1LL * vl[i - 1] % Mod;
}
for ( int i = 0; i < sz; i++ ){
int nxt = cnt * 1LL * pf[i] % Mod * sf[i + 2] % Mod;
dfs(dfs, child[i], nxt);
}
};
dfs(dfs, 0, 1);
seg.modify(m, dp, a);
}
int count_ways(int L, int R) {
L -= n, R -= n;
seg.upd(L, R);
return seg.qry();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
856 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
856 KB |
Output is correct |
19 |
Correct |
1 ms |
856 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
0 ms |
600 KB |
Output is correct |
28 |
Correct |
0 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
856 KB |
Output is correct |
37 |
Correct |
1 ms |
856 KB |
Output is correct |
38 |
Correct |
1 ms |
856 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
344 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
345 ms |
6112 KB |
Output is correct |
2 |
Correct |
573 ms |
11600 KB |
Output is correct |
3 |
Correct |
564 ms |
11608 KB |
Output is correct |
4 |
Correct |
542 ms |
11608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
345 ms |
6112 KB |
Output is correct |
2 |
Correct |
573 ms |
11600 KB |
Output is correct |
3 |
Correct |
564 ms |
11608 KB |
Output is correct |
4 |
Correct |
542 ms |
11608 KB |
Output is correct |
5 |
Correct |
479 ms |
5976 KB |
Output is correct |
6 |
Correct |
576 ms |
11608 KB |
Output is correct |
7 |
Correct |
561 ms |
11600 KB |
Output is correct |
8 |
Correct |
535 ms |
11608 KB |
Output is correct |
9 |
Correct |
288 ms |
760 KB |
Output is correct |
10 |
Correct |
563 ms |
1128 KB |
Output is correct |
11 |
Correct |
546 ms |
1124 KB |
Output is correct |
12 |
Correct |
539 ms |
1124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
856 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
345 ms |
6112 KB |
Output is correct |
14 |
Correct |
573 ms |
11600 KB |
Output is correct |
15 |
Correct |
564 ms |
11608 KB |
Output is correct |
16 |
Correct |
542 ms |
11608 KB |
Output is correct |
17 |
Correct |
479 ms |
5976 KB |
Output is correct |
18 |
Correct |
576 ms |
11608 KB |
Output is correct |
19 |
Correct |
561 ms |
11600 KB |
Output is correct |
20 |
Correct |
535 ms |
11608 KB |
Output is correct |
21 |
Correct |
288 ms |
760 KB |
Output is correct |
22 |
Correct |
563 ms |
1128 KB |
Output is correct |
23 |
Correct |
546 ms |
1124 KB |
Output is correct |
24 |
Correct |
539 ms |
1124 KB |
Output is correct |
25 |
Correct |
633 ms |
17208 KB |
Output is correct |
26 |
Correct |
625 ms |
17488 KB |
Output is correct |
27 |
Correct |
657 ms |
17488 KB |
Output is correct |
28 |
Correct |
521 ms |
17600 KB |
Output is correct |
29 |
Correct |
681 ms |
46412 KB |
Output is correct |
30 |
Correct |
632 ms |
46568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
856 KB |
Output is correct |
19 |
Correct |
1 ms |
856 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
0 ms |
600 KB |
Output is correct |
28 |
Correct |
0 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
856 KB |
Output is correct |
37 |
Correct |
1 ms |
856 KB |
Output is correct |
38 |
Correct |
1 ms |
856 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
344 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
428 ms |
912 KB |
Output is correct |
44 |
Correct |
558 ms |
960 KB |
Output is correct |
45 |
Correct |
555 ms |
960 KB |
Output is correct |
46 |
Correct |
477 ms |
1292 KB |
Output is correct |
47 |
Correct |
582 ms |
1304 KB |
Output is correct |
48 |
Correct |
529 ms |
1296 KB |
Output is correct |
49 |
Correct |
540 ms |
1296 KB |
Output is correct |
50 |
Correct |
504 ms |
1300 KB |
Output is correct |
51 |
Correct |
575 ms |
856 KB |
Output is correct |
52 |
Correct |
570 ms |
856 KB |
Output is correct |
53 |
Correct |
502 ms |
2392 KB |
Output is correct |
54 |
Correct |
525 ms |
1288 KB |
Output is correct |
55 |
Correct |
545 ms |
1124 KB |
Output is correct |
56 |
Correct |
564 ms |
1072 KB |
Output is correct |
57 |
Correct |
552 ms |
992 KB |
Output is correct |
58 |
Correct |
552 ms |
2648 KB |
Output is correct |
59 |
Correct |
560 ms |
2648 KB |
Output is correct |
60 |
Correct |
540 ms |
2648 KB |
Output is correct |
61 |
Correct |
573 ms |
1316 KB |
Output is correct |
62 |
Correct |
538 ms |
936 KB |
Output is correct |
63 |
Correct |
521 ms |
912 KB |
Output is correct |
64 |
Correct |
592 ms |
1108 KB |
Output is correct |
65 |
Correct |
279 ms |
848 KB |
Output is correct |
66 |
Correct |
548 ms |
1132 KB |
Output is correct |
67 |
Correct |
543 ms |
1124 KB |
Output is correct |
68 |
Correct |
530 ms |
1128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
856 KB |
Output is correct |
19 |
Correct |
1 ms |
856 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
0 ms |
600 KB |
Output is correct |
28 |
Correct |
0 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
856 KB |
Output is correct |
37 |
Correct |
1 ms |
856 KB |
Output is correct |
38 |
Correct |
1 ms |
856 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
344 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
345 ms |
6112 KB |
Output is correct |
44 |
Correct |
573 ms |
11600 KB |
Output is correct |
45 |
Correct |
564 ms |
11608 KB |
Output is correct |
46 |
Correct |
542 ms |
11608 KB |
Output is correct |
47 |
Correct |
479 ms |
5976 KB |
Output is correct |
48 |
Correct |
576 ms |
11608 KB |
Output is correct |
49 |
Correct |
561 ms |
11600 KB |
Output is correct |
50 |
Correct |
535 ms |
11608 KB |
Output is correct |
51 |
Correct |
288 ms |
760 KB |
Output is correct |
52 |
Correct |
563 ms |
1128 KB |
Output is correct |
53 |
Correct |
546 ms |
1124 KB |
Output is correct |
54 |
Correct |
539 ms |
1124 KB |
Output is correct |
55 |
Correct |
633 ms |
17208 KB |
Output is correct |
56 |
Correct |
625 ms |
17488 KB |
Output is correct |
57 |
Correct |
657 ms |
17488 KB |
Output is correct |
58 |
Correct |
521 ms |
17600 KB |
Output is correct |
59 |
Correct |
681 ms |
46412 KB |
Output is correct |
60 |
Correct |
632 ms |
46568 KB |
Output is correct |
61 |
Correct |
428 ms |
912 KB |
Output is correct |
62 |
Correct |
558 ms |
960 KB |
Output is correct |
63 |
Correct |
555 ms |
960 KB |
Output is correct |
64 |
Correct |
477 ms |
1292 KB |
Output is correct |
65 |
Correct |
582 ms |
1304 KB |
Output is correct |
66 |
Correct |
529 ms |
1296 KB |
Output is correct |
67 |
Correct |
540 ms |
1296 KB |
Output is correct |
68 |
Correct |
504 ms |
1300 KB |
Output is correct |
69 |
Correct |
575 ms |
856 KB |
Output is correct |
70 |
Correct |
570 ms |
856 KB |
Output is correct |
71 |
Correct |
502 ms |
2392 KB |
Output is correct |
72 |
Correct |
525 ms |
1288 KB |
Output is correct |
73 |
Correct |
545 ms |
1124 KB |
Output is correct |
74 |
Correct |
564 ms |
1072 KB |
Output is correct |
75 |
Correct |
552 ms |
992 KB |
Output is correct |
76 |
Correct |
552 ms |
2648 KB |
Output is correct |
77 |
Correct |
560 ms |
2648 KB |
Output is correct |
78 |
Correct |
540 ms |
2648 KB |
Output is correct |
79 |
Correct |
573 ms |
1316 KB |
Output is correct |
80 |
Correct |
538 ms |
936 KB |
Output is correct |
81 |
Correct |
521 ms |
912 KB |
Output is correct |
82 |
Correct |
592 ms |
1108 KB |
Output is correct |
83 |
Correct |
279 ms |
848 KB |
Output is correct |
84 |
Correct |
548 ms |
1132 KB |
Output is correct |
85 |
Correct |
543 ms |
1124 KB |
Output is correct |
86 |
Correct |
530 ms |
1128 KB |
Output is correct |
87 |
Correct |
0 ms |
344 KB |
Output is correct |
88 |
Correct |
402 ms |
15264 KB |
Output is correct |
89 |
Correct |
609 ms |
10840 KB |
Output is correct |
90 |
Correct |
629 ms |
10840 KB |
Output is correct |
91 |
Correct |
611 ms |
17744 KB |
Output is correct |
92 |
Correct |
628 ms |
17744 KB |
Output is correct |
93 |
Correct |
615 ms |
17748 KB |
Output is correct |
94 |
Correct |
672 ms |
17744 KB |
Output is correct |
95 |
Correct |
608 ms |
17744 KB |
Output is correct |
96 |
Correct |
592 ms |
11192 KB |
Output is correct |
97 |
Correct |
622 ms |
11192 KB |
Output is correct |
98 |
Correct |
558 ms |
41296 KB |
Output is correct |
99 |
Correct |
662 ms |
17492 KB |
Output is correct |
100 |
Correct |
633 ms |
14232 KB |
Output is correct |
101 |
Correct |
583 ms |
13136 KB |
Output is correct |
102 |
Correct |
606 ms |
11352 KB |
Output is correct |
103 |
Correct |
637 ms |
46612 KB |
Output is correct |
104 |
Correct |
696 ms |
48600 KB |
Output is correct |
105 |
Correct |
585 ms |
48592 KB |
Output is correct |
106 |
Correct |
606 ms |
14928 KB |
Output is correct |
107 |
Correct |
559 ms |
10064 KB |
Output is correct |
108 |
Correct |
490 ms |
10584 KB |
Output is correct |
109 |
Correct |
509 ms |
11724 KB |
Output is correct |