跳到正文

算法·理论

依赖树

STUDENT00约 21 分钟
原文
文章信息与版本

文章信息

发布
2024年3月10日
收录
2026年2月22日
更新
2026年11月11日
篇幅
约 21 分钟

版本

  1. v2当前
  2. v1

引

引入例题:P3919 【模板】可持久化线段树 1(可持久化数组)

相信在座的各位都是用“可持久化线段树”解这题的吧!

“可持久化线段树”代码冗长、容易出错。考虑到本题支持离线,“依赖树”显然是一种更优的抉择。

义

Q:什么是依赖树?

A:依赖树中的边 (u,v)(u,v) 表示:版本 vv 是在版本 uu 的基础上修改而成的(如果是查询,可视为复制);依赖树中的节点 uu 存储着修改/查询的信息。

Q:如何修改/查询?

A:对依赖树进行一遍 DFS。节点 uu 在上级的基础上,根据本节点信息修改/查询。修改时,记得存下更改的位置、此位置更改前的值等。子树 DFS 完毕后,即根据存下的信息复原,以便上级对兄弟节点进行遍历。

Q:该算法的时间复杂度?

A:一遍 DFS,严格 O(n)O(n)。

Q:该算法相比于“可持久化线段树”有哪些优势/劣势?

A:优势,时复、空复、常数、码量均远优于“可持久化线段树”;劣势,不能处理强制在线的问题。

Q:为什么该算法“不能处理强制在线的问题”?

A:在线时,我们永远不知道修改/查询的历史版本编号,只好把所有历史版本都保留下来。

离线时,我们可以把所有操作有序地排列到一棵树上。这里的树边相当于“可持久化线段树”里连接两个版本的边,只是数量缩减到了原先的 1log2n\frac{1}{log_2^n}。

Q:本题“可持久化线段树”写法里的基础结构“线段树”可以改为“数组”吗?

A:不能。数组虽然能 O(1)O(1) 修改,但多次修改后会退化成链表,只能 O(n)O(n) 查询。

码

