hdu6305(笛卡尔树/分治)

这道需要注意到一点。。就是如果询问的区间如果覆盖了最大数,那么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

猜你喜欢

转载自blog.csdn.net/qkoqhh/article/details/81184886