题目大意:利用 splay 维护序列,支持区间反转操作。
题解:这里用 splay 维护的是下标的偏序关系,最后查询时也是按照下标从小到大进行查询。注:初始化引入两个极值点的作用是避免考虑 l-1,r+1 越界带来的不必要的麻烦。
代码如下
#include <bits/stdc++.h> using namespace std; const int maxn=1e5+10; const int inf=0x3f3f3f3f; int n,m,a[maxn]; struct node{ #define ls(x) t[x].ch[0] #define rs(x) t[x].ch[1] int ch[2],fa,val,size,rev; }t[maxn]; int tot,root; inline bool get(int o){return o==rs(t[o].fa);} inline void pushup(int o){t[o].size=t[ls(o)].size+t[rs(o)].size+1;} inline void pushdown(int o){ if(t[o].rev){ t[o].rev=0,t[ls(o)].rev^=1,t[rs(o)].rev^=1; swap(ls(ls(o)),rs(ls(o))),swap(ls(rs(o)),rs(rs(o))); } } inline void rotate(int o){ int fa=t[o].fa,gfa=t[fa].fa,d1=get(o),d2=get(fa); t[fa].ch[d1]=t[o].ch[d1^1],t[t[o].ch[d1^1]].fa=fa; t[fa].fa=o,t[o].ch[d1^1]=fa; t[o].fa=gfa,t[gfa].ch[d2]=o; pushup(fa),pushup(o); } inline void splay(int o,int goal){ while(t[o].fa!=goal){ int fa=t[o].fa,gfa=t[fa].fa; if(gfa!=goal)get(o)==get(fa)?rotate(fa):rotate(o); rotate(o); } if(!goal)root=o; } int build(int fa,int l,int r){ if(l>r)return 0; int o=++tot; int mid=l+r>>1; t[o].val=a[mid],t[o].size=1,t[o].fa=fa; ls(o)=build(o,l,mid-1),rs(o)=build(o,mid+1,r); return pushup(o),o; } int find(int o,int k){ pushdown(o); if(k<=t[ls(o)].size)return find(ls(o),k); else if(k>t[ls(o)].size+1)return find(rs(o),k-t[ls(o)].size-1); else return o; } int split(int l,int r){ int x=find(root,l-1),y=find(root,r+1); splay(x,0),splay(y,x); return ls(y); } void reverse(int l,int r){ int o=split(l,r); t[o].rev^=1,swap(ls(o),rs(o)); } void print(int o){ pushdown(o); if(ls(o))print(ls(o)); if(t[o].val!=inf)printf("%d ",t[o].val); if(rs(o))print(rs(o)); } int main(){ scanf("%d%d",&n,&m); a[1]=inf,a[n+2]=inf; for(int i=2;i<=n+1;i++)a[i]=i-1; root=build(0,1,n+2); while(m--){ int l,r; scanf("%d%d",&l,&r); reverse(l+1,r+1); } print(root); return 0; }
来源:https://www.cnblogs.com/wzj-xhjbk/p/10593109.html