This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt __builtin_popcount
#define pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
#define ll long long
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
// template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(Tree <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
// for random generations
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// mt19937 myrand(131);
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
// void setIO(string str = "") {
// fastIO();
// // if(str == "input") {
// // freopen("input.txt", "r", stdin);
// // freopen("output.txt", "w", stdout);
// // } else if(str != "") {
// // freopen((str + ".in").c_str(), "r", stdin);
// // freopen((str + ".out").c_str(), "w", stdout);
// // }
// }
const long long mod = 1000002022;
const int N = 3e5 + 10;
vector<int> adj[N];
long long dp[N][2];
int n, m, a[N], par[N];
int cnt = 0;
long long add(long long a, long long b) {
return (a + b) % mod;
}
long long mult(long long a, long long b) {
return (a * b) % mod;
}
long long sub(long long a, long long b) {
return (a - b + 2 * mod) % mod;
}
struct segTree {
struct node {
long long a, b;
long long lazy;
};
vector<node> tree;
int size = 1;
void init(int s) {
while(size < s) {
size *= 2;
}
tree.assign(2 * size - 1, {0, 0, 0});
}
node merge(node a, node b) {
a.a = add(a.a, b.a);
a.b = add(a.b, b.b);
a.lazy = 0;
return a;
}
void propogate(int x, int lx, int rx) {
if(rx - lx == 1 || !tree[x].lazy) {
tree[x].lazy = 0;
return;
}
tree[2 * x + 1].lazy += tree[x].lazy;
tree[2 * x + 1].lazy %= 2;
swap(tree[2 * x + 1].a, tree[2 * x + 1].b);
tree[2 * x + 2].lazy += tree[x].lazy;
tree[2 * x + 2].lazy %= 2;
swap(tree[2 * x + 2].a, tree[2 * x + 2].b);
tree[x].lazy = 0;
}
void upd(int u, int is_on, long long val, int x, int lx, int rx) {
if(rx - lx == 1) {
if(is_on) {
tree[x].a = val;
tree[x].b = 0;
} else {
tree[x].a = 0;
tree[x].b = val;
}
return;
}
int mid = (lx + rx) / 2;
if(u < mid) {
upd(u, is_on, val, 2 * x + 1, lx, mid);
} else {
upd(u, is_on, val, 2 * x + 2, mid, rx);
}
tree[x] = merge(tree[2 * x + 1], tree[2 * x + 2]);
}
void upd(int u, int is_on, long long val) {
upd(u, is_on, val, 0, 0, size);
}
void change(int l, int r, int x, int lx, int rx) {
propogate(x, lx, rx);
if(lx >= r || rx <= l) {
return;
}
if(lx >= l && rx <= r) {
assert(tree[x].lazy == 0);
swap(tree[x].a, tree[x].b);
tree[x].lazy++;
propogate(x, lx, rx);
return;
}
int mid = (lx + rx) / 2;
change(l, r, 2 * x + 1, lx, mid);
change(l, r, 2 * x + 2, mid, rx);
tree[x] = merge(tree[2 * x + 1], tree[2 * x + 2]);
}
void change(int l, int r) {
change(l, r, 0, 0, size);
}
};
segTree seg;
long long s[N];
void dfs1(int node) {
if(sz(adj[node]) == 0) {
s[node] = 1;
} else {
s[node] = sz(adj[node]);
for(auto i: adj[node]) {
dfs1(i);
s[node] = mult(s[node], s[i]);
}
}
}
void dfs2(int node, long long coef) {
if(sz(adj[node]) == 0) {
dp[node][a[node]] = coef;
dp[node][1 - a[node]] = 0;
} else if(sz(adj[node]) == 1) {
int cur = adj[node][0];
par[cur] = node;
dfs2(cur, coef);
} else {
int first = adj[node][0], second = adj[node][1];
par[first] = par[second] = node;
vector<long long> pref(sz(adj[node]) + 1, 1), suf(sz(adj[node]) + 1, 1);
for(int i = 0; i < sz(adj[node]); i++) {
pref[i + 1] = mult(pref[i], s[adj[node][i]]);
}
for(int i = sz(adj[node]); i >= 1; i--) {
suf[i - 1] = mult(suf[i], s[adj[node][i - 1]]);
}
for(int i = 0; i < sz(adj[node]); i++) {
long long mulfCof = mult(pref[i], suf[i + 1]);
dfs2(adj[node][i], mult(coef, mulfCof));
}
}
}
void init(int maxN, int M, vector<int> P, vector<int> A) {
n = maxN, m = M;
for(int i = 1; i < sz(P); i++) {
adj[P[i]].push_back(i);
}
par[0] = -1;
for(int i = 0; i < m; i++) {
a[n + i] = A[i];
cnt += a[n + i];
}
dfs1(0);
dfs2(0, 1);
seg.init(n + m + 5);
for(int i = 0; i < m; i++) {
int cur = n + i;
seg.upd(cur, a[cur], dp[cur][a[cur]]);
}
}
int count_ways(int L, int R) {
seg.change(L, R + 1);
seg.propogate(0, 0, seg.size);
return seg.tree[0].a;
}
# | 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... |