这道需要注意到一点。。就是如果询问的区间如果覆盖了最大数,那么rmq一定是确定的。。故以这个最大数为分界,左右2个区间是完全独立的。。因此就可以采用分治的做法去做。。聪明的汪聚聚已经实现。。

然后dls还给我们介绍了另一个数据结构——笛卡尔树。。这也是个二叉排序树。。然而其关键字满足堆的性质。。即根的权值最大。。

然后用这个可以很轻松的表示出B数组元素需要满足的关系,即根是子树中最大的。。满足这个条件的概率是1/子树节点数(假设n个数确定,该数最大的概率为1/n),然后满足条件的前提下由于可以随机取,所以权重的期望都是n/2直接乘上去即可。 。

问题是如何构造笛卡尔树了。。运用单调栈。。将未成形的,小于自己的数都拉进自己的左子树即可。。

另外dls求逆元的姿势也学到了。。貌似是拓展gcd。。

/***          ┏┓    ┏┓*          ┏┛┗━━━━━━━┛┗━━━┓*          ┃       ┃  *          ┃   ━    ┃*          ┃ >   < ┃*          ┃       ┃*          ┃... ⌒ ...  ┃*          ┃              ┃*          ┗━┓          ┏━┛*          ┃          ┃ Code is far away from bug with the animal protecting          *          ┃          ┃   神兽保佑,代码无bug*          ┃          ┃           *          ┃          ┃        *          ┃          ┃*          ┃          ┃           *          ┃          ┗━━━┓*          ┃              ┣┓*          ┃              ┏┛*          ┗┓┓┏━━━━━━━━┳┓┏┛*           ┃┫┫       ┃┫┫*           ┗┻┛       ┗┻┛*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1LL<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 1000005
#define nm 5000005
#define N 1000005
#define M(x,y) x=max(x,y)
const double pi=acos(-1);
const ll inf=1e9+7;
using namespace std;
ll read(){ll x=0,f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return f*x;
}int a[NM],l[NM],r[NM],n,root,size[NM];
ll inv[NM],ans;
stack<int>s;
void dfs(int x){size[x]=1;if(l[x])dfs(l[x]),size[x]+=size[l[x]];if(r[x])dfs(r[x]),size[x]+=size[r[x]];ans*=inv[size[x]];ans%=inf;
}int main(){n=1e6;inv[1]=1;inc(i,2,n)inv[i]=inv[inf%i]*(inf-inf/i)%inf;int _=read();while(_--){ans=n=read();ans*=inv[2];ans%=inf;inc(i,1,n)a[i]=read(),l[i]=r[i]=0;inc(i,1,n){while(!s.empty()&&a[i]>a[s.top()])l[i]=s.top(),s.pop();if(!s.empty())r[s.top()]=i;s.push(i);}while(!s.empty())root=s.top(),s.pop();dfs(root);printf("%lld\n",ans);}return 0;
}

RMQ Similar Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 255535/255535 K (Java/Others)
Total Submission(s): 572    Accepted Submission(s): 166

Problem Description

Chiaki has a sequence A={a1,a2,…,an}. Let RMQ(A,l,r) be the minimum i (lir) such that ai is the maximum value in al,al+1,…,ar.

Two sequences A and B are called \textit{RMQ Similar}, if they have the same length n and for every 1≤lrn, RMQ(A,l,r)=RMQ(B,l,r).

For a given the sequence A={a1,a2,…,an}, define the weight of a sequence B={b1,b2,…,bn} be ∑i=1nbi (i.e. the sum of all elements in B) if sequence B and sequence A are RMQ Similar, or 0 otherwise. If each element of B is a real number chosen independently and uniformly at random between 0 and 1, find the expected weight of B.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤106) -- the length of the sequence.
The second line contains n integers a1,a2,…,an (1≤ain) denoting the sequence.
It is guaranteed that the sum of all n does not exceed 3×106.

Output

For each test case, output the answer as a value of a rational number modulo 109+7.
Formally, it is guaranteed that under given constraints the probability is always a rational number pq (p and q are integer and coprime, q is positive), such that q is not divisible by 109+7. Output such integer a between 0 and 109+6 that paq is divisible by 109+7.

Sample Input

3 3 1 2 3 3 1 2 1 5 1 2 3 2 1

Sample Output

250000002 500000004 125000001

Source

2018 Multi-University Training Contest 1

Recommend

liuyiding

Statistic | Submit | Discuss | Note

hdu6305(笛卡尔树/分治)相关推荐

  1. 牛客多校3 - Sort the Strings Revision(笛卡尔树+分治)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数字串 s[ 0 ],每个位置的赋值初始时为 s[ i ] = i % 10 ( i ∈ [ 0 , n - 1 ] ),现在有一个长度为 n 的排 ...

  2. 【IOI2018】会议【笛卡尔树】【dp】【线段树】

    题意:长度为nnn的序列,qqq次询问,每次给定一个区间,钦定区间中的一个位置xxx,使得区间所有点 与xxx之间的最大值(含端点) 之和 最小,输出最小值. n,q≤7.5×105n,q\leq7. ...

  3. YbtOJ#752-最优分组【笛卡尔树,线段树】

    正题 题目链接:http://www.ybtoj.com.cn/problem/752 题目大意 nnn个人,每个人有cic_ici​和did_idi​分别表示这个人所在的队伍的最少/最多人数. 然后 ...

  4. HDU - 6305 RMQ Similar Sequence(笛卡尔树)

    http://acm.hdu.edu.cn/showproblem.php?pid=6305 题目 对于A,B两个序列,任意的l,r,如果RMQ(A,l,r)=RMQ(B,l,r),B序列里的数为[0 ...

  5. [算法学习] 线段树,树状数组,数堆,笛卡尔树

    都是树的变种,用途不同 [线段树 Interval Tree] 区间管理,是一种平衡树 可看做是对一维数组的索引进行管理.一维数组不需要是排序好的 深度不超过logL 任一个区间(线段)都分成不超过2 ...

  6. 洛谷 - P4755 Beautiful Pair(笛卡尔树+主席树)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数列 a,现在一个数对 ( i , j ) 如果满足 a[ i ] * a[ j ] <=max( a[ i ] ~ a[ j ] ),则称其 ...

  7. 牛客 - sequence(笛卡尔树+线段树)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数列 a 和数列 b ,求 题目分析:不算难的题目,对于每个 a[ i ] 求一下贡献然后维护最大值就好,具体思路就是,先找出每个 a[ i ] 左 ...

  8. POJ - 2559 Largest Rectangle in a Histogram(笛卡尔树,单调栈实现)

    题目链接:点击查看 题目大意:给出一排高度不同,宽度都为 1 的矩形,问拼起来后最大的矩形面积是多少 题目分析:普通做法是用单调栈直接维护,我一直觉得单调栈处理这种矩形问题都比较抽象,也可能是我太菜了 ...

  9. POJ - 2201 Cartesian Tree(笛卡尔树-单调栈/暴跳父亲)

    题目链接:点击查看 题目大意:给出n个节点的key和val,构造出其笛卡尔树的原型 笛卡尔树的定义: 所谓笛卡尔树,就是将给定的n个二元组(key,val)建成一棵树.使得: 如果只关注key,那么这 ...

最新文章

  1. 004-ubuntu安装配置SSH服务
  2. 99% 的新移动恶意程序是针对 Android
  3. java8获取实现某个接口的所有类_Java 试题八
  4. 使用React和Tailwind CSS搭建项目模板
  5. linux oracle11g开机,Linux 下Oracle11g 自动随系统启动
  6. java实验的总结_java实验总结
  7. 【css练习】斑马线表格,美人尖,断线下划线
  8. Deploy Office Communications Server 2007R2 Group Chat Server(一)
  9. 解决远程桌面关闭后teamviewer不能连接的问题
  10. 什么样的人适合微信创业
  11. [Python从零到壹] 十.网络爬虫之Selenium爬取在线百科知识万字详解(NLP语料构造必备技能)
  12. 【opencv450】Vibe算法进行前景检测
  13. 使用Jenkins实现自动化构建!
  14. OpenCV——SAD立体匹配
  15. Unity 5.3 官方VR教程(二) 创建第一个VR项目
  16. C#、C++、Java、Python 选择哪个好?
  17. 根据设计稿,用JS计算rem的值
  18. 【云学习笔记】二、免费云服务器与免费域名组合打造自己的个人空间
  19. Qt表格分页,跳转,首尾页
  20. 爱普生EPSON实时时钟芯片-RX8111CE

热门文章

  1. MySQL索引基础续
  2. EI 期刊目录 下载和查询方法(很简单)
  3. 关于魔兽世界封号机制的数据统计
  4. EasyPlayerPro:安卓视频播放器Android H.265硬解码方案(内含代码)
  5. 人的记忆组成图(原创整理,转载请注明)
  6. 2022年起重机械安全管理复训题库模拟考试平台操作
  7. 解读艾略特波浪理论中的自然法则
  8. Maven: Non-resolvable import POM:Failure to find *** in *** was cached in the local repository.
  9. 勒贝格外侧度为0的集合勒贝格可测.
  10. 疫情在家游戏玩腻了?那就一起来开发H5小游戏吧