1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| #include<bits/stdc++.h> #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> #define W while #define I inline #define RI register int #define LL long long #define int LL #define Cn const #define CI Cn int& #define gc getchar #define D isdigit(c=gc()) #define pc(c) putchar((c)) #define min(x,y) ((x)<(y)?(x):(y)) #define max(x,y) ((x)>(y)?(x):(y)) using namespace std; namespace Debug{ Tp I void _debug(Cn char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts I void _debug(Cn char* f,Ty x,Ar... y){W(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,Cn vector<Ty>& V){os<<"[";for(Cn auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; namespace FastIO{ Tp I void read(Ty& x){char c;int f=1;x=0;W(!D) f=c^'-'?1:-1;W(x=(x<<3)+(x<<1)+(c&15),D);x*=f;} Ts I void read(Ty& x,Ar&... y){read(x),read(y...);} Tp I void write(Ty x){x<0&&(pc('-'),x=-x,0),x<10?(pc(x+'0'),0):(write(x/10),pc(x%10+'0'),0);} Tp I void writeln(Cn Ty& x){write(x),pc('\n');} }using namespace FastIO; Cn int N=3e5+10,P=998244353; int n,a[N],b[N],cnt,rt[N],Ans,Inv; vector<int> v[N]; class SegmentTree{ private: int id;struct node{int l,r,v,t;}T[N*80]; #define mid (l+r>>1) #define PT int& x,CI l=1,CI r=cnt #define LT T[x].l,l,mid #define RT T[x].r,mid+1,r #define PU(x) (T[x].v=(T[T[x].l].v+T[T[x].r].v)%P) I void PD(CI x){ T[x].t!=1&&( T[x].l&&(T[T[x].l].v=1LL*T[T[x].l].v*T[x].t%P,T[T[x].l].t=1LL*T[T[x].l].t*T[x].t%P), T[x].r&&(T[T[x].r].v=1LL*T[T[x].r].v*T[x].t%P,T[T[x].r].t=1LL*T[T[x].r].t*T[x].t%P), T[x].t=1); } public: I void U(CI p,PT){ if(!x) T[x=++id]=(node){0,0,0,1}; if(l==r) return void(T[x].v=1); PD(x),p<=mid?U(p,LT):U(p,RT),PU(x); } I int M(CI x,CI y,CI p,CI l=1,CI r=cnt,CI xmul=0,CI ymul=0){ if(!x&&!y) return 0; if(!x) return T[y].v=1LL*T[y].v*ymul%P,T[y].t=1LL*T[y].t*ymul%P,y; if(!y) return T[x].v=1LL*T[x].v*xmul%P,T[x].t=1LL*T[x].t*xmul%P,x; PD(x),PD(y); RI lx=T[T[x].l].v,ly=T[T[y].l].v,rx=T[T[x].r].v,ry=T[T[y].r].v; return T[x].l=M(T[x].l,T[y].l,p,l,mid,(xmul+1LL*ry*(1-p+P))%P,(ymul+1LL*rx*(1-p+P))%P), T[x].r=M(T[x].r,T[y].r,p,mid+1,r,(xmul+1LL*ly*p)%P,(ymul+1LL*lx*p)%P),PU(x),x; } I int Q(CI p,PT){ if(l==r) return T[x].v; return PD(x),p<=mid?Q(p,LT):Q(p,RT); } }T; I int QP(RI a,RI b){RI s=1;W(b) b&1&&(s=1LL*s*a%P),a=1LL*a*a%P,b>>=1;return s;} #define LW(x) (lower_bound(b+1,b+cnt+1,x)-b) I void Dfs(CI x){ if(!v[x].size()) return T.U(a[x],rt[x]); for(auto i:v[x]) Dfs(i); if(v[x].size()==1) rt[x]=rt[v[x][0]]; else rt[x]=T.M(rt[v[x][0]],rt[v[x][1]],a[x]); } I int Sqr(CI x){return 1LL*x*x%P;} signed main(){ RI i,x;for(Inv=QP(10000,P-2),read(n),i=1;i<=n;i++) read(x),v[x].push_back(i); for(i=1;i<=n;i++) read(a[i]),!v[i].size()?b[++cnt]=a[i]:a[i]=1LL*a[i]*Inv%P; for(sort(b+1,b+cnt+1),i=1;i<=n;i++) !v[i].size()&&(a[i]=LW(a[i])); for(Dfs(1),i=1;i<=cnt;i++) Ans=(1LL*Ans+1LL*i*b[i]%P*Sqr(T.Q(i,rt[1])))%P; return writeln(Ans),0; }
|