#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 200005;
const int MOD = 1000002022;
vector<int> adj[MAXN];
int f[1003][2], g[1003][1003];
int pa[MAXN], sz[MAXN], nArr, mArr;
bool state[MAXN];
void init(int _N, int _M, vector<int> _P, vector<int> _A) {
nArr = _N, mArr = _M;
for (int i = 0; i < nArr + mArr; ++i) {
pa[i] = _P[i];
if(pa[i] != -1)
adj[pa[i]].push_back(i);
}
for (int i = 0; i < mArr; ++i)
state[nArr + i] = _A[i];
}
int count_ways(int L, int R) {
for (int i = L; i <= R; ++i)
state[i] = 1 - state[i];
for (int i = nArr + mArr - 1; i >= 0; --i) {
for (int j = 0; j <= sz[i]; ++j)
g[i][j] = 0;
f[i][0] = f[i][1] = 0;
}
for (int i = nArr + mArr - 1; i >= 0; --i) {
if(i >= nArr) {
f[i][state[i]] = 1;
sz[i] = 1;
} else {
sz[i] = 0;
g[i][0] = 1;
for (int it = 0; it < sz(adj[i]); ++it) {
int j(adj[i][it]);
++sz[i];
for (int k = sz[i]; k >= 0; --k)
g[i][k] = (1LL * g[i][k] * f[j][0] + 1LL * (k > 0 ? g[i][k - 1] : 0) * f[j][1]) % MOD;
}
for (int j = 0; j <= sz[i]; ++j) {
f[i][0] = (f[i][0] + 1LL * g[i][j] * (sz[i] - j)) % MOD;
f[i][1] = (f[i][1] + 1LL * g[i][j] * j) % MOD;
}
}
}
return f[0][1];
}
#ifdef Nhoksocqt1
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TASK "circuit"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
vector<int> _P, _A;
int _N, _M, _Q;
cin >> _N >> _M >> _Q;
_P.resize(_N + _M);
for (int i = 0; i < _N + _M; ++i)
cin >> _P[i];
_A.resize(_M);
for (int i = 0; i < _M; ++i)
cin >> _A[i];
init(_N, _M, _P, _A);
for (int t = 0; t < _Q; ++t) {
int _L, _R;
cin >> _L >> _R;
cout << count_ways(_L, _R) << '\n';
}
return 0;
}
#endif // Nhoksocqt1
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8536 KB |
Output is correct |
3 |
Correct |
6 ms |
10584 KB |
Output is correct |
4 |
Correct |
7 ms |
10472 KB |
Output is correct |
5 |
Correct |
9 ms |
10740 KB |
Output is correct |
6 |
Correct |
6 ms |
10584 KB |
Output is correct |
7 |
Correct |
6 ms |
10584 KB |
Output is correct |
8 |
Correct |
7 ms |
10584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
10676 KB |
Output is correct |
3 |
Runtime error |
10 ms |
21168 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8536 KB |
Output is correct |
3 |
Correct |
6 ms |
10584 KB |
Output is correct |
4 |
Correct |
7 ms |
10472 KB |
Output is correct |
5 |
Correct |
9 ms |
10740 KB |
Output is correct |
6 |
Correct |
6 ms |
10584 KB |
Output is correct |
7 |
Correct |
6 ms |
10584 KB |
Output is correct |
8 |
Correct |
7 ms |
10584 KB |
Output is correct |
9 |
Correct |
3 ms |
8536 KB |
Output is correct |
10 |
Correct |
2 ms |
10676 KB |
Output is correct |
11 |
Runtime error |
10 ms |
21168 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
19 ms |
19880 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
19 ms |
19880 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
10676 KB |
Output is correct |
3 |
Runtime error |
10 ms |
21168 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8536 KB |
Output is correct |
3 |
Correct |
6 ms |
10584 KB |
Output is correct |
4 |
Correct |
7 ms |
10472 KB |
Output is correct |
5 |
Correct |
9 ms |
10740 KB |
Output is correct |
6 |
Correct |
6 ms |
10584 KB |
Output is correct |
7 |
Correct |
6 ms |
10584 KB |
Output is correct |
8 |
Correct |
7 ms |
10584 KB |
Output is correct |
9 |
Correct |
3 ms |
8536 KB |
Output is correct |
10 |
Correct |
2 ms |
10676 KB |
Output is correct |
11 |
Runtime error |
10 ms |
21168 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8536 KB |
Output is correct |
3 |
Correct |
6 ms |
10584 KB |
Output is correct |
4 |
Correct |
7 ms |
10472 KB |
Output is correct |
5 |
Correct |
9 ms |
10740 KB |
Output is correct |
6 |
Correct |
6 ms |
10584 KB |
Output is correct |
7 |
Correct |
6 ms |
10584 KB |
Output is correct |
8 |
Correct |
7 ms |
10584 KB |
Output is correct |
9 |
Correct |
3 ms |
8536 KB |
Output is correct |
10 |
Correct |
2 ms |
10676 KB |
Output is correct |
11 |
Runtime error |
10 ms |
21168 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |