平衡树系列终于完结,撒花

裸的树套树,扔代码跑

  1 const mo=20306789;
  2 var w,b,s,key,fa:array[0..4000010] of longint;
  3     son:array[0..4000010,1..2] of longint;
  4     a,root:array[0..60010*4] of longint;
  5     i,n,m,x,y,k,ch,ans,t:longint;
  6 
  7 function max(a,b:longint):longint;
  8   begin
  9     if a>b then exit(a) else exit(b);
 10   end;
 11 
 12 function min(a,b:longint):longint;
 13   begin
 14     if a>b then exit(b) else exit(a);
 15   end;
 16 
 17 procedure open(x:longint);
 18   begin
 19     inc(t);
 20     b[t]:=x;
 21     w[t]:=1;  s[t]:=1;
 22     key[t]:=trunc(random*mo)+1;
 23   end;
 24 
 25 procedure update(x:longint);
 26   begin
 27     s[x]:=s[son[x,1]]+s[son[x,2]]+w[x];
 28   end;
 29 
 30 procedure rotate(i,x,w:longint);
 31   var y:longint;
 32   begin
 33     y:=fa[x];
 34     if fa[y]<>0 then
 35     begin
 36       if son[fa[y],1]=y then son[fa[y],1]:=x
 37       else son[fa[y],2]:=x;
 38     end
 39     else root[i]:=x;
 40     fa[x]:=fa[y];
 41     son[y,3-w]:=son[x,w];
 42     if son[x,w]<>0 then fa[son[x,w]]:=y;
 43     son[x,w]:=y;
 44     fa[y]:=x;
 45     update(y);
 46     update(x);
 47   end;
 48 
 49 procedure up(i,x:longint);
 50   var y:longint;
 51   begin
 52     y:=fa[x];
 53     while (y<>0) and (key[y]>key[x]) do
 54     begin
 55       if son[y,1]=x then rotate(i,x,2)
 56       else rotate(i,x,1);
 57       y:=fa[x];
 58     end;
 59   end;
 60 
 61 procedure add(i,x:longint);
 62   var p:longint;
 63   begin
 64     if root[i]=0 then
 65     begin
 66       open(x);
 67       root[i]:=t;
 68     end
 69     else begin
 70       p:=root[i];
 71       repeat
 72         inc(s[p]);
 73         if b[p]=x then
 74         begin
 75           inc(w[p]);
 76           exit;
 77         end
 78         else if b[p]>x then
 79         begin
 80           if son[p,1]=0 then break;
 81           p:=son[p,1];
 82         end
 83         else begin
 84           if son[p,2]=0 then break;
 85           p:=son[p,2];
 86         end;
 87       until false;
 88       open(x);
 89       fa[t]:=p;
 90       if b[p]>x then son[p,1]:=t else son[p,2]:=t;
 91       up(i,t);
 92     end;
 93   end;
 94 
 95 procedure sift(i,x:longint);
 96   var j1,j2:longint;
 97   begin
 98     repeat
 99       j1:=son[x,1]; j2:=son[x,2];
