跳到正文

个人记录

(伪)Euler Tour Tree

peop1e约 14 分钟
原文
文章信息与版本

文章信息

发布
2022年4月16日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 14 分钟

版本

  1. v1当前

提示:这棵树没有任何实用价值

我们发现,树剖是在dfs序的基础上把重链上的点拼接在了一起,使其连续,相当于在普通dfs序做了一些平移。

重链保证了链的数量是log的。

那么如果我们想让它像LCT一样,直接拉出一整条链呢?

平移啊!

用splay维护就可以实现区间平移。

而这个平移的过程与access中虚实边切换一模一样,可以保证次数是log的。

所以,我们可以通过平移使一个点到根的路径全部聚集在dfs序的左侧。

同时还能保证子树连续。

只是代码有亿点点难写而已。并且常数巨大。

但是可以实现link/cut/换子树(通过平移)。

这棵树也可以叫做伪ETT,因为真正的ETT内置的是欧拉序,通过区间翻转可以换根。而我们的树不行。

其实换子树操作我们的树也实现不了,因为我们的辅助树LCT他不支持换子树。

除了换子树操作,其他操作一只log的top tree都可以实现。

所以这玩意其实一点用没有 优点是比top tree好写?

代码已过P3384 【模板】轻重链剖分/树链剖分。

