就是将区间上的点移位,用2次rev的话代码量降下来了(orzLSJ
build忘记return。。。然而本地测试居然没事?卡了好久挺不值啊
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(int i=l;i<=r;i++) 3 #define dec(i,l,r) for(int i=l;i>=r;i--) 4 #define link(x) for(edge *j=h[x];j;j=j->next) 5 #define mem(a) memset(a,0,sizeof(a)) 6 #define inf 1e9 7 #define ll long long 8 #define succ(x) (1<<x) 9 #define NM 100000+5 10 using namespace std; 11 int read(){ 12 int x=0,f=1;char ch=getchar(); 13 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 14 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 15 return x*f; 16 } 17 int a[NM],b[NM],c[NM][2],rev[NM],tmp[NM],w[NM],f[NM]; 18 int n,m,_x,_y,root; 19 char st[100]; 20 void pushdown(int r){ 21 if(!r)return; 22 if(rev[r]){ 23 swap(c[r][0],c[r][1]); 24 rev[r]=0;rev[c[r][0]]^=1;rev[c[r][1]]^=1; 25 } 26 } 27 void up(int r){ 28 a[0]=c[0][0]=c[1][0]=b[0]=0; 29 int x=c[r][0],y=c[r][1]; 30 b[r]=b[x]+b[y]+1; 31 } 32 void newnode(int r,int fa,int k){ 33 a[r]=k;f[r]=fa;b[r]=1; 34 } 35 int build(int fa,int x,int y){ 36 int r=x+y>>1; 37 if(x>y)return 0; 38 c[r][0]=build(r,x,r-1); 39 newnode(r,fa,tmp[r]); 40 c[r][1]=build(r,r+1,y); 41 up(r); 42 return r; 43 } 44 int _d(int x){ 45 return c[f[x]][1]==x; 46 } 47 void rot(int x){ 48 int y=f[x],kind=!_d(x); 49 pushdown(y);pushdown(x); 50 c[y][!kind]=c[x][kind];f[c[x][kind]]=y; 51 c[f[y]][_d(y)]=x;f[x]=f[y]; 52 c[x][kind]=y;f[y]=x; 53 up(y);up(x); 54 } 55 void splay(int x,int goal=0){ 56 for(int y=f[x];y!=goal;y=f[x]){ 57 if(f[y]!=goal) 58 _d(x)==_d(y)?rot(y):rot(x); 59 rot(x); 60 } 61 if(goal==0)root=x; 62 } 63 int find(int x){ 64 for(int r=root;;pushdown(r)) 65 if(b[c[r][0]]>=x)r=c[r][0]; 66 else{ 67 x-=b[c[r][0]]+1; 68 if(!x)return r; 69 r=c[r][1]; 70 } 71 } 72 int pick(int x,int y){ 73 x--;y++;x=find(x);y=find(y); 74 splay(x);splay(y,x); 75 pushdown(y);pushdown(x); 76 return c[y][0]; 77 } 78 void out(int r){ 79 if(!r)return; 80 pushdown(r); 81 out(c[r][0]); 82 printf("%d ",a[r]); 83 out(c[r][1]); 84 } 85 int main(){ 86 n=read();m=read(); 87 inc(i,1,n)tmp[i+1]=read(); 88 build(0,1,n+2);root=n+3>>1; 89 inc(i,2,n+1)w[tmp[i]]=i; 90 // out(root);printf("\n"); 91 while(m--){ 92 scanf("%s",st);_x=read(); 93 if(st[0]=='Q')printf("%d\n",a[find(_x+1)]); 94 else{ 95 _x=w[_x]; 96 splay(_x);pushdown(_x); 97 _x=b[c[_x][0]]+1; 98 if(st[0]=='A')printf("%d\n",_x-2); 99 else if(st[0]=='T'){ 100 int r=pick(2,_x);rev[r]^=1; 101 r=pick(3,_x);rev[r]^=1;pushdown(r); 102 }else if(st[0]=='B'){ 103 int r=pick(_x,n+1);rev[r]^=1; 104 // out(root);printf("\n"); 105 r=pick(_x,n);rev[r]^=1;pushdown(r); 106 }else{ 107 _y=_x+read();if(_x>_y)swap(_x,_y); 108 int r=pick(_x,_y);rev[r]^=1;pushdown(r); 109 } 110 } 111 // out(root);printf("\n"); 112 } 113 return 0; 114 }