笛卡尔树

Posted on 2026-10-23 14:22  暮光落阳  阅读(18)  评论(0)    收藏  举报

一种树形数据结构,其键满足二叉搜索树的性质(从左向右递增),值满足堆的性质(子结点小于父节点)。

形象地说,对于一个数组,对它建成的笛卡尔树中,各个节点的横向相对位置不变,而纵向根据其值分层。

生动的图——源自维基百科

自然地,会发现这棵树非常美妙,如:

  • 每个子树都对应原数组中的一段互不交叉的连续区间
  • 各子树根节点的值都是对应区间的最值

这就使它能处理一类与区间最值相关的问题,可以显著降低思维复杂度。

但是由于笛卡尔树对树高没有保证,所以处理搜索,查询等操作时最坏时间复杂度(链)为 \(O(n)\) ,并没有优势,而建树,整体遍历时时空复杂度均为 \(O(n)\) ,碾压一众 \(O(n\log n)\) 的树和堆。


建树

从左向右建,根据值的大小确定父子关系,通过断掉已有的边把新节点插入树的合适位置。

详细地说,用栈维护从根一直走右儿子的链(栈顶是当前最右端的元素 \(a[i-1]\),栈底是根,下标与值均单调),加入新元素 \(a[i]\) 时,保持单调栈性质,弹出元素至可加入 \(a[i]\) ,将此时栈顶节点的右子树作为 \(i\) 的左子树,再让 \(i\) 作为栈顶节点的右子树(若栈空,将此时根节点作为 \(i\) 的左子树,让 \(i\) 作为根节点)。

一个示例:

stack<int> st;
root=0;//常用0作为空
for(int i=1;i<=n;i++){
    while(!st.empty()&&a[st.top()]>a[i])st.pop();
    if(!st.empty())tr[i].lc=tr[st.top()].rc,tr[st.top()].rc=i;
    else tr[i].lc=root,root=i;
    st.push(i);
}

例题

P5854 【模板】笛卡尔树

如上建树,之后如树般遍历即可(遍历数组也行)。

Code
#include <bits/stdc++.h>

using namespace std;

inline int read(){
    int x=0;char c=getchar();
    while(c<'0'||c>'9')c=getchar();
    while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
    return x;
}//1e7,不开快读?

struct Node{
    int lc=0,rc=0;
};

int n,root;
int a[10000010];
Node tr[10000010];
int st[10000010],k=0;//数组模拟栈

signed main(){
    //freopen("test.in","r",stdin);
    //freopen("test.out","w",stdout);

    n=read();
    for(int i=1;i<=n;i++)a[i]=read();
    root=0;
    for(int i=1;i<=n;i++){
        while(k>0&&a[st[k]]>a[i])k--;
        if(k)tr[i].lc=tr[st[k]].rc,tr[st[k]].rc=i;
        else tr[i].lc=root,root=i;
        st[++k]=i;
    }
    long long l=0,r=0;
    for(int i=1;i<=n;i++)l^=1ll*i*(tr[i].lc+1),r^=1ll*i*(tr[i].rc+1);//遍历数组
    printf("%lld %lld\n",l,r);

    return 0;
}


HISTOGRA - Largest Rectangle in a Histogram

笛卡尔树经典入门题,只需建小根树,之后 dfs 遍历得到各子树对应区间长度,与子树根节点乘积取最大值即可。

正确性上,对于任一子树对应区间,扩展后区间最小值必然减小,且区间长度小于等于其祖先节点对应区间。

Code
#include <bits/stdc++.h>
#define int long long

using namespace std;

int read(){
    int i;
    scanf("%lld",&i);
    return i;
}

int n;
int a[100010];
int lc[100010],rc[100010];
int st[100010],key=0,root=0,ans;

int dfs(int i){
    int re=1;
    if(lc[i])re+=dfs(lc[i]);
    if(rc[i])re+=dfs(rc[i]);
    ans=max(re*a[i],ans);
    return re;
}

