#include "circuit.h"
#ifdef NYAOWO
#include "stub.cpp"
#endif
#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define eb emplace_back
#define int LL
using namespace std;
using i32 = int32_t;
using LL = long long;
using pii = pair<int, int>;
const int MAXN = 200'000;
const int MOD = 1'000'002'022;
int n, m;
vector<int> adj[MAXN + 10];
int p[MAXN + 10];
int a[MAXN + 10];
int s[MAXN + 10];
void dfs(int x) {
if(x >= n) {
s[x] = 1;
return;
}
s[x] = sz(adj[x]);
for(auto &i:adj[x]) {
dfs(i);
s[x] = s[x] * s[i] % MOD;
}
}
int ss[MAXN + 10];
void dfs2(int x, int owo) {
if(x >= n) {
ss[x] = owo;
return;
}
int l = adj[x][0];
int r = adj[x][1];
dfs2(l, owo * s[r] % MOD);
dfs2(r, owo * s[l] % MOD);
}
int ans = 0;
void update_leaf(int pos, int val) {
ans = ((ans + val * ss[pos]) % MOD + MOD) % MOD;
}
void init(i32 N, i32 M, std::vector<i32> P, std::vector<i32> A) {
n = N; m = M;
p[0] = -1;
For(i, 1, n + m - 1) {
adj[P[i]].eb(i);
p[i] = P[i];
}
dfs(0);
dfs2(0, 1);
For(i, 0, m - 1) {
a[n + i] = A[i];
update_leaf(n + i, A[i]);
}
}
i32 count_ways(i32 L, i32 R) {
int i = min(L, R);
int last = i;
int d = 1 - 2 * a[i];
a[i] += d;
// int cum = 1;
// while(i != 0) {
// i = p[i];
// int l = adj[i][0];
// int r = adj[i][1];
// if(last == l) {
// cum = (cum * s[r]) % MOD;
// } else {
// cum = (cum * s[l]) % MOD;
// }
// }
// ans = (ans + cum * d) % MOD;
// ans = (ans + MOD) % MOD;
update_leaf(i, d);
return (i32)ans;
}
/*
3 4 3
-1 0 1 2 1 1 0
1 0 1 0
3 4
4 5
3 6
2
0
6
*/
Compilation message
circuit.cpp: In function 'i32 count_ways(i32, i32)':
circuit.cpp:77:9: warning: unused variable 'last' [-Wunused-variable]
77 | int last = i;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4944 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4944 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4944 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
424 ms |
8272 KB |
Output is correct |
2 |
Correct |
779 ms |
11592 KB |
Output is correct |
3 |
Correct |
760 ms |
11600 KB |
Output is correct |
4 |
Correct |
597 ms |
11656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
424 ms |
8272 KB |
Output is correct |
2 |
Correct |
779 ms |
11592 KB |
Output is correct |
3 |
Correct |
760 ms |
11600 KB |
Output is correct |
4 |
Correct |
597 ms |
11656 KB |
Output is correct |
5 |
Incorrect |
662 ms |
8272 KB |
1st lines differ - on the 1st token, expected: '105182172', found: '2777846' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4944 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4944 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4944 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |