DS 总结

· · 个人记录

树分块

对于只有链操作的树分块,考虑有一种简单的撒点方式,即将点按 dep 从大到小排序,然后对于每一个点,向上 S 步,若中途路过了关键点,就跳出,否则将其 S 级祖先定为关键点,然后对关键点建虚树,虚树上的节点才是真的关键点。

最后令每一个点属于离它最近的关键点祖先(1 必须是关键点),虽然无法保证每个块的节点个数,但是可以保证关键点的个数(每次设定关键点都至少会使 S 个点不会设置关键点),且一条路径上最多也只有 O(\frac{n}{S}) 节点,每一个节点向上跳的步数最多也只有 S 步,比较优秀且好写。

(⊙o⊙)?我好像忘记建虚树了。

然后考虑分讨不少细节,从模板题出发。

模板

此处取 S=\sqrt n。

即链上数颜色,用刚才的方式撒点,预处理两两关键点间的答案和每一个关键点到根的每个元素出现次数的桶,这一步时空都是 O(n\sqrt n) 的。

然后散块暴力跳,有不少细节(挨个挨个拍出来的)。

以下标黑的点是关键点。

  1. 同块(最近的关键点祖先相同):暴力相邻跳,再清空。
  2. 两个端点与根在同一条链上,也即:

暴力跳出链上深度小于等于 u 的最浅的关键点,处理出答案,散块暴力跳。

  1. 中途转弯(与上面可以写在一起)。也即:

还是暴力跳出路径上深度不超过 u 的最浅的关键点(我知道不超过 LCA 就行,但这样方便一些),然后散块暴力跳。

  1. 常规情况,也即:

此时保证离两个起点最近的关键点祖宗与 LCA 都是关键点,散块暴力,整块预处理。

代码(有注释):

#include<bits/stdc++.h>

using namespace std;

namespace Fread {
    const int SIZE=1<<21;char buf[SIZE],*S,*T;
    inline char getchar() {if(S==T){T=(S=buf)+fread(buf,1,SIZE,stdin);if(S==T)return '\n';}return *S++;}
}
namespace Fwrite {
    const int SIZE=1<<21;
    char buf[SIZE],*S=buf,*T=buf+SIZE;
    inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}
    inline void putchar(char c){*S++=c;if(S==T)flush();}
    struct POPOSSIBLE{~POPOSSIBLE(){flush();}}ztr;
}
#define getchar Fread :: getchar
#define putchar Fwrite :: putchar
namespace Fastio{
    struct Reader{
        template<typename T>
        Reader& operator >> (T& x) {
            char c=getchar();T f=1;
            while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}x=0;
            while(c>='0'&&c<='9'){x=x*10+(c-'0');c=getchar();}x*=f;
            return *this;
        }
        Reader(){}
    }cin;
    struct Writer{
        template<typename T>
        Writer& operator << (T x) {
            if(x==0){putchar('0');return *this;}
            if(x<0){putchar('-');x=-x;}
            static int sta[45];int top=0;
            while(x){sta[++top]=x%10;x/=10;}
            while(top){putchar(sta[top]+'0');--top;}
            return *this;
        }
        Writer& operator << (char c) {putchar(c);return *this;}
        Writer(){}
    }cout;
}
#define endl '\n'
#define cin Fastio :: cin
#define cout Fastio :: cout
#define up(x,y) now=x;while(now!=y){las+=(!t[val[now]]++)&&!ask(xx,yy,z,val[now]);now=father[now];}
#define del(x,y) now=x;while(now!=y){--t[val[now]];now=father[now];}

const int B=410;
const int N=2e5+10;
struct edge{
    int to,ne;
    edge(int to=0,int ne=0):
        to(to),ne(ne){}
}a[N];
int top[N],dep[N],siz[N],father[N],son[N],b[N],res[B][N],ans[B][B],ys[N];
int cnt,tot,h[N],k,lenth,id[N],idx,val[N],n,m,tmp,t[N],fr[N],ID[N];
vector < int > p;

inline void add(const int x,const int y){a[++cnt]=edge(y,h[x]);h[x]=cnt;}
inline bool cmp(const int x,const int y){return dep[x]>dep[y];}

//预处理树剖

void dfs1(int x,int y){
    siz[x]=1;father[x]=y;
    dep[x]=dep[y]+1;ID[x]=++ID[0];
    for(int i=h[x];i;i=a[i].ne)
        if(a[i].to!=y){
            dfs1(a[i].to,x);
            siz[x]+=siz[a[i].to];
            son[x]=siz[son[x]]>siz[a[i].to]?son[x]:a[i].to;
        }
}

