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 forn(i,n) for(int i=0; i<n; ++i)
#define pb push_back
#define all(x) x.begin(), x.end()
#define pi pair<int,int>
#define f first
#define s second
using ll = long long;
#define int int64_t
const int N=2e5+5;
int n,m;
vector<int> adj[N];
int dp[N][2];
int a[N];
const int mod = 1e9+2022;
int f[N];
int z[N];
int one=1;
void dfs(int u) {
z[u]=adj[u].size();
z[u]=max(z[u],one);
for(auto&v:adj[u]) {
dfs(v);
z[u]=(z[u]*z[v])%mod;
}
}
void dfs(int u, int o) {
vector<int> pr={1}, sf={1};
for(auto&v:adj[u]) pr.pb( (pr.back()*z[v])%mod );
reverse(all(adj[u]));
for(auto&v:adj[u]) sf.pb( (sf.back()*z[v])%mod );
reverse(all(sf));
reverse(all(adj[u]));
f[u]=o;
forn(i,adj[u].size()) {
int x = (o*pr[i])%mod;
x = (x*sf[i+1])%mod;
dfs(adj[u][i],x);
}
}
const int sz=1<<17;
struct node {
int sum=0,now=0,lazy=0;
};
node t[4*sz];
node merge(node a, node b) {
node n;
n.sum=(a.sum+b.sum)%mod;
n.now=(a.now+b.now)%mod;
n.lazy=0;
return n;
}
void build(int v, int l, int r) {
if (r-l==1) return;
int m=(l+r)>>1;
build(2*v+1,l,m);
build(2*v+2,m,r);
t[v]=merge(t[2*v+1],t[2*v+2]);
}
void build() {
forn(i,m) {
t[sz-1+i].sum=f[n+i];
t[sz-1+i].now=a[i]*f[n+i];
t[sz-1+i].lazy=0;
}
build(0,0,sz);
}
void push(int v) {
if (t[v].lazy) {
t[v].now=(t[v].sum-t[v].now+mod)%mod;
t[2*v+1].lazy^=t[v].lazy;
t[2*v+2].lazy^=t[v].lazy;
t[v].lazy=0;
}
}
void flip(int v, int l, int r, int lx, int rx) {
if (t[v].lazy) push(v);
if (rx<=l || r<=lx) return;
if (lx<=l && r<=rx) {
t[v].lazy^=1;
push(v);
return;
}
int m=(l+r)>>1;
flip(2*v+1,l,m,lx,rx);
flip(2*v+2,m,r,lx,rx);
t[v]=merge(t[2*v+1],t[2*v+2]);
}
void flip(int l, int r) {
flip(0,0,sz,l,r);
}
#undef int
void init(int _n, int _m, vector<int>p, vector<int>_a) {
n=_n, m=_m;
forn(i,m) a[i]=_a[i];
for (int i=1; i<n+m; ++i) {
adj[p[i]].pb(i);
}
dfs(0);
dfs(0,1);
build();
}
int count_ways(int l, int r) {
flip(l-n,r-n+1);
return t[0].now;
}
Compilation message (stderr)
circuit.cpp: In function 'void dfs(int64_t, int64_t)':
circuit.cpp:4:33: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define forn(i,n) for(int i=0; i<n; ++i)
......
43 | forn(i,adj[u].size()) {
| ~~~~~~~~~~~~~~~
circuit.cpp:43:3: note: in expansion of macro 'forn'
43 | forn(i,adj[u].size()) {
| ^~~~
# | 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... |