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 "circuit.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
typedef long long ll;
const int N = 200005, M = 1000002022;
int n, m;
vector<int> g[N];
int s[N];
int a[N];
void dfs0(int x)
{
a[x] = 1;
if (g[x].empty())
return;
for (int i = 0; i < g[x].size(); ++i)
{
int h = g[x][i];
dfs0(h);
a[x] = (a[x] * 1LL * a[h]) % M;
}
a[x] = (a[x] * 1LL * sz(g[x])) % M;
}
int u[N];
void dfs1(int x, int aa)
{
if (g[x].empty())
{
u[x - n] = aa;
return;
}
vector<int> p;
for (int i = 0; i < g[x].size(); ++i)
{
int h = g[x][i];
if (p.empty())
p.push_back(a[h]);
else
p.push_back((p.back() * 1LL * a[h]) % M);
}
vector<int> s;
for (int i = sz(g[x]) - 1; i >= 0; --i)
{
int h = g[x][i];
if (s.empty())
s.push_back(a[h]);
else
s.push_back((s.back() * 1LL * a[h]) % M);
}
reverse(all(s));
for (int i = 0; i < g[x].size(); ++i)
{
int h = g[x][i];
int yaa = aa;
if (i)
yaa = (yaa * 1LL * p[i - 1]) % M;
if (i + 1 < sz(g[x]))
yaa = (yaa * 1LL * s[i + 1]) % M;
dfs1(h, yaa);
}
}
int t[N * 4], su[N * 4];
bool laz[N * 4];
void bil(int tl, int tr, int pos)
{
if (tl == tr)
{
t[pos] = u[tl] * s[tl];
su[pos] = u[tl];
return;
}
int m = (tl + tr) / 2;
bil(tl, m, pos * 2);
bil(m + 1, tr, pos * 2 + 1);
t[pos] = (t[pos * 2] + t[pos * 2 + 1]) % M;
su[pos] = (su[pos * 2] + su[pos * 2 + 1]) % M;
}
void ave(int pos)
{
laz[pos] ^= true;
t[pos] = (su[pos] - t[pos] + M) % M;
}
void shi(int pos)
{
if (!laz[pos])
return;
ave(pos * 2);
ave(pos * 2 + 1);
laz[pos] = false;
}
void ubd(int tl, int tr, int l, int r, int pos)
{
if (l > r)
return;
if (tl == l && tr == r)
{
ave(pos);
return;
}
shi(pos);
int m = (tl + tr) / 2;
ubd(tl, m, l, min(m, r), pos * 2);
ubd(m + 1, tr, max(m + 1, l), r, pos * 2 + 1);
t[pos] = (t[pos * 2] + t[pos * 2 + 1]) % M;
}
void init(int NN, int MM, std::vector<int> P, std::vector<int> A)
{
n = NN;
m = MM;
for (int i = 1; i < n + m; ++i)
g[P[i]].push_back(i);
for (int i = 0; i < m; ++i)
s[i] = A[i];
dfs0(0);
dfs1(0, 1);
bil(0, m - 1, 1);
}
int count_ways(int L, int R)
{
L -= n;
R -= n;
ubd(0, m - 1, L, R, 1);
return t[1];
}
/*
3 4 3
-1 0 1 2 1 1 0
1 0 1 0
3 4
4 5
3 6
*/
Compilation message (stderr)
circuit.cpp: In function 'void dfs0(int)':
circuit.cpp:22:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 0; i < g[x].size(); ++i)
| ~~^~~~~~~~~~~~~
circuit.cpp: In function 'void dfs1(int, int)':
circuit.cpp:41:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int i = 0; i < g[x].size(); ++i)
| ~~^~~~~~~~~~~~~
circuit.cpp:60:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 0; i < g[x].size(); ++i)
| ~~^~~~~~~~~~~~~
# | 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... |