C++
#include<bits/stdc++.h>
using namespace std;
#define FIO
struct IO{
#ifdef FIO
const static int BUFSIZE=1<<20;char buf[BUFSIZE],obuf[BUFSIZE],*p1,*p2,*pp;inline char getchar(){return(p1==p2&&(p2=(p1=buf)+fread(buf,1,BUFSIZE,stdin),p1==p2)?EOF:*p1++);}inline void putchar(char x){((pp-obuf==BUFSIZE&&(fwrite(obuf,1,BUFSIZE,stdout),pp=obuf)),*pp=x,pp++);}inline void flush(){fwrite(obuf,1,pp-obuf,stdout);}IO(){fwrite("Warning: Macro 'FIO' has been defined, keyboard input will be ignored.\n",1,71,stderr);p1=buf,p2=buf,pp=obuf;}~IO(){fwrite(obuf,1,pp-obuf,stdout);}
#else
int(*getchar)()=&::getchar;int(*putchar)(int)=&::putchar;inline void flush(){};
#endif
string sep=" ";int k=2;template<typename Tp,typename enable_if<is_integral<Tp>::value>::type* =nullptr>inline int read(Tp&s){int f=1;char ch=getchar();s=0;while(!isdigit(ch)&&ch!=EOF)f=(ch=='-'?-1:1),ch=getchar();while(isdigit(ch))s=s*10+(ch^48),ch=getchar();s*=f;return ch!=EOF;}template<typename Tp,typename enable_if<is_floating_point<Tp>::value>::type* =nullptr>inline int read(Tp&s){int f=1;char ch=getchar();s=0;while(!isdigit(ch)&&ch!=EOF&&ch!='.')f=(ch=='-'?-1:1),ch=getchar();while(isdigit(ch))s=s*10+(ch^48),ch=getchar();if(ch==EOF)return false;if(ch=='.'){Tp eps=0.1;ch=getchar();while(isdigit(ch))s=s+(ch^48)*eps,ch=getchar(),eps/=10;}s*=f;return ch!=EOF;}inline int read(char&c){char ch=getchar();while(isspace(ch)&&ch!=EOF)ch=getchar();if(ch!=EOF)c=ch;return ch!=EOF;}inline int read(char*c){char ch=getchar();while(isspace(ch)&&ch!=EOF)ch=getchar();while(!isspace(ch)&&ch!=EOF)*(c++)=ch,ch=getchar();return ch!=EOF;}inline int read(string&s){s.clear();char ch=getchar();while(isspace(ch)&&ch!=EOF)ch=getchar();while(!isspace(ch)&&ch!=EOF)s+=ch,ch=getchar();return ch!=EOF;}inline int getline(char*c,const char&ed='\n'){char ch=getchar();while(ch!=ed&&ch!=EOF)*(c++)=ch,ch=getchar();return ch!=EOF;}inline int getline(string&s,const char&ed='\n'){s.clear();char ch=getchar();while(ch!=ed&&ch!=EOF)s+=ch,ch=getchar();return ch!=EOF;}template<typename Tp=int>inline Tp read(){Tp x;read(x);return x;}template<typename Tp,typename...Ts>int read(Tp&x,Ts&...val){return read(x)&&read(val...);}template<typename Tp,typename enable_if<is_integral<Tp>::value>::type* =nullptr>IO&write(Tp x){if(x<0)putchar('-'),x=-x;static char sta[20];int top=0;do sta[top++]=x%10+'0',x/=10;while(x);while(top)putchar(sta[--top]);return*this;}inline IO&write(const string&str){for(char ch:str)putchar(ch);return*this;}inline IO&write(const char*str){while(*str!='\0')putchar(*(str++));return*this;}inline IO&write(char*str){return write((const char*)str);}inline IO&write(const char&ch){return putchar(ch),*this;}template<typename Tp,typename enable_if<is_floating_point<Tp>::value>::type* =nullptr>inline IO&write(Tp x){const static long long pow10[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000,1000000000000,10000000000000,100000000000000,1000000000000000,10000000000000000,100000000000000000,100000000000000000,100000000000000000};const auto&n=pow10[k];if(x==0){putchar('0'),putchar('.');for(int i=1;i<=k;++i)putchar('0');return*this;}if(x<0)putchar('-'),x=-x;long long d=(long long)(x+1e-12),y=(long long)(x*n+0.5);write(d),putchar('.');static char sta[20];int top=0;for(;top<k;y/=10)sta[top++]=y%10^48;while(top)putchar(sta[--top]);return*this;}template<typename Tp,typename...Ts>inline IO&write(Tp x,Ts...val){write(x);write(sep);write(val...);return*this;}template<typename...Ts>inline IO&writeln(Ts...val){write(val...);putchar('\n');return*this;}inline IO&writeln(void){putchar('\n');return*this;}template<typename Tp>inline IO&writeWith(Tp x,const string&s){write(x),write(s);return*this;}inline IO&setsep(const string&s){return sep=s,*this;}inline IO&setprecision(const int&K){return k=K,*this;}}io;
const int N=1000005;
const int INF=1e9+7;
int n,m;
int a[N];
int head[N],ver[N<<1],nxt[N<<1],tot;
int pos[N],val[N];
int ans[N];
inline void add(int u,int v){
	ver[++tot]=v;
	nxt[tot]=head[u];
	head[u]=tot;
}
void calc(int u){
	int x=a[pos[u]];
	if(val[u]!=INF) a[pos[u]]=val[u];
	else ans[u]=x;
	for(int i=head[u];~i;i=nxt[i]) calc(ver[i]);
	a[pos[u]]=x;
}
int main(){
	memset(head,-1,sizeof(head));
	io.read(n,m);
	for(int i=1;i<=n;i++) io.read(a[i]);
	for(int i=1;i<=m;i++){
		int v,op,loc,value;io.read(v,op,loc);
		add(v,i);
		pos[i]=loc;
		if(op==1){
			io.read(value);
			val[i]=value;
		}else val[i]=INF;
	}
	calc(0);
	for(int i=1;i<=m;i++){
		if(val[i]==INF) io.writeln(ans[i]);
	}
	return 0;
}

