#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
const int mod = 1000002022;
vector<vector<int>> g;
vector<int> a;
int n, m;
vector<int> P;
void dfs1(int v){
P[v] = (int) g[v].size();
if (!P[v]) P[v] = 1;
for (int i: g[v]){
dfs1(i);
P[v] = (1LL * P[v] * P[i]) % mod;
}
}
vector<int> f;
void dfs2(int v){
if (g[v].empty()) return;
vector<int> p1(g[v].size() + 1), p2(g[v].size() + 2);
p1[0] = 1;
for (int i = 1; i <= g[v].size(); i++){
p1[i] = (1LL * p1[i - 1] * P[g[v][i - 1]]) % mod;
}
p2.back() = 1;
for (int i = (int) g[v].size(); i > 0; i--){
p2[i] = (1LL * p2[i + 1] * P[g[v][i - 1]]) % mod;
}
for (int ii = 1; ii <= g[v].size(); ii++){
int i = g[v][ii - 1];
int x = (1LL * p1[ii - 1] * p2[ii + 1]) % mod;
f[i] = (1LL * f[v] * x) % mod;
dfs2(i);
}
}
struct node{
int s, X;
};
vector<node> t;
vector<bool> p;
void build(int v, int tl, int tr){
if (tl == tr){
t[v].X = f[tl + n - 1];
t[v].s = a[tl - 1] * t[v].X;
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
build(vv, tl, tm);
build(vv + 1, tm + 1, tr);
t[v].s = (t[vv].s + t[vv + 1].s) % mod;
t[v].X = (t[vv].X + t[vv + 1].X) % mod;
}
void push(int& v){
if (!p[v]) return;
int vv = 2 * v;
t[vv].s = (t[vv].X - t[vv].s) % mod;
if (t[vv].s < 0) t[vv].s += mod;
t[vv + 1].s = (t[vv + 1].X - t[vv + 1].s) % mod;
if (t[vv + 1].s < 0) t[vv + 1].s += mod;
p[vv] = !p[vv]; p[vv + 1] = !p[vv + 1];
p[v] = 0;
}
void upd(int v, int tl, int tr, int& l, int& r){
if (l > tr || r < tl) return;
if (l <= tl && tr <= r){
t[v].s = (t[v].X - t[v].s) % mod;
if (t[v].s < 0) t[v].s += mod;
p[v] = !p[v];
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
push(v);
upd(vv, tl, tm, l, r);
upd(vv + 1, tm + 1, tr, l, r);
t[v].s = (t[vv].s + t[vv + 1].s) % mod;
}
void upd(int l, int r){
upd(1, 1, m, l, r);
}
int count_ways(int l, int r){
l = (l - n + 1);
r = (r - n + 1);
upd(l, r);
return t[1].s;
}
void init(int N, int M, vector<int> pr, vector<int> A){
n = N; m = M; a = A;
g.resize(n + m);
for (int i = 1; i < n + m; i++){
g[pr[i]].pb(i);
}
P.resize(n + m);
dfs1(0);
f.resize(n + m);
f[0] = 1;
dfs2(0);
t.resize(4 * m);
p.resize(4 * m);
build(1, 1, m);
}
# | 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... |