Catalog
splay-学习

其实……
说实话我还真的不是非常会用qwq
先存一下板子把,以后慢慢来qwq

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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define re register
#define INF 0x7fffffff
#define ll long long

using namespace std;

int read()
{
int x = 0,f = 1; char ch;
ch = getchar();
while(ch >'9' || ch < '0'){if(ch == '-') f = -f; ch = getchar();}
while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}

struct node{
int fa,son[2],v,tie,siz;
}e[1000000];

int root,tot;

void rotate(int x)
{
int y = e[x].fa, z = e[y].fa, mode = 0;
if(e[z].son[0] == y) e[z].son[0] = x;
else e[z].son[1] = x;
e[x].fa = z;

if(e[y].son[0] == x) mode ++;
e[y].son[mode ^ 1] = e[x].son[mode];
e[e[x].son[mode]].fa = y;
e[x].son[mode] = y;
e[y].fa = x;

e[y].siz = e[e[y].son[0]].siz + e[e[y].son[1]].siz + e[y].tie;
e[x].siz = e[e[x].son[0]].siz + e[e[x].son[1]].siz + e[x].tie;
}

void splay(int x)
{
while(e[x].fa)
{
int y = e[x].fa, z = e[y].fa;
if(z)
{
if((e[y].son[0] == y) ^ (e[y].son[0] == x)) rotate(x);
else rotate(y);
}
rotate(x);
}
root = x;
}

int find(int now, int w)
{
while(e[now].v != w)
{
if(w < e[now].v)
{
if(e[now].son[0]) now = e[now].son[0];
else break;
}
else
{
if(e[now].son[1]) now = e[now].son[1];
else break;
}
}
return now;
}

void add(int f, int w)
{
e[++ tot].fa = f;
e[tot].tie = 1;
e[tot].siz = 1;
e[tot].v = w;
}

void ins(int p)
{
if(!tot)
{
add(0,p);
root = 1;
return ;
}
int pos = find(root,p);
if(e[pos].v == p) e[pos].tie ++;
else
{
add(pos,p);
if(p < e[pos].v) e[pos].son[0] = tot;
else e[pos].son[1] = tot;
}
for(int now = pos; now; ++e[now].siz,now = e[now].fa);
if(e[pos].v == p) splay(pos);
else splay(tot);
}

void del(int p)
{
int pos = find(root,p);
if(e[pos].v != p) return ;
splay(pos);
if(e[pos].tie > 1)
{
e[pos].tie --;
e[pos].siz --;
return ;
}
if(!e[pos].son[0])
{
e[e[pos].son[1]].fa = 0;
root = e[pos].son[1];
if(!root) tot = 0;
}
else
{
e[e[pos].son[0]].fa = 0;
int lax = find(e[pos].son[0],1000000);
splay(lax);
e[root].siz += e[e[pos].son[1]].siz;
e[root].son[1] = e[pos].son[1];
e[e[pos].son[1]].fa = root;
}
e[pos].v = 0;
e[pos].tie = 0;
e[pos].siz = 0;
e[pos].fa = 0;
e[pos].son[0] = 0;
e[pos].son[1] = 0;
}

int rank(int p)
{
int pos = find(root,p);
splay(pos);
return e[e[pos].son[0]].siz + 1;
}

int k_th(int p)
{
int now = root;
for(int bot = e[e[now].son[0]].siz; p <= bot || p > bot + e[now].tie; bot = e[e[now].son[0]].siz)
if(p > bot + e[now].tie)
{
p = p - bot - e[now].tie;
now = e[now].son[1];
}
else now = e[now].son[0];
return e[now].v;
}

int pred(int p)
{
int pos = find(root,p);
if(e[pos].v < p) return e[pos].v;
splay(pos);
return e[find(e[pos].son[0],10000000)].v;
}

int succ(int p)
{
int pos = find(root,p);
if(e[pos].v > p) return e[pos].v;
splay(pos);
return e[find(e[pos].son[1],0)].v;
}

int main()
{
// freopen("a.txt","w",stdout);
int m,mode,x;
scanf("%d",&m);
while (m--)
{
scanf("%d%d",&mode,&x);
switch (mode)
{
case 1:ins(x);break;
case 2:del(x);break;
case 3:printf("%d\n",rank(x));break;
case 4:printf("%d\n",k_th(x));break;
case 5:printf("%d\n",pred(x));break;
case 6:printf("%d\n",succ(x));break;
}
}
return 0;
}
Author: wflight
Link: http://yoursite.com/2019/08/02/splay-学习/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付寶

Comment