#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n"
#define pii pair<int, int>
#define st first
#define nd second
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define LL node * 2
#define RR node * 2 + 1
#define ll long long
#define MAXN 100005
const int modulo = 1000002022;
const ll INF = 2e18 + 7;
int add(ll a, ll b){
if (a + b < modulo) return a + b;
return a + b - modulo;
}
int subs(ll a, ll b){
if (a < b) return a - b + modulo;
return a - b;
}
int mul(ll a, ll b){
return (a * b) % modulo;
}
int fe(ll a, ll b){
if (b == 0) return 1;
if (b % 2) return mul(a, fe(a, b - 1));
int tmp = fe(a, b / 2);
return mul(tmp, tmp);
}
struct SegTree{
vector<int> t, lazy, arr, sum;
int n;
void build(int node, int l, int r){
if (l == r){
sum[node] = arr[l];
t[node] = 0;
return;
}
int mid = (l + r) / 2;
build(LL, l, mid);
build(RR, mid + 1, r);
sum[node] = add(sum[LL], sum[RR]);
t[node] = 0;
}
void push(int node, int l, int r){
if (lazy[node] == 0) return;
t[node] = subs(sum[node], t[node]);
lazy[node] = 0;
if (l != r) lazy[LL] ^= 1, lazy[RR] ^= 1;
}
void update(int node, int l, int r, int sl, int sr){
push(node, l, r);
if (l > sr || r < sl) return;
if (l >= sl && r <= sr){
lazy[node] ^= 1;
push(node, l, r);
return;
}
int mid = (l + r) / 2;
update(LL, l, mid, sl, sr);
update(RR, mid + 1, r, sl, sr);
t[node] = add(t[LL], t[RR]);
}
int query(int node, int l, int r, int sl, int sr){
push(node, l, r);
if (l > sr || r < sl) return 0;
if (l >= sl && r <= sr) return t[node];
int mid = (l + r) / 2;
return add(query(LL, l, mid, sl, sr), query(RR, mid + 1, r, sl, sr));
}
SegTree(int N, vector<int> a){
n = N;
t.resize(4 * n + 5, 0), lazy.resize(4 * n + 5, 0), sum.resize(4 * n + 5, 0);
arr = a;
build(1, 0, n - 1);
}
};
vector<int> adj[MAXN];
vector<int> pre[MAXN], suf[MAXN];
int val[MAXN], sz[MAXN], n, m;
void dfs(int node){
sz[node] = max((int)1, (int)adj[node].size());
pre[node].resize(adj[node].size());
suf[node].resize(adj[node].size());
for (auto i : adj[node]){
dfs(i);
sz[node] = mul(sz[node], sz[i]);
}
if (adj[node].empty()) return;
pre[node][0] = sz[adj[node][0]];
suf[node][(int)adj[node].size() - 1] = sz[adj[node].back()];
for (int i = 1; i < adj[node].size(); i++)
pre[node][i] = mul(pre[node][i - 1], sz[adj[node][i]]);
for (int i = (int)adj[node].size() - 2; i >= 0; i--)
suf[node][i] = mul(suf[node][i + 1], sz[adj[node][i]]);
}
void dfs2(int node, int x){
val[node] = x;
if (adj[node].empty()) return;
for (int i = 0; i < adj[node].size(); i++){
int m = 1;
if (i > 0) m = mul(m, pre[node][i - 1]);
if (i + 1 < adj[node].size()) m = mul(m, suf[node][i + 1]);
dfs2(adj[node][i], mul(x, m));
}
}
SegTree *T;
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
n = N, m = M;
for (int i = 1; i < n + m; i++)
adj[P[i]].pb(i);
dfs(0);
dfs2(0, 1);
vector<int> arr(m);
for (int i = 0; i < m; i++) arr[i] = val[i + n];
T = new SegTree(m, arr);
for (int i = 0; i < m; i++)
if (A[i]) T->update(1, 0, m - 1, i, i);
}
int count_ways(int L, int R) {
T->update(1, 0, m - 1, L - n, R - n);
return T->query(1, 0, m - 1, 0, m - 1);
}
/*
int main() {
fileio();
int N, M, Q;
assert(3 == scanf("%d %d %d", &N, &M, &Q));
std::vector<int> P(N + M), A(M);
for (int i = 0; i < N + M; ++i) {
assert(1 == scanf("%d", &P[i]));
}
for (int j = 0; j < M; ++j) {
assert(1 == scanf("%d", &A[j]));
}
init(N, M, P, A);
for (int i = 0; i < Q; ++i) {
int L, R;
assert(2 == scanf("%d %d", &L, &R));
printf("%d\n", count_ways(L, R));
}
return 0;
}
*/
Compilation message
circuit.cpp: In function 'void dfs(int)':
circuit.cpp:116:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | for (int i = 1; i < adj[node].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~
circuit.cpp: In function 'void dfs2(int, int)':
circuit.cpp:128:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | for (int i = 0; i < adj[node].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
circuit.cpp:131:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
131 | if (i + 1 < adj[node].size()) m = mul(m, suf[node][i + 1]);
| ~~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8024 KB |
Output is correct |
2 |
Correct |
1 ms |
8024 KB |
Output is correct |
3 |
Correct |
1 ms |
8024 KB |
Output is correct |
4 |
Correct |
1 ms |
8024 KB |
Output is correct |
5 |
Correct |
2 ms |
8024 KB |
Output is correct |
6 |
Correct |
1 ms |
8024 KB |
Output is correct |
7 |
Correct |
2 ms |
8076 KB |
Output is correct |
8 |
Correct |
2 ms |
8024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8024 KB |
Output is correct |
2 |
Correct |
1 ms |
8024 KB |
Output is correct |
3 |
Correct |
2 ms |
8024 KB |
Output is correct |
4 |
Correct |
2 ms |
8024 KB |
Output is correct |
5 |
Correct |
1 ms |
8024 KB |
Output is correct |
6 |
Correct |
2 ms |
8024 KB |
Output is correct |
7 |
Correct |
2 ms |
8024 KB |
Output is correct |
8 |
Correct |
2 ms |
8024 KB |
Output is correct |
9 |
Correct |
2 ms |
8024 KB |
Output is correct |
10 |
Correct |
2 ms |
8280 KB |
Output is correct |
11 |
Correct |
2 ms |
8280 KB |
Output is correct |
12 |
Correct |
2 ms |
8024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8024 KB |
Output is correct |
2 |
Correct |
1 ms |
8024 KB |
Output is correct |
3 |
Correct |
1 ms |
8024 KB |
Output is correct |
4 |
Correct |
1 ms |
8024 KB |
Output is correct |
5 |
Correct |
2 ms |
8024 KB |
Output is correct |
6 |
Correct |
1 ms |
8024 KB |
Output is correct |
7 |
Correct |
2 ms |
8076 KB |
Output is correct |
8 |
Correct |
2 ms |
8024 KB |
Output is correct |
9 |
Correct |
2 ms |
8024 KB |
Output is correct |
10 |
Correct |
1 ms |
8024 KB |
Output is correct |
11 |
Correct |
2 ms |
8024 KB |
Output is correct |
12 |
Correct |
2 ms |
8024 KB |
Output is correct |
13 |
Correct |
1 ms |
8024 KB |
Output is correct |
14 |
Correct |
2 ms |
8024 KB |
Output is correct |
15 |
Correct |
2 ms |
8024 KB |
Output is correct |
16 |
Correct |
2 ms |
8024 KB |
Output is correct |
17 |
Correct |
2 ms |
8024 KB |
Output is correct |
18 |
Correct |
2 ms |
8280 KB |
Output is correct |
19 |
Correct |
2 ms |
8280 KB |
Output is correct |
20 |
Correct |
2 ms |
8024 KB |
Output is correct |
21 |
Correct |
2 ms |
8024 KB |
Output is correct |
22 |
Correct |
3 ms |
8024 KB |
Output is correct |
23 |
Correct |
2 ms |
8020 KB |
Output is correct |
24 |
Correct |
2 ms |
8024 KB |
Output is correct |
25 |
Correct |
2 ms |
8024 KB |
Output is correct |
26 |
Correct |
2 ms |
8024 KB |
Output is correct |
27 |
Correct |
2 ms |
8280 KB |
Output is correct |
28 |
Correct |
2 ms |
8024 KB |
Output is correct |
29 |
Correct |
2 ms |
8020 KB |
Output is correct |
30 |
Correct |
2 ms |
8024 KB |
Output is correct |
31 |
Correct |
1 ms |
8024 KB |
Output is correct |
32 |
Correct |
2 ms |
8024 KB |
Output is correct |
33 |
Correct |
2 ms |
8024 KB |
Output is correct |
34 |
Correct |
1 ms |
8024 KB |
Output is correct |
35 |
Correct |
2 ms |
8024 KB |
Output is correct |
36 |
Correct |
2 ms |
8092 KB |
Output is correct |
37 |
Correct |
2 ms |
8336 KB |
Output is correct |
38 |
Correct |
2 ms |
8280 KB |
Output is correct |
39 |
Correct |
2 ms |
8024 KB |
Output is correct |
40 |
Correct |
2 ms |
8024 KB |
Output is correct |
41 |
Correct |
2 ms |
8024 KB |
Output is correct |
42 |
Correct |
2 ms |
8024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
347 ms |
13912 KB |
Output is correct |
2 |
Runtime error |
32 ms |
27984 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
347 ms |
13912 KB |
Output is correct |
2 |
Runtime error |
32 ms |
27984 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8024 KB |
Output is correct |
2 |
Correct |
1 ms |
8024 KB |
Output is correct |
3 |
Correct |
2 ms |
8024 KB |
Output is correct |
4 |
Correct |
2 ms |
8024 KB |
Output is correct |
5 |
Correct |
1 ms |
8024 KB |
Output is correct |
6 |
Correct |
2 ms |
8024 KB |
Output is correct |
7 |
Correct |
2 ms |
8024 KB |
Output is correct |
8 |
Correct |
2 ms |
8024 KB |
Output is correct |
9 |
Correct |
2 ms |
8024 KB |
Output is correct |
10 |
Correct |
2 ms |
8280 KB |
Output is correct |
11 |
Correct |
2 ms |
8280 KB |
Output is correct |
12 |
Correct |
2 ms |
8024 KB |
Output is correct |
13 |
Correct |
347 ms |
13912 KB |
Output is correct |
14 |
Runtime error |
32 ms |
27984 KB |
Execution killed with signal 6 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8024 KB |
Output is correct |
2 |
Correct |
1 ms |
8024 KB |
Output is correct |
3 |
Correct |
1 ms |
8024 KB |
Output is correct |
4 |
Correct |
1 ms |
8024 KB |
Output is correct |
5 |
Correct |
2 ms |
8024 KB |
Output is correct |
6 |
Correct |
1 ms |
8024 KB |
Output is correct |
7 |
Correct |
2 ms |
8076 KB |
Output is correct |
8 |
Correct |
2 ms |
8024 KB |
Output is correct |
9 |
Correct |
2 ms |
8024 KB |
Output is correct |
10 |
Correct |
1 ms |
8024 KB |
Output is correct |
11 |
Correct |
2 ms |
8024 KB |
Output is correct |
12 |
Correct |
2 ms |
8024 KB |
Output is correct |
13 |
Correct |
1 ms |
8024 KB |
Output is correct |
14 |
Correct |
2 ms |
8024 KB |
Output is correct |
15 |
Correct |
2 ms |
8024 KB |
Output is correct |
16 |
Correct |
2 ms |
8024 KB |
Output is correct |
17 |
Correct |
2 ms |
8024 KB |
Output is correct |
18 |
Correct |
2 ms |
8280 KB |
Output is correct |
19 |
Correct |
2 ms |
8280 KB |
Output is correct |
20 |
Correct |
2 ms |
8024 KB |
Output is correct |
21 |
Correct |
2 ms |
8024 KB |
Output is correct |
22 |
Correct |
3 ms |
8024 KB |
Output is correct |
23 |
Correct |
2 ms |
8020 KB |
Output is correct |
24 |
Correct |
2 ms |
8024 KB |
Output is correct |
25 |
Correct |
2 ms |
8024 KB |
Output is correct |
26 |
Correct |
2 ms |
8024 KB |
Output is correct |
27 |
Correct |
2 ms |
8280 KB |
Output is correct |
28 |
Correct |
2 ms |
8024 KB |
Output is correct |
29 |
Correct |
2 ms |
8020 KB |
Output is correct |
30 |
Correct |
2 ms |
8024 KB |
Output is correct |
31 |
Correct |
1 ms |
8024 KB |
Output is correct |
32 |
Correct |
2 ms |
8024 KB |
Output is correct |
33 |
Correct |
2 ms |
8024 KB |
Output is correct |
34 |
Correct |
1 ms |
8024 KB |
Output is correct |
35 |
Correct |
2 ms |
8024 KB |
Output is correct |
36 |
Correct |
2 ms |
8092 KB |
Output is correct |
37 |
Correct |
2 ms |
8336 KB |
Output is correct |
38 |
Correct |
2 ms |
8280 KB |
Output is correct |
39 |
Correct |
2 ms |
8024 KB |
Output is correct |
40 |
Correct |
2 ms |
8024 KB |
Output is correct |
41 |
Correct |
2 ms |
8024 KB |
Output is correct |
42 |
Correct |
2 ms |
8024 KB |
Output is correct |
43 |
Correct |
391 ms |
8536 KB |
Output is correct |
44 |
Correct |
519 ms |
8536 KB |
Output is correct |
45 |
Correct |
544 ms |
8532 KB |
Output is correct |
46 |
Correct |
543 ms |
8792 KB |
Output is correct |
47 |
Correct |
619 ms |
8792 KB |
Output is correct |
48 |
Correct |
541 ms |
8792 KB |
Output is correct |
49 |
Correct |
604 ms |
8792 KB |
Output is correct |
50 |
Correct |
545 ms |
8792 KB |
Output is correct |
51 |
Correct |
538 ms |
8536 KB |
Output is correct |
52 |
Correct |
562 ms |
8536 KB |
Output is correct |
53 |
Correct |
486 ms |
8792 KB |
Output is correct |
54 |
Correct |
519 ms |
8792 KB |
Output is correct |
55 |
Correct |
561 ms |
8536 KB |
Output is correct |
56 |
Correct |
581 ms |
8536 KB |
Output is correct |
57 |
Correct |
549 ms |
8536 KB |
Output is correct |
58 |
Correct |
541 ms |
9304 KB |
Output is correct |
59 |
Correct |
545 ms |
9304 KB |
Output is correct |
60 |
Correct |
506 ms |
9300 KB |
Output is correct |
61 |
Correct |
511 ms |
8536 KB |
Output is correct |
62 |
Correct |
547 ms |
8536 KB |
Output is correct |
63 |
Correct |
580 ms |
8536 KB |
Output is correct |
64 |
Correct |
525 ms |
8536 KB |
Output is correct |
65 |
Correct |
254 ms |
8280 KB |
Output is correct |
66 |
Correct |
558 ms |
8792 KB |
Output is correct |
67 |
Correct |
572 ms |
8792 KB |
Output is correct |
68 |
Correct |
559 ms |
8792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8024 KB |
Output is correct |
2 |
Correct |
1 ms |
8024 KB |
Output is correct |
3 |
Correct |
1 ms |
8024 KB |
Output is correct |
4 |
Correct |
1 ms |
8024 KB |
Output is correct |
5 |
Correct |
2 ms |
8024 KB |
Output is correct |
6 |
Correct |
1 ms |
8024 KB |
Output is correct |
7 |
Correct |
2 ms |
8076 KB |
Output is correct |
8 |
Correct |
2 ms |
8024 KB |
Output is correct |
9 |
Correct |
2 ms |
8024 KB |
Output is correct |
10 |
Correct |
1 ms |
8024 KB |
Output is correct |
11 |
Correct |
2 ms |
8024 KB |
Output is correct |
12 |
Correct |
2 ms |
8024 KB |
Output is correct |
13 |
Correct |
1 ms |
8024 KB |
Output is correct |
14 |
Correct |
2 ms |
8024 KB |
Output is correct |
15 |
Correct |
2 ms |
8024 KB |
Output is correct |
16 |
Correct |
2 ms |
8024 KB |
Output is correct |
17 |
Correct |
2 ms |
8024 KB |
Output is correct |
18 |
Correct |
2 ms |
8280 KB |
Output is correct |
19 |
Correct |
2 ms |
8280 KB |
Output is correct |
20 |
Correct |
2 ms |
8024 KB |
Output is correct |
21 |
Correct |
2 ms |
8024 KB |
Output is correct |
22 |
Correct |
3 ms |
8024 KB |
Output is correct |
23 |
Correct |
2 ms |
8020 KB |
Output is correct |
24 |
Correct |
2 ms |
8024 KB |
Output is correct |
25 |
Correct |
2 ms |
8024 KB |
Output is correct |
26 |
Correct |
2 ms |
8024 KB |
Output is correct |
27 |
Correct |
2 ms |
8280 KB |
Output is correct |
28 |
Correct |
2 ms |
8024 KB |
Output is correct |
29 |
Correct |
2 ms |
8020 KB |
Output is correct |
30 |
Correct |
2 ms |
8024 KB |
Output is correct |
31 |
Correct |
1 ms |
8024 KB |
Output is correct |
32 |
Correct |
2 ms |
8024 KB |
Output is correct |
33 |
Correct |
2 ms |
8024 KB |
Output is correct |
34 |
Correct |
1 ms |
8024 KB |
Output is correct |
35 |
Correct |
2 ms |
8024 KB |
Output is correct |
36 |
Correct |
2 ms |
8092 KB |
Output is correct |
37 |
Correct |
2 ms |
8336 KB |
Output is correct |
38 |
Correct |
2 ms |
8280 KB |
Output is correct |
39 |
Correct |
2 ms |
8024 KB |
Output is correct |
40 |
Correct |
2 ms |
8024 KB |
Output is correct |
41 |
Correct |
2 ms |
8024 KB |
Output is correct |
42 |
Correct |
2 ms |
8024 KB |
Output is correct |
43 |
Correct |
347 ms |
13912 KB |
Output is correct |
44 |
Runtime error |
32 ms |
27984 KB |
Execution killed with signal 6 |
45 |
Halted |
0 ms |
0 KB |
- |