1 #include<cstdio> 2 #include<iostream> 3 #include<cstdlib> 4 #define rep(i,j,k) for(int i = j; i <= k; i++ ) 5 using namespace std; 6 int n = 0; 7 8 int read() 9 { 10 int s = 0, t = 1; 11 char c = getchar(); 12 while( !isdigit(c) ){ 13 if( c == ‘-‘ ) t = -1; 14 c = getchar(); 15 } 16 while( isdigit(c) ){ 17 s = s * 10 + c - ‘0‘; 18 c = getchar(); 19 } 20 return s * t; 21 } 22 23 struct node{ 24 node*ch[2]; 25 int size, key, r; 26 node(int key):key(key){ 27 ch[0] = NULL, ch[1] = NULL, r = rand(),size = 1; 28 } 29 int cmp(int x) const{ 30 if( x == key ) return -1; 31 return x < key ? 0 : 1; 32 } 33 void maintain() { 34 size = 1; 35 if( ch[0] != NULL ) size += ch[0] -> size; 36 if( ch[1] != NULL ) size += ch[1] -> size; 37 } 38 39 }; 40 41 void rorate(node*& o,int d) 42 { 43 node*k = o->ch[d^1]; 44 o->ch[d^1] = k->ch[d]; 45 k->ch[d] = o; 46 o->maintain(); 47 k->maintain(); 48 o = k; 49 } 50 51 void insert(node*&u,int v) 52 { 53 if( u == NULL ){ 54 u = new node(v); 55 } 56 else { 57 int d = u->cmp(v); 58 insert(u->ch[d], v); 59 if( u->ch[d]->r > u->r ){ 60 rorate(u,d^1); 61 } 62 } 63 u->maintain(); 64 } 65 66 void remove(node*&u,int v) 67 { 68 if( u == NULL ) return; 69 int d = u->cmp(v); 70 if( d == -1){ 71 if( u->ch[0] == NULL ){ 72 u = u->ch[1]; 73 } 74 else if( u->ch[1] == NULL ){ 75 u = u->ch[0]; 76 } 77 else { 78 int d2 = (u->ch[0]->r > u->ch[1]->r ? 1 : 0); 79 rorate(u,d2); 80 remove(u->ch[d2],v); 81 } 82 } 83 else remove(u->ch[d],v); 84 if( u != NULL ) u->maintain(); 85 } 86 87 bool infind(node*&u,int v) 88 { 89 while( u != NULL ) 90 { 91 int d = u->cmp(v); 92 if( d == -1 )return 1; 93 u = u->ch[d]; 94 } 95 return 0; 96 } 97 98 node* find(node*u,int v) 99 { 100 while( u != NULL ) 101 { 102 int d = u->cmp(v); 103 if( d == -1 )return u; 104 u = u->ch[d]; 105 } 106 return NULL; 107 } 108 109 int kth(node*& u,int k) 110 { 111 if( u == NULL|| k <= 0 || k > u->size ) return 0; 112 int d = u->ch[1] == NULL? 0 : u->ch[1]->size; 113 if( k == d+1 ) return u->key; 114 else { 115 if( d >= k ){ 116 return kth(u->ch[1],k); 117 } 118 else return kth(u->ch[0],k-d-1); 119 } 120 } 121 122 int rfind(node*u,int x) 123 { 124 int z = 0; 125 while( u != NULL ) 126 { 127 int d = u->cmp(x); 128 if( d == -1 ) 129 return z + ( u->ch[1] == NULL ? 1 : u->ch[1]->size+1 ); 130 if( d == 0) z += (u->ch[1] == NULL ? 1 : u->ch[1]->size+1); 131 u = u->ch[d]; 132 } 133 if( u == NULL ) return 0; 134 } 135 136 int rank(node*&u,int x) 137 { 138 int k = rfind(u,x); 139 return k; 140 } 141 142 int pre(node*u,int x) 143 { 144 int t = rank(u,x); 145 if( !t ) return 0; 146 return kth(u,t+1); 147 } 148 149 int suc(node*&u,int x) 150 { 151 int t = rank(u,x); 152 if( !t ) return 0; 153 return kth(u,t-1); 154 } 155 156 int minl(node*&u) 157 { 158 return kth(u,n); 159 } 160 161 int maxl(node*&u) 162 { 163 return kth(u,1); 164 } 165 166 void print(node* u) 167 { 168 if( u->ch[0] != NULL ){ 169 print(u->ch[0]); 170 } 171 cout<<u->key<<" "; 172 if( u->ch[1] != NULL ){ 173 print(u->ch[1]); 174 } 175 } 176 177 int main() 178 { 179 node* root = NULL; 180 int x; 181 while( scanf("%d", &x) == 1 && x ){ 182 insert(root,x); 183 n++; 184 } 185 cout<<n<<endl; 186 char c; 187 while( scanf("%c", &c) == 1 ){ 188 if( c == ‘d‘ ){ 189 int x = read(); 190 remove(root,x); 191 n--; 192 } 193 else if( c == ‘k‘ ){ 194 int x = read(); 195 cout<<kth(root,x)<<endl; 196 } 197 else if( c == ‘r‘ ){ 198 int x = read(); 199 cout<<rank(root,x)<<endl; 200 } 201 else if( c == ‘p‘ ){ 202 int x = read(); 203 cout<<pre(root,x)<<endl; 204 } 205 else if( c == ‘s‘ ){ 206 int x = read(); 207 cout<<suc(root,x)<<endl; 208 } 209 else if( c == ‘m‘){ 210 cout<<maxl(root)<<endl; 211 } 212 else if( c == ‘n‘ ){ 213 cout<<minl(root)<<endl; 214 } 215 print(root); 216 cout<<endl<<endl; 217 } 218 return 0; 219 }
时间: 2024-11-06 18:06:14