http://acm.hdu.edu.cn/showproblem.php?pid=6305

题目

对于A,B两个序列,任意的l,r,如果RMQ(A,l,r)=RMQ(B,l,r),B序列里的数为[0,1]的实数,B的重量为B的所有元素的和,否则为0。问你B的期望重量是多少。

分析

准备知识:笛卡尔树https://skywt.cn/posts/cartesian-tree/

RMQ-Similar实际上就是A和B的笛卡尔树一样。于是这个题就是笛卡尔树同构的问题,假设A的笛卡尔树的子树大小为sz[u],那么序列B与A同构的概率为,因为B中的数满足均匀分布(因为B中的元素为[0,1]中的任意实数),所以每个位置的期望值为(0+1)/2,那么B的重量总和为n/2,所以B的重量的期望值为

实际上对这题我还没完全懂,暂且记录一下吧。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<cassert>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn=1e6+;
const int inf=0x3f3f3f3f;
const int mod=1e9+; stack<int>st;
ll inv[maxn];
int n; struct node{
int val,sz;
int l,r,par;
}t[maxn]; void init()
{
for(int i=;i<=n;i++)
t[i].l=,t[i].r=,t[i].par=,t[i].sz=;//初始化
t[].val=inf;
while(!st.empty())
st.pop();
st.push();
} void build()
{
for(int i=;i<=n;i++){
while(!st.empty()&&t[st.top()].val<t[i].val)//从栈顶往栈底遍历,
st.pop();
int par=st.top();
t[i].par=par;//i.par为st.pop()
t[i].l=t[par].r;
t[t[par].r].par=i;
t[par].r=i;//右子树
st.push(i);
}
} void dfs(int u)
{
if(u==) return ;
t[u].sz=;
dfs(t[u].l);
dfs(t[u].r);
t[u].sz+=t[t[u].l].sz+t[t[u].r].sz;
} void Inv(){//扩展gcd求逆元
inv[]=;
for(int i=;i<maxn;i++)
inv[i]=inv[mod%i]*(mod-mod/i)%mod;
} int main()
{
int T;
Inv();
scanf("%d",&T);
while(T--){
scanf("%d",&n);
init();
for(int i=;i<=n;i++)
scanf("%d",&t[i].val);
build();
dfs(t[].r); ll ans=n*inv[]%mod;
for(int i=;i<=n;i++)
ans=ans*inv[t[i].sz]%mod;
printf("%lld\n",ans);
}
}

HDU - 6305 RMQ Similar Sequence(笛卡尔树)的更多相关文章

  1. hdu 6305 RMQ Similar Sequence——概率方面的思路+笛卡尔树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6305 看题解,得知: 0~1内随机取实数,取到两个相同的数的概率是0,所以认为 b 序列是一个排列. 两个 ...

  2. HDU 6305.RMQ Similar Sequence-笛卡尔树+数学期望 (2018 Multi-University Training Contest 1 1008)

    6305.RMQ Similar Sequence 这个题的意思就是对于A,B两个序列,任意的l,r,如果RMQ(A,l,r)=RMQ(B,l,r),B序列里的数为[0,1]的实数,B的重量为B的所有 ...

  3. [乱搞]hdu 6406 Taotao picks apples 笛卡尔树+倍增

    题目链接 Problem Description There is an apple tree in front of Taotao's house. When autumn comes, n app ...

  4. 2018 Multi-University Training Contest 1 H - RMQ Similar Sequence(HDU - 6305 笛卡尔树)

    题意: 对于一个序列a,构造一个序列b,使得两个序列,对于任意的区间 [l, r] 的区间最靠近左端点的那个最大值的位置,并且序列 b 满足 0 < bi < 1. 给定一个序列 a ,求 ...

  5. [模板] 笛卡尔树 && RMQ

    话说我noip之前为什么要学这种东西... 简介 笛卡尔树(Cartesian Tree) 是一种二叉树, 且同时具有以下两种性质: 父亲节点的值大于/小于子节点的值; 中序遍历的结果为原序列. 笛卡 ...

  6. hdu 1506 Largest Rectangle in a Histogram——笛卡尔树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506 关于笛卡尔树的构建:https://www.cnblogs.com/reverymoon/p/952 ...

  7. HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)

    题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...

  8. HDU 1506 Largest Rectangle in a Histogram(单调栈、笛卡尔树)

    题意:给定n个连续排列的矩形的高,矩形的宽都为1.问最大矩形覆盖. 例如:n = 7,h[i] = (2 1 4 5 1 3 3),最大覆盖为8. Sample Input 7 2 1 4 5 1 3 ...

  9. 笛卡尔树--牛客第四场(sequence)

    思路: O(n)建一颗笛卡尔树,再O(n)dfs向上合并答案就行了. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include &l ...

随机推荐

  1. 解决 phpstorm 运行卡,自动关闭等问题

    解决 phpstorm 自动关闭问题: 使用文件搜索工具(可在本博客搜索“管理工具”,或查找安装目录) 找到phpstorm.vmoptions文件,使用记事本打开. 添加以下两行代码: -Dawt. ...

  2. python 识别验证码

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/instal ...

  3. 【题解】 bzoj1135: [POI2009]Lyz (线段树+霍尔定理)

    题面戳我 Solution 二分图是显然的,用二分图匹配显然在这个范围会炸的很惨,我们考虑用霍尔定理. 我们任意选取穿\(l,r\)的号码鞋子的人,那么这些人可以穿的鞋子的范围是\(l,r+d\),这 ...

  4. Nginx优化文件编写

    server_tokens off; #并不会让nginx执行的速度更快,关闭它可隐藏错误页面中的nginx版本号charset utf-8,gbk; #字符#sendfile on;#tcp_nop ...

  5. Think Python 2E中译本 _site

    http://codingpy.com/books/thinkpython2/index.html

  6. hdu 1160 FatMouse's Speed (最长上升子序列+打印路径)

    Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...

  7. Mybatis中org.apache.ibatis.binding.BindingException错误问题总结

    1. Mybatis出现多个参数,但是多个参数中没有使用@Param注解进行修饰 2. Xml文件中字段名和PO绑定时候,字段写错了 3.XML中<foreach/>标签中的colleac ...

  8. 简明的Python教程当中的几个疑惑点分析#1

    #1简明的Python教程中的第11章面向对象编程学习中的类与对象的方法里面 有这么一个案例:使用类与对象的变量 #coding:utf-8 #类与对象的变量学习 class Person: popu ...

  9. JavaServer Faces (JSF) with Spring

    JavaServer Faces (JSF) with Spring Last modified: April 30, 2018 by baeldung Spring+ Spring MVC JSF ...

  10. Java collection 容器

    http://www.codeceo.com/article/java-container-brief-introduction.html Java实用类库提供了一套相当完整的容器来帮助我们解决很多具 ...