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

传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=1503

题目大意

给定m,支持
I x:插入x(若x< m则不插入)
S x:对全体权值-x,若更新后的权值小于m则踢出
A x:对全体权值+x
F x:查询第k大
最后输出踢出的人数(不包括插入时小于m的)

题解

平衡树模板题

const
 maxn=300005;
var
 w:array[-1..maxn,1..7]of longint;
 i,j,k:longint;
 n,m,sum,a,root,tail:longint;
 cha:char;
procedure rotate(a,kind:longint);
var b,unkind:longint;
begin
 unkind:=kind xor 3; b:=w[a,3];
 w[a,4]:=w[b,4]; dec(w[b,4],w[a,5]+w[w[a,kind],4]);
 w[w[a,unkind],3]:=b; w[b,kind]:=w[a,unkind];
 w[a,3]:=w[b,3]; w[a,unkind]:=b;
 w[b,3]:=a;
 if w[a,3]<>-1
 then
  if w[w[a,3],1]=b
  then w[w[a,3],1]:=a
  else w[w[a,3],2]:=a;
end;

procedure splay(a,goal:longint);
var b,kind,unkind:longint;
begin
 while w[a,3]<>goal do
  begin
   if w[w[a,3],1]=a then kind:=1 else kind:=2;
   unkind:=kind xor 3; b:=w[a,3];
   if w[b,3]=goal then rotate(a,kind)
   else
    if w[w[b,3],kind]=b
    then begin rotate(b,kind); rotate(a,kind); end
    else begin rotate(a,kind); rotate(a,unkind); end;
  end;
 if goal=-1 then root:=a;
end;

procedure pushdown(a:longint);
begin
 if (w[a,6]<>1000000007)and(w[a,6]<>-1000000007) then inc(w[a,6],w[a,7]);
 if w[a,1]<>-1 then inc(w[w[a,1],7],w[a,7]);
 if w[a,2]<>-1 then inc(w[w[a,2],7],w[a,7]);
 w[a,7]:=0;
end;

procedure init(a:longint);
var tt,fa,kind:longint;
begin
 tt:=root;
 while tt<>-1 do
  begin
   if w[tt,7]<>0 then pushdown(tt);
   inc(w[tt,4]); fa:=tt;
   if w[tt,6]=a then break;
   if a<w[tt,6]
   then begin tt:=w[tt,1]; kind:=1; end
   else begin tt:=w[tt,2]; kind:=2; end;
  end;
 if tt<>-1
 then begin inc(w[tt,5]); splay(tt,-1); end
 else begin inc(tail); w[tail,1]:=-1; w[tail,2]:=-1; w[tail,3]:=fa; w[tail,4]:=1; w[tail,5]:=1; w[tail,6]:=a; w[tail,7]:=0; w[fa,kind]:=tail; splay(tail,-:span class="hljs-number">1); end;
end;

function getkth(k:longint):longint;
var tt:longint;
begin
 tt:=root;
 while (k<=w[w[tt,1],4])or(k>=w[w[tt,1],4]+w[tt,5]+1) do
  begin
   if w[tt,7]<>0 then pushdown(tt);
   if k<=w[w[tt,1],4]
   then tt:=w[tt,1]
   else begin k:=k-w[w[tt,1],4]-w[tt,5]; tt:=w[tt,2]; end;
  end;
 pushdown(tt);
 exit(w[tt,6]);
end;

begin
 readln(n,m); sum:=0; root:=2; tail:=2;
 w[1,1]:=-1; w[1,2]:=-1; w[1,3]:=2; w[1,4]:=1; w[1,5]:=1; w[1,6]:=-1000000007; w[1,7]:=0;
 w[2,1]:=1; w[2,2]:=-1; w[2,3]:=-1; w[2,4]:=2; w[2,5]:=1; w[2,6]:=1000000007; w[2,7]:=0;
 for i:=1 to n do
  begin
   readln(cha,a);
   case cha of
   'I':begin if a>=m then init(a); end;
   'A':begin inc(w[root,7],a); end;
   'S':begin inc(w[root,7],-a); init(m); inc(sum,w[w[root,1],4]-1);
   if w[root,5]=1
   then begin root:=w[root,2]; w[root,3]:=-1; init(-1000000007); end
   else begin w[root,4]:=w[w[root,2],4]+w[root,5]-1; dec(w[root,5]); w[root,1]:=-1; init(-1000000007); end;
   end;
   'F':begin if w[root,4]-2<a then writeln(-1)
   else writeln(getkth(w[root,4]-a));
   end;
   end;
  end;
 writeln(sum);
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值