P1503 鬼子进村

$Splay$

求前驱后继。

查看代码
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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<iomanip>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define gh() getchar()
#define underMax(x,y) ((x)>(y)?(x):(y))
#define underMin(x,y) ((x)<(y)?(x):(y))
typedef long long ll;
using namespace std;
const int MAXN=1e5+1;
const int INF=0x7f7f7f7f;
template<class T>
inline void underRead(T &x)
{
x=0;
char ch=gh(),t=0;
while(ch<'0'||ch>'9') t|=ch=='-',ch=gh();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=gh();
if(t) x=-x;
}
int N,M,Idx,Vis[MAXN];
struct Splay
{
int chi[2],fa;
int size,val;
}Tree[MAXN];
int Root,Qx;
char op;
inline void underPushUp(int x)
{
Tree[x].size=Tree[Tree[x].chi[0]].size+Tree[Tree[x].chi[1]].size+1;
}
inline void underRotate(int x)
{
int y=Tree[x].fa,z=Tree[y].fa;
int k=Tree[y].chi[1]==x;
Tree[z].chi[Tree[z].chi[1]==y]=x;
Tree[x].fa=z;
Tree[y].chi[k]=Tree[x].chi[k^1];
Tree[Tree[x].chi[k^1]].fa=y;
Tree[x].chi[k^1]=y;
Tree[y].fa=x;
underPushUp(y);
underPushUp(x);
}
inline void Splay(int x,int k)
{
while(Tree[x].fa!=k)
{
int y=Tree[x].fa,z=Tree[y].fa;
if(z!=k)
if((Tree[y].chi[1]==x)^(Tree[z].chi[1]==y))
underRotate(x);
else
underRotate(y);
underRotate(x);
}
if(!k) Root=x;
}
inline void underInsert(int x)
{
int u=Root,p;
while(u)
{
p=u;
u=Tree[u].chi[x>Tree[u].val];
}
u=++Idx;
if(p) Tree[p].chi[x>Tree[p].val]=u;
Tree[u].val=x;
Tree[u].size=1;
Tree[u].fa=p;
Splay(u,0);
}
inline void underInit()
{
Root=++Idx;
Tree[Root].fa=0;
Tree[Root].val=0;
Tree[Root].size=1;
underInsert(N+1);
}
inline int underGetPre(int x)
{
int u=Root,res,v=Root;
while(u)
{
v=u;
if(Tree[u].val<x)
{
res=u;
u=Tree[u].chi[1];
}
else u=Tree[u].chi[0];
}
Splay(v,0);
return res;
}
inline int underGetNxt(int x)
{
int u=Root,res,v=Root;
while(u)
{
v=u;
if(Tree[u].val>x)
{
res=u;
u=Tree[u].chi[0];
}
else u=Tree[u].chi[1];
}
Splay(v,0);
return res;
}
inline void underErase(int x)
{
int l=underGetPre(x),r=underGetNxt(x);
Splay(l,0),Splay(r,l);
Tree[Tree[r].chi[0]].size=0;
Tree[Tree[r].chi[0]].fa=0;
Tree[r].chi[0]=0;
Splay(r,0);
}
int top,S[MAXN];
int main()
{
// freopen("splay.in","r",stdin);
// freopen("splay.out","w",stdout);
underRead(N),underRead(M);
underInit();
for(int i=1;i<=M;++i)
{
cin>>op;
if(op=='D')
{
underRead(Qx);
underInsert(Qx);
S[++top]=Qx;
Vis[Qx]=1;
}
else if(op=='R')
{
Vis[S[top]]=0;
underErase(S[top--]);
}
else if(op=='Q')
{
underRead(Qx);
if(Vis[Qx]) printf("0\n");
else printf("%d\n",Tree[underGetNxt(Qx)].val-Tree[underGetPre(Qx)].val-1);
}
}
return 0;
}
/*
7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
*/