-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScapeGoatTree.c
194 lines (174 loc) · 3.73 KB
/
ScapeGoatTree.c
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
#include <stdio.h>
#include <stdlib.h>
#define INF 2147483647
const double beta=0.70;
const double alpha=0.80;
struct tree
{
struct tree* ls;
struct tree* rs;
int siz,con,tsiz,data;
};
typedef struct tree stu;
typedef struct tree* ptr;
stu pol[100100];
int tmp; ptr* doit;
int size(ptr now)
{
if (now==NULL) return 0;
return now->siz;
}
int total(ptr now)
{
if (now==NULL) return 0;
return now->tsiz;
}
void pushup(ptr now)
{
if (now==NULL) return;
now->tsiz=1;
now->siz=now->con;
now->siz+=size(now->ls);
now->siz+=size(now->rs);
now->tsiz+=total(now->ls);
now->tsiz+=total(now->rs);
return;
}
int isbad(ptr now)
{
if (now==NULL) return 0;
if (now->ls && 1.0*total(now->ls)>total(now)*alpha+5) return 1;
if (now->rs && 1.0*total(now->rs)>total(now)*alpha+5) return 1;
if (total(now)*beta>1.0*size(now)) return 1; return 0;
}
void dfs(ptr now)
{
if (now==NULL) return;
dfs(now->ls);
if (now->con)
pol[tmp++]=*now;
dfs(now->rs);
free(now); return;
}
ptr bulid(int l,int r)
{
if (l>=r) return NULL;
int mid=(l+r)>>1;
ptr now=(ptr)malloc(sizeof(stu));
*now=pol[mid];
now->ls=bulid(l,mid);
now->rs=bulid(mid+1,r);
pushup(now); return now;
}
void rebulid(ptr *now)
{
tmp=0,dfs(*now);
*now=bulid(0,tmp);
return;
}
void ins(ptr* now,int num)
{
if (*now==NULL)
{
*now=(ptr)malloc(sizeof(stu));
(*now)->data=num,(*now)->ls=(*now)->rs=NULL;
(*now)->tsiz=(*now)->siz=(*now)->con=1; return;
}
if ((*now)->data==num)
{
(*now)->con++;
pushup(*now); return;
}
if ((*now)->data>num) ins(&(*now)->ls,num);
else ins(&(*now)->rs,num); pushup(*now);
if (isbad(*now)) doit=now; return;
}
void del(ptr* now,int num)
{
if (*now==NULL) return;
if ((*now)->data==num)
{
if ((*now)->con)
(*now)->con--;
}
else
{
if ((*now)->data>num) del(&(*now)->ls,num);
else del(&(*now)->rs,num);
}
pushup(*now); if (isbad(*now))
doit=now; return;
}
int fnd(ptr now,int num)
{
if (now==NULL) return 0;
if (now->con && now->data==num) return size(now->ls);
if (now->data>num) return fnd(now->ls,num);
return size(now->ls)+now->con+fnd(now->rs,num);
}
int kth(ptr now,int num)
{
if (now==NULL) return INF;
if (size(now->ls)>=num) return kth(now->ls,num);
if (size(now->ls)+now->con>=num) return now->data;
return kth(now->rs,num-size(now->ls)-now->con);
}
void print(ptr p)
{
printf("data:%d,con:%d,",p->data,p->con);
printf("siz:%d,tsiz:%d ",p->siz,p->tsiz);
return;
}
void printfst(ptr now)
{
if (now)
{
print(now);
if (now->ls) printfst(now->ls);
if (now->rs) printfst(now->rs);
}
else printf("NULL");
return;
}
void printmid(ptr now)
{
if (now)
{
if (now->ls) printmid(now->ls);
print(now);
if (now->rs) printmid(now->rs);
}
else printf("NULL");
return;
}
void printlst(ptr now)
{
if (now)
{
if (now->ls) printlst(now->ls);
if (now->rs) printlst(now->rs);
print(now);
}
else printf("NULL");
return;
}
int main()
{
int t,x,y;
ptr root=NULL;
scanf("%d",&t);
while (t--)
{
scanf("%d",&x);
if (x==1) {scanf("%d",&y),doit=NULL,ins(&root,y); if (doit) rebulid(doit); continue;}
if (x==2) {scanf("%d",&y),doit=NULL,del(&root,y); if (doit) rebulid(doit); continue;}
if (x==3) {scanf("%d",&y),printf("%d\n",fnd(root,y)+1); continue;}
if (x==4) {scanf("%d",&y),printf("%d\n",kth(root,y)); continue;}
if (x==5) {scanf("%d",&y),printf("%d\n",kth(root,fnd(root,y))); continue;}
if (x==6) {scanf("%d",&y),printf("%d\n",kth(root,fnd(root,y+1)+1)); continue;}
if (x==7) {printfst(root),puts(""); continue;}
if (x==8) {printmid(root),puts(""); continue;}
if (x==9) {printlst(root),puts(""); continue;}
}
return 0;
}