8 2 CUT 3 5 4 FLIP 2 6 -1 -1
1 4 3 7 6 2 5 8
很顯然是Splay維護區間。然而這道題沒有自動過濾行末空格,感覺真是坑......(論OIer做ACM題的心態調整2333)
#include#include #include #include #include #include #define F(i,j,n) for(int i=j;i<=n;i++) #define D(i,j,n) for(int i=j;i>=n;i--) #define LL long long #define pa pair #define MAXN 300005 #define key c[c[rt][1]][0] using namespace std; int flag,n,m,rt,x,y,z,c[MAXN][2],s[MAXN],fa[MAXN]; bool rev[MAXN]; char ch[10]; inline int read() { int ret=0,flag=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();} while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();} return ret*flag; } inline void build(int x,int y,int f) { if (x>y) return; int mid=(x+y)>>1; s[mid]=1;fa[mid]=f;s[mid]=y-x+1; c[f][mid>f]=mid; if (x==y) return; build(x,mid-1,mid);build(mid+1,y,mid); } inline void pushup(int k) { s[k]=s[c[k][0]]+s[c[k][1]]+1; } inline void update(int k) { if (!k) return; rev[k]^=1; swap(c[k][0],c[k][1]); } inline void pushdown(int k) { if (!k||!rev[k]) return; update(c[k][0]);update(c[k][1]); rev[k]=0; } inline int find(int k,int x) { pushdown(k); if (s[c[k][0]]+1==x) return k; else if (s[c[k][0]]>=x) return find(c[k][0],x); else return find(c[k][1],x-s[c[k][0]]-1); } inline void rotate(int x,int &k) { int y=fa[x],z=fa[y],l=(c[y][1]==x),r=l^1; if (y==k) k=x; else if (c[z][0]==y) c[z][0]=x;else c[z][1]=x; fa[x]=z;fa[y]=x;fa[c[x][r]]=y; c[y][l]=c[x][r];c[x][r]=y; pushup(y); } inline void relax(int x,int k) { if (x!=k) relax(fa[x],k); pushdown(x); } inline void splay(int x,int &k) { relax(x,k); while (x!=k) { int y=fa[x],z=fa[y]; if (y!=k) { if ((c[y][0]==x)^(c[z][0]==y)) rotate(x,k); else rotate(y,k); } rotate(x,k); } pushup(x); } inline void split(int l,int r) { int x=find(rt,l-1),y=find(rt,r+1); splay(x,rt);splay(y,c[rt][1]); } inline void solvecut(int l,int r,int x) { split(l,r); int tmp=key;key=0;pushup(c[rt][1]);pushup(rt); split(x+1,x); key=tmp;fa[tmp]=c[rt][1];pushup(c[rt][1]);pushup(rt); } inline void solverev(int l,int r) { split(l,r);update(key); pushup(c[rt][1]);pushup(rt); } inline void getans(int k) { if (!k) return; pushdown(k); getans(c[k][0]); if (k!=1&&k!=n+2) { if (flag++) printf(" %d",k-1); else printf("%d",k-1); } getans(c[k][1]); } int main() { n=read();m=read(); while (n>=0&&m>=0) { rt=0; memset(c,0,sizeof(c)); memset(s,0,sizeof(s)); memset(fa,0,sizeof(fa)); memset(rev,0,sizeof(rev)); build(1,n+2,0); rt=(n+2)>>1; F(i,1,m) { scanf("%s",ch);x=read()+1;y=read()+1; if (ch[0]=='C'){z=read()+1;solvecut(x,y,z);} else solverev(x,y); } flag=0;getans(rt);printf("\n"); n=read();m=read(); } }