笛卡尔树

· · 个人记录

模板题

构造笛卡尔树的过程特别像递归,非常美妙。现在大概有两种求笛卡尔树的方法。

并查集求法

时间复杂度 \Theta(n \log n),不过如果是排列就可以 \Theta(n)。

#include <bits/stdc++.h>
#define FAST ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
int n;
int a[10000010];
int t[10000010];
int l[10000010],r[10000010],p[10000010];//l,r如题,p 是并查集 

inline int find(const int x){
    if(p[x]==x) return x;
    p[x] = find(p[x]);
    return p[x];
}
inline void merge(const int x,const int y){
    p[find(x)] = find(y);
    return;
}
inline int qread(){
    int c,ret;
    while((c=getchar())> '9'||c< '0');
    ret=c-'0';
    while((c=getchar())>='0'&&c<='9')
    ret=ret*10+c-'0';
    return ret;
}

signed main()
{
    n = qread();
    for (register int i=1;i<=n;++i){
        a[i] = qread();
        t[a[i]] = p[i] = i;
    }
    register long long L = 0,R = 0;
    for (register int i=n;i>=1;--i){
        const int pos = t[i];
        if(a[pos-1]>a[pos]){
            register long long h = pos;
            h *= (1+find(pos-1));
            L ^= h;
            merge(pos-1,pos);
        }
        else L ^= pos;

        if(a[pos+1]>a[pos]){
            register long long h = pos;
            h *= (1+find(pos+1));
            R ^= h;
            merge(pos+1,pos);
        }
        else R ^= pos;
    }
    printf("%lld %lld",L,R);
    return 0;
}

单调栈求法

维护从根往下走的“右链”,用单调栈即可维护。

#include <bits/stdc++.h>
#define FAST ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
int n;
int a[10000010];
int l[10000010],r[10000010];//左儿子,右儿子,父亲 

inline int qread(){
    int c,ret;
    while((c=getchar())> '9'||c< '0');
    ret=c-'0';
    while((c=getchar())>='0'&&c<='9')
    ret=ret*10+c-'0';
    return ret;
}

signed main()
{
    n = qread();
    for (int i=1;i<=n;i++) a[i] = qread();
    stack<int> st;
    int root = 0;
    for (int i=1;i<=n;i++){
        int f = 0;
        while(!st.empty()&&a[i]<a[st.top()]){
            f = st.top();
            st.pop();
        }
        if(!st.empty()) r[st.top()] = i;
        else root = i;//这是根 
        l[i] = f;
        st.push(i);
    }
    long long L = 0,R = 0;
    for (int i=1;i<=n;i++){
        long long h = i;
        h *= (1+l[i]);
        L ^= h;

        h = i;
        h *= (1+r[i]);
        R ^= h;
    }
    cout << L << " " << R << "\n";
    return 0;
}

实际用处?

```cpp #include <bits/stdc++.h> #define FAST ios::sync_with_stdio(false);cin.tie(0) using namespace std; namespace GenHelper { unsigned z1,z2,z3,z4,b; unsigned rand_() { b=((z1<<6)^z1)>>13; z1=((z1&4294967294U)<<18)^b; b=((z2<<2)^z2)>>27; z2=((z2&4294967288U)<<2)^b; b=((z3<<13)^z3)>>21; z3=((z3&4294967280U)<<7)^b; b=((z4<<3)^z4)>>12; z4=((z4&4294967168U)<<13)^b; return (z1^z2^z3^z4); } } void srand(unsigned x) {using namespace GenHelper; z1=x; z2=(~x)^0x233333333U; z3=x^0x1234598766U; z4=(~x)+51;} int read() { using namespace GenHelper; int a=rand_()&32767; int b=rand_()&32767; return a*32768+b; } int n,m,s; int a[20000010]; int l[20000010],r[20000010]; signed main() { cin >> n >> m >> s; srand(s); for (int i=1;i<=n;i++) a[i] = read(); //构造笛卡尔树 stack<int> st; int root = 0; for (int i=1;i<=n;i++){ int k = 0; while(!st.empty()&&a[i]>a[st.top()]){ k = st.top(); st.pop(); } if(!st.empty()) r[st.top()] = i; else root = i; st.push(i); l[i] = k; } unsigned long long ans = 0; for (int i=1;i<=m;i++){ int L = read()%n+1,R = read()%n+1; if(L>R) swap(L,R); int x = root; while(x<L||x>R){ if(x<L) x = r[x]; else x = l[x]; } ans += a[x]; } cout << ans << "\n"; return 0; } ``` ### Imbalance 神题。 建出树上的笛卡尔树(也叫kruskal重构树),建一个大根树,一个小根树。此时我们注意到,在(大/小)根树上,任意一条从祖先走向后代的路径,路径最(大/小)值都是祖先对应的值。因此问题转化成了这样:有两棵树,问有多少个点对 $(x,y)$,满足在其中一颗树上 $x$ 是 $y$ 的祖先,另一棵树上 $y$ 是 $x$ 的祖先。而这个东西用 $dfs$ 序随便做一做就好啦~ ```cpp #include <bits/stdc++.h> #define int long long #define FAST ios::sync_with_stdio(false);cin.tie(0) using namespace std; int n; vector<int> v[500010]; vector<int> g1[500010]; vector<int> g2[500010]; int p[500010]; int l[500010],r[500010],idx; int t[500010]; int ans = 0; void clear(){ for (int i=1;i<=n;i++) p[i] = i; return; } int find(int x){ if(x==p[x]) return x; p[x] = find(p[x]); return p[x]; } void dfs1(int d,int fa){ l[d] = ++idx; for (auto i : g1[d]){ if(i!=fa) dfs1(i,d); } r[d] = idx; return; } int lowbit(int x){ return (x&-x); } void add(int x,int c){ while(x<=n){ t[x] += c; x += lowbit(x); } return; } int query(int x){ int res = 0; while(x){ res += t[x]; x -= lowbit(x); } return res; } void dfs2(int d,int fa){ ans += query(r[d])-query(l[d]-1); add(l[d],1); for (auto i : g2[d]){ if(i!=fa) dfs2(i,d); } add(l[d],-1); return; } signed main() { freopen("imbalance.in","r",stdin); freopen("imbalance.out","w",stdout); cin >> n; for (int i=1;i<=n-1;i++){ int x,y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } //先建小根笛卡尔树 clear(); for (int i=n;i>=1;i--){ for (auto j : v[i]){ if(j>i){ g1[i].push_back(find(j)); p[find(j)] = i; } } } //再建大根笛卡尔树 clear(); for (int i=1;i<=n;i++){ for (auto j : v[i]){ if(j<i){ g2[i].push_back(find(j)); p[find(j)] = i; } } } //现在,我们要求出点对 (x,y) 的数量, //满足在 g1 和 g2 上,(x,y) 都是祖先与后代的关系 dfs1(1,0); dfs2(n,0); cout << ans << "\n"; return 0; } ``` ### 一些巧思 求 $\sum_{i=1}^n \sum_{j=i}^n \min(a_i,a_{i+1},...,a_{j}),n \leq 10^7

建出笛卡尔树,然后用左子树大小乘上右子树大小就行了。