bzoj 1208 宠物收养所
这个题也是单点维护,不管来的是人还是狗,只要num=0就插入,否则就删除,想清楚了就很好写。
Pets
1 const 2 maxn=100000; 3 ms=1000000; 4 var 5 c:array[0..maxn,0..1]of longint; 6 size,fa,key:array[0..maxn]of longint; 7 i,n,a,b,tot,sum,num,now,ans,tmp,rot:longint; 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 21 else 22 if y=c[z,0] then c[z,0]:=x else c[z,1]:=x; 23 fa[x]:=z; fa[y]:=x; fa[c[x,q]]:=y; 24 c[y,p]:=c[x,q]; c[x,q]:=y; 25 update(y); update(x); 26 end; 27 28 procedure splay(var rot:longint; x:longint); 29 var 30 y,z:longint; 31 begin 32 while x<>rot do 33 begin 34 y:=fa[x]; z:=fa[y]; 35 if y<>rot then 36 if (c[y,0]=x)xor(c[z,0]=y) then 37 rotate(rot,x) 38 else rotate(rot,y); 39 rotate(rot,x); 40 end; 41 end; 42 43 procedure find(t,x:longint); 44 begin 45 if t=0 then exit; 46 if ( abs(key[t]-x)<abs(key[tmp]-x) ) 47 or ( (abs(key[t]-x)=abs(key[tmp]-x))and(key[tmp]>key[t]) ) then 48 tmp:=t; 49 if (x<key[t])and(c[t,0]<>0) then find(c[t,0],x) 50 else 51 if (x>key[t])and(c[t,1]<>0) then find(c[t,1],x); 52 end; 53 54 function getmax(x:longint):longint; 55 begin 56 if c[x,1]=0 then exit(x) else exit(getmax(c[x,1])); 57 end; 58 59 procedure insert(var t:longint;x,anc:longint); 60 begin 61 if t=0 then 62 begin 63 inc(num); 64 t:=num; 65 c[t,0]:=0;c[t,1]:=0; 66 key[t]:=x; 67 fa[t]:=anc; 68 splay(rot,t); 69 end 70 else 71 if x<key[t] then insert(c[t,0],x,t) 72 else 73 if x>key[t] then insert(c[t,1],x,t); 74 end; 75 76 procedure delete(var t:longint); 77 var 78 p:longint; 79 begin 80 if (c[t,0]=0)and(c[t,1]=0) then t:=0 81 else 82 if (c[t,0]<>0)and(c[t,1]=0) then 83 begin 84 fa[c[t,0]]:=0; 85 t:=c[t,0]; 86 end 87 else 88 if (c[t,1]<>0)and(c[t,0]=0) then 89 begin 90 fa[c[t,1]]:=0; 91 t:=c[t,1]; 92 end 93 else 94 begin 95 p:=getmax(c[t,0]); 96 splay(c[t,0],p); 97 c[p,1]:=c[t,1]; 98 fa[c[t,1]]:=p; 99 fa[p]:=0; 100 c[t,0]:=0; c[t,1]:=0; fa[t]:=0; 101 t:=p; 102 end; 103 end; 104 105 begin 106 readln(n); 107 for i:=1 to n do 108 begin 109 readln(a,b); 110 if (sum=0)or(a=now) then 111 begin 112 insert(rot,b,0); 113 inc(sum); 114 now:=a; 115 end 116 else 117 begin 118 tmp:=rot; 119 find(rot,b); 120 ans:=(ans+abs(key[tmp]-b)) mod ms; 121 splay(rot,tmp); 122 delete(rot); 123 dec(sum); 124 end; 125 end; 126 writeln(ans); 127 end.
AC without art, no better than WA !