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] ...
随机推荐
- Blazor和Vue对比学习(基础1.4):事件和子传父
Blazor和Vue的组件事件,都使用事件订阅者模式.相对于上一章的组件属性,需要多绕一个弯,无论Blazor还是Vue,都是入门的第一个难点.要突破这个难点,一是要熟悉事件订阅模式<其实不难& ...
- 通过有序线性结构构造AVL树
通过有序线性结构构造AVL树 本博客旨在结局利用有序数组和有序链表构造平衡二叉树(下文使用AVL树代指)问题. 直接通过旋转来构造AVL树似乎是一个不错的选择,但是稍加分析就会发现,这样平白无故做了许 ...
- 国产开源优秀新一代MPP数据库StarRocks入门之旅-数仓新利器(上)
概述 背景 Apache Doris官方地址 https://doris.apache.org/ Apache Doris GitHub源码地址 https://github.com/apache/i ...
- 每日一题20180330-Linux
一.问题 1.1 统计/var/log/下所有文件个数 1.2 查找出/var/log目录下面修改时间是7天以前,大小在50k到2M之间,并以.log结尾的文件把这些文件复制到/data目录中 1.3 ...
- Flask框架实现登录注册功能(mysql数据库)
前言: 本例使用Flask框架完成登录和注册操作,包括前端(index.html,regist.html)和后端(app.py)两部分,前端页面不过多介绍,直接进入后端部分: 逻辑思路: 登录部分:运 ...
- Tomcat启动失败:java.lang.NoSuchMethodError: org.apache.tomcat.util.res.StringManager.getManager(Ljava/lang/Class;)Lorg/apache/tomcat/util/res/StringManager
项目开发中发现服务器上Tomcat启动失败 开始定位 第一步:打开tomcat日志catalina.log: 2017-07-25 17:02:43,799 [Catalina-startStop-1 ...
- Typora使用手册(小白入门级)
Typora软件的简单使用 1.简介 Typora是一款支持Markdown语法的文档编辑器 特点:功能强大.画面极简. 下载地址:https://typoraio.cn/ 2.基础设置 偏 ...
- SQL的语法
SQL的语法 SQL通用语法 SQL语句可以单行或多行书写,以分号(";")结尾. SQL语句可以使用空格或缩进增强可读性. MySQL数据库的SQL语句不区分大小写(建议关键字大 ...
- 技术分享 | app自动化测试(Android)--高级定位技巧
原文链接 XPath高级定位技巧 XPath 简介 XPath 的英文全称为:XML Path Language,意旨对 XML 中的元素进行路径定位的一种语言,它可适用 XML 标记语言,Html ...
- 2 万字 + 30 张图 | 细聊 MySQL undo log、redo log、binlog 有什么用?
作者:小林coding 计算机八股文网站:https://xiaolincoding.com/ 大家好,我是小林. 从这篇「执行一条 SQL 查询语句,期间发生了什么?」中,我们知道了一条查询语句经历 ...