Skip to content

Commit bb1cf79

Browse files
committed
Create hunnu27858.cpp
1 parent 6db4d2b commit bb1cf79

File tree

1 file changed

+242
-0
lines changed

1 file changed

+242
-0
lines changed

the-kth-number-code/hunnu27858.cpp

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
#include<iostream>
2+
#include<stdlib.h>
3+
#include<stdio.h>
4+
using namespace std ;
5+
6+
struct Heap{
7+
8+
const static int HEAP_MAXN = 200005 ;
9+
10+
int h[HEAP_MAXN] , size ;
11+
void init(){
12+
size = 0 ;
13+
}
14+
void push(int v){
15+
int i , j ;
16+
h[++size] = v ; i = size ;
17+
while(i > 1){
18+
j = i>>1 ;
19+
if(h[j] > h[i]){
20+
swap(h[i] , h[j]);
21+
}else break ;
22+
i = j ;
23+
}
24+
}
25+
int pop(){
26+
int res = h[1] , i = 1 , j ;
27+
h[1] = h[size--] ;
28+
while(2*i <= size){
29+
j = i<<1 ;
30+
if(h[j+1]<h[j] && j+1<=size) ++j ;
31+
if(h[j] < h[i]){
32+
swap(h[i] , h[j]);
33+
}else break ;
34+
i = j ;
35+
}
36+
return res ;
37+
}
38+
int top(){
39+
return h[1] ;
40+
}
41+
int empty() {
42+
return size==0 ;
43+
}
44+
};
45+
46+
struct SplayTreeNode{
47+
int ch[2] , pre , sz ;
48+
int v ;
49+
};
50+
51+
struct SplayTree{
52+
53+
const static int SPLAY_MAXN = 200005 ;
54+
55+
inline int L(int x) { return e[x].ch[0] ; }
56+
inline int R(int x) { return e[x].ch[1] ; }
57+
inline int F(int x) { return e[x].pre ; }
58+
59+
SplayTreeNode e[SPLAY_MAXN] ;
60+
int es , root , stack[SPLAY_MAXN] , top , size ;
61+
62+
inline int get(int x){ return e[x].v ; }
63+
64+
void init(){
65+
es = root = top = size = 0 ;
66+
e[0].ch[0] = e[0].ch[1] = e[0].pre = e[0].sz = 0 ;
67+
}
68+
69+
int newNode(int v ,int father){
70+
int x = (top ? stack[--top] : ++es ) ;
71+
e[x].pre = father ;
72+
e[x].sz = 1 ;
73+
e[x].v = v ;
74+
e[x].ch[0] = e[x].ch[1] = 0 ;
75+
return x ;
76+
}
77+
78+
void clrNode(int x){
79+
if(x) stack[top++] = x ;
80+
}
81+
82+
void rotate(int x){
83+
if(!x) return ;
84+
int y , z , f ;
85+
y = F(x) , z = F(y) , f = (R(y)==x);
86+
e[y].ch[f] = e[x].ch[!f] ;
87+
if(e[x].ch[!f])
88+
e[e[x].ch[!f]].pre = y ;
89+
e[x].ch[!f] = y ;
90+
e[y].pre = x ;
91+
e[x].pre = z ;
92+
if(z)
93+
e[z].ch[R(z)==y] = x ;
94+
e[x].sz = e[y].sz ;
95+
e[y].sz = e[L(y)].sz + e[R(y)].sz + 1 ;
96+
if(y == root) root = x ;
97+
}
98+
99+
void splay(int x , int goal){
100+
int y , z , f ;
101+
while(F(x)!=goal){
102+
if(F(F(x))==goal){
103+
rotate(x);
104+
}else{
105+
y = F(x) , z = F(y) , f = (R(z)==y);
106+
if(e[y].ch[f] == x){
107+
rotate(y); rotate(x);
108+
}else{
109+
rotate(x); rotate(x);
110+
}
111+
}
112+
}
113+
}
114+
115+
void insert(int v){
116+
int x = root , y = 0 ;
117+
++ size ;
118+
if(root == 0){
119+
root = newNode(v , 0); return ;
120+
}
121+
while(x){
122+
y = x ;
123+
if(v <= e[x].v){
124+
x = L(x);
125+
}else{
126+
x = R(x);
127+
}
128+
}
129+
x = newNode(v , y);
130+
if(v <= e[y].v ){
131+
e[y].ch[0] = x ;
132+
}else{
133+
e[y].ch[1] = x ;
134+
}
135+
splay(x , 0);
136+
}
137+
138+
int find(int v){
139+
int y = 0 , x = root ;
140+
while(x){
141+
y = x ;
142+
if(v == e[x].v ) break ;
143+
if(v < e[x].v){
144+
x = L(x);
145+
}else{
146+
x = R(x);
147+
}
148+
}
149+
splay(x ? x:y , 0);
150+
return x ;
151+
}
152+
153+
void erase(int v){
154+
if(find(v)==0) return ;
155+
int y = L(root) , z = R(root) ;
156+
-- e[root].sz ; -- size ;
157+
if(y == 0){
158+
clrNode(root);
159+
root = z ;
160+
e[root].pre = 0 ;
161+
}else{
162+
while(R(y)){
163+
--e[y].sz ; y = R(y);
164+
}
165+
-- e[y].sz ;
166+
splay(y , root);
167+
if(z){
168+
e[z].pre = y ; e[y].ch[1] = z ;
169+
}
170+
clrNode(root);
171+
root = y ;
172+
e[root].pre = 0 ;
173+
}
174+
}
175+
176+
int getkth(int k){
177+
if(k < 1 || k > size ) return 0 ;
178+
int x = root , y = 0 , ls ;
179+
while(x){
180+
y = x ;
181+
ls = e[L(x)].sz + 1 ;
182+
if(k == ls ) break ;
183+
if(k < ls ){
184+
x = L(x);
185+
}else{
186+
k -= ls ; x = R(x);
187+
}
188+
}
189+
splay(x , 0);
190+
return x ;
191+
}
192+
193+
void traval(int x){
194+
if(x){
195+
traval(L(x));
196+
printf(" %d" , e[x].v);
197+
traval(R(x));
198+
}
199+
}
200+
201+
void debug(){
202+
printf("The Element Is :"); traval(root); printf(" =====Size = %d\n" , size);
203+
}
204+
};
205+
206+
SplayTree T ;
207+
Heap H ;
208+
209+
int main(){
210+
int cmdSize , minPay , k , addPay , delCnt ;
211+
char cmd[5] ;
212+
scanf("%d%d" , &cmdSize , &minPay);
213+
T.init() ;
214+
H.init() ;
215+
addPay = 0 ; delCnt = 0 ;
216+
while(cmdSize--){
217+
scanf("%s%d" , cmd , &k);
218+
if(cmd[0]=='I'){
219+
if(k >= minPay){
220+
T.insert(k - addPay);
221+
H.push(k-addPay);
222+
}
223+
}else if(cmd[0]=='A'){
224+
addPay += k ;
225+
}else if(cmd[0]=='S'){
226+
addPay -= k ;
227+
while(!H.empty() && H.top() + addPay < minPay ){
228+
k = H.pop() ;
229+
T.erase(k); delCnt ++ ;
230+
}
231+
}else if(cmd[0]=='F'){
232+
if(k > T.size )
233+
printf("-1\n");
234+
else
235+
printf("%d\n" , T.get(T.getkth(T.size-k+1)) + addPay);
236+
}
237+
//T.debug();
238+
}
239+
printf("%d\n" , delCnt);
240+
//system("pause");
241+
return 0 ;
242+
}

0 commit comments

Comments
 (0)