读入优化:
char *p1,*p2,buf[100000]; #define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++) int rd() { int x=0; char c; while(c<48) c=nc(); while(c>47) x=(((x<<2)+x)<<1)+(c^48),c=nc(); return x; }
splay (普通平衡树)
#include <cstdio> #include <algorithm> #include <cstring> #include <ctime> #define ll long long #define N 200007 #define inf 2000000000 #define setIO(s) freopen(s".in","r",stdin) using namespace std; int val[N],n,m,root; struct Splay_Tree { #define lson s[x].ch[0] #define rson s[x].ch[1] struct data { int v,si,f,ch[2]; }s[N*10]; int tot; inline int newnode() { return ++tot; } inline int get(int x) { return s[s[x].f].ch[1]==x; } inline void pushup(int x) { s[x].si=s[lson].si+s[rson].si+1; } void rotate(int x) { int old=s[x].f,fold=s[old].f,which=get(x); s[old].ch[which]=s[x].ch[which^1]; if(s[old].ch[which]) s[s[old].ch[which]].f=old; s[x].ch[which^1]=old,s[old].f=x,s[x].f=fold; if(fold) s[fold].ch[s[fold].ch[1]==old]=x; pushup(old),pushup(x); } void splay(int x,int &tar) { for(int u=s[tar].f,fa;(fa=s[x].f)!=u;rotate(x)) if(s[fa].f!=u) rotate(get(fa)==get(x)?fa:x); tar=x; } void build(int l,int r,int &x) { x=newnode(); int mid=(l+r)>>1; s[x].v=val[mid]; if(mid>l) build(l,mid-1,lson),s[lson].f=x; if(r>mid) build(mid+1,r,rson),s[rson].f=x; pushup(x); } void ins(int &x,int fa,int v) { if(!x) s[x=newnode()].f=fa,s[x].v=v; else ins(s[x].ch[v>s[x].v],x,v); pushup(x); } int find(int x,int v) { if(s[x].v==v) return x; else return find(s[x].ch[v>s[x].v],v); } int getpre(int x,int v) { if(!x) return 0; if(s[x].v<v) { int tmp=getpre(rson,v); return tmp?tmp:x; }else return getpre(lson,v); } int getaft(int x,int v) { if(!x) return 0; if(s[x].v>v) { int tmp=getaft(lson,v); return tmp?tmp:x; }else return getaft(rson,v); } void del(int v) { int x=find(root,v),l,r; for(splay(x,root),l=lson,r=rson;s[l].ch[1];l=s[l].ch[1]); splay(l,s[x].ch[0]); s[r].f=l,s[l].f=0,s[l].ch[1]=r,pushup(l),root=l; } int getrank(int v) { int x=getpre(root,v); splay(x,root); return s[lson].si+1; } int getnum(int x,int kth) { if(kth==s[lson].si+1) return x; else if(kth<=s[lson].si) return getnum(lson,kth); else return getnum(rson,kth-s[lson].si-1); } }bst; char *p1,*p2,buf[100000]; #define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++) int rd() { int x=0; char c; while(c<48) c=nc(); while(c>47) x=(((x<<2)+x)<<1)+(c^48),c=nc(); return x; } int main() { // setIO("input"); n=rd(),m=rd(); for(int i=1;i<=n;++i) val[i]=rd(); sort(val+1,val+1+n),val[0]=-inf,val[1+n]=inf; bst.build(0,n+1,root); int lastans=0,op,x,y,ans=0; for(int i=1;i<=m;++i) { op=rd(),x=rd()^lastans,y=0; if(op==1) bst.ins(root,0,x),y=bst.tot; if(op==2) bst.del(x); if(op==3) lastans=bst.getrank(x),ans^=lastans; if(op==4) y=bst.getnum(root,x+1); if(op==5) y=bst.getpre(root,x); if(op==6) y=bst.getaft(root,x); if(op>=4) lastans=bst.s[y].v,ans^=lastans; if(y&&(i%10==0)) bst.splay(y,root); } printf("%d\n",ans); return 0; }
splay(文艺平衡树)
#include <cstdio> #include <algorithm> #include <ctime> #define ll long long #define N 200007 #define setIO(s) freopen(s".in","r",stdin) using namespace std; int val[N],n,m,root; struct BST { #define lson s[x].ch[0] #define rson s[x].ch[1] struct data { int v,si,f,ch[2],tag; }s[N]; int tot; inline int newnode() { return ++tot; } inline int get(int x) { return s[s[x].f].ch[1]==x; } inline void mark(int x) { swap(lson,rson),s[x].tag^=1; } inline void pushup(int x) { s[x].si=s[lson].si+s[rson].si+1; } inline void pushdown(int x) { if(s[x].tag) { if(lson) mark(lson); if(rson) mark(rson); s[x].tag=0; } } void rotate(int x) { int old=s[x].f,fold=s[old].f,which=get(x); s[old].ch[which]=s[x].ch[which^1]; if(s[old].ch[which]) s[s[old].ch[which]].f=old; s[x].ch[which^1]=old,s[old].f=x,s[x].f=fold; if(fold) s[fold].ch[s[fold].ch[1]==old]=x; pushup(old),pushup(x); } void splay(int x,int &tar) { int u=s[tar].f; for(int fa;(fa=s[x].f)!=u;rotate(x)) if(s[fa].f!=u) rotate(get(fa)==get(x)?fa:x); tar=x; } void build(int l,int r,int &x) { x=newnode(); int mid=(l+r)>>1; s[x].v=val[mid]; if(mid>l) build(l,mid-1,lson),s[lson].f=x; if(r>mid) build(mid+1,r,rson),s[rson].f=x; pushup(x); } int getnode(int x,int kth) { pushdown(x); if(s[lson].si+1==kth) return x; else if(kth<=s[lson].si) return getnode(lson,kth); else return getnode(rson,kth-s[lson].si-1); } void OP(int l,int r) { int x=getnode(root,l); int y=getnode(root,r+2); splay(x,root),splay(y,s[x].ch[1]); mark(s[s[x].ch[1]].ch[0]); } void dfs(int x) { pushdown(x); if(lson) dfs(lson); if(s[x].v) printf("%d ",s[x].v); if(rson) dfs(rson); } }sp; char *p1,*p2,buf[100000]; #define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++) int rd() { int x=0; char c=nc(); while(c<48) c=nc(); while(c>47) x=(((x<<2)+x)<<1)+(c^48),c=nc(); return x; } int main() { // setIO("input"); n=rd(),m=rd(); for(int i=1;i<=n;++i) val[i]=i; sp.build(0,n+1,root); int l,r; for(int i=1;i<=m;++i) l=rd(),r=rd(),sp.OP(l,r); sp.dfs(root); return 0; }