splay练习题:
oj1160 维护数列 sequence
oj1849 排序机械臂(sort)
oj1160
大数据结构,写了半天,心态崩了.....
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> #define il inline #define _(d) while(d(isdigit(ch=getchar()))) using namespace std; const int N=1e6,inf=0x3f3f3f3f; char s[10];queue<int> q; int n,m,rt,son[N][2],mx[N],sum[N],lx[N],rx[N],sz[N],fa[N],id[N],v[N],rev[N],laz[N],tot,p,a[N]; il int read(){int x,f=1;char ch;_(!)ch=='-'?f=-1:f;x=ch^48;_()x=(x<<1)+(x<<3)+(ch^48);return f*x;} il void C(int k,int c){ if(k==0)return;sum[k]=c*sz[k];lx[k]=rx[k]=max(0,sum[k]); mx[k]=max(sum[k],c);laz[k]=c;v[k]=c;rev[k]=0; } il void R(int x){rev[x]^=1;swap(son[x][0],son[x][1]);swap(lx[x],rx[x]);} il void update(int x){ int l=son[x][0],r=son[x][1]; sum[x]=sum[l]+sum[r]+v[x]; mx[x]=max(mx[l],mx[r]);mx[x]=max(mx[x],rx[l]+lx[r]+v[x]); rx[x]=max(rx[r],rx[l]+v[x]+sum[r]); lx[x]=max(lx[l],lx[r]+v[x]+sum[l]); sz[x]=sz[l]+sz[r]+1; } il void pushdown(int x){ if(laz[x]!=inf){C(son[x][0],laz[x]);C(son[x][1],laz[x]);laz[x]=inf;rev[x]=0;} if(rev[x]){R(son[x][0]);R(son[x][1]);rev[x]=0;} } il void build(int x,int l,int r){ int mid=(l+r)>>1;int now=id[mid],pre=id[x];laz[now]=inf;rev[now]=0; if(l==r){ sum[now]=mx[now]=a[l]; sz[now]=1;lx[now]=rx[now]=max(a[l],0); } if(l<mid)build(mid,l,mid-1);if(mid<r)build(mid,mid+1,r); fa[now]=pre;son[pre][mid>=x]=now;v[now]=a[mid];update(now); } il int kth(int x,int rk){ pushdown(x); if(sz[son[x][0]]+1==rk)return x; if(rk<=sz[son[x][0]])return kth(son[x][0],rk); return kth(son[x][1],rk-sz[son[x][0]]-1); } il void rotate(int x,int &k){ int y=fa[x],z=fa[y],tp;tp=(x==son[y][1]); if(k==y)k=x;else son[z][son[z][1]==y]=x; son[y][tp]=son[x][tp^1];fa[son[y][tp]]=y; son[x][tp^1]=y;fa[y]=x;fa[x]=z;update(y);update(x); } il void splay(int x,int &k){ while(x!=k){ int y=fa[x],z=fa[y]; if(y!=k){ if((son[y][0]==x)^(son[z][0]==y))rotate(x,k); else rotate(y,k); } rotate(x,k); } } il void rey(int x){ if(son[x][0])rey(son[x][0]); if(son[x][1])rey(son[x][1]); q.push(x);son[x][0]=son[x][1]=0;fa[x]=0;laz[x]=inf; } int main() { n=read();m=read();for(int i=1;i<=n;i++)a[i+1]=read(); rt=(n+3)>>1;for(int i=1;i<=n+2;i++)id[i]=i;mx[0]=a[1]=a[n+2]=-inf; build(0,1,n+2);for(int i=n+3;i<=500000;i++)q.push(i); while(m--){ scanf(" %s",&s); if(s[0]=='I'){ p=read();tot=read(); for(int i=1;i<=tot;i++)a[i]=read(),id[i]=q.front(),q.pop(); build(0,1,tot);int z=id[(1+tot)>>1]; int x=kth(rt,p+1),y=kth(rt,p+2); splay(x,rt);splay(y,son[x][1]); fa[z]=y;son[y][0]=z;update(y);update(x); } if(s[0]=='D'){ p=read();tot=read(); int x=kth(rt,p),y=kth(rt,p+1+tot); splay(x,rt);splay(y,son[x][1]); rey(son[y][0]);son[y][0]=0;update(y);update(x); } if(s[2]=='K'){ p=read();tot=read();int c=read(); int x=kth(rt,p),y=kth(rt,p+tot+1); splay(x,rt);splay(y,son[x][1]); C(son[y][0],c);update(y);update(x); } if(s[0]=='R'){ p=read();tot=read(); int x=kth(rt,p),y=kth(rt,p+tot+1); splay(x,rt);splay(y,son[x][1]); R(son[y][0]);update(y);update(x); } if(s[0]=='G'){ p=read();tot=read(); int x=kth(rt,p),y=kth(rt,p+tot+1); splay(x,rt);splay(y,son[x][1]); printf("%d ",sum[son[y][0]]);update(y);update(x); } if(s[2]=='X')printf("%d ",mx[rt]); } return 0; }
再见!再也不想看到这道题!
oj1849
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> #define il inline #define _(d) while(d(isdigit(ch=getchar()))) using namespace std; const int N=1e5+8,inf=2e9+1; struct node{int v,id;}a[N]; int n,rt,son[N][2],num[N],fa[N],sz[N],rev[N]; il int read(){int x;char ch;_(!);x=ch^48;_()x=(x<<1)+(x<<3)+(ch^48);return x;} bool cmp(node t1,node t2){return (t1.v<t2.v||(t1.v==t2.v&&t1.id<t2.id));} il void update(int x){sz[x]=sz[son[x][0]]+sz[son[x][1]]+1;} il void pushdown(int x){ if(!rev[x])return;swap(son[x][0],son[x][1]); rev[son[x][1]]^=1;rev[son[x][0]]^=1;rev[x]=0; } il void build(int x,int l,int r){ if(l>r)return;int mid=(l+r)>>1; fa[mid]=x;sz[mid]=1; if(mid<x)son[x][0]=mid;else son[x][1]=mid; if(l==r)return; build(mid,l,mid-1);build(mid,mid+1,r); update(mid); } il void rotate(int x,int &k){ int y=fa[x],z=fa[y],tp;tp=(x==son[y][1]); if(y==k)k=x;else {son[z][son[z][1]==y]=x;} son[y][tp]=son[x][tp^1];fa[son[y][tp]]=y; son[x][tp^1]=y;fa[y]=x;fa[x]=z; sz[x]=sz[y];update(y); } il void splay(int x,int &k){ while(x!=k){ pushdown(fa[fa[x]]);pushdown(fa[x]);pushdown(x); int y=fa[x],z=fa[y]; if(y!=k){ if((son[y][0]==x)^(son[z][0]==y)==0)rotate(y,k); else rotate(x,k); } rotate(x,k); } } il int kth(int x,int k){ pushdown(x); if(sz[son[x][0]]+1==k)return x; if(sz[son[x][0]]>=k)return kth(son[x][0],k); return kth(son[x][1],k-sz[son[x][0]]-1); } int main() { n=read();for(int i=2;i<=n+1;i++)a[i].v=read(),a[i].id=i; a[1]=(node){-inf,1};a[n+2]=(node){inf,n+2}; sort(a+2,a+2+n,cmp);rt=(n+3)>>1;build(rt,1,n+2); for(int i=2;i<=n;i++){ splay(a[i].id,rt); int ans=sz[son[rt][0]]+1;printf("%d ",ans-1); int x=kth(rt,i-1),y=kth(rt,ans+1); splay(x,rt);splay(y,son[x][1]); rev[son[y][0]]^=1; } printf("%d ",n); return 0; }
才刚知道splay里也要pushdown,而且顺序不能乱