![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <stdlib.h> #include <stdio.h> int size,k; typedef struct tree_node Tree; struct tree_node { Tree * left, * right; int item; }; int count=0,min; Tree * splay (int i, Tree * t) { Tree N, *l, *r, *y; if (t == NULL) return t; N.left = N.right = NULL; l = r = &N; while(1) { if (i < t->item) { if (t->left == NULL) { break; } if (i < t->left->item) { y = t->left; t->left = y->right; y->right = t; t = y; if (t->left == NULL) { break; } } r->left = t; r = t; t = t->left; } else if (i > t->item) { if (t->right == NULL) { break; } if (i > t->right->item) { y = t->right; t->right = y->left; y->left = t; t = y; if (t->right == NULL) { break; } } l->right = t; l = t; t = t->right; } else { break; } } l->right = t->left; r->left = t->right; t->left = N.right; t->right = N.left; return t; } Tree * insert(int i, Tree * t) { Tree *n; n = (Tree *) malloc (sizeof (Tree)); n->item = i; if (t == NULL) { n->left = n->right = NULL; size = 1; return n; } t = splay(i,t); if (i < t->item) { n->left = t->left; n->right = t; t->left = NULL; size ++; return n; } else if (i >= t->item) { n->right = t->right; n->left = t; t->right = NULL; size++; return n; } else { free(n); return t; } } Tree * Delete(int i, Tree * t) { Tree * x; if (t==NULL) { return NULL; } t = splay(i,t); if (i == t->item) { if (t->left == NULL) { x = t->right; } else { x = splay(i, t->left); x->right = t->right; } size--; count++; free(t); return x; } return t; } void search(Tree *t) { if(t) { if(k==0)return ; search(t->right); if(k==1) printf("%d\n",t->item); k--; search(t->left); } } int yanzheng(Tree *l) { while(l) { if(l->item<min) return l->item; else if(min<=l->item) l=l->left; } return min; } Tree *adjust(Tree *pnode) { int s; s=yanzheng(pnode); while(s<min) { pnode=Delete(s,pnode); s=yanzheng(pnode); } return pnode; } int prea(Tree *pnode) { if(pnode) { prea(pnode->left); pnode->item+=k; prea(pnode->right); } } void pres(Tree *pnode) { if(pnode) { pnode->item-=k; pres(pnode->left); pres(pnode->right); } } int main() { Tree * root; char s,c[10]; int i,n; scanf("%d%d",&n,&min); root = NULL; size = count = 0; while(n--) { gets(c); scanf("%c %d",&s,&k); if(s=='I') { if(k<min) { continue; } else root=insert(k,root); } else if(s=='A') prea(root); else if(s=='S') { pres(root); root=adjust(root); } else if(s=='F') if(k>size) printf("-1\n"); else search(root); } printf("%d\n",count); return 0; }