标签:style blog io color os sp for div log
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<iostream> #define MAXMOD 999983 #define maxn 1111111 using namespace std; int n,m,v,root,tot,a[maxn],l[maxn],r[maxn],data[maxn],num[maxn]; int RR(int x) { int y=l[x]; l[x]=r[y]; r[y]=x; return y; } int LR(int x) { int y=r[x]; r[x]=l[y]; l[y]=x; return y; } int insert(int root,int key) { if (!(root+1)) { num[tot++]=rand()%MAXMOD; data[tot]=key; l[tot]=r[tot]=-1; return tot; } if (key<data[root]) { l[root]=insert(l[root],key); if (num[l[root]]>num[root]) root=RR(root); } else { r[root]=insert(r[root],key); if (num[r[root]]>num[root]) root=LR(root); } return root; } int delete1(int root,int key) { if (key<data[root]) { l[root]=delete1(l[root],key); return root; } else if (key>data[root]) { r[root]=delete1(r[root],key); return root; } else { if (l[root]==-1 && r[root]==-1) return -1; if (l[root]!=-1 && (r[root]!=-1 && num[l[root]]>num[r[root]] || r[root]==-1)) { root=RR(root); r[root]=delete1(r[root],key); return root; } if (r[root]!=-1 && (l[root]!=-1 && num[r[root]]>num[l[root]] || l[root]==-1)) { root=LR(root); l[root]=delete1(l[root],key); return root; } } } void dfs(int x) { if (l[x]+1) dfs(l[x]); printf("%d ",data[x]); if (r[x]+1) dfs(r[x]); } int main() { freopen("1.in","r",stdin); freopen("1.out","w",stdout); srand(time(0)); scanf("%d",&n); for (int i=0;i<n;i++) l[i]=r[i]=-1; root=-1; for (int i=0;i<n;i++) { scanf("%d",&a[i]); root=insert(root,a[i]); } dfs(root); cout<<"\n"; scanf("%d",&m); for (int i=0;i<m;i++) { scanf("%d",&v); root=delete1(root,v); dfs(root); cout<<"\n"; } dfs(root); return 0; }
标签:style blog io color os sp for div log
原文地址:http://www.cnblogs.com/strawberryfgalois/p/4091916.html