This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
pair<int, int> pii;
const int MOD = 1000002022;
const int maxn = 2e5 + 100;
int n, k;
int p[maxn];
vector<int> g[maxn];
int a[maxn];
int t[maxn * 4];
ll sum[maxn];
ll d[2][maxn * 4];
ll dp[maxn];
void pre(int v){
swap(d[0][v], d[1][v]);
t[v] ^= 1;
}
void push(int v, int tl, int tr){
if(tl == tr || !t[v]) return;
t[v] = 0; pre(v<<1); pre(v<<1|1);
}
void build(int v = 1, int tl = k + 1, int tr = n){
if(tl == tr){
d[a[tl]][v] = dp[tl];
} else{
int mid = (tl + tr) >> 1;
build(v<<1, tl, mid);
build(v<<1|1, mid+1, tr);
d[0][v] = (d[0][v<<1] + d[0][v<<1|1]) % MOD;
d[1][v] = (d[1][v<<1] + d[1][v<<1|1]) % MOD;
}
}
void upd(int l, int r, int v = 1, int tl = k + 1, int tr = n){
push(v, tl, tr);
if(tl > r || tr < l) return;
if(l <= tl && tr <= r) pre(v);
else{
int mid = (tl + tr) >> 1;
upd(l, r, v<<1, tl, mid);
upd(l, r, v<<1|1, mid+1, tr);
d[0][v] = (d[0][v<<1] + d[0][v<<1|1]) % MOD;
d[1][v] = (d[1][v<<1] + d[1][v<<1|1]) % MOD;
}
}
void init(int N, int M, std::vector<int> P, std::vector<int> A){
n = N + M; k = N;
for(int i = 1; i <= n; i++){
p[i] = P[i-1]; p[i]++;
g[p[i]].push_back(i);
dp[i] = 1;
}
for(int v = n; v > 0; v--){
sum[v] = 1;
for(int to: g[v]){
sum[v] = sum[v] * sum[to] % MOD;
}
if(v <= k) sum[v] = sum[v] * (ll)g[v].size() % MOD;
}
for(int i = k + 1; i <= n; i++){
a[i] = A[i - k - 1];
}
for(int v = 1; v <= k; v++){
ll d = 1;
for(int to: g[v]){
dp[to] = dp[v];
dp[to] = dp[to] * d % MOD;
d = d * sum[to] % MOD;
}
d = 1;
reverse(g[v].begin(), g[v].end());
for(int to: g[v]){
dp[to] = dp[to] * d % MOD;
d = d * sum[to] % MOD;
}
}
build();
}
int count_ways(int L, int R){
upd(L + 1, R + 1);
return d[1][1];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |