[bzoj1503][NOI2004]郁闷的出纳员

来源:互联网 发布:淘宝网充值中心在哪 编辑:程序博客网 时间:2024/06/03 02:19

本题依然是一道数据结构题,对本题而言,依然可以用splay来解。

本题的一个难点(对于我这个弱菜而言)是如何动态的改变平衡树中的值。其实我们可以借鉴线段树中的lazy tag的思想,因为若要变动则是整棵树一起变,所以我们可以开设一个全局变量delta表示此时对整棵树的改变值,这样一来,在每次插入节点x时,我们把x-delta插入平衡树中(想想这是为什么,不难),然后检查时若此时的节点y满足y+delta<min则说明工资低于最低值,删除这个点和其左子树。

最后就是要注意细节,首先在插入时可能一开始工资就低于min,此时是不需要把他算入离开公司员工数的。还有就是删除时,要找到真正的第一个y+delta<min的值。

program chashier;constMAXN=100000;typedate=recordch:array[0..1]of longint;v,f,sz:longint;end;varn,min,i,top,sum,root,delta,tst,x:longint;c:char;st:array[1..MAXN]of longint;t:array[0..MAXN+10]of date;procedure newnode(var x:longint;f,k:longint);beginif(tst<>0)then begin x:=st[tst];dec(tst) endelse begin inc(top);x:=top; end;t[x].ch[0]:=0;t[x].ch[1]:=0;t[x].v:=k;t[x].f:=f;t[x].sz:=1;end;function cmp(x,k:longint):longint;beginif(t[x].ch[0]=k)then exit(0) else exit(1);end;procedure pushup(k:longint);begint[k].sz:=1+t[t[k].ch[1]].sz+t[t[k].ch[0]].sz;end;procedure rotate(k,c:longint);var i:longint;begini:=t[k].f;t[i].ch[c xor 1]:=t[k].ch[c];t[t[k].ch[c]].f:=i;t[k].ch[c]:=i;t[k].f:=t[i].f;if(t[i].f<>0)then t[t[i].f].ch[cmp(t[i].f,i)]:=k;t[i].f:=k;pushup(i);end;procedure splay(k,goal:longint);var x,y,f1,f2:longint;beginwhile(t[k].f<>goal)dobeginx:=t[k].f;y:=t[x].f;if(y=goal)then rotate(k,cmp(x,k)xor 1)else beginf1:=cmp(y,x);f2:=cmp(x,k);if(f1=f2)then begin rotate(x,f1 xor 1);rotate(k,f1 xor 1) endelse begin rotate(k,f2 xor 1);rotate(k,f1 xor 1) end;end;end;pushup(k);if(goal=0)then root:=k;end;procedure insert(k:longint);var i,j,x:longint;beginif(root=0)thenbeginnewnode(root,0,k);exit;end;j:=0;i:=root;repeatj:=i;if(k<t[i].v)then i:=t[i].ch[0] else i:=t[i].ch[1];until i=0;if(k<t[j].v)then x:=0 else x:=1;newnode(t[j].ch[x],j,k);splay(t[j].ch[x],0);end;function find(k:longint):longint;var i:longint;begini:=root;while(t[t[i].ch[1]].sz<>k)dobeginif(k<t[t[i].ch[1]].sz)then i:=t[i].ch[1]else begink:=k-t[t[i].ch[1]].sz-1;i:=t[i].ch[0];end;end;splay(i,0);exit(t[i].v+delta);end;procedure del(k:longint);beginif(k=0)then exit;inc(sum);inc(tst);st[tst]:=k;del(t[k].ch[0]);del(t[k].ch[1])end;function found(k:longint):longint;var i:longint;beginif(k=0)then exit(0);if(t[k].v+delta<min)thenbegini:=found(t[k].ch[1]);if(i=0)then exit(k) else exit(i);end;exit(found(t[k].ch[0]));end;procedure update;var x:longint;beginx:=found(root);if(x<>0)thenbeginsplay(x,0);if(t[x].ch[1]=0)thenbegindelta:=0;root:=0;del(x);endelse beginroot:=t[root].ch[1];t[root].f:=0;t[x].ch[1]:=0;del(x);end;end;end;procedure init;beginsum:=0;delta:=0;tst:=0;top:=0;root:=0;end;beginreadln(n,min);init;for i:=1 to n dobeginreadln(c,x);if(c='S')and(root<>0)then begin delta:=delta-x;update; end;if(c='A')and(root<>0)then delta:=delta+x;if(c='I')and(x>=min)then insert(x-delta);if(c='F')thenbeginif(t[root].sz<x)then writeln(-1) else writeln(find(x-1));end;end;writeln(sum);end.


原创粉丝点击