题面

题解

强势安利一波巨佬的$blog$

线段树合并吼题啊

合并的时候要记一下$A$点权值小于$l$的概率和$A$点权值大于$r$的概率,对$B$点同样做

时空复杂度$\text O(nlogw)$,$w$为其中权值的最大值

代码

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x))

inline int read()
{
	int data = 0, w = 1; char ch = getchar();
	while(ch != '-' && (!isdigit(ch))) ch = getchar();
	if(ch == '-') w = -1, ch = getchar();
	while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
	return data * w;
}

const int Mod(998244353), Inv(796898467), maxn(300010), LIM((1 << 30) - 1);
inline int Add(int a, int b) { return (a + b) % Mod; }
int ans, cnt, son[2][maxn * 20], tag[maxn * 20], P[maxn * 20], cur, root[maxn];

inline int newNode()
{
	tag[++cur] = 1; P[cur] = 0;
	son[0][cur] = son[1][cur] = 0;
	return cur;
}

inline void pushup(int x) { P[x] = Add(P[son[0][x]], P[son[1][x]]); }
inline void put_tag(int x, int p)
{
	if(!x) return;
	tag[x] = 1ll * tag[x] * p % Mod;
	P[x] = 1ll * P[x] * p % Mod;
}

inline void pushdown(int x)
{
	put_tag(son[0][x], tag[x]);
	put_tag(son[1][x], tag[x]);
	tag[x] = 1;
}

void calc(int x, int l = 0, int r = LIM)
{
	if(!x) return;
	if(l == r)
	{
		++cnt; ans = Add(ans, 1ll * cnt * P[x] % Mod * P[x] % Mod * l % Mod);
		return;
	}
	pushdown(x);
	int mid = (l + r) >> 1;
	calc(son[0][x], l, mid);
	calc(son[1][x], mid + 1, r);
}

void insert(int &x, int id, int l = 0, int r = LIM)
{
	if(!x) x = newNode();
	tag[x] = P[x] = 1;
	if(l == r) return;
	int mid = (l + r) >> 1;
	if(id <= mid) insert(son[0][x], id, l, mid);
	else insert(son[1][x], id, mid + 1, r);
}

int S[2][maxn], tot[maxn], W[maxn], n;
int merge(int a, int b, int pa, int pb, int pmax)
{
	if(!a && !b) return 0;
	if(!a) return put_tag(b, pa), b;
	if(!b) return put_tag(a, pb), a;
	pushdown(a), pushdown(b);
	int pal = Add(pa, 1ll * P[son[1][a]] * Add(Mod - pmax, 1) % Mod),
		pbl = Add(pb, 1ll * P[son[1][b]] * Add(Mod - pmax, 1) % Mod),
		par = Add(pa, 1ll * P[son[0][a]] * pmax % Mod),
		pbr = Add(pb, 1ll * P[son[0][b]] * pmax % Mod);
	son[0][a] = merge(son[0][a], son[0][b], pal, pbl, pmax);
	son[1][a] = merge(son[1][a], son[1][b], par, pbr, pmax);
	return pushup(a), a;
}

void dfs(int x)
{
	if(!tot[x]) return (void) (insert(root[x], W[x]));
	W[x] = 1ll * W[x] * Inv % Mod;
	for(RG int i = 0; i < tot[x]; i++) dfs(S[i][x]);
	if(tot[x] == 1) root[x] = root[S[0][x]];
	else root[x] = merge(root[S[0][x]], root[S[1][x]], 0, 0, W[x]);
}

int main()
{
#ifndef ONLINE_JUDGE
	file(cpp);
#endif
	n = read();
	for(RG int i = 1, fa; i <= n; i++)
		fa = read(), S[tot[fa]++][fa] = i;
	for(RG int i = 1; i <= n; i++) W[i] = read();
	dfs(1); calc(root[1]); printf("%d\n", ans);
	return 0;
}

