题目链接:https://www.luogu.org/problemnew/show/P3369
修改了一下之前的模板,支持重复数值的存储
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct node 4 { 5 int val; 6 node *father; 7 node *son[2]; 8 int cnt; 9 int siz; 10 } tree[100005],*root; 11 inline void init(node *p,int val=0) 12 { 13 p->father=NULL; 14 p->son[0]=p->son[1]=NULL; 15 p->val=val; 16 p->siz=1; 17 p->cnt=1; 18 } 19 inline int siz(node *t) 20 { 21 return t == NULL ? 0 : t->siz; 22 } 23 inline void update(node *t)//rotate与erase操作时需更新 24 { 25 t->siz = t->cnt; 26 t->siz += siz(t->son[0]); 27 t->siz += siz(t->son[1]); 28 } 29 inline bool son(node *f, node *s) 30 { 31 return f->son[1] == s; 32 } 33 inline void rotate(node *t) 34 { 35 node *f = t->father; 36 node *g = f->father; 37 bool a = son(f, t), b = !a; 38 f->son[a] = t->son[b]; 39 if (t->son[b] != NULL) 40 t->son[b]->father = f; 41 t->son[b] = f; 42 f->father = t; 43 t->father = g; 44 if (g != NULL) 45 g->son[son(g, f)] = t; 46 else 47 root = t; 48 update(t); 49 update(f); 50 } 51 inline void splay(node *t, node *p) 52 { 53 while (t->father != p) 54 { 55 node *f = t->father; 56 node *g = f->father; 57 if (g == p) 58 rotate(t); 59 else 60 { 61 if (son(g, f) ^ son(f, t)) 62 rotate(t), rotate(t); 63 else 64 rotate(f), rotate(t); 65 } 66 } 67 update(t); 68 if(p!=NULL) update(p); 69 } 70 inline void insert(node* p) 71 { 72 if (root == NULL) 73 { 74 root = p; 75 return; 76 } 77 for(node* t=root; t; t = t->son[t->val < p->val]) 78 { 79 if(t->val==p->val) 80 { 81 t->cnt++; 82 splay(t,NULL); 83 return; 84 } 85 if(t->son[t->val < p->val]==NULL) 86 { 87 t->son[t->val < p->val]=p; 88 p->father=t; 89 splay(p,NULL); 90 return; 91 } 92 } 93 } 94 inline void erase(node *t) 95 { 96 splay(t,NULL); 97 if (t->son[0] == NULL) 98 { 99 root = t->son[1]; 100 if (root != NULL) 101 root->father = NULL; 102 } 103 else 104 { 105 node *p = t->son[0]; 106 while (p->son[1] != NULL) 107 p = p->son[1]; 108 splay(p, t); 109 root = p; 110 root->father = NULL; 111 p->son[1] = t->son[1]; 112 if (p->son[1] != NULL) 113 p->son[1]->father = p; 114 update(p); 115 } 116 } 117 int n,m; 118 bool flag; 119 inline node* findx(int kth) 120 { 121 node* p=root; 122 while(1) 123 { 124 if(kth<=siz(p->son[0])) p=p->son[0]; 125 else 126 { 127 kth-=siz(p->son[0])+p->cnt; 128 if(kth<=0) return p; 129 p=p->son[1]; 130 } 131 } 132 } 133 inline node* findkth(int x) 134 { 135 node* p=root; 136 while(1) 137 { 138 if(p==NULL) return NULL; 139 if(p->val==x) return p; 140 p=p->son[p -> val < x]; 141 } 142 } 143 int main() 144 { 145 int m; 146 scanf("%d",&m); 147 int tot=0; 148 for(int i=1;i<=m;i++) 149 { 150 int op; 151 scanf("%d",&op); 152 if(op==1) 153 { 154 int x; 155 scanf("%d",&x); 156 init(&tree[++tot],x); 157 insert(&tree[tot]); 158 } 159 if(op==2) 160 { 161 int x; 162 scanf("%d",&x); 163 node* tmp=findkth(x); 164 if(tmp!=NULL) 165 { 166 tmp->cnt--; 167 if(!tmp->cnt) 168 erase(tmp); 169 else 170 splay(tmp,NULL); 171 } 172 } 173 if(op==3) 174 { 175 int x; 176 scanf("%d",&x); 177 node* tmp=findkth(x); 178 splay(tmp,NULL); 179 if(tmp->son[0]==NULL) 180 puts("1"); 181 else 182 printf("%d\n",tmp->son[0]->siz+1); 183 } 184 if(op==4) 185 { 186 int kth; 187 scanf("%d",&kth); 188 node *tmp=findx(kth); 189 if(tmp!=NULL) printf("%d\n",tmp->val); 190 } 191 if(op==5) 192 { 193 int x; 194 scanf("%d",&x); 195 node* p=root; 196 int tmp; 197 while(1) 198 { 199 if(p==NULL) break; 200 if(p->val>=x) 201 p=p->son[0]; 202 else 203 { 204 tmp=p->val; 205 p=p->son[1]; 206 } 207 } 208 printf("%d\n",tmp); 209 } 210 if(op==6) 211 { 212 int x; 213 scanf("%d",&x); 214 node* p=root; 215 int tmp; 216 while(1) 217 { 218 if(p==NULL) break; 219 if(p->val<=x) 220 p=p->son[1]; 221 else 222 { 223 tmp=p->val; 224 p=p->son[0]; 225 } 226 } 227 printf("%d\n",tmp); 228 } 229 } 230 }