1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204 | // 没啥活好整了,给大家咬个打火机吧
#include <bits/stdc++.h>
using namespace std;
const double alpha = 0.2928;
typedef struct NODE{
int value;
int size;
NODE *lchild,*rchild;
NODE(int x){
this->value = x;
this->size = 1;
this->lchild = this->rchild = nullptr;
}
NODE(){
this->value = INT_MAX;
this->size = 0;
this->lchild = this->rchild = nullptr;
}
}NODE,*PNODE;
PNODE root;
inline PNODE newNode(int x){
return new NODE(x);
}
inline void deleteNode(PNODE &T){
delete T;
T = nullptr;
}
inline bool isLeaf(PNODE T){
return T->lchild == nullptr;
}
inline void pushup(PNODE T){
if(!isLeaf(T)){
T->value = T->rchild->value;
T->size = T->lchild->size + T->rchild->size;
}
}
inline void rotate(PNODE T,bool d){
PNODE temp;
if(!d){
temp = T->rchild;
T->rchild = T->lchild;
T->lchild = T->rchild->lchild;
T->rchild->lchild = T->rchild->rchild;
T->rchild->rchild = temp;
}
else{
temp = T->lchild;
T->lchild = T->rchild;
T->rchild = T->lchild->rchild;
T->lchild->rchild = T->lchild->lchild;
T->lchild->lchild = temp;
}
pushup(T->lchild);
pushup(T->rchild);
pushup(T);
}
inline void maintain(PNODE T){
bool d = 0;
if(!isLeaf(T)){
if(T->lchild->size < T->size * alpha)
d = 1;
else if(T->rchild->size < T->size * alpha)
d = 0;
else
return ;
if(d){
if(T->rchild->lchild->size >= T->rchild->size * (1-2*alpha)/(1-alpha))
rotate(T->rchild,!d);
}
else{
if(T->lchild->rchild->size >= T->lchild->size * (1-2*alpha)/(1-alpha))
rotate(T->lchild,!d);
}
rotate(T,d);
}
}
inline void Insert(PNODE T, int x){
if(isLeaf(T)){
T->lchild = newNode(x);
T->rchild = newNode(T->value);
if(T->lchild->value>T->rchild->value)
swap(T->lchild,T->rchild);
pushup(T);
return;
}
if(T->lchild->value >= x)
Insert(T->lchild,x);
else
Insert(T->rchild,x);
pushup(T);
maintain(T);
}
inline void Delete(PNODE &T,int x){
if(isLeaf(T))
return ;
if(x<=T->lchild->value){
if(isLeaf(T->lchild)){
if(x!=T->lchild->value)
return;
deleteNode(T->lchild);
PNODE buf = T->rchild;
*T = *buf;
deleteNode(buf);
}
else
Delete(T->lchild,x);
}
else{
if(isLeaf(T->rchild)){
if(x!=T->rchild->value)
return ;
deleteNode(T->rchild);
PNODE buf = T->lchild;
*T = *buf;
deleteNode(buf);
}
else
Delete(T->rchild,x);
}
pushup(T);
maintain(T);
}
inline int GetRnk(PNODE T,int x){
if(isLeaf(T))
return 1;
if(x <= T->lchild->value)
return GetRnk(T->lchild,x);
return T->lchild->size + GetRnk(T->rchild,x);
}
inline int GetNum(PNODE T,int rnk){
if(isLeaf(T))
return T->value;
if(rnk <= T->lchild->size)
return GetNum(T->lchild,rnk);
return GetNum(T->rchild,rnk - T->lchild->size);
}
inline int GetPre(PNODE T,int num){
return GetNum(T,GetRnk(T,num)-1);
}
inline int GetSuf(PNODE T,int num){
return GetNum(T,GetRnk(T,num+1));
}
int main(){
root = new NODE;
int n;
cin>>n;
while(n--){
int opt;
cin>>opt;
switch(opt){
case 1:{
int x;
cin>>x;
Insert(root,x);
break;
}
case 2:{
int x;
cin>>x;
Delete(root,x);
break;
}
case 3:{
int x;
cin>>x;
cout<<GetRnk(root,x)<<endl;
break;
}
case 4:{
int x;
cin>>x;
cout<<GetNum(root,x)<<endl;
break;
}
case 5:{
int x;
cin>>x;
cout<<GetPre(root,x)<<endl;
break;
}
case 6:{
int x;
cin>>x;
cout<<GetSuf(root,x)<<endl;
break;
}
}
}
return 0;
}
|