100       if j1+j2=0 then break;
101       if (j2<>0) and ((key[j1]>key[j2]) or (j1=0)) then
102       begin
103         rotate(i,j2,1);
104         dec(s[j2]);
105       end
106       else begin
107         rotate(i,j1,2);
108         dec(s[j1]);
109       end;
110     until false;
111     if son[fa[x],1]=x then son[fa[x],1]:=0 else son[fa[x],2]:=0;
112     fa[x]:=0; w[x]:=0; s[x]:=0;
113   end;
114 
115 procedure del(i,x:longint);
116   var p:longint;
117   begin
118     p:=root[i];
119     repeat
120       dec(s[p]);
121       if b[p]=x then
122       begin
123         if w[p]=1 then sift(i,p)
124         else dec(w[p]);
125         break;
126       end
127       else if b[p]>x then p:=son[p,1]
128       else p:=son[p,2];
129     until false;
130   end;
131 
132 procedure build(i,l,r,x:longint);
133   var m:longint;
134   begin
135     add(i,a[x]);
136     if l<>r then
137     begin
138       m:=(l+r) shr 1;
139       if x<=m then build(i*2,l,m,x)
140       else build(i*2+1,m+1,r,x);
141     end;
142   end;
143 
144 procedure change(i,l,r,x,y:longint);
145   var m:longint;
146   begin
147     add(i,y);
148     del(i,a[x]);
149     if l<>r then
150     begin
151       m:=(l+r) shr 1;
152       if x<=m then change(i*2,l,m,x,y)
153       else change(i*2+1,m+1,r,x,y);
154     end;
155   end;
156 
157 function rank(i,x:longint):longint;
158   var p:longint;
159   begin
160     p:=root[i];
161     rank:=0;
162     while p<>0 do
163     begin
164       if b[p]=x then
165       begin
166         rank:=rank+s[son[p,1]];
167         break;
168       end
169       else if b[p]>x then p:=son[p,1]
170       else begin
171         rank:=rank+s[son[p,1]]+w[p];
172         p:=son[p,2];
173       end;
174     end;
175   end;
176 
177 procedure getrank(i,l,r,k:longint);
178   var m:longint;
179   begin
180     if (x<=l) and (y>=r) then ans:=ans+rank(i,k)
181     else begin
182       m:=(l+r) shr 1;
183       if x<=m then getrank(i*2,l,m,k);
184       if y>m then getrank(i*2+1,m+1,r,k);
185     end;
186   end;
187 
188 function what(k:longint):longint;
189   var l,r,m:longint;
190   begin
191     l:=0;
192     r:=100000000;
193     what:=0;
194     while l<=r do
195     begin
196       m:=(l+r) shr 1;
197       ans:=0; getrank(1,1,n,m);
198       if ans<=k then
199       begin
200         what:=m;
201         l:=m+1;
202       end
203       else r:=m-1;
204     end;
205   end;
206 
207 procedure pre(i,x:longint);
208   var p:longint;
209   begin
210     p:=root[i];
211     while p<>0 do
212     begin
213       if b[p]>=x then p:=son[p,1]
214       else begin
215         ans:=max(ans,b[p]);
216         p:=son[p,2];
217       end;
218     end;
219   end;
220 
221 procedure suffix(i,x:longint);
222   var p:longint;
223   begin
224     p:=root[i];
225     while p<>0 do
226     begin
227       if b[p]<=x then p:=son[p,2]
228       else begin
229         ans:=min(ans,b[p]);
230         p:=son[p,1];
231       end;
232     end;
233   end;
234 
235 procedure askpre(i,l,r:longint);
236   var m:longint;
237   begin
238     if (x<=l) and (y>=r) then pre(i,k)
239     else begin
240       m:=(l+r) shr 1;
241       if x<=m then askpre(i*2,l,m);
242       if y>m then askpre(i*2+1,m+1,r);
243     end;
244   end;
245 
246 procedure asksuf(i,l,r:longint);
247   var m:longint;
248   begin
249     if (x<=l) and (y>=r) then suffix(i,k)
250     else begin
251       m:=(l+r) shr 1;
252       if x<=m then asksuf(i*2,l,m);
253       if y>m then asksuf(i*2+1,m+1,r);
254     end;
255   end;
256 
257 begin
258   randomize;
259   readln(n,m);
260   for i:=1 to n do
261     read(a[i]);
262   for i:=1 to n do
263     build(1,1,n,i);
264   for i:=1 to m do
265   begin
266     read(ch);
267     if ch=1 then
268     begin
269       readln(x,y,k);
270       ans:=0; getrank(1,1,n,k);
271       writeln(ans+1);
272     end
273     else if ch=2 then
274     begin
275       readln(x,y,k);
276       writeln(what(k-1));
277     end
278     else if ch=3 then
279     begin
280       readln(x,k);
281       change(1,1,n,x,k);
282       a[x]:=k;
283     end
284     else if ch=4 then
285     begin
286       readln(x,y,k);
287       ans:=0; askpre(1,1,n);
288       writeln(ans);
289     end
290     else if ch=5 then
291     begin
292       readln(x,y,k);
293       ans:=100000000; asksuf(1,1,n);
294       writeln(ans);
295     end;
296   end;
297 end.
View Code

 

posted on 2015-05-09 17:48  acphile  阅读(210)  评论(0编辑  收藏  举报