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] ...
随机推荐
- scanf需要多输入一行是什么问题
有大佬知道用scanf输入,执行程序要多输入一行才能运行一般是什么问题呢 scanf的问题,其中多了\n. scanf如果加入\n,会导致需要多输入一次数据. 错误实例:
- 上半年最中意的 GitHub 更新「GitHub 热点速览 v.22.21」
2022 年的热点速览加入 GitHub 产品动态之后,这次的图片显示是最得我意的,因为可以提升图片查看人的看片体验.而 GitHub 社区方面,GitHub 官方发布了部分开源项目的新版本.与此同时 ...
- hexo + typora 图片插入解决办法
Typora 是一款知名的 Markdown 编辑器,简单好用,体验良好.使用 hexo 搭建好博客后,主要是用 Markdown 来编写博客,typora 便是我的首选编辑器.但直接使用 typor ...
- IOU->GIOU->CIOU->Focal_loss
IOU->GIOU->CIOU->Focal_loss 参考b站 总览 2022-1-3号补充 该链接下关于算是函数讨论 https://zhuanlan.zhihu.com/p/1 ...
- 史上最全Spring Cloud Alibaba--Nacos教程(涵盖负载均衡、配置管理、多环境切换、配置共享/刷新、灰度、集群)
能够实现Nacos安装 基于Nacos能实现应用负载均衡 能基于Nacos实现配置管理 配置管理 负载均衡 多环境切换 配置共享 配置刷新 灰度发布 掌握Nacos集群部署 1 Nacos安装 Nac ...
- python生产exe文件yi以及解释器配置等
原文链接:https://blog.csdn.net/weixin_42691768/article/details/81044666 https://www.cnblogs.com/paulwhw/ ...
- generatorConfig.xml自动生成实体类,dao和xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration ...
- Python爬虫-正则
介绍: 是 一门全新的语言,一种使用表达式的方式对字符串进行匹配的语法规则 我们抓取到的网页源代码本质上就是一个超长的字符串,想从里面提取内容,用正则再适合不过 优点:速度快.效率高.准确性高 缺点: ...
- DAST 黑盒漏洞扫描器 第六篇:运营篇(终)
0X01 前言 转载请标明来源:https://www.cnblogs.com/huim/ 当项目功能逐渐成熟,同时需要实现的是运营流程和指标体系建设.需要工程化的功能逐渐少了,剩下的主要工作转变成持 ...
- mysql中in的用法详解
一.基础用法 mysql中in常用于where表达式中,其作用是查询某个范围内的数据. select * from where field in (value1,value2,value3,-) 当 ...