一、文藝平衡樹解決什么問題
您需要寫一種資料結構,來維護一個有序序列,
其中需要提供以下操作:翻轉一個區間,例如原有序列是5 4 3 2 1,翻轉區間是[2,4],結果為5 2 3 4 1
二、文藝平衡樹與普通平衡樹
a[5]={ 5 , 4 , 3 , 1 , 2 }那么存入文藝平衡樹之后,再中序遍歷的結果應該還是:{ 5 ,4 ,3,1,2},即下標從小到大,而不是里面的值從小到大!這是與普通平衡樹的最大的不同!文藝平衡樹經過rotate旋轉之后,它的中序遍歷是不變的(即,下標從小到大),但是讓這顆樹的一部磁區間倒置之后,這個中序遍歷下標就不是遞增的了,
三、文藝平衡樹的構造
1.建樹
就像給線段樹建樹一樣,但是在原陣列的基礎上加一個-INF,+INF,(比如原序列是1,2,3,4,你建樹的時候要給-INF,1,2,3,4,+INF建樹)
至于為什么這樣做,就是為了可以給區間[ 1,n ]倒置
主函式:
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
a[i+1]=i;
a[1]=-INF,a[n+2]=-INF;
root=build_tree(0,1,n+2);
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
turn(l,r);
}
write(root);
//system("pause");
return 0;
}
這個沒有輸入對應的n個數,而是用1,2,……n來代表,你也可以輸入
struct splay_tree
{
int size,ch[2],val,ff,tag;
}tr[N];
void pushup(int x)//更新節點資訊
{
tr[x].size=tr[tr[x].ch[0]].size+tr[tr[x].ch[1]].size+1;
}
void pushdown(int x) //相當于線段樹操作的懶惰標記
{
if(x&&tr[x].tag)
{//這個tag標記就是用來看這個子樹用不用交換(他的交換也就對應著區間的交換)
tr[tr[x].ch[0]].tag^=1;
tr[tr[x].ch[1]].tag^=1;
swap(tr[x].ch[0],tr[x].ch[1]);
tr[x].tag=0;
}
}
int build_tree(int fx,int l,int r)
{//用所給陣列data中資料建一顆樹(就是普通線段樹建樹)
if(l>r) return 0;
int mid=(l+r)>>1;
int now=++tot;
tr[now].ch[0]=tr[now].ch[1]=0;
tr[now].val=a[mid];
tr[now].ff=fx;
tr[now].tag=0;
tr[now].ch[0]=build_tree(now,l,mid-1);
tr[now].ch[1]=build_tree(now,mid+1,r);
pushup(now);
return now;
}

中序遍歷輸出
void write(int now)
{
pushdown(now);
if(tr[now].ch[0]) write(tr[now].ch[0]);
if(tr[now].val!=INF&&tr[now].val!=-INF) printf("%d ",tr[now].val);
if(tr[now].ch[1]) write(tr[now].ch[1]);
}

綠色的線就是輸出的程序,綠色的數字就是輸出順序
那么最重要的區間翻轉操作,改怎么找到翻轉區間呢(畢竟只有先找到了才能打上標記呀)
那么實際上我們可以發現,在反轉區間[l~r]的時候,我們可以考慮利用Splay的性質,將l-1翻轉至根節點,再將r+1翻轉至根節點的右兒子,類似這樣:

void splay(int x,int s)
{
while(tr[x].ff!=s)
{
int y=tr[x].ff,z=tr[y].ff;
if(z!=s)
(tr[z].ch[0]==y)^(tr[y].ch[0]==x)?rotate(x):rotate(y);
rotate(x);
}
if(s==0) root=x;
}
int kth(int x)
{
int u=root;
while(1)
{
pushdown(u);
int y=tr[u].ch[0];
if(x>tr[y].size)
{
x-=tr[y].size+1;
if(!x) return u;
u=tr[u].ch[1];
}
else u=y;
}
}
void turn(int l,int r) //將區間[l,r]翻轉
{
l=kth(l);
r=kth(r+2);
splay(l,0);
splay(r,l);
//pushdown(root);
int rson=tr[root].ch[1];
tr[tr[rson].ch[0]].tag^=1;
}
那么至于旋轉操作的話和普通平衡樹是一樣的,這里就不過多贅述了,只是在旋轉程序中不要忘了pushdown哦,
void rotate(int x)
{
int y=tr[x].ff;
int z=tr[y].ff;
int k=(tr[y].ch[1]==x);
pushdown(y);
pushdown(x);
tr[z].ch[(tr[z].ch[1]==y)]=x;
tr[x].ff=z;
tr[y].ch[k]=tr[x].ch[k^1];
tr[tr[x].ch[k^1]].ff=y;
tr[x].ch[k^1]=y;
tr[y].ff=x;
pushup(y);
pushup(x);
}
四、例題:
Luogu P3391 文藝平衡樹
完整代碼:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10,INF=1e9+10;
struct splay_tree
{
int size,ch[2],val,ff,tag;
}tr[N];
int a[N];
int root,tot;
void pushup(int x)
{
tr[x].size=tr[tr[x].ch[0]].size+tr[tr[x].ch[1]].size+1;
}
void pushdown(int x)
{
if(x&&tr[x].tag)
{
tr[tr[x].ch[0]].tag^=1;
tr[tr[x].ch[1]].tag^=1;
swap(tr[x].ch[0],tr[x].ch[1]);
tr[x].tag=0;
}
}
int build_tree(int fx,int l,int r)
{
if(l>r) return 0;
int mid=(l+r)>>1;
int now=++tot;
tr[now].ch[0]=tr[now].ch[1]=0;
tr[now].val=a[mid];
tr[now].ff=fx;
tr[now].tag=0;
tr[now].ch[0]=build_tree(now,l,mid-1);
tr[now].ch[1]=build_tree(now,mid+1,r);
pushup(now);
return now;
}
void rotate(int x)
{
int y=tr[x].ff;
int z=tr[y].ff;
int k=(tr[y].ch[1]==x);
pushdown(y);
pushdown(x);
tr[z].ch[(tr[z].ch[1]==y)]=x;
tr[x].ff=z;
tr[y].ch[k]=tr[x].ch[k^1];
tr[tr[x].ch[k^1]].ff=y;
tr[x].ch[k^1]=y;
tr[y].ff=x;
pushup(y);
pushup(x);
}
void splay(int x,int s)
{
while(tr[x].ff!=s)
{
int y=tr[x].ff,z=tr[y].ff;
if(z!=s)
(tr[z].ch[0]==y)^(tr[y].ch[0]==x)?rotate(x):rotate(y);
rotate(x);
}
if(s==0) root=x;
}
int kth(int x)
{
int u=root;
while(1)
{
pushdown(u);
int y=tr[u].ch[0];
if(x>tr[y].size)
{
x-=tr[y].size+1;
if(!x) return u;
u=tr[u].ch[1];
}
else u=y;
}
}
void turn(int l,int r) //將區間[l,r]翻轉
{
l=kth(l);
r=kth(r+2);
splay(l,0);
splay(r,l);
//pushdown(root);
int rson=tr[root].ch[1];
tr[tr[rson].ch[0]].tag^=1;
}
void write(int now)
{
pushdown(now);
if(tr[now].ch[0]) write(tr[now].ch[0]);
if(tr[now].val!=INF&&tr[now].val!=-INF) printf("%d ",tr[now].val);
if(tr[now].ch[1]) write(tr[now].ch[1]);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
a[i+1]=i;
a[1]=-INF,a[n+2]=-INF;
root=build_tree(0,1,n+2);
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
turn(l,r);
}
write(root);
//system("pause");
return 0;
}
五、更多操作
除了區間翻轉,還可以實作
1.區間加
2.區間洗掉
3.在第x個數后插入一個數p
4.刪去第x個數(其實就是變相的區間洗掉)
5.查詢區間最小值
6.查詢區間和
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10,INF=1e9+10;
struct splay_tree
{
int size,ch[2],val,ff,minn;
int tag,add;
}tr[N];
int a[N];
int root,tot;
void pushup(int x)
{
tr[x].size=tr[tr[x].ch[0]].size+tr[tr[x].ch[1]].size+1;
tr[x].minn=tr[x].val;
if(tr[x].minn>tr[tr[x].ch[0]].minn&&tr[x].ch[0]) tr[x].minn=tr[tr[x].ch[0]].minn;
if(tr[x].minn>tr[tr[x].ch[1]].minn&&tr[x].ch[1]) tr[x].minn=tr[tr[x].ch[1]].minn;
}
void pushdown(int x)
{
if(x&&tr[x].tag) //區間反轉標記
{
tr[tr[x].ch[0]].tag^=1;
tr[tr[x].ch[1]].tag^=1;
swap(tr[x].ch[0],tr[x].ch[1]);
tr[x].tag=0;
}
if(x&&tr[x].add) //區間加標記
{
tr[tr[x].ch[0]].add+=tr[x].add;
tr[tr[x].ch[1]].add+=tr[x].add;
tr[tr[x].ch[0]].val+=tr[x].add;
tr[tr[x].ch[1]].val+=tr[x].add;
tr[tr[x].ch[0]].minn+=tr[x].add;
tr[tr[x].ch[1]].minn+=tr[x].add;
tr[x].add=0;
}
}
int build_tree(int fx,int l,int r)
{
if(l>r) return 0;
int mid=(l+r)>>1;
int now=++tot;
tr[now].ch[0]=tr[now].ch[1]=0;
tr[now].val=a[mid];
tr[now].ff=fx;
tr[now].tag=tr[now].add=0;
tr[now].ch[0]=build_tree(now,l,mid-1);
tr[now].ch[1]=build_tree(now,mid+1,r);
pushup(now);
return now;
}
void rotate(int x)
{
int y=tr[x].ff;
int z=tr[y].ff;
int k=(tr[y].ch[1]==x);
pushdown(y);
pushdown(x);
tr[z].ch[(tr[z].ch[1]==y)]=x;
tr[x].ff=z;
tr[y].ch[k]=tr[x].ch[k^1];
tr[tr[x].ch[k^1]].ff=y;
tr[x].ch[k^1]=y;
tr[y].ff=x;
pushup(y);
pushup(x);
}
void splay(int x,int s)
{
while(tr[x].ff!=s)
{
int y=tr[x].ff,z=tr[y].ff;
if(z!=s)
(tr[z].ch[0]==y)^(tr[y].ch[0]==x)?rotate(x):rotate(y);
rotate(x);
}
if(s==0) root=x;
}
int kth(int x)
{
int u=root;
while(1)
{
pushdown(u);
int y=tr[u].ch[0];
if(x>tr[y].size)
{
x-=tr[y].size+1;
if(!x) return u;
u=tr[u].ch[1];
}
else u=y;
}
}
//下面為各種操作
void turn(int l,int r) //將區間[l,r]翻轉
{
l=kth(l);
r=kth(r+2);
splay(l,0);
splay(r,l);
pushdown(root);
int rson=tr[root].ch[1];
tr[tr[rson].ch[0]].tag^=1;
}
void Delete(int l,int r) //區間洗掉
{
l=kth(l);
r=kth(r+2);
splay(l,0);
splay(r,l);
pushdown(root);
int rson=tr[root].ch[1];
tr[rson].ch[0]=0;
}
void insert(int x,int y) //在第x個數之后插入數p
{
x=kth(x+1);
splay(x,0);
tr[++tot].val=y;
tr[tot].ff=x;
tr[tot].tag=tr[tot].add=0;
tr[tot].ch[1]=tr[x].ch[1];
tr[tr[x].ch[1]].ff=tot;
tr[x].ch[1]=tot;
pushup(tot),pushup(x);
}
void add(int l,int r,int x) //區間加
{
l=kth(l);
r=kth(r+2);
splay(l,0);
splay(r,l);
pushdown(root);
int rson=tr[root].ch[1];
tr[tr[rson].ch[0]].add+=x;
tr[tr[rson].ch[0]].val+=x;
tr[tr[rson].ch[0]].minn+=x;
}
void write(int now)
{
pushdown(now);
if(tr[now].ch[0]) write(tr[now].ch[0]);
if(tr[now].val!=INF&&tr[now].val!=-INF) printf("%d ",tr[now].val);
if(tr[now].ch[1]) write(tr[now].ch[1]);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i+1]);
a[1]=-INF,a[n+2]=-INF;
root=build_tree(0,1,n+2);
while(m--)
{
int op;
scanf("%d",&op);
if(op==1) //區間翻轉操作
{
int l,r;
scanf("%d%d",&l,&r);
turn(l,r);
}
else if(op==2) //區間洗掉操作
{
int l,r;
scanf("%d%d",&l,&r);
Delete(l,r);
}
else if(op==3) //在第x個數后插入數p
{
int x,y;
scanf("%d%d",&x,&y);
insert(x,y);
}
else if(op==4) //區間加
{
int l,r,x;
scanf("%d%d%d",&l,&r,&x);
add(l,r,x);
}
}
write(root);
//system("pause");
return 0;
}
有些我也沒在題目里交過,如果有錯,望告知,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/260637.html
標籤:其他
