BZOJ 3196 二x平衡树

Link

Solution

外层区间线段树,内层平衡树存数值。K值用到二分答案,比较经典。 之前写过,为了试试新的代码写法,写了一遍。结果Treap旋转的时候没有更新信息,WA了一次。

Code

treap在trans的时候没有pushup

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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//Code by Lucida
#include<bits/stdc++.h>
#define get(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
//#define debug(x) std::cout<<#x<<'='<<x<<std::endl
template <class T> inline bool chkmx(T &a,const T &b){return a<b?a=b,1:0;}
template <class T> inline bool chkmn(T &a,const T &b){return a>b?a=b,1:0;}
const int MAXN=50000+10,INF=0x1f1f1f1f;
using std::max;
using std::min;
namespace IO
{
#ifndef get
const int INBUF=1e6;
char in[INBUF],*iptr=in,*iend=in;
#define getc() ( (iptr==iend && (iend=(iptr=in)+fread(in,1,INBUF,stdin),iptr==iend))?EOF:*iptr++ )
inline void get(int &x)
{
static int f,ch;
f=1;
while(!isdigit(ch=getc())) if(ch=='-') f=-1;
x=ch-'0';
while(isdigit(ch=getc())) (x*=10)+=ch-'0';
x*=f;
}
#endif
#ifndef put
const int OUTBUF=1e6;
char out[OUTBUF],*optr=out,*oend=out+OUTBUF-1;
#define putc(x) (optr==oend?(fwrite(out,1,optr-out,stdout),optr=out,*optr++=(x)):*optr++=(x))
inline void put(int x)
{
if(x==0) putc('0');
if(x<0) putc('-'),x=-x;
if(x)
{
static int stack[100],*top=stack;
while(x) *++top=x%10,x/=10;
while(top!=stack) putc((*top--)+'0');
}
putc('\n');
}
#endif
void flush()
{
#ifndef put
fwrite(out,1,optr-out,stdout);
#endif
}
}
using namespace IO;

