1 #include <cstdio> 2 const int Maxn=400010; 3 inline void Get_Int(int & x) 4 { 5 char ch=getchar(); x=0; 6 while (ch<'0' || ch>'9') ch=getchar(); 7 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 8 } 9 //======================================== 10 char str[20]; 11 int n,m,u,v; 12 struct Node; 13 inline void Swap(Node *&x,Node *&y); 14 struct Node 15 { 16 Node * pre,* ch[2]; 17 int rev; 18 inline int d() {return pre->ch[1]==this;} 19 inline void Rev() {Swap(ch[0],ch[1]),rev^=1;} 20 inline void setc(Node * r,int d) {ch[d]=r; r->pre=this;} 21 }; 22 Node Memory[Maxn],* Port=Memory,* null,* S[Maxn],* t[Maxn]; 23 inline void Swap(Node *&x,Node *&y) {Node * t=x;x=y;y=t;} 24 inline void Init() {null=Port++; null->rev=0; null->pre=null->ch[0]=null->ch[1]=null;} 25 inline Node * NewNode(){Node * Ret=Port++; Ret->rev=0; Ret->pre=Ret->ch[0]=Ret->ch[1]=null; return Ret;} 26 inline bool IsRoot(Node * x) {return x->pre==null || (x->pre->ch[0]!=x && x->pre->ch[1]!=x);} 27 inline bool Push_Down(Node * x) 28 { 29 if (x->rev) 30 { 31 x->rev=0; 32 x->ch[0]->Rev(); 33 x->ch[1]->Rev(); 34 } 35 } 36 inline void Rotate(Node * x) 37 { 38 Node * y=x->pre; int d=x->d(); 39 Push_Down(y); Push_Down(x); 40 if (IsRoot(y)) x->pre=y->pre; else y->pre->setc(x,y->d()); 41 y->setc(x->ch[!d],d); 42 x->setc(y,!d); 43 } 44 inline void Splay(Node * x) 45 { 46 Push_Down(x); 47 while (!IsRoot(x)) 48 if (IsRoot(x->pre)) Rotate(x); else 49 (x->pre->d()==x->d()) ? (Rotate(x->pre),Rotate(x)) : (Rotate(x),Rotate(x)); 50 } 51 inline void Access(Node * f) 52 { 53 Node * c=null; 54 while (f!=null) 55 { 56 Splay(f); 57 f->ch[1]=c; 58 c=f; 59 f=f->pre; 60 } 61 } 62 inline void Make_Root(Node * x) 63 { 64 Access(x),Splay(x),x->Rev(); 65 } 66 inline void Link(Node * x,Node * y) 67 { 68 Make_Root(x),x->pre=y; 69 } 70 inline void Cut(Node * x,Node * y) 71 { 72 Access(x),Splay(x),Splay(y); 73 if (y->pre!=x) Swap(x,y); 74 Access(x),Splay(x),Splay(y); 75 y->pre=null; 76 } 77 inline Node * Find(Node * x) 78 { 79 while (x->pre!=null) x=x->pre; return x; 80 } 81 inline bool Check(Node * x,Node * y) 82 { 83 if (Find(x)==Find(y)) return true; else return false; 84 } 85 int main() 86 { 87 Init(); 88 Get_Int(n),Get_Int(m); 89 for (int i=1;i<=n;i++) t[i]=NewNode(); 90 for (int i=1;i<=m;i++) 91 { 92 scanf("%s",str); Get_Int(u),Get_Int(v); 93 if (str[0]=='C') Link(t[u],t[v]); 94 if (str[0]=='D') Cut(t[u],t[v]); 95 if (str[0]=='Q') puts(Check(t[u],t[v])?"Yes":"No"); 96 } 97 return 0; 98 } 99
1 #include <cstdio> 2 inline void Get_Int(int &x) 3 { 4 x=0; register char ch=getchar(); int f=1; 5 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 6 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} x*=f; 7 } 8 inline void Put_Int(int x) 9 { 10 char ch[20]; register int top=0; 11 if (x<0) putchar('-'),x=-x; 12 if (x==0) ch[++top]='0'; 13 while (x) ch[++top]=x%10+'0',x/=10; 14 while (top) putchar(ch[top--]); putchar(' '); 15 } 16 inline int Min(int x,int y) {return x>y?y:x;} 17 //=============================================== 18 const int Maxn=400010; 19 int n,m,k[Maxn],x,y,Type; 20 struct Node 21 { 22 Node * pre,* ch[2]; 23 int size,rev; 24 inline int d() {return pre->ch[1]==this;} 25 inline void setc(Node * r,int d) {ch[d]=r; r->pre=this;} 26 }; 27 Node Memory[Maxn],* port=Memory,* t[Maxn],* null,* S[Maxn]; 28 inline void Swap(Node *&x,Node *&y) {Node * t=x;x=y;y=t;} 29 inline void Init() {null=port++; null->rev=0; null->size=0;null->pre=null->ch[0]=null->ch[1]=null;} 30 inline Node * NewNode(){Node * Ret=port++; Ret->ch[0]=Ret->ch[1]=Ret->pre=null;Ret->size=1;Ret->rev=0; return Ret;} 31 inline void Push_Up(Node * x) {x->size=x->ch[0]->size+x->ch[1]->size+1;} 32 inline void Push_Down(Node * x) 33 { 34 if (x->rev) 35 { 36 x->ch[0]->rev^=1; 37 x->ch[1]->rev^=1; 38 x->rev=0; 39 Swap(x->ch[0],x->ch[1]); 40 } 41 } 42 inline bool IsRoot(Node * x) {return x->pre==null || (x->pre->ch[0]!=x && x->pre->ch[1]!=x);} 43 44 inline void Rotate(Node * x) 45 { 46 Node * y=x->pre; int d=x->d(); 47 if (IsRoot(y)) x->pre=y->pre; else y->pre->setc(x,y->d()); 48 y->setc(x->ch[!d],d); 49 x->setc(y,!d); 50 Push_Up(y),Push_Up(x); 51 } 52 inline void Splay(Node * x) 53 { 54 int s=1; S[1]=x; Node * r=x; 55 while (!IsRoot(r->pre)) S[++s]=r=r->pre; 56 while (s) Push_Down(S[s--]); 57 58 while (!IsRoot(x)) 59 if (IsRoot(x->pre)) Rotate(x); else 60 (x->pre->d()==x->d()) ? (Rotate(x->pre),Rotate(x)) : (Rotate(x),Rotate(x)); 61 Push_Up(x); 62 } 63 64 inline void Access(Node * f) 65 { 66 Node * c=null; 67 while (f!=null) 68 { 69 Splay(f); 70 f->ch[1]=c; 71 c=f; 72 f=f->pre; 73 } 74 } 75 inline void Make_Root(Node * r) 76 { 77 Access(r),Splay(r),r->rev^=1; 78 } 79 inline void Link(Node * x,Node * y) 80 { 81 Make_Root(x);x->pre=y; 82 } 83 inline void Cut(Node * x,Node * y) 84 { 85 Access(x),Splay(y); 86 if (y->pre!=x) Swap(x,y); 87 Access(x),Splay(y); 88 y->pre=null; 89 } 90 int main() 91 { 92 // freopen("c.in","r",stdin); 93 Init(); 94 Get_Int(n); 95 for (int i=1;i<=n+1;i++) t[i]=NewNode(); 96 for (int i=1;i<=n;i++) 97 Get_Int(x),k[i]=Min(i+x,n+1),Link(t[i],t[k[i]]); 98 Get_Int(m); Make_Root(t[n+1]); 99 for (int i=1;i<=m;i++) 100 { 101 Get_Int(Type); 102 if (Type==1) 103 { 104 Get_Int(x); x++; 105 Make_Root(t[n+1]); 106 Access(t[x]),Splay(t[x]); 107 Put_Int(t[x]->size-1); 108 } 109 if (Type==2) 110 { 111 Get_Int(x),Get_Int(y); x++; 112 Cut(t[x],t[k[x]]); 113 k[x]=Min(n+1,x+y); 114 Link(t[x],t[k[x]]); 115 } 116 } 117 return 0; 118 } 119
调了好久,明天在做几题。
8.17
1 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <algorithm> 6 using namespace std; 7 inline void Get_Int(int &x) 8 { 9 x=0; char ch=getchar(); 10 while (ch<'0' || ch>'9') ch=getchar(); 11 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 12 } 13 inline int Max(int x,int y) {return x>y?x:y;} 14 inline int Min(int x,int y) {return x>y?y:x;} 15 //=========================================== 16 const int Maxn=300100; 17 struct Node 18 { 19 Node * pre,* ch[2]; int Rev,Key,v; 20 inline int d() {return pre->ch[1]==this;} 21 inline void setc(Node * x,int d) {ch[d]=x; x->pre=this;} 22 }; 23 Node Memory[Maxn<<1],* port=Memory,* null,* Nd[Maxn]; 24 int u,v,n,m,x,Type; 25 inline bool IsRoot(Node * x) {return x->pre==null || (x->pre->ch[1]!=x && x->pre->ch[0]!=x);} 26 inline void Swap(Node *&x,Node *&y) {Node * T=x;x=y;y=T;} 27 inline Node * NewNode(int v) {Node * Ret=port++; Ret->v=Ret->Key=v; Ret->Rev=0; Ret->pre=Ret->ch[0]=Ret->ch[1]=null; return Ret;} 28 inline void Init() {null=port++; null->pre=null->ch[0]=null->ch[1]=null; null->Rev=null->Key=null->v=0;} 29 inline void Get_Rev(Node * x) 30 { 31 if (x==null) return; 32 x->Rev^=1; Swap(x->ch[0],x->ch[1]); 33 } 34 inline void Push_Down(Node * x) 35 { 36 if (x->Rev) 37 { 38 x->Rev=0; 39 Get_Rev(x->ch[1]); 40 Get_Rev(x->ch[0]); 41 } 42 } 43 inline void Push_Up(Node * x) 44 { 45 x->v=x->ch[0]->v^x->ch[1]->v^x->Key; 46 } 47 inline void Rotate(Node * x) 48 { 49 Node * y=x->pre; int d=x->d(); 50 Push_Down(y),Push_Down(x); 51 if (IsRoot(y)) x->pre=y->pre; else y->pre->setc(x,y->d()); 52 y->setc(x->ch[!d],d); 53 x->setc(y,!d); 54 Push_Up(y),Push_Up(x); 55 } 56 inline void Splay(Node * x) 57 { 58 Push_Down(x); 59 while (!IsRoot(x)) 60 if (IsRoot(x->pre)) Rotate(x); else 61 (x->d()==x->pre->d())?(Rotate(x->pre),Rotate(x)):(Rotate(x),Rotate(x)); 62 Push_Up(x); 63 } 64 inline void Access(Node * f) 65 { 66 Node * x=null; 67 while (f!=null) 68 { 69 Splay(f); 70 f->ch[1]=x; 71 x=f; 72 f=f->pre; 73 } 74 } 75 inline void Make_Root(Node * x) 76 { 77 Access(x); Splay(x); Get_Rev(x); 78 } 79 inline void Link(Node * x,Node * y) 80 { 81 Make_Root(x),x->pre=y; 82 } 83 inline void Cut(Node * x,Node * y) 84 { 85 Access(x),Splay(x),Splay(y); 86 if (y->pre!=x) Swap(x,y); 87 Access(x),Splay(x),Splay(y); 88 y->pre=null; 89 } 90 inline Node * Find(Node * x) 91 { 92 while (x->pre!=null) x=x->pre; return x; 93 } 94 inline bool Check(int p,int q) 95 { 96 if (Find(Nd[p])==Find(Nd[q])) return true; return false; 97 } 98 //================================== 99 inline void Query() 100 { 101 Get_Int(u),Get_Int(v); 102 Make_Root(Nd[u]),Access(Nd[v]),Splay(Nd[v]); 103 printf("%d ",Nd[v]->v); 104 } 105 inline void Connect() 106 { 107 Get_Int(u),Get_Int(v); 108 if (Check(u,v)) return; 109 Link(Nd[u],Nd[v]); 110 } 111 inline void Delete() 112 { 113 Get_Int(u),Get_Int(v); 114 if (!Check(u,v)) return; 115 Cut(Nd[u],Nd[v]); 116 } 117 inline void Modify() 118 { 119 Get_Int(u),Get_Int(v); 120 Make_Root(Nd[u]); Nd[u]->Key=v; Push_Up(Nd[u]); 121 } 122 int main() 123 { 124 Get_Int(n),Get_Int(m); Init(); 125 for (int i=1;i<=n;i++) Get_Int(x),Nd[i]=NewNode(x); 126 for (int i=1;i<=m;i++) 127 { 128 Get_Int(Type); 129 if (Type==0) Query(); 130 if (Type==1) Connect(); 131 if (Type==2) Delete(); 132 if (Type==3) Modify(); 133 } 134 return 0; 135 }