Stretch tree template is really good long ...
Cut a B C: Extend the number of A-1 to the root node, extend the number of b+1 to the right subtree of a, and then take Ch[ch[root][1][0]] away, and place it under the C node of the remaining tree.
Flip a B: Extend the number of A-1 to the root node, extend the number of b+1 to the right subtree of a, then flip Ch[ch[root][1][0]];
Due to the situation on both sides of the operation, two-1 nodes are added.
Attention:
1, the output should pay attention to the space and newline.
2, when take off the subtree should pay attention to push_up ();
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #include < vector>using namespace std; #define MAXN 330000#define Mem (A, B) memset (A,b,sizeof (a)) #define ROOT10 ch[ch[root][1]][ 0] #define ROOT1 ch[root][1]int pre[maxn],ch[maxn][2],root,tot;int size[maxn];int rev[maxn];int key[maxn];int n;void Treaval (int x) {if (x) {treaval (ch[x][0]); printf ("Knot%2d: Left son%2d right son%2d parent node%2d size =%2d, key =%2d \ n", x,ch[x][0],ch[x][1],pre[x],size[x],key[x]); Treaval (ch[x][1]); }}void Debug () {printf ("%d\n", root); Treaval (root);} Above Debugvoid init () {root=tot=0; MEM (pre,0); MEM (ch,0); MEM (size,0); MEM (rev,0);} void NewNode (int &x,int k,int father) {X=++tot; Pre[x]=father; Size[x]=1; ch[x][0]=ch[x][1]=0; rev[x]=0; Key[x]=k;} void Push_down (int x) {if (rev[x]) {rev[x]=0; Rev[ch[x][0]]^=1; Rev[ch[x][1]]^=1; Swap (ch[x][0],ch[x][1]); }}void push_up (int x) {size[x]=size[ch[x][0]]+size[ch[x][1]]+1;} void rot (int x,int kind) {int y=pre[x]; Push_down (y); Push_down (x); Ch[y][!kind]=ch[x][kind]; Pre[ch[x][kind]]=y; if (Pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x; Pre[x]=pre[y]; Ch[x][kind]=y; Pre[y]=x; Push_up (y); PUSH_UP (x);} void splay (int x,int goal) {Push_down (x); while (Pre[x]!=goal) {if (pre[pre[x]]==goal) {Push_down (pre[x]); Push_down (x); Rot (x,ch[pre[x]][0]==x); } else {int y=pre[x]; Push_down (Pre[y]); Push_down (y); Push_down (x); int kind=ch[pre[y]][0]==y; if (ch[y][kind]==x) {rot (x,!kind); Rot (x,kind); } else {rot (y,kind); Rot (x,kind); }}} push_up (x); if (goal==0) root=x;} void Buildtree (int &x,int l,int r,int father) {if (l>r) return; int mid= (L+R)/2; NewNode (X,mid,father); Buildtree (CH[X][0],L,MID-1,X); Buildtree (CH[X][1],MID+1,R,X); PUSH_UP (x);} int get_kth (int x,int k) {Push_down (x); int p=size[ch[x][0]]; if (p+1==k) return x; else if (k<=p) return get_kth (CH[X][0],K); else get_kth (ch[x][1],k-p-1);} int get_min (int r) {Push_down (R); while (Ch[r][0]) {r=ch[r][0]; Push_down (R); } return R;} int Get_max (int r) {Push_down (R); while (Ch[r][1]) {r=ch[r][1]; Push_down (R); } return R;} void Cut (int a,int b,int c) {int x=get_kth (root,a); int y=get_kth (root,b+2); Splay (x,0); Splay (Y,root); int t=root10; root10=0; PUSH_UP (ROOT1); Push_up (root); int z=get_kth (root,c+1); Splay (z,0); int p=get_min (ROOT1); Splay (P,root); root10=t; PRE[ROOT10]=ROOT1; PUSH_UP (ROOT1); Push_up (root);} void Flip (int a,int b) {int x=get_kth (root,a); int y=get_kth (root,b+2); Splay (x,0); Splay (Y,root); Rev[root10]^=1;}vector<int>vec;void print (int x) {if (x==0) return; Push_down (x); Print (ch[x][0]); if (key[x]!=-1) Vec.push_back (key[x]); Print (ch[x][1]);} int main () {int m,a,b,c; Char str[110]; int i; while (scanf ("%d%d", &n,&m) && (n+1| | m+1)) {init (); NewNode (root,-1,0); NewNode (Ch[root][1],-1,root); size[root]=2; Buildtree (ROOT10,1,N,ROOT1); PUSH_UP (ROOT1); Push_up (root); for (i=1;i<=m;i++) {scanf ("%s", str); if (str[0]== ' C ') {scanf ("%d%d%d", &a,&b,&c); Cut (A,B,C); } else {scanf ("%d%d", &a,&b); Flip (A, b); }} vec.clear (); print (root); For (I=0;i<vec.size (); i++) {cout<<vec[i]; if (I!=vec.size ()-1) cout<< ""; else cout<<endl; }} return 0;}