「PKUWC2018」Minimax的更多相关文章

  1. loj#2537. 「PKUWC2018」Minimax

    题目链接 loj#2537. 「PKUWC2018」Minimax 题解 设\(f_{u,i}\)表示选取i的概率,l为u的左子节点,r为u的子节点 $f_{u,i} = f_{l,i}(p \sum ...

  2. LOJ2537. 「PKUWC2018」Minimax【概率DP+线段树合并】

    LINK 思路 首先暴力\(n^2\)是很好想的,就是把当前节点概率按照权值大小做前缀和和后缀和然后对于每一个值直接在另一个子树里面算出贡献和就可以了,注意乘上选最大的概率是小于当前权值的部分,选最小 ...

  3. loj2537 「PKUWC2018」Minimax 【概率 + 线段树合并】

    题目链接 loj2537 题解 观察题目的式子似乎没有什么意义,我们考虑计算出每一种权值的概率 先离散化一下权值 显然可以设一个\(dp\),设\(f[i][j]\)表示\(i\)节点权值为\(j\) ...

  4. 【LOJ】#2537. 「PKUWC2018」Minimax

    题解 加法没写取模然后gg了QwQ,de了半天 思想还是比较自然的,线段树合并的维护方法我是真的很少写,然后没想到 很显然,我们有个很愉快的想法是,对于每个节点枚举它所有的叶子节点,对于一个叶子节点的 ...

  5. LOJ2537. 「PKUWC2018」Minimax [DP,线段树合并]

    传送门 思路 首先有一个\(O(n^2)\)的简单DP:设\(dp_{x,w}\)为\(x\)的权值为\(w\)的概率. 假设\(w\)来自\(v1\)的子树,那么有 \[ dp_{x,w}=dp_{ ...

  6. Loj #2542. 「PKUWC2018」随机游走

    Loj #2542. 「PKUWC2018」随机游走 题目描述 给定一棵 \(n\) 个结点的树,你从点 \(x\) 出发,每次等概率随机选择一条与所在点相邻的边走过去. 有 \(Q\) 次询问,每次 ...

  7. 「PKUWC2018」随机游走(min-max容斥+FWT)

    「PKUWC2018」随机游走(min-max容斥+FWT) 以后题目都换成这种「」形式啦,我觉得好看. 做过重返现世的应该看到就想到 \(min-max\) 容斥了吧. 没错,我是先学扩展形式再学特 ...

  8. 「PKUWC2018」猎人杀

    「PKUWC2018」猎人杀 解题思路 首先有一个很妙的结论是问题可以转化为已经死掉的猎人继续算在概率里面,每一轮一直开枪直到射死一个之前没死的猎人为止. 证明,设所有猎人的概率之和为 \(W\) , ...

  9. LOJ3044. 「ZJOI2019」Minimax 搜索

    LOJ3044. 「ZJOI2019」Minimax 搜索 https://loj.ac/problem/3044 分析: 假设\(w(1)=W\),那么使得这个值变化只会有两三种可能,比\(W\)小 ...

随机推荐

  1. [attribute |= value] 与 [attribute ^= value],[attribute ~= value] 与 [attribute *= value] 的联系与区别

    [attribute |= value] 与 [attribute ^= value] 的联系与区别: 一.联系: 1. 两个选择器的 attribute 属性值等于 value 时都可以匹配 < ...

  2. win7装postgresql10.4

    第一步: 第二步: 第三步: 第四步: 第五步: 下载地址:https://get.enterprisedb.com/postgresql/postgresql-10.4-1-windows-x64. ...

  3. 【[国家集训队]Crash的数字表格 / JZPTAB】

    这道题我们要求的是 \[\sum_{i=1}^N\sum_{j=1}^Mlcm(i,j)\] 总所周知\(lcm\)的性质不如\(gcd\)优雅,但是唯一分解定理告诉我们\(gcd(i,j)\time ...

  4. C/C++——存储

    关于各内存空间: 栈(stack):变量,数组.栈的大小是2M(也有的是1M),反正不大,一般递归写错了,没有出口,都会报错stack overflow. 全局区(静态区):全局变量.数组,静态变量. ...

  5. 【Vue.js】高仿饿了么外卖App(一)

    1.架构从传统的MVC向REST API+前端MV*迁移 参考链接: http://blog.csdn.net/broadview2006/article/details/8615055 http:/ ...

  6. EasyUI Calendar 日历插件,只显示年月。

    从别人的博客园搬过来的,放在这里只是为了方便自己用.已经注明原文出处,尊重别人的劳动成果. 原文地址:http://www.cnblogs.com/hmYao/p/5779463.html 此日历插件 ...

  7. UVALive - 6837 Kruskal+一点性质(暴力枚举)

    ICPC (Isles of Coral Park City) consist of several beautiful islands. The citizens requested constru ...

  8. Gradle Goodness: Run a Build Script With a Different Name

    Normally Gradle looks for a build script file with the name build.gradle in the current directory to ...

  9. HTML5手机端拍照上传

    1.accept="image/*" capture="camera" 自动调用手机端拍照功能 accept="image/*" captu ...

  10. 【.net开发者自学java系列】使用Eclipse开发SpringMVC(3)

    [.net开发者自学java系列]使用Eclipse开发SpringMVC(3) 标签(空格分隔): Spring RESTful 很久没继续学习java的spring了.接下来继续 回忆一下上个随笔 ...