/
P3369_6.cpp
171 lines (150 loc) · 3.49 KB
/
P3369_6.cpp
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
/**
* P3369
* splay
*/
#include <stdio.h>
#include <stdlib.h>
class __splay {
struct __node {
int v, w, s;
__node *fa, *ls, *rs, **pt;
}*root = NULL;
inline void _update(__node *x) {
if (x != NULL)
x->s = ((x->ls == NULL) ? (0) : (x->ls->s)) + x->w + ((x->rs == NULL) ? (0) : (x->rs->s));
}
inline void _rotate(register __node *x) {
if (x == NULL || x->fa == NULL)return ;
register __node *y = x->fa, **s = ((x == x->fa->ls) ? (&x->rs) : (&x->ls));
if (*s != NULL) {
(*s)->fa = x->fa;
(*s)->pt = x->pt;
*(*s)->pt = (*s);
} else
*x->pt = NULL;
x->fa = y->fa;
x->pt = y->pt;
*x->pt = x;
y->fa = x;
y->pt = s;
*y->pt = y;
_update(y);
_update(x);
}
void _splay(__node *x, __node **p) {
if (x == NULL)return ;
while (x != *p)
if (x->fa == *p)_rotate(x);
else if ((x == x->fa->ls) == (x->fa == x->fa->fa->ls))
_rotate(x->fa), _rotate(x);
else _rotate(x), _rotate(x);
}
public:
void _insert(register int v) {
register __node **x = &root, *f = NULL;
while ((*x) != NULL && (*x)->v != v)
if (v < (*x)->v)f = *x, x = &(*x)->ls;
else f = *x, x = &(*x)->rs;
if (*x == NULL)
*x = (__node *)calloc(sizeof(__node), 1), (*x)->v = v, (*x)->pt = x, (*x)->fa = f;
_splay(*x, &root);
++root->w, ++root->s;
}
void _delete(register int v) {
register __node *x = root;
while (x != NULL && x->v != v)
if (v < x->v)x = x->ls;
else x = x->rs;
if (x == NULL)return ;
_splay(x, &root);
--x->w, --x->s;
if (x->w == 0) {
if (x->ls == x->rs)
*x->pt = NULL;
else if (x->ls == NULL)
x->rs->fa = NULL, x->rs->pt = &root, root = x->rs;
else if (x->rs == NULL)
x->ls->fa = NULL, x->ls->pt = &root, root = x->ls;
else {
x = x->rs;
while (x->ls != NULL)x = x->ls;
_splay(x, &root->rs);
if (root->ls != NULL)root->ls->fa = x, root->ls->pt = &x->ls;
x->ls = root->ls, x->fa = NULL, x->pt = &root, root = x;
}
_update(root);
}
}
int _n2r(register int v) {
register __node *x = root, *f = NULL;
while (x != NULL && x->v != v)
if (v < x->v)f = x, x = x->ls;
else f = x, x = x->rs;
if (x == NULL)x = f;
_splay(x, &root);
if (v <= x->v)
return ((x->ls == NULL) ? (0) : (x->ls->s)) + 1;
else
return ((x->ls == NULL) ? (0) : (x->ls->s)) + x->w + 1;
}
int _r2n(register int r) {
register __node *x = root;
while (r > 0)
if (r <= ((x->ls == NULL) ? (0) : (x->ls->s)))
x = x->ls;
else if (r <= ((x->ls == NULL) ? (0) : (x->ls->s)) + x->w)
r = 0;
else r -= ((x->ls == NULL) ? (0) : (x->ls->s)) + x->w, x = x->rs;
_splay(x, &root);
return x->v;
}
int _last(int v) {
register __node *x = root , *f = NULL;
register int res;
while (x != NULL)
if (v <= x->v)f = x, x = x->ls;
else res = x->v, f = x, x = x->rs;
_splay(f, &root);
return res;
}
int _next(int v) {
register __node *x = root , *f = NULL;
register int res;
while (x != NULL)
if (v < x->v)res = x->v, f = x, x = x->ls;
else f = x, x = x->rs;
_splay(f, &root);
return res;
}
} splay;
signed main() {
freopen("P3369_11.in", "r", stdin);
int n, o, x;
scanf("%d", &n);
for (register int i = 0; i < n; ++i) {
scanf("%d%d", &o, &x);
switch (o) {
case 1:
splay._insert(x);
break;
case 2:
splay._delete(x);
break;
case 3:
printf("%d\n", splay._n2r(x));
break;
case 4:
printf("%d\n", splay._r2n(x));
break;
case 5:
printf("%d\n", splay._last(x));
break;
case 6:
printf("%d\n", splay._next(x));
break;
default:
break;
}
}
return 0;
}