文艺平衡树

· · 算法·理论

给序列 a_{1\sim n},初始有 a_i=i。进行 m 次区间翻转操作,输出最终序列。

Splay 做法

把序列建成一棵树,满足中序遍历为原序列。考虑用 Splay 来维护这棵树。

每次区间翻转,首先要把使某棵子树的中序遍历恰为 [l,r] 。

把 a_{l-1} Splay 到根节点,那么它的右子树是 [l,n]。

在这个右子树中把 a_{r+1} Splay 到根,那么它的左子树是 [l,r]。

这是容易实现的:在 kth 时返回点的下标即可。

由于 l-1 和 r+1 可能不在原序列中,所以建树时常额外加入 a_0,a_{n+1} 两个点作为哨兵。

然后我们从这段区间的树根开始,对于每个点,交换两个儿子。

考虑这样会发生什么?设当前在 f 点,左右子树的中序遍历分别为 \texttt x 和 \texttt y,那么原中序遍历为 \texttt f=\texttt x+f+\texttt y,f 的子树全部交换后,中序遍历为 \texttt f_{rev}=\texttt y_{rev}+f+\texttt x_{rev},施归纳法容易证明,\texttt f_{rev} 恰为 \texttt f 的翻转。

那么可以在 $O(m\log n)$ 的时间内解决问题。 代码: ```cpp #include<bits/stdc++.h> using namespace std; #define _r return*this #define _o &operator namespace IO { const int _S=1<<21; char b[_S],*p1=b,*p2=b,pb[_S],*pp=pb; void fl(){fwrite(pb,1,pp-pb,stdout),pp=pb;} struct input { inline char gc(){if(p1==p2)p2=(p1=b)+fread(b,1,_S,stdin);return p1==p2?' ':*p1++;} inline input _o>>(char&num){do num=gc();while(num==' '||num=='\n'||num=='\r'||num=='\t');_r;} template<typename T>inline input _o>>(T&x) { char c=gc();T f=1; for(x=0;!isdigit(c);)(c=='-'?f=-1:1),c=gc(); while(isdigit(c))x=(x*10)+(c^48),c=gc(); x*=f;_r; } input(){} }in; struct output { inline void pt(char num){*pp++=num;if(pp-pb==_S)fl();} inline output _o<<(char num){pt(num);_r;} template<typename T>inline output _o<<(T x) { if(!x){pt(48);_r;} if(x<0)pt('-'),x=-x; int s[64],t=0; while(x)s[++t]=x%10,x/=10; while(t)pt(s[t--]+48); _r; } inline output _o<<(const char*siz){int num=0;while(siz[num])pt(siz[num++]);_r;} output(){} }out; struct fe{~fe(){fl();}}fls; int i;int read(){return in>>i,i;} long long l;long long readll(){return in>>l,l;} } using IO::in; using IO::out; using IO::read; using IO::readll; using ll=long long; #define N 100010 ll n,m; namespace Splay { #define ls son[0] #define rs son[1] struct node { ll f,son[2],siz; ll val; bool rev; }t[N]; ll root,tot; bool dir(ll x){return x==t[t[x].f].rs;} void push_up(ll x){t[x].siz=t[t[x].ls].siz+1+t[t[x].rs].siz;} void push_tag(ll x){swap(t[x].ls,t[x].rs),t[x].rev^=1;} void push_down(ll x){if(t[x].rev)push_tag(t[x].ls),push_tag(t[x].rs),t[x].rev=0;} void rotate(ll x) { ll y=t[x].f,z=t[y].f; bool d=dir(x); push_down(x),push_down(y); t[z].son[dir(y)]=x; t[x].f=z; t[y].son[d]=t[x].son[d^1]; t[t[x].son[d^1]].f=y; t[x].son[d^1]=y; t[y].f=x; push_up(y); push_up(x); } void splay(ll x,ll to=0) { while(t[x].f!=to) { ll f=t[x].f,g=t[f].f; if(g!=to)dir(x)^dir(f)?rotate(x):rotate(f); rotate(x); } if(!to)root=x; } ll kth(ll k) { k++; ll x=root; while(1) { push_down(x); if(k<=t[t[x].ls].siz)x=t[x].ls; else if((k-=t[t[x].ls].siz+1)<=0)return splay(x),x; else x=t[x].rs; } } void reverse(ll l,ll r) { ll L=kth(l-1),R=kth(r+1); splay(L),splay(R,L),push_tag(t[t[L].rs].ls); } #define mid ((l+r)>>1) ll build(ll l,ll r,ll f) { if(l>r)return 0; ll x=++tot; return t[x].f=f,t[x].val=mid,t[x].ls=build(l,mid-1,x),t[x].rs=build(mid+1,r,x),push_up(x),x; } void dfs(ll x) { push_down(x); if(t[x].ls)dfs(t[x].ls); if(1<=t[x].val&&t[x].val<=n)out<<t[x].val<<" "; if(t[x].rs)dfs(t[x].rs); } }; ll l,r; int main() { in>>n>>m,Splay::root=Splay::build(0,n+1,0); while(m--)in>>l>>r,Splay::reverse(l,r); Splay::dfs(Splay::root); } ``` ## ODT 做法 考虑把 $l\sim r$ 连续排列的一段区间压缩为三个数 $[l,r,v]$,$v$ 的值是 $1$ 或 $-1$,表示这段区间是从小到大还是从大到小排列。 初始区间就是 $[1,n,1]$。 我们怎么做区间翻转?首先要把被 $[l,r]$ 包含的所有结点 $[l,r,v]$ 拿出来,这要求我们把跨过区间端点的结点分成两段区间,这是容易做到的: - $v=1\Rightarrow[l,r]=[l,p-1]+[p,r]$。 - $v=-1\Rightarrow[l,r]=[l+r-p+1,r]+[l,l+r-p]$。 可以发现这个东西就是一个显然的 ODT,用链表维护这些结点即可。 再考虑怎么做区间翻转:我们直接将 $[l,r]$ 中所有结点提出来,然后暴力翻转即可。 这样的复杂度是不能接受的,考虑改变 $[l,r,v]$ 的定义:有一段 $a_l\sim a_r$ 连续排列的区间,然后我们每过 $B$ 次操作,基于当前的 ODT 重构 $a$ 数组。 由于每次区间翻转会新增 $O(1)$ 个结点,而定期重构保证了 ODT 内的结点数为 $O(B)$,一次区间翻转也是 $O(B)$,一次重构是 $O(n)$,取 $B=\dfrac n{\sqrt m}$ 可得 $O(n\sqrt m)$。