signed main(){
    //freopen("test.in","r",stdin);
    //freopen("test.out","w",stdout);

    while((n=read())){
        for(int i=1;i<=n;i++)a[i]=read();
        for(int i=1;i<=n;i++)lc[i]=rc[i]=0;
        root=key=0;
        for(int i=1;i<=n;i++){
            while(key&&a[st[key]]>a[i])key--;
            if(key)lc[i]=rc[st[key]],rc[st[key]]=i;
            else lc[i]=root,root=i;
            st[++key]=i;
        }
        ans=0;
        dfs(root);
        printf("%lld\n",ans);
    }
    return 0;
}


PERIODNI - Periodni

与上一道图相似,这很笛卡尔树。

要求不能同行/同列,不过可以通过高度隔断。

询问填写方案数。

考虑暴力 dp ,被分开的两段是互不干扰的,于是可以从上向下 dp,每次处理纵坐标连续的一段,将已处理的连通区域合并。

但是也可以以高度小为根建立笛卡尔树,通过笛卡尔树将图划分为若干个矩形,节点对应横向范围为其子树,纵向范围为与父结点高度的差值。这样就转化成了树形 dp ,先合并其左右儿子,再与节点本身合并。

设 \(dp[u][x]\) 为节点 \(u\) 子树内已选定 \(x\) 个点的方案数,那么有:

\[dp[u][w]=\sum_{i+j+k=w} dp[lson_u][i]\times dp[rson_u][j]\times \begin{pmatrix} x_u-i-j\\k \end{pmatrix} \begin{pmatrix} y_u\\k \end{pmatrix} k! \]

\(x_u\) 是节点对应横向范围,\(y_u\) 是节点对应纵向范围。

分别合并,每个节点 \(n^2\) 次操作,总时间复杂度 \(O(n^3)\) 。

Code
#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
using namespace std;

int read(){
 int i;
 scanf("%lld",&i);
 return i;
}

struct Node{
 int lc,rc;
};

int n,k;
int a[510];
Node tr[510];
int x[510],y[510];
int dp[510][510];
int fc[1000010],ro[1000010];
int root;

void mct(){
 root=0;int st[510]={},key=0;
 for(int i=1;i<=n;i++){
  while(key&&a[st[key]]>a[i])key--;
  if(key)tr[i].lc=tr[st[key]].rc,tr[st[key]].rc=i;
  else tr[i].lc=root,root=i;
  st[++key]=i;
 }
}

void dfs(int u){
 if(tr[u].lc)y[tr[u].lc]=a[tr[u].lc]-a[u],dfs(tr[u].lc),x[u]+=x[tr[u].lc];
 if(tr[u].rc)y[tr[u].rc]=a[tr[u].rc]-a[u],dfs(tr[u].rc),x[u]+=x[tr[u].rc];
 int te[510]={};
 for(int i=0;i<=k;i++)
  for(int j=0;i+j<=k;j++)
   te[i+j]=(te[i+j]+dp[tr[u].lc][i]*dp[tr[u].rc][j]%mod)%mod;
 for(int i=0;i<=k;i++)
  for(int j=0;i+j<=k&&j<=x[u]-i&&j<=y[u];j++)
   dp[u][i+j]=(dp[u][i+j]+te[i]*fc[x[u]-i]%mod*ro[x[u]-i-j]%mod*fc[y[u]]%mod*ro[y[u]-j]%mod*ro[j]%mod)%mod;
}

signed main(){
//  freopen("test.in","r",stdin);
 n=read(),k=read();
 fc[0]=ro[0]=1;ro[1000000]=397802501ll;
 for(int i=1;i<=1000000;i++)fc[i]=fc[i-1]*i%mod;
 for(int i=999999;i>=1;i--)ro[i]=ro[i+1]*(i+1)%mod;
 for(int i=1,p=0,te;i<=n;i++){
  te=read();
  if(te==a[p])x[p]++;
  else a[++p]=te,x[p]=1;
  if(i==n){
   n=p;
   break;
  }
 }

 mct();
 dp[0][0]=1;y[root]=a[root];
 dfs(root);
 printf("%lld",dp[root][k]%mod);
}