本代码抢到了最优解第 55 名,强烈 zc!

扩

“可持久化数组”最经典的应用莫过于“可持久化并查集”了!

本题只能使用“按秩合并”,不能使用“路径压缩”。原因:

路径压缩时复 O(logn)O(logn) 是均摊出来的。出题人可以构造一组极端数据,使在版本 kk 中加入一条边 (u,v)(u,v) 后,所有 fafa(或者 O(n)O(n) 级别的也行)都发生了改变。然后不断 (2,k),(1,u,v)(2,k),(1,u,v),即可把时复卡至 O(n2)O(n^2)。

按秩合并时复 O(nlogn)O(nlogn),代码如下:

C++
#include<bits/stdc++.h>
using namespace std;
#define FIO
struct IO{
#ifdef FIO
const static int BUFSIZE=1<<20;char buf[BUFSIZE],obuf[BUFSIZE],*p1,*p2,*pp;inline char getchar(){return(p1==p2&&(p2=(p1=buf)+fread(buf,1,BUFSIZE,stdin),p1==p2)?EOF:*p1++);}inline void putchar(char x){((pp-obuf==BUFSIZE&&(fwrite(obuf,1,BUFSIZE,stdout),pp=obuf)),*pp=x,pp++);}inline void flush(){fwrite(obuf,1,pp-obuf,stdout);}IO(){fwrite("Warning: Macro 'FIO' has been defined, keyboard input will be ignored.\n",1,71,stderr);p1=buf,p2=buf,pp=obuf;}~IO(){fwrite(obuf,1,pp-obuf,stdout);}
#else
int(*getchar)()=&::getchar;int(*putchar)(int)=&::putchar;inline void flush(){};
#endif
string sep=" ";int k=2;template<typename Tp,typename enable_if<is_integral<Tp>::value>::type* =nullptr>inline int read(Tp&s){int f=1;char ch=getchar();s=0;while(!isdigit(ch)&&ch!=EOF)f=(ch=='-'?-1:1),ch=getchar();while(isdigit(ch))s=s*10+(ch^48),ch=getchar();s*=f;return ch!=EOF;}template<typename Tp,typename enable_if<is_floating_point<Tp>::value>::type* =nullptr>inline int read(Tp&s){int f=1;char ch=getchar();s=0;while(!isdigit(ch)&&ch!=EOF&&ch!='.')f=(ch=='-'?-1:1),ch=getchar();while(isdigit(ch))s=s*10+(ch^48),ch=getchar();if(ch==EOF)return false;if(ch=='.'){Tp eps=0.1;ch=getchar();while(isdigit(ch))s=s+(ch^48)*eps,ch=getchar(),eps/=10;}s*=f;return ch!=EOF;}inline int read(char&c){char ch=getchar();while(isspace(ch)&&ch!=EOF)ch=getchar();if(ch!=EOF)c=ch;return ch!=EOF;}inline int read(char*c){char ch=getchar();while(isspace(ch)&&ch!=EOF)ch=getchar();while(!isspace(ch)&&ch!=EOF)*(c++)=ch,ch=getchar();return ch!=EOF;}inline int read(string&s){s.clear();char ch=getchar();while(isspace(ch)&&ch!=EOF)ch=getchar();while(!isspace(ch)&&ch!=EOF)s+=ch,ch=getchar();return ch!=EOF;}inline int getline(char*c,const char&ed='\n'){char ch=getchar();while(ch!=ed&&ch!=EOF)*(c++)=ch,ch=getchar();return ch!=EOF;}inline int getline(string&s,const char&ed='\n'){s.clear();char ch=getchar();while(ch!=ed&&ch!=EOF)s+=ch,ch=getchar();return ch!=EOF;}template<typename Tp=int>inline Tp read(){Tp x;read(x);return x;}template<typename Tp,typename...Ts>int read(Tp&x,Ts&...val){return read(x)&&read(val...);}template<typename Tp,typename enable_if<is_integral<Tp>::value>::type* =nullptr>IO&write(Tp x){if(x<0)putchar('-'),x=-x;static char sta[20];int top=0;do sta[top++]=x%10+'0',x/=10;while(x);while(top)putchar(sta[--top]);return*this;}inline IO&write(const string&str){for(char ch:str)putchar(ch);return*this;}inline IO&write(const char*str){while(*str!='\0')putchar(*(str++));return*this;}inline IO&write(char*str){return write((const char*)str);}inline IO&write(const char&ch){return putchar(ch),*this;}template<typename Tp,typename enable_if<is_floating_point<Tp>::value>::type* =nullptr>inline IO&write(Tp x){const static long long pow10[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000,1000000000000,10000000000000,100000000000000,1000000000000000,10000000000000000,100000000000000000,100000000000000000,100000000000000000};const auto&n=pow10[k];if(x==0){putchar('0'),putchar('.');for(int i=1;i<=k;++i)putchar('0');return*this;}if(x<0)putchar('-'),x=-x;long long d=(long long)(x+1e-12),y=(long long)(x*n+0.5);write(d),putchar('.');static char sta[20];int top=0;for(;top<k;y/=10)sta[top++]=y%10^48;while(top)putchar(sta[--top]);return*this;}template<typename Tp,typename...Ts>inline IO&write(Tp x,Ts...val){write(x);write(sep);write(val...);return*this;}template<typename...Ts>inline IO&writeln(Ts...val){write(val...);putchar('\n');return*this;}inline IO&writeln(void){putchar('\n');return*this;}template<typename Tp>inline IO&writeWith(Tp x,const string&s){write(x),write(s);return*this;}inline IO&setsep(const string&s){return sep=s,*this;}inline IO&setprecision(const int&K){return k=K,*this;}}io;
const int N=100005;
const int M=200005;
int n,m;
int head[M],ver[M],nxt[M],tot;
int op[M],a[M],b[M];
int ans[M];
int fa[N],dep[N];
void calc(int);
void son(int);
inline void add(int u,int v){
	ver[++tot]=v;
	nxt[tot]=head[u];
	head[u]=tot;
}
void son(int u){
	for(int i=head[u];~i;i=nxt[i]) calc(ver[i]);
}
inline int find(int x){
	while(fa[x]!=x) x=fa[x];
	return x;
}
void calc(int u){
	if(op[u]==2){son(u);return;}
	int x=a[u],y=b[u];
	x=find(x),y=find(y);
	if(op[u]==3){ans[u]=(x==y);son(u);return;}
	if(dep[x]>dep[y]) swap(x,y);
	int p=dep[y];
	fa[x]=y,dep[y]=max(dep[y],dep[x]+1);
	son(u);
	fa[x]=x,dep[y]=p;
}
int main(){
	memset(head,-1,sizeof(head));
	io.read(n,m);
	for(int i=1;i<=n;i++) fa[i]=i,dep[i]=1;
	for(int i=1;i<=m;i++){
		io.read(op[i]);
		if(op[i]==1||op[i]==3){
			io.read(a[i],b[i]);
			add(i-1,i);
		}else{
			int kk;io.read(kk);
			add(kk,i);
		}
	}
	calc(0);
	for(int i=1;i<=m;i++){
		if(op[i]==3) io.writeln(ans[i]);
	}
	return 0;
}

本代码抢到了最优解第 44 名,强烈 zc!

原文
存档评论11 条抓取于