void dfs2(int x,int topf){
    top[x]=topf;
    if(son[x]) dfs2(son[x],topf);
    for(int i=h[x];i;i=a[i].ne)
        if(!top[a[i].to]) dfs2(a[i].to,a[i].to);
}

inline int LCA(int x,int y){
    while(top[x]!=top[y]) dep[top[x]]>dep[top[y]]?x=father[top[x]]:y=father[top[y]];
    return dep[x]<dep[y]?x:y;
}

//撒点

void Up(int x,int t){
    if(id[x]) return ;
    if(!t) id[x]=1;
    Up(father[x],t-1);
}

//预处理两两答案
void Init(int x,int y,int Id){
    tmp+=!t[val[x]]++;
    if(id[x]) ans[id[x]][Id]=tmp;
    for(int i=h[x];i;i=a[i].ne) if(a[i].to!=y) Init(a[i].to,x,Id);
    tmp-=!--t[val[x]];
}

//预处理到根的桶
void dfs3(int x,int y){
    ++t[val[x]];fr[x]=(id[x]?x:fr[y]);
    if(id[x]) for(int i=1;i<=tot;++i) res[id[x]][i]=t[i];
    for(int i=h[x];i;i=a[i].ne) if(a[i].to!=y) dfs3(a[i].to,x);
    --t[val[x]];
}

inline int ask(const int x,const int y,const int z,const int v){return res[id[x]][v]+res[id[y]][v]-(res[id[z]][v]<<1)+(val[z]==v);}//看 v 在 x->y 的路径上出现了多少次
inline bool in(const int x,const int y){return ID[y]<=ID[x]&&ID[x]<=ID[y]+siz[y]-1;}//判 x 是否在 y 的子树里
inline bool Cmp(const int x,const int y){return ID[x]<ID[y];}
inline void BuildTree(){
    sort(p.begin(),p.end(),Cmp);
    int len=p.size();
    for(int i=1;i<len;++i) p.emplace_back(LCA(p[i-1],p[i]));
    sort(p.begin(),p.end(),Cmp);
    p.erase(unique(p.begin(),p.end()),p.end());
    for(auto i:p) id[i]=++idx;
}

/*
    8 2
    105 2 9 3 8 5 7 7 
    1 2
    1 3
    1 4
    3 5
    3 6
    3 7
    4 8
    2 5
    3 8
*/

signed main(){
    cin>>n>>m;id[1]=++idx;lenth=sqrt(n);

    //离散化
    for(int i=1;i<=n;++i){cin>>val[i];ys[++tot]=val[i];}
    sort(ys+1,ys+1+tot);tot=unique(ys+1,ys+1+tot)-ys-1;
    for(int i=1;i<=n;++i) val[i]=lower_bound(ys+1,ys+1+tot,val[i])-ys;

    for(int i=1,x,y;i<n;++i){cin>>x>>y;add(x,y);add(y,x);}

    //预处理
    dfs1(1,0);dfs2(1,1);
    for(int i=1;i<=n;++i) b[i]=i;
    sort(b+1,b+1+n,cmp);
    for(int i=1;i<=n;++i) Up(b[i],lenth);
    for(int i=1;i<=n;++i) if(id[i]) p.emplace_back(i);
    BuildTree();dfs3(1,0);
    for(int i:p) Init(i,i,id[i]);

    //up 和 del 是宏定义
    for(int i=1,x,y,z,las=0;i<=m;++i){
        cin>>x>>y;x^=las;
        int xx=fr[x],yy=fr[y],now;
        if(dep[xx]<dep[yy]){swap(xx,yy);swap(x,y);}
        if(xx==yy){//情况1
            xx=x;yy=y;las=0;
            while(xx!=yy){if(dep[xx]<dep[yy]) swap(xx,yy);las+=!t[val[xx]]++;xx=father[xx];}
            cout<<(las+=!t[val[xx]])<<endl;xx=x;yy=y;
            while(xx!=yy){if(dep[xx]<dep[yy]) swap(xx,yy);--t[val[xx]];xx=father[xx];}
        }else if(!in(xx,yy)){//情况4
            las=ans[id[xx]][id[yy]];z=LCA(xx,yy);
            up(x,xx);up(y,yy);
            del(x,xx);del(y,yy);
            cout<<las<<endl;
        }else {//情况2、3
            yy=xx;while(dep[fr[father[yy]]]>=dep[y]) yy=fr[father[yy]];//跳到最浅的深度大于等于 y 的关键点
            las=ans[id[xx]][id[yy]];z=yy;int lca=LCA(yy,y);
            up(x,xx);up(father[yy],father[lca]);up(y,lca);//从father开始是因为关键点的预处理是包含关键点的
            cout<<las<<endl;
            del(x,xx);del(father[yy],father[lca]);del(y,lca);
        }
    }
    return 0;
}

