//#include "circuit.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define iter(id, v) for(auto id : v)
#define fs first
#define se second
#define MP make_pair
#define pb push_back
#define bit(msk, i) ((msk >> i) & 1)
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 2e5 + 7;
const int Mod = 1e9 + 2022;
const int INF = 1e9;
const ll BASE = 137;
const int szBL = 350;
int n, m, Q;
int P[N], a[N];
vector<int> ke[N];
pii qr[N];
bool sub4c = 0;
namespace sub3 {
const int N1 = 2e3 + 7;
ll dp[N1][2], f[N1][N1];
void dfs (int u, int p) {
iter (&v, ke[u]) {
if (v != p) {///aware!!!!!!!!
dfs(v, u);
}
}
int m = SZ(ke[u]);
rep (i, 0, m)
rep (j, 0, m) f[i][j] = 0;
f[0][0] = 1;
rep (i, 1, m) {
int v = ke[u][i - 1];
rep (j, 0, m ) {
f[i][j] = f[i - 1][j] * dp[v][0] % Mod;
if (j) (f[i][j] += f[i - 1][j - 1] * dp[v][1] % Mod) %= Mod;
// cout << u<<" "<<v<<","<<i<<" "<<j<<" "<<f[i][j] <<"\n";
}
}
ll pre = f[m][0];
rep (i, 1, m) {
(dp[u][0] += pre) %= Mod;
(pre += f[m][i]) %= Mod;
}
ll suf = 0;
reb (i, m, 1) {
(suf += f[m][i]) %= Mod;
(dp[u][1] += suf) %= Mod;
}
// cout << u<<" "<<dp[u][0]<<","<<dp[u][1] <<"\n";
}
ll solution() {
rep (i, 1, n + m) rep (j, 0, 1) dp[i][j] = 0;
rep (i, n + 1, n + m) dp[i][a[i]] = 1;
dfs(1, 0);
return dp[1][1];
// cout << dp[1][1] <<"\n";
}
}
namespace sub4 {
ll dp[N][2];
int pa[N];
bool check () {
int K = 1; while (K < m) K *= 2;
if (K == m && m == n + 1) {
rep (i, 1, n + m - 1) if (P[i + 1] != (i - 1) / 2) return 0;
return 1;
}
return 0;
}
void update (int u) {
if (SZ(ke[u]) == 0) {
dp[u][a[u]] = 1;
dp[u][a[u] ^ 1] = 0;
return;
}
int v1 = ke[u][0], v2 = ke[u][1];
ll delta = (dp[v1][0] * dp[v2][1] % Mod + dp[v1][1] * dp[v2][0] % Mod) % Mod;
dp[u][0] = (delta + 2LL * dp[v1][0] * dp[v2][0] % Mod) % Mod;
dp[u][1] = (delta + 2LL * dp[v1][1] * dp[v2][1] % Mod) % Mod;
}
void dfs (int u, int p) {
pa[u] = p;
iter (&v, ke[u]) {
dfs(v, u);
}
update (u);
}
void init () {
dfs(1, 0);
}
int solution(int u) {
while (u != 0) {
update(u);
u = pa[u];
}
return dp[1][1];
}
}
int count_ways (int L, int R) {
++L;
++R;
rep (i, L, R) a[i] ^= 1;
if (n <= 1000 & m <= 1000)
return sub3 :: solution();
else if (L == R && sub4c)
return sub4 :: solution(L);
}
void init (int _n, int _m, vector<int> _P, vector<int> _a) {
n = _n;
m = _m;
rep (i, 1, n + m) P[i] = _P[i - 1];
rep (i, 1, m) a[i + n] = _a[i - 1];
rep (i, 1, n + m) {
if (i > 1) {
ke[P[i] + 1].pb(i);
// cout << i <<" "<<P[i] + 1 <<"\n";
}
}
sub4c = sub4 :: check();
if (sub4c)
sub4 :: init();
}
void solution() {
cin >> n >> m >> Q;
rep (i, 1, n + m) {
cin >> P[i];
if (i > 1) {
ke[P[i] + 1].pb(i);
// cout << i <<" "<<P[i] + 1 <<"\n";
}
}
rep (i, n + 1, n + m) cin >> a[i];
if (sub4 :: check())
sub4 :: init();
rep (i, 1, Q) {
int L, R;
cin >> L >> R;
cout << count_ways(L, R) <<"\n";
}
}
//#define file(name) freopen(name".inp","r",stdin); \
//freopen(name".out","w",stdout);
//int main () {
//// file("c");
// ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
// int num_Test = 1;
//// init(3, 4, {-1, 0, 1, 2, 1, 1, 0}, {1, 0, 1, 0});
//// cout << count_ways(3, 4) <<"\n";
//// cin >> num_Test;
// while (num_Test--)
// solution();
//}
/*
no bug +5
3 4 3
-1 0 0 1 1 2 2
1 0 1 0
3 3
4 4
2 2
*/
Compilation message
circuit.cpp:172:1: warning: multi-line comment [-Wcomment]
172 | //#define file(name) freopen(name".inp","r",stdin); \
| ^
circuit.cpp: In function 'int count_ways(int, int)':
circuit.cpp:131:11: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
131 | if (n <= 1000 & m <= 1000)
| ~~^~~~~~~
circuit.cpp:135:1: warning: control reaches end of non-void function [-Wreturn-type]
135 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
23 ms |
16472 KB |
Output is correct |
4 |
Correct |
25 ms |
16984 KB |
Output is correct |
5 |
Correct |
24 ms |
16984 KB |
Output is correct |
6 |
Correct |
25 ms |
16984 KB |
Output is correct |
7 |
Correct |
25 ms |
16984 KB |
Output is correct |
8 |
Correct |
30 ms |
16984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
3 ms |
5460 KB |
Output is correct |
4 |
Correct |
2 ms |
5208 KB |
Output is correct |
5 |
Correct |
3 ms |
5208 KB |
Output is correct |
6 |
Correct |
2 ms |
5208 KB |
Output is correct |
7 |
Correct |
3 ms |
5208 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
2 ms |
5208 KB |
Output is correct |
10 |
Correct |
3 ms |
5208 KB |
Output is correct |
11 |
Correct |
4 ms |
5208 KB |
Output is correct |
12 |
Correct |
3 ms |
5208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
23 ms |
16472 KB |
Output is correct |
4 |
Correct |
25 ms |
16984 KB |
Output is correct |
5 |
Correct |
24 ms |
16984 KB |
Output is correct |
6 |
Correct |
25 ms |
16984 KB |
Output is correct |
7 |
Correct |
25 ms |
16984 KB |
Output is correct |
8 |
Correct |
30 ms |
16984 KB |
Output is correct |
9 |
Correct |
3 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
5208 KB |
Output is correct |
11 |
Correct |
3 ms |
5460 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
5208 KB |
Output is correct |
14 |
Correct |
2 ms |
5208 KB |
Output is correct |
15 |
Correct |
3 ms |
5208 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
18 |
Correct |
3 ms |
5208 KB |
Output is correct |
19 |
Correct |
4 ms |
5208 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5208 KB |
Output is correct |
22 |
Correct |
2 ms |
5208 KB |
Output is correct |
23 |
Correct |
3 ms |
5208 KB |
Output is correct |
24 |
Correct |
4 ms |
5208 KB |
Output is correct |
25 |
Correct |
3 ms |
5208 KB |
Output is correct |
26 |
Correct |
3 ms |
5208 KB |
Output is correct |
27 |
Correct |
3 ms |
5208 KB |
Output is correct |
28 |
Correct |
3 ms |
5208 KB |
Output is correct |
29 |
Correct |
25 ms |
16868 KB |
Output is correct |
30 |
Correct |
24 ms |
16984 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
3 ms |
5208 KB |
Output is correct |
34 |
Correct |
2 ms |
5208 KB |
Output is correct |
35 |
Correct |
6 ms |
6488 KB |
Output is correct |
36 |
Correct |
5 ms |
5208 KB |
Output is correct |
37 |
Correct |
25 ms |
16984 KB |
Output is correct |
38 |
Correct |
28 ms |
16984 KB |
Output is correct |
39 |
Correct |
2 ms |
5208 KB |
Output is correct |
40 |
Correct |
3 ms |
5204 KB |
Output is correct |
41 |
Correct |
3 ms |
5208 KB |
Output is correct |
42 |
Correct |
3 ms |
5208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
451 ms |
8536 KB |
Output is correct |
2 |
Correct |
717 ms |
11864 KB |
Output is correct |
3 |
Correct |
694 ms |
12084 KB |
Output is correct |
4 |
Correct |
709 ms |
12120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
451 ms |
8536 KB |
Output is correct |
2 |
Correct |
717 ms |
11864 KB |
Output is correct |
3 |
Correct |
694 ms |
12084 KB |
Output is correct |
4 |
Correct |
709 ms |
12120 KB |
Output is correct |
5 |
Incorrect |
910 ms |
8536 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 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
3 ms |
5460 KB |
Output is correct |
4 |
Correct |
2 ms |
5208 KB |
Output is correct |
5 |
Correct |
3 ms |
5208 KB |
Output is correct |
6 |
Correct |
2 ms |
5208 KB |
Output is correct |
7 |
Correct |
3 ms |
5208 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
2 ms |
5208 KB |
Output is correct |
10 |
Correct |
3 ms |
5208 KB |
Output is correct |
11 |
Correct |
4 ms |
5208 KB |
Output is correct |
12 |
Correct |
3 ms |
5208 KB |
Output is correct |
13 |
Correct |
451 ms |
8536 KB |
Output is correct |
14 |
Correct |
717 ms |
11864 KB |
Output is correct |
15 |
Correct |
694 ms |
12084 KB |
Output is correct |
16 |
Correct |
709 ms |
12120 KB |
Output is correct |
17 |
Incorrect |
910 ms |
8536 KB |
1st lines differ - on the 1st token, expected: '105182172', found: '2777846' |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
23 ms |
16472 KB |
Output is correct |
4 |
Correct |
25 ms |
16984 KB |
Output is correct |
5 |
Correct |
24 ms |
16984 KB |
Output is correct |
6 |
Correct |
25 ms |
16984 KB |
Output is correct |
7 |
Correct |
25 ms |
16984 KB |
Output is correct |
8 |
Correct |
30 ms |
16984 KB |
Output is correct |
9 |
Correct |
3 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
5208 KB |
Output is correct |
11 |
Correct |
3 ms |
5460 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
5208 KB |
Output is correct |
14 |
Correct |
2 ms |
5208 KB |
Output is correct |
15 |
Correct |
3 ms |
5208 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
18 |
Correct |
3 ms |
5208 KB |
Output is correct |
19 |
Correct |
4 ms |
5208 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5208 KB |
Output is correct |
22 |
Correct |
2 ms |
5208 KB |
Output is correct |
23 |
Correct |
3 ms |
5208 KB |
Output is correct |
24 |
Correct |
4 ms |
5208 KB |
Output is correct |
25 |
Correct |
3 ms |
5208 KB |
Output is correct |
26 |
Correct |
3 ms |
5208 KB |
Output is correct |
27 |
Correct |
3 ms |
5208 KB |
Output is correct |
28 |
Correct |
3 ms |
5208 KB |
Output is correct |
29 |
Correct |
25 ms |
16868 KB |
Output is correct |
30 |
Correct |
24 ms |
16984 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
3 ms |
5208 KB |
Output is correct |
34 |
Correct |
2 ms |
5208 KB |
Output is correct |
35 |
Correct |
6 ms |
6488 KB |
Output is correct |
36 |
Correct |
5 ms |
5208 KB |
Output is correct |
37 |
Correct |
25 ms |
16984 KB |
Output is correct |
38 |
Correct |
28 ms |
16984 KB |
Output is correct |
39 |
Correct |
2 ms |
5208 KB |
Output is correct |
40 |
Correct |
3 ms |
5204 KB |
Output is correct |
41 |
Correct |
3 ms |
5208 KB |
Output is correct |
42 |
Correct |
3 ms |
5208 KB |
Output is correct |
43 |
Incorrect |
478 ms |
5208 KB |
1st lines differ - on the 1st token, expected: '196037954', found: '0' |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
23 ms |
16472 KB |
Output is correct |
4 |
Correct |
25 ms |
16984 KB |
Output is correct |
5 |
Correct |
24 ms |
16984 KB |
Output is correct |
6 |
Correct |
25 ms |
16984 KB |
Output is correct |
7 |
Correct |
25 ms |
16984 KB |
Output is correct |
8 |
Correct |
30 ms |
16984 KB |
Output is correct |
9 |
Correct |
3 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
5208 KB |
Output is correct |
11 |
Correct |
3 ms |
5460 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
5208 KB |
Output is correct |
14 |
Correct |
2 ms |
5208 KB |
Output is correct |
15 |
Correct |
3 ms |
5208 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
18 |
Correct |
3 ms |
5208 KB |
Output is correct |
19 |
Correct |
4 ms |
5208 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5208 KB |
Output is correct |
22 |
Correct |
2 ms |
5208 KB |
Output is correct |
23 |
Correct |
3 ms |
5208 KB |
Output is correct |
24 |
Correct |
4 ms |
5208 KB |
Output is correct |
25 |
Correct |
3 ms |
5208 KB |
Output is correct |
26 |
Correct |
3 ms |
5208 KB |
Output is correct |
27 |
Correct |
3 ms |
5208 KB |
Output is correct |
28 |
Correct |
3 ms |
5208 KB |
Output is correct |
29 |
Correct |
25 ms |
16868 KB |
Output is correct |
30 |
Correct |
24 ms |
16984 KB |
Output is correct |
31 |
Correct |
2 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
3 ms |
5208 KB |
Output is correct |
34 |
Correct |
2 ms |
5208 KB |
Output is correct |
35 |
Correct |
6 ms |
6488 KB |
Output is correct |
36 |
Correct |
5 ms |
5208 KB |
Output is correct |
37 |
Correct |
25 ms |
16984 KB |
Output is correct |
38 |
Correct |
28 ms |
16984 KB |
Output is correct |
39 |
Correct |
2 ms |
5208 KB |
Output is correct |
40 |
Correct |
3 ms |
5204 KB |
Output is correct |
41 |
Correct |
3 ms |
5208 KB |
Output is correct |
42 |
Correct |
3 ms |
5208 KB |
Output is correct |
43 |
Correct |
451 ms |
8536 KB |
Output is correct |
44 |
Correct |
717 ms |
11864 KB |
Output is correct |
45 |
Correct |
694 ms |
12084 KB |
Output is correct |
46 |
Correct |
709 ms |
12120 KB |
Output is correct |
47 |
Incorrect |
910 ms |
8536 KB |
1st lines differ - on the 1st token, expected: '105182172', found: '2777846' |
48 |
Halted |
0 ms |
0 KB |
- |