Minimax 社论
题面
LOJ #2537 / 洛谷 P5298 「PKUWC2018」Minimax
一棵有根二叉树 \(\mathcal T\) .
定义结点 \(x\) 的权值为:
- 若 \(x\) 是叶子,则权值在输入中给出(叶子权值各不相同)
- 若不然,则有 \(p_x\) 的概率是其子节点权值最大值,\(1-p_x\) 的概率是其子节点权值最小值 .
假设 \(1\) 号结点的权值有 \(m\) 种可能性,权值第 \(i\) 小的可能性的权值是 \(V_i\),它的概率为 \(D_i\)(\(D_i>0\)),求:
\]
答案对 \(998244353\) 取模 .
题解
令 \(dp_{u, w}\) 表示点 \(u\) 的权值为 \(w\) 的概率 .
由于 \(\mathcal T\) 是二叉树,于是权值来源只可能有两个 .
设权值来源子树根为 \(a\),另一子树根为 \(b\),则:
\]
直接 dp 是 \(O(n^2)\) 的,可以获得 40pts .
展开写,令 \(ls\) 为 \(u\) 的左儿子,\(rs\) 为右儿子,则:
\(\displaystyle\begin{aligned}dp_{u, w} &= dp_{ls,w}\cdot\left(p_u\cdot\sum_{w'=1}^{j-1}dp_{rs, w'}+(1-p_u)\sum_{w'=j+1}^mdp_{rs,w'}\right)+dp_{rs,w}\cdot\left(p_u\cdot\sum_{w'=1}^{j-1}dp_{ls, w'}+(1-p_u)\sum_{w'=j+1}^mdp_{ls,w'}\right)\\&=p_u\left(dp_{rs,w}\sum_{w'=1}^{j-1}dp_{ls, w'}+dp_{ls,w}\sum_{w'=1}^{j-1}dp_{rs, w'}\right)+(1-p_u)\left(dp_{rs,w}\sum_{w'=j+1}^mdp_{ls, w'}+dp_{ls,w}\sum_{w'=j+1}^mdp_{rs, w'}\right)\end{aligned}\)
化成这个只是为了展示这个前后缀和的形式 .
考虑线段树合并 .
维护前缀、后缀概率和,于是合并的时候可以直接算,注意合并时一棵树有某节点但另一棵树没有时相当于一个整体乘操作,打一个乘法懒标记即可 .
离散化权值之后,时空复杂度均为 \(O(n\log n)\) .
细节见代码
代码
Code :
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <climits>
#include <vector>
#include <queue>
#include <cmath>
#include <unordered_map>
#include <set>
#include <random>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 3e5+50, M = 40*N;
const ll P = 998244353, invS = 796898467;
template<typename T>
inline int chkmin(T& a, const T& b){if (a > b) a = b; return a;}
template<typename T>
inline int chkmax(T& a, const T& b){if (a < b) a = b; return a;}
int n, m, deg[N], son[N][2], root[N];
ll val[N];
vector<ll> G;
inline int discrete(ll w){return lower_bound(G.begin(), G.end(), w) - G.begin();}
struct SMF
{
int ch[M][2], cc;
ll sum[M], laz[M]; // mul tag (1)
inline void pushup(int u){sum[u] = (sum[ch[u][0]] + sum[ch[u][1]]) % P;}
inline void pushmul(int u, ll v){if (!u) return ; sum[u] = sum[u] * v % P; laz[u] = laz[u] * v % P;}
inline void pushdown(int u)
{
if (laz[u] == 1) return ; // none
if (ch[u][0]) pushmul(ch[u][0], laz[u]);
if (ch[u][1]) pushmul(ch[u][1], laz[u]);
laz[u] = 1;
}
inline void insert(int& u, int l, int r, int p, ll v)
{
if (!u){u = ++cc; laz[u] = 1;}
if (l == r){sum[u] = v % P; return ;}
int mid = (l + r) >> 1;
pushdown(u);
if (p <= mid) insert(ch[u][0], l, mid, p, v);
else insert(ch[u][1], mid+1, r, p, v);
pushup(u);
}
inline int merge(int x, int y, int l, int r, ll xmul, ll ymul, ll v)
{
if (!x && !y) return 0;
if (!x){pushmul(y, ymul); return y;}
if (!y){pushmul(x, xmul); return x;}
pushdown(x); pushdown(y);
int mid = (l + r) >> 1;
ll lsx = sum[ch[x][0]] % P, lsy = sum[ch[y][0]] % P, rsx = sum[ch[x][1]] % P, rsy = sum[ch[y][1]] % P;
ch[x][0] = merge(ch[x][0], ch[y][0], l, mid, (xmul + rsy * (1-v+P) % P) % P, (ymul + rsx * (1-v+P) % P) % P, v);
ch[x][1] = merge(ch[x][1], ch[y][1], mid+1, r, (xmul + lsy * v % P) % P, (ymul + lsx * v % P) % P, v); // magic
pushup(x); return x;
}
inline ll order(int x, int l, int r) // get answer
{
if (l == r) return l * G[l] % P * sum[x] % P * sum[x] % P;
pushdown(x);
int mid = (l + r) >> 1;
return (order(ch[x][0], l, mid) + order(ch[x][1], mid+1, r)) % P;
}
}T;
inline void dfs(int u)
{
if (!deg[u]) T.insert(root[u], 1, m, val[u], 1);
else if (deg[u] == 1){dfs(son[u][0]); root[u] = root[son[u][0]];}
else if (deg[u] == 2)
{
dfs(son[u][0]); dfs(son[u][1]);
root[u] = T.merge(root[son[u][0]], root[son[u][1]], 1, m, 0, 0, val[u]);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("i.in", "r", stdin);
#endif
scanf("%d", &n); G.emplace_back(-114514);
for (int i=1, x; i<=n; i++){scanf("%d", &x); son[x][deg[x]++] = i;}
for (int i=1; i<=n; i++)
{
scanf("%lld", val+i);
if (deg[i]) val[i] = val[i] * invS % P;
else G.emplace_back(val[i]); // leaf
}
stable_sort(G.begin(), G.end());
G.erase(unique(G.begin(), G.end()), G.end());
m = G.size() + 1;
for (int i=1; i<=n; i++)
if (!deg[i]) val[i] = discrete(val[i]);
dfs(1);
printf("%lld\n", T.order(root[1], 1, m));
return 0;
}
Reference
链接形式 ref .
- https://www.luogu.com.cn/blog/qiuly/solution-p5298
- https://www.luogu.com.cn/blog/CFA-44/solution-p5298
- https://www.luogu.com.cn/blog/Mrsrz/solution-p5298
- https://www.luogu.com.cn/blog/Isaunoya/solution-p5298
Minimax 社论的更多相关文章
- HackerRank and MiniMax
传送门 Sherlock and MiniMax Authored by darkshadows on May 07 2014 Problem Statement Watson gives Sherl ...
- uva 1331 - Minimax Triangulation(dp)
option=com_onlinejudge&Itemid=8&page=show_problem&category=514&problem=4077&mosm ...
- Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy
1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...
- 【loj3044】【zjoi2019】Minimax
题目 描述 给出一颗树,定义根节点1的深度为1,其他点深度为父亲深度+1: 如下定义一个点的点权: 1.叶子:为其编号:2.奇数深度:为其儿子编号最大值:3.偶数深度:为其儿子编号最小值: ...
- loj#2537. 「PKUWC2018」Minimax
题目链接 loj#2537. 「PKUWC2018」Minimax 题解 设\(f_{u,i}\)表示选取i的概率,l为u的左子节点,r为u的子节点 $f_{u,i} = f_{l,i}(p \sum ...
- maple minimax函数
numapprox[minimax] - minimax rational approximation Calling Sequence minimax(f, x=a..b, [m, n], w, ...
- LOJ3044. 「ZJOI2019」Minimax 搜索
LOJ3044. 「ZJOI2019」Minimax 搜索 https://loj.ac/problem/3044 分析: 假设\(w(1)=W\),那么使得这个值变化只会有两三种可能,比\(W\)小 ...
- 【HackerRank】Sherlock and MiniMax
题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer ...
- BZOJ5461: [PKUWC2018]Minimax
BZOJ5461: [PKUWC2018]Minimax https://lydsy.com/JudgeOnline/problem.php?id=5461 分析: 写出\(dp\)式子:$ f[x] ...
随机推荐
- 试驾 Citus 11.0 beta
https://www.citusdata.com/blog/2022/03/26/test-drive-citus-11-beta-for-postgres/ Citus 11.0 beta 的最大 ...
- 【工具-Nginx】从入门安装到高可用集群搭建
文章已收录至https://lichong.work,转载请注明原文链接. ps:欢迎关注公众号"Fun肆编程"或添加我的私人微信交流经验 一.Nginx安装配置及常用命令 1.环 ...
- 132_Power BI之建模必备要素&Power Query之数据表字段名称管理
博客:www.jiaopengzi.com 焦棚子的文章目录 请点击下载附件 一.背景 近段时间比较忙,也没有看到很好的DAX素材,很久没有更新文章了,刚好有时间就来凑个热闹. 今天主题是Power ...
- BUUCTF刷题记录(更新中...)
极客大挑战 2019]EasySQL-1 直接通过输入万能密码:' or 1=1#实现注入: 思考:服务端sql语句应该为:select * from users where username='xx ...
- 安装Redis到Linux(源码)
运行环境 系统版本:Ubuntu 16.04.2 LTS 软件版本:redis-5.0.4 硬件要求:无 安装过程 1.配置系统参数 root@localhost:~# vim /etc/sysctl ...
- [gym102978C] Count Min Ratio
[gym102978C] Count Min Ratio 给定 \(B\) 个蓝色的球. \(R\) 个红色的球以及一个绿色的球,同颜色的球不可区分.对于一种球的排列方式,记 \(l_B,r_B,l_ ...
- 从数字化概念到落地,都说是一道坎,JNPF能为企业带来什么呢?
数字经济席卷全球推动产业转型是必然趋势,人类社会正在数字化大潮中发生深刻变革,数字化越来越成为推动经济社会发展的核心驱动力.企业正处于数字经济大潮的风口浪尖,故企业经验决策者应深刻认识数字化转型对于企 ...
- GDKOI 2021 Day2 TG 总结
又是爆炸的一天,炸多了本蒟蒻已经习以为常 但今天比昨天整整高了 40 分!!!!却还是没有 100 今天本蒟蒻本想模仿奆佬的打字速度,结果思路混乱让我无法开始 T1 不是吧怎么是期望 dp ,期望值怎 ...
- Jenkins之配置GitHub-Webhook
前提条件1: 运行Jenkins的机器需要安装git,并且git.exe文件在系统的环境变量里面,或者手动在 Manage Jenkins -> Global Tool Configuratio ...
- 使用Vite2+TypeScript4+Vue3技术栈,如何入手开发项目
前言 今天,我们使用Vite2.0+Vue3+TS来试玩一下,开发一个demo项目.实战 我们,打开Vite官方网站(https://cn.vitejs.dev/). Vite (法语意为 " ...