雪辉

强制在线求:树上多条链的并的颜色数和 mex,静态。

好像没有比 bitset 更简洁的做法了,具体来讲就是求出两两关键点间的 bitset,然后暴力取并,剩下的暴力跳,代码不给了。

李超线段树

理论

被带着复习了一遍,证明都是自己胡的,有错告诉我一下。

基于一类贪心的线段树,其基本思想是维护每一个区间的优势线段(中点处取值最大的线段),并标记永久化。

首先不考虑加入线段,只考虑加入直线:

证明时间复杂度是 O(\log n) 的:因为最开始的交换操作保证了在中点处加入线段的取值更小,所以说明加入线段最多只有一段比节点线段更大,所以每次最多向一边递归。

加线段同理,找到线段树上对应的 O(\log n) 个区间,再做一遍如上操作,O(n\log^2n)。

以下只讨论加直线,加线段就需要多乘一个 O ( \log n)。

模板

#include<bits/stdc++.h>

using namespace std;

#define ls now<<1
#define rs now<<1|1
#define int long long

const int V=5e4;
const int N=4e5+10;
const double eps=1e-6;

struct Line{
    double k,b;
    Line(double k=0.0,double b=0.0):k(k),b(b){;}
    inline double get(const int &x){return k*x+b;}
}a[N];

inline int cmp(const double &x,const double &y){
    if(x-y>eps) return 1;
    if(y-x>eps) return -1;
    return 0;
}

struct node{
    double ans;int id;
    node(double ans=0.0,int id=0):ans(ans),id(id){;}
};

inline node Max(const node &x,const node &y){
    int tmp=cmp(x.ans,y.ans);
    if(tmp) return tmp==1?x:y;
    return x.id<y.id?x:y;
}

int id[N<<2],tot,n,m,las;

inline void insert(double x1,double y1,double x2,double y2){
    ++tot;
    if(x1==x2) a[tot]=Line(0,max(y1,y2));
    else {
        a[tot].k=(y1-y2)/(x1-x2);
        a[tot].b=y1-a[tot].k*x1;
    }
}

void update(int now,int l,int r,int x){
    int mid=(l+r)>>1;if(!x) return ;
    int fg=cmp(a[id[now]].get(mid),a[x].get(mid));
    if((fg==-1)||((!fg)&&(id[now]>x))) swap(id[now],x);
    int fgl=cmp(a[id[now]].get(l),a[x].get(l));
    int fgr=cmp(a[id[now]].get(r),a[x].get(r));
    if((fgl==-1)||((!fgl)&&(id[now]>x))) update(ls,l,mid,x);
    if((fgr==-1)||((!fgr)&&(id[now]>x))) update(rs,mid+1,r,x);
}

void change(int now,int l,int r,int x,int y,int d){
    if(x<=l&&r<=y){update(now,l,r,d);return ;}
    int mid=(l+r)>>1;
    if(mid>=x) change(ls,l,mid,x,y,d);
    if(mid<y) change(rs,mid+1,r,x,y,d);
}

node ask(int now,int l,int r,int to){
    if(l==r) return node(a[id[now]].get(to),id[now]);
    node res=node(a[id[now]].get(to),id[now]),res2;int mid=(l+r)>>1;
    if(mid>=to) res2=ask(ls,l,mid,to);
    else res2=ask(rs,mid+1,r,to);
    return Max(res,res2);
}

signed main(){
    a[0]=Line(0,-1e15);
    cin>>n;
    for(int i=1,opt,x1,x2,y1,y2;i<=n;++i){
        cin>>opt;
        if(opt==1){
            cin>>x1>>y1>>x2>>y2;
            x1=(x1+las-1)%39989+1;
            x2=(x2+las-1)%39989+1;
            y1=(y1+las-1)%1000000000+1;
            y2=(y2+las-1)%1000000000+1;
            if(x1>x2){swap(x1,x2);swap(y1,y2);}
            insert(x1,y1,x2,y2);
            change(1,1,V,x1,x2,tot);
        }else {
            cin>>x1;x1=(x1+las-1)%39989+1;
            cout<<(las=ask(1,1,V,x1).id)<<endl;
        }
    }
    return 0;
}

