Luogu P5298 [PKUWC2018]Minimax

好劲的题目啊,根本没往线段树合并方面去想啊

首先每种权值都有可能出现,因此我们先排个序然后一个一个求概率

由于此时数的值域变成\([1,m]\)(离散以后),我们可以设一个DP:\(f_{x,i}\)表示节点\(x\)的权值为\(i\)的概率

转移的话分\(x\)有几个子节点讨论,若没有或是只有一个都是随便转移的

考虑如果有两个,记为\(lc\)\(rc\),显然我们可以列出转移方程(此时\(i\)在左儿子中,右儿子同理):

\[f_{x,i}=f_{lc,i}\times(p_x\times \sum_{j=1}^{i-1} f_{rc,j}+(1-p_x)\sum_{j=i+1}^m f_{rc,j}) \]

我们发现这个式子有一些特征:它转移要乘上的是一段前缀和与一段后缀和,这个如果我们用线段树来维护会很好处理

然后由于每个数只会在一棵子树里,换句话说每个数在每个节点的概率只会造成一次的贡献

更妙的是,我们发现一个数造成贡献的时候由于这一段值域区间乘上的数都是一样的,因此打一个标记每次下传即可

用线段树合并实现,总复杂度\(O(n\log n)\)

#include<cstdio>
#include<algorithm>
#define RI register int
#define CI const int&
using namespace std;
const int N=300005,mod=998244353;
struct edge
{
	int to,nxt;
}e[N]; int n,head[N],a[N],rst[N],cnt,num,x,rt[N],ans;
inline int sum(CI x,CI y)
{
	int t=x+y; return t>=mod?t-mod:t;
}
inline int sub(CI x,CI y)
{
	int t=x-y; return t<0?t+mod:t;
}
inline int quick_pow(int x,int p=mod-2,int mul=1)
{
	for (;p;p>>=1,x=1LL*x*x%mod) if (p&1) mul=1LL*mul*x%mod; return mul;
}
inline void addedge(CI x,CI y)
{
	e[++cnt]=(edge){y,head[x]}; head[x]=cnt;
}
class Segment_Tree
{
	private:
		struct segment
		{
			int ch[2],val,tag;
		}node[N*40]; int tot;
		#define lc(x) node[x].ch[0]
		#define rc(x) node[x].ch[1]
		#define V(x) node[x].val
		#define T(x) node[x].tag
		#define TN CI l=1,CI r=num
		inline void pushdown(CI now)
		{
			if (T(now)!=1) V(lc(now))=1LL*V(lc(now))*T(now)%mod,V(rc(now))=1LL*V(rc(now))*T(now)%mod,
			T(lc(now))=1LL*T(lc(now))*T(now)%mod,T(rc(now))=1LL*T(rc(now))*T(now)%mod,T(now)=1;
		}
	public:
		inline void insert(int& now,CI p,TN)
		{
			now=++tot; V(now)=T(now)=1; if (l==r) return; int mid=l+r>>1;
			if (p<=mid) insert(lc(now),p,l,mid); else insert(rc(now),p,mid+1,r);
		}
		inline int merge(CI x,CI y,CI p,CI lx=0,CI rx=0,CI ly=0,CI ry=0,TN)
		{
			if (!x&&!y) return 0; int now=++tot;
			if (!y) return pushdown(x),T(now)=sum(1LL*p*ly%mod,1LL*sub(1,p)*ry%mod),
			V(now)=1LL*V(x)*T(now)%mod,lc(now)=lc(x),rc(now)=rc(x),now;
			if (!x) return pushdown(y),T(now)=sum(1LL*p*lx%mod,1LL*sub(1,p)*rx%mod),
			V(now)=1LL*V(y)*T(now)%mod,lc(now)=lc(y),rc(now)=rc(y),now;
			int mid=l+r>>1; pushdown(x); pushdown(y); T(now)=1;
			lc(now)=merge(lc(x),lc(y),p,lx,sum(rx,V(rc(x))),ly,sum(ry,V(rc(y))),l,mid);
			rc(now)=merge(rc(x),rc(y),p,sum(lx,V(lc(x))),rx,sum(ly,V(lc(y))),ry,mid+1,r);
			return V(now)=sum(V(lc(now)),V(rc(now))),now;
		}
		inline int query(CI now,CI p,TN)
		{
			if (l==r) return V(now); pushdown(now); int mid=l+r>>1;
			if (p<=mid) return query(lc(now),p,l,mid); else return query(rc(now),p,mid+1,r);
		}
		#undef lc
		#undef rc
		#undef V
		#undef T
		#undef TN
}SEG;
#define to e[i].to
inline void DFS(CI now=1)
{
	if (!head[now]) return SEG.insert(rt[now],lower_bound(rst+1,rst+num+1,a[now])-rst);
	for (RI i=head[now];i;i=e[i].nxt) DFS(to),rt[now]=rt[now]?SEG.merge(rt[now],rt[to],a[now]):rt[to];
}
#undef to
int main()
{
	RI i; for (scanf("%d",&n),i=1;i<=n;++i) if (scanf("%d",&x),x) addedge(x,i);
	for (i=1;i<=n;++i) if (scanf("%d",&a[i]),head[i])
	a[i]=1LL*a[i]*quick_pow(10000)%mod; else rst[++num]=a[i];
	for (sort(rst+1,rst+num+1),DFS(),i=1;i<=num;++i)
	x=SEG.query(rt[1],i),ans=sum(ans,1LL*i*rst[i]%mod*x%mod*x%mod);
	return printf("%d",ans),0;
}
posted @ 2019-12-21 19:52  空気力学の詩  阅读(210)  评论(0编辑  收藏  举报