loj2537 「PKUWC 2018」Minimax
pkusc 快到了……做点题涨涨 rp。
初见时 yy 了一个类似于归并的东西,\(O(n^2)\),50 分。
50 分 yy 做法
对于一个点,枚举他能到达的权值(假设这个权值在左子树,在右子树是一样的),选上这个点的概率就是“在左子树选上这个点的概率 \(\times\) (选择子结点最大值的概率 \(\times\) 右子树选出比这个点小的点的概率和+选择子结点最小值的概率 \(\times\) 右子树选出比这个点大的点的概率和)”。
100 分
我们发现,瓶颈在于合并。我们先想到启发式合并,然后就不会了。
我们又想到线段树合并。这里就参考ref这里就可以了。
#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
int n, uu, num[300005], bb, rot[300005], tot, maxa, maxb, ans;
const int mod=998244353;
struct Node{
int l, r, v;
}nd[300005];
struct SGTNode{
int l, r, v, t;
}sgt[5000005];
int ksm(int a, int b){
int re=1;
while(b){
if(b&1) re = (ll)re * a % mod;
a = (ll)a * a % mod;
b >>= 1;
}
return re;
}
void insert(int &x, int l, int r, int v){
x = ++tot;
sgt[x].v = sgt[x].t = 1;
if(l==r) ;
else{
int mid=(l+r)>>1;
if(v<=mid) insert(sgt[x].l, l, mid, v);
else insert(sgt[x].r, mid+1, r, v);
}
}
void pushDown(int x){
if(!x || sgt[x].t<=1) return ;
sgt[sgt[x].l].t = (ll)sgt[sgt[x].l].t * sgt[x].t % mod;
sgt[sgt[x].r].t = (ll)sgt[sgt[x].r].t * sgt[x].t % mod;
sgt[x].v = (ll)sgt[x].v * sgt[x].t % mod;
sgt[x].t = 1;
}
int merge(int x, int y, int p){
if(!x && !y) return 0;
pushDown(x); pushDown(y);
if(!y){
maxa = (maxa + sgt[x].v) % mod;
sgt[x].t = ((maxb+p)%mod-(ll)2*maxb*p%mod+mod) % mod;
pushDown(x);
return x;
}
if(!x){
maxb = (maxb + sgt[y].v) % mod;
sgt[y].t = ((maxa+p)%mod-(ll)2*maxa*p%mod+mod) % mod;
pushDown(y);
return y;
}
sgt[x].r = merge(sgt[x].r, sgt[y].r, p);
sgt[x].l = merge(sgt[x].l, sgt[y].l, p);
sgt[x].v = (sgt[sgt[x].l].v + sgt[sgt[x].r].v) % mod;
return x;
}
void dfs(int x){
if(!nd[x].l)
insert(rot[x], 1, bb, nd[x].v);
else if(!nd[x].r){
dfs(nd[x].l);
rot[x] = rot[nd[x].l];
}
else{
dfs(nd[x].l);
dfs(nd[x].r);
maxa = maxb = 0;
rot[x] = merge(rot[nd[x].l], rot[nd[x].r], nd[x].v);
}
}
void getAns(int x, int l, int r){
if(!x) return ;
pushDown(x);
if(l==r)
ans = (ans + (ll)l*num[l]%mod*sgt[x].v%mod*sgt[x].v%mod) % mod;
else{
int mid=(l+r)>>1;
getAns(sgt[x].l, l, mid);
getAns(sgt[x].r, mid+1, r);
}
}
int main(){
cin>>n;
for(int i=1; i<=n; i++){
scanf("%d", &uu);
if(nd[uu].l) nd[uu].r = i;
else nd[uu].l = i;
}
int inv=ksm(10000, mod-2);
for(int i=1; i<=n; i++){
scanf("%d", &uu);
if(nd[i].l) nd[i].v = (ll)uu * inv % mod;
else{
nd[i].v = uu;
num[++bb] = uu;
}
}
sort(num+1, num+1+bb);
bb = unique(num+1, num+1+bb) - (num + 1);
for(int i=1; i<=n; i++)
if(!nd[i].l)
nd[i].v = lower_bound(num+1, num+1+bb, nd[i].v) - num;
dfs(1);
getAns(rot[1], 1, bb);
cout<<ans<<endl;
return 0;
}
loj2537 「PKUWC 2018」Minimax的更多相关文章
- LOJ #2537. 「PKUWC 2018」Minimax (线段树合并 优化dp)
题意 小 \(C\) 有一棵 \(n\) 个结点的有根树,根是 \(1\) 号结点,且每个结点最多有两个子结点. 定义结点 \(x\) 的权值为: 1.若 \(x\) 没有子结点,那么它的权值会在输入 ...
- 「PKUWC 2018」Minimax
传送门:Here 一道线段树合并好题 如果要维护点$ x$的信息,相当于合并$ x$的两棵子树 对于这题显然有:任何叶子节点的权值都可能出现在其祖先上 因而我们只需要在线段树合并的时候维护概率即可 我 ...
- LOJ #2542. 「PKUWC 2018」随机游走(最值反演 + 树上期望dp + FMT)
写在这道题前面 : 网上的一些题解都不讲那个系数是怎么推得真的不良心 TAT (不是每个人都有那么厉害啊 , 我好菜啊) 而且 LOJ 过的代码千篇一律 ... 那个系数根本看不出来是什么啊 TAT ...
- LOJ #2541. 「PKUWC 2018」猎人杀(容斥 , 期望dp , NTT优化)
题意 LOJ #2541. 「PKUWC 2018」猎人杀 题解 一道及其巧妙的题 , 参考了一下这位大佬的博客 ... 令 \(\displaystyle A = \sum_{i=1}^{n} w_ ...
- LOJ #2540. 「PKUWC 2018」随机算法(概率dp)
题意 LOJ #2540. 「PKUWC 2018」随机算法 题解 朴素的就是 \(O(n3^n)\) dp 写了一下有 \(50pts\) ... 大概就是每个点有三个状态 , 考虑了但不在独立集中 ...
- LOJ #2538. 「PKUWC 2018」Slay the Spire (期望dp)
Update on 1.5 学了 zhou888 的写法,真是又短又快. 并且空间是 \(O(n)\) 的,速度十分优秀. 题意 LOJ #2538. 「PKUWC 2018」Slay the Spi ...
- loj2538 「PKUWC 2018」Slay the Spire
pkusc 快到了--做点题涨涨 rp. ref我好菜啊QAQ. 可以发现期望只是一个幌子.我们的目的是:对于所有随机的选择方法(一共 \(\binom{2n}{m}\)种),这些选择方法都最优地打出 ...
- loj2540 「PKUWC 2018」随机算法
pkusc 快到了--做点题涨涨 rp. 记 \(f(S,i)\) 表示 \(S\) 这个集合是决计不能选的(要么属于独立集,要么和独立集相连),或称已经考虑了的,\(i\) 表示此集合对应的最大独立 ...
- 「PKUWC 2018」随机算法 (第二版,正解做法)
上一版貌似是打了 O(3 ^ N) 暴力和 一条链的情况,得了60分.... 第一次做的时候光想练一练暴力...就没去想正解,谁知道正解比暴力好写不知道多少,mmp 设 f(S) 为 选集合S中的点可 ...
随机推荐
- Oracle Business Intelligence Enterprise Edition 12.2.1.2.0 Books
Oracle Business Intelligence Enterprise Edition 12.2.1.2.0 Books Documentation for Oracle Business I ...
- linux中配置yum源
1.配置163或者阿里云yum源: 阿里云yum源地址:https://mirrors.aliyun.com/centos/6.9/os/x86_64/Packages/ 阿里云给出的解决办法:htt ...
- 中标麒麟高级服务器操作系统V6
平台: linux 类型: 虚拟机镜像 软件包: java-1.6.0 mysql-5.1.5 python-2.6 qt3-3.3.8b basic software linux neokylin ...
- centos升级3.10内核到4.4
为了满足k8s对内核要求,默认Centos 7的内核为3.10建议升级到4.x内核 默认3.10内核升级为4.x内核 rpm -Uvh http://www.elrepo.org/elrepo-rel ...
- 流媒体 6——MPEG电视
1.电视图像的数据率 1.1 ITU-R BT.601标准数据率 按照奈奎斯特(Nyquist)采样理论,模拟电视信号经过采样(把连续的时间信号变成离散的时间信号)和量化 (把连续的幅度变成离散的幅度 ...
- JavaScript:理解Promise方法
什么是promise? Promise的核心思想是代表异步操作的一个结果,并且promise具有三个状态(pending初始状态,fulfilled成功状态,rejected失败状态).我们可以理解为 ...
- POJ 3126 Prime Path(筛法,双向搜索)
题意:一个4位的素数每次变动一个数位,中间过程也要上素数,问变成另一个的最小步数. 线性筛一遍以后bfs就好.我写的双向,其实没有必要. #include<cstdio> #include ...
- Android(java)学习笔记94: SurfaceView使用
1. SurfaceView简介 在一般的情况下,应用程序的View都是在相同的GUI线程(UI主线程)中绘制的.这个主应用程序线程同时也用来处理所有的用户交互(例如,按钮单击或者文本输入). ...
- 问题 B: Curriculum Vitae
问题 B: Curriculum Vitae 时间限制: 1 Sec 内存限制: 128 MB提交: 109 解决: 25[提交][状态][讨论版][命题人:acm4302] 题目描述 Hideo ...
- MySQL 5.7 在线启用和关闭GTID
1.相关基础 MySQL 5.7.6之后GTID_MODE提供了两个新的选项分别为ON_PERMISSIVE和OFF_PERMISSIVEOFF_PERMISSIVE:不产生GTID事务, Slave ...