本质

实际上是在维护凸壳的边集,\max 是上凸壳,\min 是下凸壳。

李超线段树的合并相当于合并两个凸壳,属于凸壳的另一种维护方式。

所以可以用来斜率优化。

动态开点李超树

即每次加直线时才动态开节点,空间复杂度 O(n)。

证明:因为标记永久化且每次最多只向一边遍历,所以每次加入直线时只会开最多 1 个点,所以线性空间。

实现简单,故略。

李超线段树合并

类似普通线段树合并,每次合并到一个点时将当前点的线段插入到另一棵树的对应位置,时间复杂度 O(n\log n),若带上废节点回收,则线性空间(下面代码没有回收)。

int merge(int x,int y,int l,int r){
    if(!x||!y) return x|y;
    update(x,l,r,id[y]);
    int mid=(l+r)>>1;
    ls[x]=merge(ls[x],ls[y],l,mid);
    rs[x]=merge(rs[x],rs[y],mid+1,r);
    return x;
}

证明:

可持久化李超线段树

与普通的可持久化没有任何区别。

唉……

可撤销李超线段树

直接拿一个栈存一下操作序列。

例题:CF932F

dp 式子很好胡:dp_i=\min\limits_{j\in Subtree_i}dp_j+a_i\times b_j。

经典的斜率优化形式,每次要拿出一个子树的李超线段树,还要动态加线段,考虑李超线段树合并,于是就成了板题。

由于李超线段树本质是维护凸壳,所以可以选择平衡树动态维护凸壳之类的(CF70D)。

#include<bits/stdc++.h>

using namespace std;

#define range -V,V
#define int long long

const int V=1e5;
const int N=2e5+10;

struct Line{
    int k,b;
    Line(int k=0.0,int b=0.0):k(k),b(b){;}
    inline int get(const int &x){return k*x+b;}
}a[N];

inline int cmp(const int &x,const int &y){
    if(x<y) return 1;
    if(y<x) return -1;
    return 0;
}

int id[N*20],tot,n,m,las,rt[N],ls[N*20],rs[N*20],C,dp[N],A[N],B[N];
vector < int > v[N];

inline void insert(int k,int b){a[++tot]=Line(k,b);}
void update(int &now,int l,int r,int x){
    if(!x) return ;
    int mid=(l+r)>>1;if(!now) now=++C;
    int fg=cmp(a[id[now]].get(mid),a[x].get(mid));
    if(fg==-1) swap(id[now],x);
    int fgl=cmp(a[id[now]].get(l),a[x].get(l));
    int fgr=cmp(a[id[now]].get(r),a[x].get(r));
    if(fgl==-1) update(ls[now],l,mid,x);
    if(fgr==-1) update(rs[now],mid+1,r,x);
}

int ask(int &now,int l,int r,int to){
    if(!now) return 1e18;
    if(l==r) return a[id[now]].get(to);
    int res=a[id[now]].get(to),res2,mid=(l+r)>>1;
    if(mid>=to) res2=ask(ls[now],l,mid,to);
    else res2=ask(rs[now],mid+1,r,to);
    return min(res,res2);
}

int merge(int x,int y,int l,int r){
    if(!x||!y) return x|y;
    update(x,l,r,id[y]);
    int mid=(l+r)>>1;
    ls[x]=merge(ls[x],ls[y],l,mid);
    rs[x]=merge(rs[x],rs[y],mid+1,r);
    return x;
}

void dfs(int x,int y){
    int fg=0;
    for(int i:v[x]){
        if(i==y) continue;
        dfs(i,x);fg=1;
        rt[x]=merge(rt[x],rt[i],range);
    }
    if(fg) dp[x]=ask(rt[x],range,A[x]);
    else dp[x]=0;
    insert(B[x],dp[x]);update(rt[x],range,tot);
}

signed main(){
    cin>>n;a[0]=Line(0,1e18);
    for(int i=1;i<=n;++i) cin>>A[i];
    for(int i=1;i<=n;++i) cin>>B[i];
    for(int i=1,x,y;i<n;++i){
        cin>>x>>y;
        v[x].emplace_back(y);
        v[y].emplace_back(x);
    }
    dfs(1,0);
    for(int i=1;i<=n;++i) cout<<dp[i]<<' ';
    return 0;
}

李超+吉司机