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] ...
随机推荐
- Java学习笔记-基础语法Ⅷ-泛型、Map
泛型 泛型本质上是参数化类型,也就是说所操作的数据类型被指定为一个参数,即将类型由原来的具体的类型参数化,然后在使用/调用时传入具体的类型,这种参数类型可以用在类.方法和接口中,分别为泛型类.泛型方法 ...
- MVC 调试页面路径变成 Views/Controller/Action.cshtml问题
MVC在路由里面已经写好了路径,但是调试时地址栏还是会变成 Views/Controller/Action.cshtml,导致报404错误,找不到路径. 原因可能是你将某一页面设为了起始页,导致每次运 ...
- 使用argparse进行调参
argparse是深度学习项目调参时常用的python标准库,使用argparse后,我们在命令行输入的参数就可以以这种形式python filename.py --lr 1e-4 --batch_s ...
- 当运行npm install 命令的时候带上ignore-scripts,会发生什么?
摘要:运行npm install 命令的时候带上ignore-scripts, 可以避免一个恶意包里的病毒. 本文分享自华为云社区<运行npm install 命令的时候带上--ignore-s ...
- 阿里巴巴开源限流组件Sentinel初探
1 Sentinel主页 https://github.com/alibaba/Sentinel/wiki/主页 1.1 Sentinel介绍 随着微服务的流行,服务和服务之间的稳定性变得越来越重要. ...
- 【C++ 字符串题目】 输入三个人名,按字母顺序排序输出
题目来源:https://acm.ujn.edu.cn Problem A: [C++ 字符串] 输入三个人名,按字母顺序排序输出 Time Limit: 1 Sec Memory Limit: 1 ...
- 2 Elment Ui 日期选择器 格式化问题
Elment Ui 日期选择器 格式化问题 在前后端联调过程中 我们常常会遇到日期无法被反序列化 这个问题 会有点头疼 下面以我这边为例 后端使用的是springboot 默认集成了jackjson ...
- node开启 https线上服务你不会吗?
var https=require("https");//https服务var fs = require("fs")var express = require( ...
- 你难道不知道Vue-cookie?
install npm install vue-cookies --save main.js import VueCookies from 'vue-cookies'Vue.use(VueCookie ...
- 2分钟实现一个Vue实时直播系统
前言 我们在不敲代码的时候可能会去看游戏直播,那么是前台怎么实现的呢?下面我们来讲一下.第一步,购买云直播服务 首先,你必须去阿里云或者腾讯云注册一个直播服务.也花不了几个钱,练手的话,几十块钱就够了 ...