[BZOJ1251] 序列终结者

传送门

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

题目大意

支持
1.区间+-
2.区间翻转
3.查询区间最大值

题解

Splay模板题
注意+-标记的下放
这个节点打上+-标记表明该节点的权值和最值已被处理过

const
 maxn=100005;
var
 w:array[-1..maxn,1..8]of longint;
 ans:array[0..maxn]of longint;
 i,j,k:longint;
 n,m,sum,root,a,b,c,d:longint;
procedure pushdown(a:longint);
var c:longint;
begin
 if w[a,7]<>0 then begin
  c:=w[a,1]; w[a,1]:=w[a,2]; w[a,2]:=c;
  w[a,7]:=0; w[w[a,1],7]:=w[w[a,1],7] xor 1; w[w[a,2],7]:=w[w[a,2],7] xor 1;
 end;
 if w[a,8]<>0 then begin
  if w[a,1]<>-1 then begin inc(w[w[a,1],8],w[a,8]); inc(w[w[a,1],5],w[a,8]); inc(w[w[a,1],6],w[a,8]); end;
  if w[a,2]<>-1 then begin inc(w[w[a,2],8],w[a,8]); inc(w[w[a,2],5],w[a,8]); inc(w[w[a,2],6],w[a,8]); end;
  w[a,8]:=0;
 end;
end;

function max(a,b:longint):longint;
begin
 if a>b then exit(a) else exit(b);
end;

procedure rotate(a,kind:longint);
var b,unkind:longint;
begin
 pushdown(b); pushdown(a);
 b:=w[a,3]; unkind:=kind xor 3;
 w[a,4]:=w[b,4]; dec(w[b,4],1+w[w[a,kind],4]);
 w[w[a,unkind],3]:=b; w[b,kind]:=w[a,unkind];
 w[a,unkind]:=b; w[a,3]:=w[b,3]; 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;
 w[b,5]:=max(max(w[w[b,1],5],w[w[b,2],5]),w[b,6]);
 w[a,5]:=max(max(w[w[a,1],5],w[w[a,2],5]),w[a,6]);
end;

procedure splay(a,goal:longint);
var kind,b,unkind:longint;
begin
 while w[a,3]<>goal do
  begin
   b:=w[a,3]; if w[b,1]=a then kind:=1 else kind:=2; unkind:=kind xor 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 init(a:longint);
var tt,fa:longint;
begin
 tt:=root;
 while tt<>-1 do
  begin fa:=tt; inc(w[tt,4]); tt:=w[tt,2]; end;
 inc(sum); w[sum,1]:=-1; w[sum,2]:=-1; w[sum,3]:=fa; w[sum,4]:=1; w[sum,5]:=0; w[sum,6]:=0; w[sum,7]:=0; w[sum,8]:=0;
 if a=n+1 then w[sum,6]:=-maxlongint; splay(sum,-1);
end;

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

begin
 readln(n,m); sum:=1; root:=1; w[-1,5]:=-maxlongint; w[-1,6]:=-maxlongint;
 w[1,1]:=-1; w[1,2]:=-1; w[1,3]:=-1; w[1,4]:=1; w[1,5]:=0; w[1,6]:=-maxlongint; w[1,7]:=0; w[1,8]:=0;
 for i:=1 to n+1 do
  init(i);
 ans[0]:=0;
 for i:=1 to m do
  begin
   read(a);
   case a of
   1:begin readln(b,c,d); splay(getkth(b),-1); splay(getkth(c+2),root);
       inc(w[w[w[root,2],1],8],d); inc(w[w[w[root,2],1],5],d); inc(w[w[w[root,2],1],6],d);
   end;
   2:begin readln(b,c); splay(getkth(b),-1); splay(getkth(c+2),root);
       w[w[w[root,2],1],7]:=w[w[w[root,2],1],7] xor 1;
   end;
   3:begin readln(b,c); splay(getkth(b),-1); splay(getkth(c+2),root);
       pushdown(w[w[root,2],1]);
       inc(ans[0]); ans[ans[0]]:=w[w[w[root,2],1],5];
   end;
   end;
  end;
 for i:=1 to ans[0] do
  writeln(ans[i]);
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值