C++
#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
#define int long long
#define __int520 signed
#define C(a) int a;cin>>a
#define F(a) for(int i=1;i<=a;i++)
#define maxn 100010
int num[maxn];
int P;
namespace S{
#define ls (x->s[0]?x->s[0]:kg)
#define rs (x->s[1]?x->s[1]:kg)
#define inf 0x3f3f3f3f
struct chr{
    chr* f;chr* s[2];
    int n,a,tg,sz;
    void pt(int w){tg+=w;n+=w;a+=sz*w%P;tg%=P;n%=P;a%=P;}
    void u(){
        sz=1;if(s[0])sz+=s[0]->sz;if(s[1])sz+=s[1]->sz;
        a=n;if(s[0])a+=s[0]->a;if(s[1])a+=s[1]->a;a%=P;
    }
    void d(){
        if(tg){
            if(s[0])s[0]->pt(tg);
            if(s[1])s[1]->pt(tg);
            tg=0;
        }
    }
    int r(){return f->s[1]==this;}
    void c(chr* &x,int o){s[o]=x;if(x)x->f=this;}
}t[maxn],kng;
int n;
int xl[maxn];
chr* kg=&kng;chr* rt=t;chr* cnt=t;
chr* sb;
chr* st[maxn];
int ed[maxn];
void rot(chr* x){
    chr* y=x->f;int r=x->r();
    if(!y->f)x->f=NULL;else y->f->c(x,y->r());
    y->c(x->s[!r],r);
    x->c(y,!r);
    y->u();
}
void ad(chr* x){
    if(x->f)ad(x->f);
    x->d();
}
void splay(chr* x,chr* w){
    ad(x);
    while(x->f!=w){
        if(x->f->f==w)rot(x);
        else if(x->r()==x->f->r())rot(x->f),rot(x);
        else rot(x),rot(x);
    }x->u();
}
void build(chr* &x,chr* y,int l,int r){
    if(l>r)return;
    int mid=(l+r)>>1;
    x=t+mid;x->f=y;
    x->n=xl[mid];
    if(mid==0)sb=x;
    build(x->s[0],x,l,mid-1);
    build(x->s[1],x,mid+1,r);
    x->u();
}
chr* qq(chr* x){
    splay(x,rt);x=x->s[0];x->d();
    while(x->s[1])x=x->s[1],x->d();
    return x;
}
chr* hj(chr* x){
    splay(x,rt);x=x->s[1];x->d();
    while(x->s[0])x=x->s[0],x->d();
    return x;
}
void move(chr* l,chr* r,chr* w){
    r=hj(r);l=qq(l);
    splay(l,rt);splay(r,l);
    chr* x=r->s[0];r->s[0]=NULL;
    chr *v=hj(w);splay(w,rt);splay(v,w);
    v->c(x,0);
}
void add(chr* l,chr* r,int w){
    splay(l,rt);splay(r,l);
    if(r->s[0])r->s[0]->pt(w);
}
int query(chr* l,chr* r){
    splay(l,rt);splay(r,l);
    if(r->s[0])return r->s[0]->a;
    return 0;
}
chr* fd(chr* x,int w){
    x->d();
    if(w<=ls->sz)return fd(ls,w);
    if(w==ls->sz+1)return x;
    return fd(rs,w-ls->sz-1);
}
int gt(chr* x){
    splay(x,rt);
    return ls->sz+2;
}
void ot(chr* x){
    if(x->s[0])ot(x->s[0]);
    cout<<xl[x-t]<<" ";
    if(x->s[1])ot(x->s[1]);
}
};
namespace L{
struct chr{
    int tag;
    chr *f;
    chr *son[2];
}tree[maxn];
chr *stack[maxn];
void push_tag(chr *x){
    x->tag^=1;
    swap(x->son[0],x->son[1]);
}
void pushup(chr *x){
}
void pushdown(chr *x){
    if(!x)return;
    if(x->tag){
        if(x->son[0])push_tag(x->son[0]);
        if(x->son[1])push_tag(x->son[1]);
        x->tag^=1;
    }
}
bool nroot(chr *x){
    if(!x->f)return 0;
    return x->f->son[0]==x||x->f->son[1]==x;
}
void rot(chr *x){
    chr *y=x->f;
    chr *z=y->f;
    int t1=y->son[1]==x;
    int t2;
    if(z)t2=z->son[1]==y;
    int t3=nroot(y);
    y->son[t1]=x->son[t1^1];
    if(x->son[t1^1])
        x->son[t1^1]->f=y;
    x->son[t1^1]=y;
    y->f=x;
    x->f=z;
    if(z&&t3)z->son[t2]=x;
    pushup(y);
}
void splay(chr *x){
    int pos=0;
    chr* t=x;
    stack[pos]=x;
    while(nroot(t)){
        t=t->f;
        pos++;
        stack[pos]=t;
    }
    while(pos>=0){
        pushdown(stack[pos]);
        pos--;
    }
    while(nroot(x)){
        chr *y=x->f;
        if(nroot(y)){
            chr *z=y->f;
            int t1=y->son[1]==x;
            int t2=z->son[1]==y;
            if(t1==t2)rot(y);
            else rot(x);
        }
        rot(x);
    }
    pushup(x);
}
chr* access(chr *x){
    chr* y=NULL;
    for(;x;x=(y=x)->f){
        splay(x);x->son[1]=y;
        if(y){
            chr* z=y;pushdown(z);
            while(z->son[0])z=z->son[0],pushdown(z);
            S::move(S::st[z-tree],S::fd(S::rt,S::gt(S::st[z-tree])+S::ed[z-tree]),S::st[x-tree]);
        }pushup(x);
    }
    return y;
}
chr* findroot(chr *x){
    access(x);splay(x);
    while(x->son[0]){pushdown(x);x=x->son[0];}
    splay(x);
    return x;
}
void makeroot(chr *x){
    access(x);splay(x);
    x->son[1]=NULL;
    push_tag(x);
}
void split(chr *x,chr *y){
    makeroot(x);
    access(y);
    splay(y);
}
void link(chr *x,chr *y){
    makeroot(x);
    if(findroot(y)==x)return;
    x->f=y;
}
void cut(chr *x,chr *y){
    makeroot(x);
    if(findroot(y)==x&&y->f==x&&(!y->son[0])){
        x->son[1]=NULL;
        y->f=NULL;
        pushup(x);
    }
}
};
namespace D{
int tot=0;
vector<int> ve[maxn];
void add(int s,int t){
    ve[s].push_back(t);
    ve[t].push_back(s);
}
void dfs(int now,int from){
    S::xl[++tot]=num[now];
    S::st[now]=S::t+tot;
    int size=ve[now].size();
    for(int i=0;i<size;i++){
        int nxt=ve[now][i];
        if(nxt==from)continue;
        dfs(nxt,now);
        L::tree[nxt].f=L::tree+now;
    }
    S::ed[now]=S::t+tot-S::st[now];
}
};
namespace M{
void love1(){
    C(s);C(t);C(w);
    L::chr* x=L::tree+s;L::chr* y=L::tree+t;
    access(x);
    S::add(S::sb,S::hj(S::st[s]),w);
    L::chr* lca=access(y);
    S::add(S::sb,S::hj(S::st[t]),w);
    S::add(S::sb,S::hj(S::st[lca-L::tree]),-w);
    if(S::qq(S::st[lca-L::tree])!=S::sb)add(S::sb,S::st[lca-L::tree],-w);
}
void love2(){
    C(s);C(t);
    L::chr* x=L::tree+s;L::chr* y=L::tree+t;
    access(x);
    int re=0;
    re+=S::query(S::sb,S::hj(S::st[s]));
    L::chr* lca=access(y);
    re+=S::query(S::sb,S::hj(S::st[t]));
    re-=S::query(S::sb,S::hj(S::st[lca-L::tree]));
    if(S::qq(S::st[lca-L::tree])!=S::sb)re-=S::query(S::sb,S::st[lca-L::tree]);
    cout<<(re%P+P)%P<<"\n";
}
void love3(){
    C(s);C(w);
    S::add(S::qq(S::st[s]),S::hj(S::fd(S::rt,S::gt(S::st[s])+S::ed[s])),w);
}
void love4(){
    C(s);
    int re=S::query(S::qq(S::st[s]),S::hj(S::fd(S::rt,S::gt(S::st[s])+S::ed[s])));
    cout<<(re%P+P)%P<<"\n";
}
};
__int520 main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    C(N);C(T);C(R);cin>>P;
    F(N)cin>>num[i];
    F(N-1){
        C(s);C(t);
        D::add(s,t);
    }
    D::dfs(R,0);
    N=D::tot+1;
    S::rt=S::t+maxn-1;
    S::build(S::rt->s[1],S::rt,0,N);
    while(T--){
        C(opt);
        switch(opt){
            case 1:M::love1();break;
            case 2:M::love2();break;
            case 3:M::love3();break;
            case 4:M::love4();break;
        }
    }
    return 0;
}
原文