namespace Treap
{
const int P=1e5+7;
struct Node
{
Node *son[2];
int v,py,sz,cnt;
Node(){py=P;sz=0;}
void up(){sz=cnt+son[0]->sz+son[1]->sz;}
int LowCount(int);
int Pred(int);
int Succ(int);
}*null=new Node;

void out(Node *pos)
{
if(pos==null) return;
out(pos->son[0]);printf("%d(%d) ",pos->v,pos->cnt);out(pos->son[1]);
}

Node *newNode(int v)
{
static const int SIZE=MAXN*30;
static Node *ME=(Node*)malloc(SIZE*sizeof(Node)),*cur;
cur=ME++;cur->v=v;cur->py=rand()%P;cur->cnt=cur->sz=1;
cur->son[0]=cur->son[1]=null;
return cur;
}
void trans(Node *&pos,int d)
{
Node *s=pos->son[d];
pos->son[d]=s->son[!d];s->son[!d]=pos;
pos->up(),s->up();
pos=s;
}
int adjust(Node *&pos)
{
int d=pos->son[0]->py>pos->son[1]->py;
if(pos->py>pos->son[d]->py) trans(pos,d),d^=1;
else d=-1;return d;
}
void Insert(Node *&pos,int v)
{
if(pos==null) pos=newNode(v);
else if(pos->v==v) pos->cnt++,pos->up();
else
{
Insert(pos->son[pos->v<v],v),pos->up();
adjust(pos);
}
}
int Node::LowCount(int v)
{
Node *pos=this;int res=0;
while(pos!=null)
{
if(pos->v<v) res+=pos->son[0]->sz+pos->cnt,pos=pos->son[1];
else pos=pos->son[0];
}
return res;
}
int Node::Pred(int v)
{
Node *pos=this;int res=-INF;
while(pos!=null)
{
if(pos->v<v) chkmx(res,pos->v),pos=pos->son[1];
else pos=pos->son[0];
}
return res;
}
int Node::Succ(int v)
{
Node *pos=this;int res=INF;
while(pos!=null)
{
if(pos->v>v) chkmn(res,pos->v),pos=pos->son[0];
else pos=pos->son[1];
}
return res;
}
void DelNode(Node *&pos)
{
int d=adjust(pos);
if(~d)
{
assert(pos!=null);
DelNode(pos->son[d]),pos->up();
}
else pos=null;
}
void Delete(Node *&pos,int v)
{
if(pos->v==v)
{
pos->cnt--,pos->up();
if(!pos->cnt)
pos->py=P,DelNode(pos);
}
else
{
Delete(pos->son[pos->v<v],v),pos->up();
assert(adjust(pos)==-1);
}
}
}
using namespace Treap;
struct Data
{
int cnt,pred,succ;
Data(int cnt=0,int pred=-INF,int succ=INF):cnt(cnt),pred(pred),succ(succ){}
}nData;
Data Merge(Data a,Data b){return Data(a.cnt+b.cnt,max(a.pred,b.pred),min(a.succ,b.succ));}
namespace Seg
{
typedef Treap::Node tNode;
struct Node
{
Node *son[2];
tNode *treap;
}*null=new Node,*root=null;int TL,TR;
Node *newNode()
{
static int SIZE=MAXN<<2;
static Node *ME=(Node*)malloc(SIZE*sizeof(Node)),*cur;
cur=ME++;cur->son[0]=cur->son[1]=null;cur->treap=Treap::null;
return cur;
}
void Add(Node *&pos,int L,int R,int goal,int v)
{
if(pos==null) pos=newNode();
Insert(pos->treap,v);
if(L!=R)
{
int MID=(L+R)>>1;
if(goal<=MID) Add(pos->son[0],L,MID,goal,v);
else Add(pos->son[1],MID+1,R,goal,v);
}
}
Data Access(Node *pos,int L,int R,int l,int r,int v)
{
if(L==l && R==r) return Data(pos->treap->LowCount(v),pos->treap->Pred(v),pos->treap->Succ(v));
else
{
int MID=(L+R)>>1;
if(r<=MID) return Access(pos->son[0],L,MID,l,r,v);
else if(MID+1<=l) return Access(pos->son[1],MID+1,R,l,r,v);
else return Merge(Access(pos->son[0],L,MID,l,MID,v),Access(pos->son[1],MID+1,R,MID+1,r,v));
}
}
void Edit(Node *&pos,int L,int R,int goal,int ori,int v)
{
Delete(pos->treap,ori);
Insert(pos->treap,v);
if(L!=R)
{
int MID=(L+R)>>1;
if(goal<=MID) Edit(pos->son[0],L,MID,goal,ori,v);
else Edit(pos->son[1],MID+1,R,goal,ori,v);
}
}
}
using namespace Seg;
int a[MAXN];
int Kth(int l,int r,int K)
{
int L=0,R=1e8,res;K--;
while(L!=R)
{
int MID=(L+R+1)>>1;
if((res=Access(root,TL,TR,l,r,MID).cnt)<=K) L=MID;
else R=MID-1;
}
return L;
}
int main()
{
freopen("input","r",stdin);
int n,m;get(n),get(m);TL=1,TR=n;
for(int i=1;i<=n;i++) get(a[i]),Add(root,TL,TR,i,a[i]);
for(int i=1;i<=m;i++)
{
int opt,pos,l,r,k;
get(opt);
if(opt==3) get(pos),get(k);
else get(l),get(r),get(k);
switch(opt)
{
case 1:put(Access(root,TL,TR,l,r,k).cnt+1);break;
case 2:put(Kth(l,r,k));break;
case 3:Edit(root,TL,TR,pos,a[pos],k);a[pos]=k;break;
case 4:put(Access(root,TL,TR,l,r,k).pred);break;
case 5:put(Access(root,TL,TR,l,r,k).succ);break;
}
}
flush();
return 0;
}
/* AC Record(Bugs) *
* WA trans pushup
* * * * * * * * * */