BZOJ2843:极地旅行社
生活随笔
收集整理的這篇文章主要介紹了
BZOJ2843:极地旅行社
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Description
不久之前,Mirko建立了一個旅行社,名叫“極地之夢”。這家旅行社在北極附近購買了N座冰島,并且提供觀光服務(wù)。 當?shù)刈钍軞g迎的當然是帝企鵝了,這些小家伙經(jīng)常成群結(jié)隊的游走在各個冰島之間。Mirko的旅行社遭受一次重大打擊,以至于觀光游輪已經(jīng)不劃算了。旅行社將在冰島之間建造大橋,并用觀光巴士來運載游客。 Mirko希望開發(fā)一個電腦程序來管理這些大橋的建造過程,以免有不可預(yù)料的錯誤發(fā)生。 這些冰島從1到N標號。一開始時這些島嶼沒有大橋連接,并且所有島上的帝企鵝數(shù)量都是知道的。每座島上的企鵝數(shù)量雖然會有所改變,但是始終在[0, 1000]之間。你的程序需要處理以下三種命令: 1."bridge A B"——在A與B之間建立一座大橋(A與B是不同的島嶼)。由于經(jīng)費限制,這項命令被接受,當且僅當A與B不聯(lián)通。若這項命令被接受,你的程序需要輸出"yes",之后會建造這座大橋。否則,你的程序需要輸出"no"。 2."penguins A X"——根據(jù)可靠消息,島嶼A此時的帝企鵝數(shù)量變?yōu)閄。這項命令只是用來提供信息的,你的程序不需要回應(yīng)。 3."excursion A B"——一個旅行團希望從A出發(fā)到B。若A與B連通,你的程序需要輸出這個旅行團一路上所能看到的帝企鵝數(shù)量(包括起點A與終點B),若不聯(lián)通,你的程序需要輸出"impossible"。Input
第一行一個正整數(shù)N,表示冰島的數(shù)量。 第二行N個范圍[0, 1000]的整數(shù),為每座島嶼初始的帝企鵝數(shù)量。 第三行一個正整數(shù)M,表示命令的數(shù)量。接下來M行即命令,為題目描述所示。 1<=N<=30000,1<=M<=100000Output
對于每個bridge命令與excursion命令,輸出一行,為題目描述所示。
Sample Input
54 2 4 5 6
10
excursion 1 1
excursion 1 2
bridge 1 2
excursion 1 2
bridge 3 4
bridge 3 5
excursion 4 5
bridge 1 3
excursion 2 4
excursion 2 5
Sample Output
4impossible
yes
6
yes
yes
15
yes
15
16
題解: LCT練習(xí)題,因為沒有cut操作,可以離線用樹鏈剖分+并查集搞。 代碼: 1 var 2 i,j,k,l,n,m,dts,y,a,b:longint; 3 fa,sm,v,rev,dt:array[0..50001]of longint; 4 ch:array[0..50001,0..1]of longint; 5 ch1,ch2:char; 6 procedure update(x:longint); 7 begin 8 if x=0 then exit; sm[x]:=v[x]; 9 if ch[x,0]<>0 then sm[x]:=sm[x]+sm[ch[x,0]]; 10 if ch[x,1]<>0 then sm[x]:=sm[x]+sm[ch[x,1]]; 11 end; 12 function isroot(x:longint):boolean; 13 begin 14 if(x=0)or(fa[x]=0)then exit(true); 15 exit((x<>ch[fa[x],0])and(x<>ch[fa[x],1])); 16 end; 17 procedure pushdown(x:longint); 18 var tt:longint; 19 begin 20 if rev[x]=1 then 21 begin 22 tt:=ch[x,1]; ch[x,1]:=ch[x,0]; ch[x,0]:=tt; rev[x]:=0; 23 if ch[x,0]<>0 then rev[ch[x,0]]:=rev[ch[x,0]]xor 1; 24 if ch[x,1]<>0 then rev[ch[x,1]]:=rev[ch[x,1]]xor 1; 25 end; 26 end; 27 procedure rotate(x:longint); 28 var a1,a2,a3,b1,b2,b3:longint; 29 begin 30 if(x=0)or(isroot(x))then exit; 31 a1:=fa[x]; a2:=fa[fa[x]]; 32 if x=ch[a1,1] then b1:=1 else b1:=0; 33 if a1=ch[a2,1] then b2:=1 else b2:=0; 34 if isroot(a1)then b3:=1 else b3:=0; a3:=ch[x,1-b1]; 35 if b3=0 then ch[a2,b2]:=x; fa[x]:=a2; ch[a1,b1]:=a3; 36 if a3>0 then fa[a3]:=a1; ch[x,1-b1]:=a1; fa[a1]:=x; 37 update(a1); update(x); if b3=0 then update(a2); 38 end; 39 procedure splay(x:longint); 40 begin 41 if x=0 then exit; dts:=0; y:=x; 42 while not isroot(y) do begin inc(dts); dt[dts]:=y; y:=fa[y]; end; 43 inc(dts); dt[dts]:=y; while dts>0 do begin pushdown(dt[dts]); dec(dts); end; 44 while not isroot(x) do 45 begin 46 if not isroot(fa[x]) then 47 begin 48 if(x=ch[fa[x],1])xor(fa[x]=ch[fa[fa[x]],1]) 49 then rotate(x) else rotate(fa[x]); 50 end; 51 rotate(x); 52 end; 53 end; 54 function access(x:longint):longint; 55 var t:longint; 56 begin 57 if x=0 then exit(0); t:=0; 58 while x>0 do 59 begin 60 splay(x); ch[x,1]:=t; update(x); 61 if t>0 then fa[t]:=x; t:=x; x:=fa[x]; 62 end; 63 exit(t); 64 end; 65 procedure cut(x,y:longint); 66 begin 67 if(x=0)and(y=0)then exit; 68 access(x); splay(x); rev[x]:=rev[x]xor 1; 69 access(y); splay(x); fa[y]:=0; ch[x,1]:=0; update(x); 70 end; 71 procedure link(x,y:longint); 72 begin 73 if(x=0)and(y=0)then exit; 74 access(x); splay(x); rev[x]:=rev[x]xor 1; fa[x]:=y; 75 end; 76 function qsum(x,y:longint):longint; 77 var a:longint; 78 begin 79 if(x=0)or(y=0)then exit(0); if x=y then exit(v[x]); 80 access(x); a:=access(y); splay(x); 81 if x=a then exit(v[a]+sm[ch[a,1]])else exit(sm[x]+v[a]+sm[ch[a,1]]); 82 end; 83 procedure change(x,y:longint); 84 begin 85 splay(x); v[x]:=y; update(x); 86 end; 87 function find(x:longint):longint; 88 begin 89 access(x); splay(x); while ch[x,0]>0 do x:=ch[x,0]; exit(x); 90 end; 91 begin 92 readln(n); for i:=1 to n do read(v[i]); sm[i]:=v[i]; readln(m); 93 for i:=1 to m do 94 begin 95 read(ch1); read(ch2); while ch2<>' ' do read(ch2); readln(a,b); 96 if ch1='b' then 97 begin 98 if find(a)=find(b) then writeln('no')else 99 begin writeln('yes'); link(a,b); end; 100 end else if ch1='p' then change(a,b) else 101 if find(a)<>find(b) then writeln('impossible')else writeln(qsum(a,b)); 102 end; 103 end. View Code
轉(zhuǎn)載于:https://www.cnblogs.com/GhostReach/p/6391777.html
總結(jié)
以上是生活随笔為你收集整理的BZOJ2843:极地旅行社的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端含金量较高的网站推荐
- 下一篇: CSS3知识点整理(三)----变形与动