bzoj 1503 郁闷的出纳员

Splay练手题,真正做起来不那么简单,虽然是简单的单点维护,但是删除的时候会比较恶心(吐槽一下,我就是因为一上来用这个题splay练手,调了半天没调出来,气死我了)。

友情提示:刚进公司就被开的不算在最后的答案里(这公司真冷酷......)

Teller
  1 const
  2 maxn=300000;
  3 var
  4 c:array[0..maxn,0..1]of longint;
  5 fa,size,key:array[0..maxn]of longint;
  6 n,i,a,limit,now,num,tot,sum,rot,tmp:longint;
  7 sign:char;
  8 procedure update(x:Longint);
  9 begin
 10 size[x]:=size[c[x,0]]+size[c[x,1]]+1;
 11 end;
 12 
 13 procedure rotate(var rot:longint;x:Longint);
 14 var
 15 y,z,p,q:longint;
 16 begin
 17 y:=fa[x]; z:=fa[y];
 18 if c[y,0]=x then p:=0 else p:=1;
 19 q:=p xor 1;
 20 if y=rot then rot:=x else
 21 if c[z,0]=y then c[z,0]:=x else c[z,1]:=x;
 22 fa[x]:=z; fa[y]:=x; fa[c[x,q]]:=y;
 23 c[y,p]:=c[x,q]; c[x,q]:=y;
 24 update(y); update(x);
 25 end;
 26 
 27 procedure splay(var rot:longint;x:longint);
 28 var
 29 y,z:longint;
 30 begin
 31 while x<>rot do
 32 begin
 33 y:=fa[x]; z:=fa[y];
 34 if y<>rot then
 35 if (c[y,0]=x)xor(c[z,0]=y) then rotate(rot,x) else rotate(rot,y);
 36 rotate(rot,x);
 37 end;
 38 end;
 39 
 40 procedure insert(var t:longint; x,p:longint);
 41 begin
 42 if t=0 then
 43 begin
 44 inc(num);
 45 t:=num;
 46 key[t]:=x;
 47 fa[t]:=p;
 48 size[t]:=1;
 49 splay(rot,t);
 50 end
 51 else
 52 if x<key[t] then
 53 insert(c[t,0],x,t)
 54 else
 55 insert(c[t,1],x,t);
 56 end;
 57 
 58 procedure delete(x:longint);
 59 var
 60 t,p:longint;
 61 begin
 62 p:=rot; t:=0;
 63 while p>0 do
 64 begin
 65 if x<=key[p] then
 66 begin
 67 t:=p;
 68 p:=c[p,0]
 69 end
 70 else
 71 p:=c[p,1];
 72 end;
 73 if t=0 then
 74 rot:=0
 75 else
 76 begin
 77 splay(rot,t);
 78 c[t,0]:=0;
 79 fa[t]:=0;
 80 update(t);
 81 end;
 82 end;
 83 
 84 function find(t,k:longint):longint;
 85 begin
 86 if k>size[t] then exit(-1);
 87 if size[c[t,1]]+1=k then exit(t);
 88 if size[c[t,1]]+1>k then exit(find(c[t,1],k));
 89 if size[c[t,1]]+1<k then exit(find(c[t,0],k-size[c[t,1]]-1));
 90 end;
 91 
 92 begin
 93 //assign(input,'cashier.in'); reset(input);
 94 //assign(output,'cashier.out'); rewrite(output);
 95 readln(n,limit);
 96 now:=0; rot:=0; num:=0;
 97 for i:=1 to n do
 98 begin
 99 readln(sign,a);
100 case sign of
101 'I':
102 if a>=limit then
103 insert(rot,a-now,0);
104 'A':inc(now,a);
105 'S':
106 begin
107 dec(now,a);
108 delete(limit-now);
109 end;
110 'F':
111 begin
112 tmp:=find(rot,a);
113 if tmp=-1 then writeln(-1)
114 else writeln(key[tmp]+now);
115 end;
116 end;
117 end;
118 update(rot);
119 writeln(num-size[rot]);
120 //close(input);
121 //close(output);
122 end.
123 
124  
125 
126  

 

转载于:https://www.cnblogs.com/zig-zag/archive/2013/03/28/2987377.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值