#include "circuit.h"
#include <vector>
#include <functional>
#include <cmath>
#include <iostream>
using ll = long long;
using namespace std;
//모든 인덱스에 1씩 더하기
int par[200005];
ll tot[200005];
ll dp[200005];
vector<int> g[200005];
vector<int> prf[200005], suf[200005];
const ll mod = 1'000'002'022;
ll ans = 0;
namespace {
int N, M;
}
//A[i]: N+1+i의 상태
struct sumseg
{
vector<ll> tree, lazy;
void setup(int n)
{
int sz = 1 << ((int)ceil(log2(n+1))+1);
tree.resize(sz); lazy.resize(sz, 1);
}
int init(int s, int e, int node, vector<ll> &arr)
{
if (s == e) return tree[node] = arr[s];
else return tree[node] = (init(s, (s+e)/2, 2*node, arr) + init((s+e)/2+1, e, 2*node+1, arr)) % mod;
}
void propagate(int s, int e, int node)
{
tree[node] *= lazy[node];
if (s != e) {
lazy[2*node] *= lazy[node];
lazy[2*node+1] *= lazy[node];
}
lazy[node] = 1;
}
void upd(int s, int e, int node, int l, int r)
{
propagate(s, e, node);
if (e < l || r < s) return;
if (l <= s && e <= r) {
lazy[node] *= -1;
propagate(s, e, node);
return;
}
upd(s, (s+e)/2, 2*node, l, r);
upd((s+e)/2+1, e, 2*node+1, l, r);
tree[node] = (tree[2*node] + tree[2*node+1]) % mod;
}
int query(int s, int e, int node, int l, int r)
{
propagate(s, e, node);
if (e < l || r < s) return 0;
if (l <= s && e <= r) return tree[node];
return (query(s, (s+e)/2, 2*node, l, r) + query((s+e)/2+1, e, 2*node+1, l, r)) % mod;
}
}seg;
void init(int N, int M, std::vector<int> P, std::vector<int> A)
{
::N = N;
::M = M;
seg.setup(M);
par[1] = 0;
for (int i = 1; i < N + M; i++) {
par[i+1] = P[i] + 1;
}
for (int i = 2; i <= N + M; i++) g[par[i]].push_back(i);
function<void(int)> dfs = [&](int v)
{
if (v <= N) {
tot[v] = g[v].size();
for (int j:g[v]) {
dfs(j);
tot[v] = tot[v] * tot[j] % mod;
}
}
else {
tot[v] = 1;
}
};
dfs(1);
function<void(int)> dfs2 = [&](int v)
{
if (v > N) return;
for (int j:g[v]) dfs2(j);
prf[v].resize(g[v].size());
suf[v].resize(g[v].size());
prf[v][0] = tot[g[v][0]];
suf[v].back() = tot[g[v].back()];
for (int i = 1; i < (int)prf[v].size(); i++) {
prf[v][i] = prf[v][i-1] * tot[g[v][i]] % mod;
}
for (int i = (int)suf[v].size() - 2; i >= 0; i--) {
suf[v][i] = suf[v][i+1] * tot[g[v][i]] % mod;
}
};
dfs2(1);
dp[1] = 1;
function<void(int)> dfs3 = [&](int v)
{
for (int j = 0; j < (int)g[v].size(); j++) {
ll mul = 1;
if (j >= 1) mul = mul * prf[v][j-1] % mod;
if (j <= (int)g[v].size() - 2) mul = mul * suf[v][j+1] % mod;
dp[g[v][j]] = dp[v] * mul % mod;
dfs3(g[v][j]);
}
};
dfs3(1);
for (int i = N + 1; i <= N + M; i++) ans = (ans + A[i-N-1] * dp[i]) % mod;
vector<ll> st(M);
for (int i = N + 1; i <= N + M; i++) {
st[i-N-1] = A[i-N-1] == 1 ? -dp[i] : dp[i];
}
seg.init(0, M-1, 1, st);
}
int count_ways(int L, int R) {
int l = L - N, r = R - N;
ans += seg.query(0, M-1, 1, l, r);
seg.upd(0, M-1, 1, l, r);
ans = (ans % mod + mod) % mod;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14288 KB |
Output is correct |
2 |
Correct |
11 ms |
14288 KB |
Output is correct |
3 |
Correct |
9 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14416 KB |
Output is correct |
5 |
Correct |
8 ms |
14400 KB |
Output is correct |
6 |
Correct |
8 ms |
14416 KB |
Output is correct |
7 |
Correct |
11 ms |
14436 KB |
Output is correct |
8 |
Correct |
8 ms |
14416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14372 KB |
Output is correct |
2 |
Correct |
13 ms |
14416 KB |
Output is correct |
3 |
Correct |
12 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14440 KB |
Output is correct |
5 |
Correct |
8 ms |
14524 KB |
Output is correct |
6 |
Correct |
8 ms |
14544 KB |
Output is correct |
7 |
Correct |
8 ms |
14544 KB |
Output is correct |
8 |
Correct |
9 ms |
14544 KB |
Output is correct |
9 |
Correct |
10 ms |
14528 KB |
Output is correct |
10 |
Correct |
8 ms |
14672 KB |
Output is correct |
11 |
Correct |
8 ms |
14672 KB |
Output is correct |
12 |
Correct |
8 ms |
14460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14288 KB |
Output is correct |
2 |
Correct |
11 ms |
14288 KB |
Output is correct |
3 |
Correct |
9 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14416 KB |
Output is correct |
5 |
Correct |
8 ms |
14400 KB |
Output is correct |
6 |
Correct |
8 ms |
14416 KB |
Output is correct |
7 |
Correct |
11 ms |
14436 KB |
Output is correct |
8 |
Correct |
8 ms |
14416 KB |
Output is correct |
9 |
Correct |
8 ms |
14372 KB |
Output is correct |
10 |
Correct |
13 ms |
14416 KB |
Output is correct |
11 |
Correct |
12 ms |
14416 KB |
Output is correct |
12 |
Correct |
8 ms |
14440 KB |
Output is correct |
13 |
Correct |
8 ms |
14524 KB |
Output is correct |
14 |
Correct |
8 ms |
14544 KB |
Output is correct |
15 |
Correct |
8 ms |
14544 KB |
Output is correct |
16 |
Correct |
9 ms |
14544 KB |
Output is correct |
17 |
Correct |
10 ms |
14528 KB |
Output is correct |
18 |
Correct |
8 ms |
14672 KB |
Output is correct |
19 |
Correct |
8 ms |
14672 KB |
Output is correct |
20 |
Correct |
8 ms |
14460 KB |
Output is correct |
21 |
Correct |
8 ms |
14544 KB |
Output is correct |
22 |
Correct |
8 ms |
14416 KB |
Output is correct |
23 |
Correct |
8 ms |
14416 KB |
Output is correct |
24 |
Correct |
8 ms |
14544 KB |
Output is correct |
25 |
Correct |
11 ms |
14544 KB |
Output is correct |
26 |
Correct |
8 ms |
14532 KB |
Output is correct |
27 |
Correct |
8 ms |
14584 KB |
Output is correct |
28 |
Correct |
9 ms |
14596 KB |
Output is correct |
29 |
Correct |
8 ms |
14416 KB |
Output is correct |
30 |
Correct |
9 ms |
14508 KB |
Output is correct |
31 |
Correct |
9 ms |
14544 KB |
Output is correct |
32 |
Correct |
9 ms |
14544 KB |
Output is correct |
33 |
Correct |
13 ms |
14512 KB |
Output is correct |
34 |
Correct |
9 ms |
14416 KB |
Output is correct |
35 |
Correct |
8 ms |
14468 KB |
Output is correct |
36 |
Correct |
8 ms |
14672 KB |
Output is correct |
37 |
Correct |
8 ms |
14672 KB |
Output is correct |
38 |
Correct |
7 ms |
14672 KB |
Output is correct |
39 |
Correct |
8 ms |
14416 KB |
Output is correct |
40 |
Correct |
8 ms |
14416 KB |
Output is correct |
41 |
Correct |
11 ms |
14416 KB |
Output is correct |
42 |
Correct |
8 ms |
14416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
737 ms |
21832 KB |
Output is correct |
2 |
Correct |
1044 ms |
29236 KB |
Output is correct |
3 |
Correct |
957 ms |
29208 KB |
Output is correct |
4 |
Correct |
910 ms |
29252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
737 ms |
21832 KB |
Output is correct |
2 |
Correct |
1044 ms |
29236 KB |
Output is correct |
3 |
Correct |
957 ms |
29208 KB |
Output is correct |
4 |
Correct |
910 ms |
29252 KB |
Output is correct |
5 |
Correct |
829 ms |
21752 KB |
Output is correct |
6 |
Correct |
1128 ms |
29244 KB |
Output is correct |
7 |
Correct |
1060 ms |
29248 KB |
Output is correct |
8 |
Correct |
980 ms |
29256 KB |
Output is correct |
9 |
Correct |
453 ms |
14828 KB |
Output is correct |
10 |
Correct |
1002 ms |
15344 KB |
Output is correct |
11 |
Correct |
881 ms |
15340 KB |
Output is correct |
12 |
Correct |
1014 ms |
15348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14372 KB |
Output is correct |
2 |
Correct |
13 ms |
14416 KB |
Output is correct |
3 |
Correct |
12 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14440 KB |
Output is correct |
5 |
Correct |
8 ms |
14524 KB |
Output is correct |
6 |
Correct |
8 ms |
14544 KB |
Output is correct |
7 |
Correct |
8 ms |
14544 KB |
Output is correct |
8 |
Correct |
9 ms |
14544 KB |
Output is correct |
9 |
Correct |
10 ms |
14528 KB |
Output is correct |
10 |
Correct |
8 ms |
14672 KB |
Output is correct |
11 |
Correct |
8 ms |
14672 KB |
Output is correct |
12 |
Correct |
8 ms |
14460 KB |
Output is correct |
13 |
Correct |
737 ms |
21832 KB |
Output is correct |
14 |
Correct |
1044 ms |
29236 KB |
Output is correct |
15 |
Correct |
957 ms |
29208 KB |
Output is correct |
16 |
Correct |
910 ms |
29252 KB |
Output is correct |
17 |
Correct |
829 ms |
21752 KB |
Output is correct |
18 |
Correct |
1128 ms |
29244 KB |
Output is correct |
19 |
Correct |
1060 ms |
29248 KB |
Output is correct |
20 |
Correct |
980 ms |
29256 KB |
Output is correct |
21 |
Correct |
453 ms |
14828 KB |
Output is correct |
22 |
Correct |
1002 ms |
15344 KB |
Output is correct |
23 |
Correct |
881 ms |
15340 KB |
Output is correct |
24 |
Correct |
1014 ms |
15348 KB |
Output is correct |
25 |
Correct |
1315 ms |
34612 KB |
Output is correct |
26 |
Correct |
1282 ms |
34896 KB |
Output is correct |
27 |
Correct |
1078 ms |
34892 KB |
Output is correct |
28 |
Correct |
960 ms |
34888 KB |
Output is correct |
29 |
Correct |
1248 ms |
44312 KB |
Output is correct |
30 |
Correct |
1000 ms |
44320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14288 KB |
Output is correct |
2 |
Correct |
11 ms |
14288 KB |
Output is correct |
3 |
Correct |
9 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14416 KB |
Output is correct |
5 |
Correct |
8 ms |
14400 KB |
Output is correct |
6 |
Correct |
8 ms |
14416 KB |
Output is correct |
7 |
Correct |
11 ms |
14436 KB |
Output is correct |
8 |
Correct |
8 ms |
14416 KB |
Output is correct |
9 |
Correct |
8 ms |
14372 KB |
Output is correct |
10 |
Correct |
13 ms |
14416 KB |
Output is correct |
11 |
Correct |
12 ms |
14416 KB |
Output is correct |
12 |
Correct |
8 ms |
14440 KB |
Output is correct |
13 |
Correct |
8 ms |
14524 KB |
Output is correct |
14 |
Correct |
8 ms |
14544 KB |
Output is correct |
15 |
Correct |
8 ms |
14544 KB |
Output is correct |
16 |
Correct |
9 ms |
14544 KB |
Output is correct |
17 |
Correct |
10 ms |
14528 KB |
Output is correct |
18 |
Correct |
8 ms |
14672 KB |
Output is correct |
19 |
Correct |
8 ms |
14672 KB |
Output is correct |
20 |
Correct |
8 ms |
14460 KB |
Output is correct |
21 |
Correct |
8 ms |
14544 KB |
Output is correct |
22 |
Correct |
8 ms |
14416 KB |
Output is correct |
23 |
Correct |
8 ms |
14416 KB |
Output is correct |
24 |
Correct |
8 ms |
14544 KB |
Output is correct |
25 |
Correct |
11 ms |
14544 KB |
Output is correct |
26 |
Correct |
8 ms |
14532 KB |
Output is correct |
27 |
Correct |
8 ms |
14584 KB |
Output is correct |
28 |
Correct |
9 ms |
14596 KB |
Output is correct |
29 |
Correct |
8 ms |
14416 KB |
Output is correct |
30 |
Correct |
9 ms |
14508 KB |
Output is correct |
31 |
Correct |
9 ms |
14544 KB |
Output is correct |
32 |
Correct |
9 ms |
14544 KB |
Output is correct |
33 |
Correct |
13 ms |
14512 KB |
Output is correct |
34 |
Correct |
9 ms |
14416 KB |
Output is correct |
35 |
Correct |
8 ms |
14468 KB |
Output is correct |
36 |
Correct |
8 ms |
14672 KB |
Output is correct |
37 |
Correct |
8 ms |
14672 KB |
Output is correct |
38 |
Correct |
7 ms |
14672 KB |
Output is correct |
39 |
Correct |
8 ms |
14416 KB |
Output is correct |
40 |
Correct |
8 ms |
14416 KB |
Output is correct |
41 |
Correct |
11 ms |
14416 KB |
Output is correct |
42 |
Correct |
8 ms |
14416 KB |
Output is correct |
43 |
Correct |
773 ms |
14928 KB |
Output is correct |
44 |
Correct |
949 ms |
15056 KB |
Output is correct |
45 |
Correct |
1016 ms |
15056 KB |
Output is correct |
46 |
Correct |
1024 ms |
15496 KB |
Output is correct |
47 |
Correct |
1071 ms |
15500 KB |
Output is correct |
48 |
Correct |
1067 ms |
15496 KB |
Output is correct |
49 |
Correct |
956 ms |
15560 KB |
Output is correct |
50 |
Correct |
824 ms |
15504 KB |
Output is correct |
51 |
Correct |
871 ms |
14952 KB |
Output is correct |
52 |
Correct |
1064 ms |
15048 KB |
Output is correct |
53 |
Correct |
479 ms |
15440 KB |
Output is correct |
54 |
Correct |
908 ms |
15512 KB |
Output is correct |
55 |
Correct |
944 ms |
15184 KB |
Output is correct |
56 |
Correct |
893 ms |
15056 KB |
Output is correct |
57 |
Correct |
899 ms |
14928 KB |
Output is correct |
58 |
Correct |
859 ms |
15956 KB |
Output is correct |
59 |
Correct |
926 ms |
16024 KB |
Output is correct |
60 |
Correct |
895 ms |
16032 KB |
Output is correct |
61 |
Correct |
935 ms |
15184 KB |
Output is correct |
62 |
Correct |
943 ms |
14928 KB |
Output is correct |
63 |
Correct |
794 ms |
14800 KB |
Output is correct |
64 |
Correct |
934 ms |
14960 KB |
Output is correct |
65 |
Correct |
511 ms |
14800 KB |
Output is correct |
66 |
Correct |
1073 ms |
15340 KB |
Output is correct |
67 |
Correct |
1079 ms |
15340 KB |
Output is correct |
68 |
Correct |
939 ms |
15344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14288 KB |
Output is correct |
2 |
Correct |
11 ms |
14288 KB |
Output is correct |
3 |
Correct |
9 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14416 KB |
Output is correct |
5 |
Correct |
8 ms |
14400 KB |
Output is correct |
6 |
Correct |
8 ms |
14416 KB |
Output is correct |
7 |
Correct |
11 ms |
14436 KB |
Output is correct |
8 |
Correct |
8 ms |
14416 KB |
Output is correct |
9 |
Correct |
8 ms |
14372 KB |
Output is correct |
10 |
Correct |
13 ms |
14416 KB |
Output is correct |
11 |
Correct |
12 ms |
14416 KB |
Output is correct |
12 |
Correct |
8 ms |
14440 KB |
Output is correct |
13 |
Correct |
8 ms |
14524 KB |
Output is correct |
14 |
Correct |
8 ms |
14544 KB |
Output is correct |
15 |
Correct |
8 ms |
14544 KB |
Output is correct |
16 |
Correct |
9 ms |
14544 KB |
Output is correct |
17 |
Correct |
10 ms |
14528 KB |
Output is correct |
18 |
Correct |
8 ms |
14672 KB |
Output is correct |
19 |
Correct |
8 ms |
14672 KB |
Output is correct |
20 |
Correct |
8 ms |
14460 KB |
Output is correct |
21 |
Correct |
8 ms |
14544 KB |
Output is correct |
22 |
Correct |
8 ms |
14416 KB |
Output is correct |
23 |
Correct |
8 ms |
14416 KB |
Output is correct |
24 |
Correct |
8 ms |
14544 KB |
Output is correct |
25 |
Correct |
11 ms |
14544 KB |
Output is correct |
26 |
Correct |
8 ms |
14532 KB |
Output is correct |
27 |
Correct |
8 ms |
14584 KB |
Output is correct |
28 |
Correct |
9 ms |
14596 KB |
Output is correct |
29 |
Correct |
8 ms |
14416 KB |
Output is correct |
30 |
Correct |
9 ms |
14508 KB |
Output is correct |
31 |
Correct |
9 ms |
14544 KB |
Output is correct |
32 |
Correct |
9 ms |
14544 KB |
Output is correct |
33 |
Correct |
13 ms |
14512 KB |
Output is correct |
34 |
Correct |
9 ms |
14416 KB |
Output is correct |
35 |
Correct |
8 ms |
14468 KB |
Output is correct |
36 |
Correct |
8 ms |
14672 KB |
Output is correct |
37 |
Correct |
8 ms |
14672 KB |
Output is correct |
38 |
Correct |
7 ms |
14672 KB |
Output is correct |
39 |
Correct |
8 ms |
14416 KB |
Output is correct |
40 |
Correct |
8 ms |
14416 KB |
Output is correct |
41 |
Correct |
11 ms |
14416 KB |
Output is correct |
42 |
Correct |
8 ms |
14416 KB |
Output is correct |
43 |
Correct |
737 ms |
21832 KB |
Output is correct |
44 |
Correct |
1044 ms |
29236 KB |
Output is correct |
45 |
Correct |
957 ms |
29208 KB |
Output is correct |
46 |
Correct |
910 ms |
29252 KB |
Output is correct |
47 |
Correct |
829 ms |
21752 KB |
Output is correct |
48 |
Correct |
1128 ms |
29244 KB |
Output is correct |
49 |
Correct |
1060 ms |
29248 KB |
Output is correct |
50 |
Correct |
980 ms |
29256 KB |
Output is correct |
51 |
Correct |
453 ms |
14828 KB |
Output is correct |
52 |
Correct |
1002 ms |
15344 KB |
Output is correct |
53 |
Correct |
881 ms |
15340 KB |
Output is correct |
54 |
Correct |
1014 ms |
15348 KB |
Output is correct |
55 |
Correct |
1315 ms |
34612 KB |
Output is correct |
56 |
Correct |
1282 ms |
34896 KB |
Output is correct |
57 |
Correct |
1078 ms |
34892 KB |
Output is correct |
58 |
Correct |
960 ms |
34888 KB |
Output is correct |
59 |
Correct |
1248 ms |
44312 KB |
Output is correct |
60 |
Correct |
1000 ms |
44320 KB |
Output is correct |
61 |
Correct |
773 ms |
14928 KB |
Output is correct |
62 |
Correct |
949 ms |
15056 KB |
Output is correct |
63 |
Correct |
1016 ms |
15056 KB |
Output is correct |
64 |
Correct |
1024 ms |
15496 KB |
Output is correct |
65 |
Correct |
1071 ms |
15500 KB |
Output is correct |
66 |
Correct |
1067 ms |
15496 KB |
Output is correct |
67 |
Correct |
956 ms |
15560 KB |
Output is correct |
68 |
Correct |
824 ms |
15504 KB |
Output is correct |
69 |
Correct |
871 ms |
14952 KB |
Output is correct |
70 |
Correct |
1064 ms |
15048 KB |
Output is correct |
71 |
Correct |
479 ms |
15440 KB |
Output is correct |
72 |
Correct |
908 ms |
15512 KB |
Output is correct |
73 |
Correct |
944 ms |
15184 KB |
Output is correct |
74 |
Correct |
893 ms |
15056 KB |
Output is correct |
75 |
Correct |
899 ms |
14928 KB |
Output is correct |
76 |
Correct |
859 ms |
15956 KB |
Output is correct |
77 |
Correct |
926 ms |
16024 KB |
Output is correct |
78 |
Correct |
895 ms |
16032 KB |
Output is correct |
79 |
Correct |
935 ms |
15184 KB |
Output is correct |
80 |
Correct |
943 ms |
14928 KB |
Output is correct |
81 |
Correct |
794 ms |
14800 KB |
Output is correct |
82 |
Correct |
934 ms |
14960 KB |
Output is correct |
83 |
Correct |
511 ms |
14800 KB |
Output is correct |
84 |
Correct |
1073 ms |
15340 KB |
Output is correct |
85 |
Correct |
1079 ms |
15340 KB |
Output is correct |
86 |
Correct |
939 ms |
15344 KB |
Output is correct |
87 |
Correct |
7 ms |
14288 KB |
Output is correct |
88 |
Correct |
661 ms |
33448 KB |
Output is correct |
89 |
Correct |
961 ms |
28076 KB |
Output is correct |
90 |
Correct |
1026 ms |
27052 KB |
Output is correct |
91 |
Correct |
1053 ms |
35016 KB |
Output is correct |
92 |
Correct |
1008 ms |
35124 KB |
Output is correct |
93 |
Correct |
991 ms |
35016 KB |
Output is correct |
94 |
Correct |
1155 ms |
34992 KB |
Output is correct |
95 |
Correct |
1208 ms |
35000 KB |
Output is correct |
96 |
Correct |
999 ms |
24136 KB |
Output is correct |
97 |
Correct |
1127 ms |
24128 KB |
Output is correct |
98 |
Correct |
781 ms |
35912 KB |
Output is correct |
99 |
Correct |
1188 ms |
34868 KB |
Output is correct |
100 |
Correct |
1116 ms |
28852 KB |
Output is correct |
101 |
Correct |
1123 ms |
26232 KB |
Output is correct |
102 |
Correct |
1098 ms |
24020 KB |
Output is correct |
103 |
Correct |
1120 ms |
44224 KB |
Output is correct |
104 |
Correct |
1122 ms |
45496 KB |
Output is correct |
105 |
Correct |
1072 ms |
45488 KB |
Output is correct |
106 |
Correct |
911 ms |
29616 KB |
Output is correct |
107 |
Correct |
1113 ms |
26312 KB |
Output is correct |
108 |
Correct |
1087 ms |
25672 KB |
Output is correct |
109 |
Correct |
1078 ms |
24176 KB |
Output is correct |