[bzoj2843&&bzoj1180]極地旅行社 (lct)

雙倍經驗雙倍的幸福。。。ios

因此另外一道是300大洋的世界T_T。。。雖然題目是同樣的,不過2843數據範圍小了一點。。。ide

都是lct基本操做ui

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<math.h>
 4 using namespace std;
 5 const int maxn=30233;
 6 struct zs{
 7     int c[2],fa,sum,val;
 8     bool rev;
 9 }tree[maxn];
10 int i,j,x,y,n,m;
11 int stack[maxn];
12 char id[233];
13 inline bool isroot(int x){return tree[tree[x].fa].c[0]!=x&&tree[tree[x].fa].c[1]!=x;
14 }
15 inline void update(int x){tree[x].sum=tree[tree[x].c[0]].sum+tree[x].val+tree[tree[x].c[1]].sum;
16 }
17 inline void pushdown(int x){
18     if(!tree[x].rev)return;
19     int l=tree[x].c[0],r=tree[x].c[1];
20     if(l)tree[l].rev^=1;if(r)tree[r].rev^=1;tree[x].rev^=1;
21     swap(tree[x].c[0],tree[x].c[1]);
22 }
23 inline void rotate(int x){
24     int fa=tree[x].fa,gfa=tree[fa].fa;
25     if(!isroot(fa))tree[gfa].c[tree[gfa].c[1]==fa]=x;
26     int l=tree[fa].c[1]==x,r=l^1;
27     tree[fa].c[l]=tree[x].c[r];tree[x].c[r]=fa;
28     tree[fa].fa=x;tree[x].fa=gfa;tree[tree[fa].c[l]].fa=fa;
29     update(fa);update(x);
30 }
31 void splay(int x){
32     int top=0,tmp=x;stack[++top]=x;
33     while(!isroot(tmp))stack[++top]=tree[tmp].fa,tmp=tree[tmp].fa;
34     while(top)pushdown(stack[top]),top--;
35     int fa,gfa;
36     while(!isroot(x)){
37         fa=tree[x].fa,gfa=tree[fa].fa;
38         if(!isroot(fa))
39             if((tree[gfa].c[0]==fa)^(tree[fa].c[0]==x))rotate(x);
40             else rotate(fa);
41         rotate(x);
42     }
43 }
44 inline void access(int x){
45     int son=0;
46     while(x){
47         splay(x);tree[x].c[1]=son;
48         update(x);son=x;x=tree[x].fa;
49     }
50 }
51 inline void makeroot(int x){
52     access(x);splay(x);tree[x].rev^=1;
53 }
54 inline void cut(int x,int y){
55     makeroot(x);access(y);splay(y);tree[y].c[0]=tree[x].fa=0;
56 }
57 inline void link(int x,int y){
58     makeroot(x);tree[x].fa=y;
59 }
60 inline int getfa(int x){
61     access(x);splay(x);
62     while(tree[x].c[0]){x=tree[x].c[0];pushdown(x);}
63     splay(x);return x;
64 }
65 inline int query(int x,int y){
66     makeroot(x);access(y);splay(y);
67     return tree[y].sum;
68 }
69 inline void change(int x,int val){
70     makeroot(x);tree[x].val=val;update(x);
71 }
72 int main(){
73     scanf("%d",&n);for(i=1;i<=n;i++)scanf("%d",&tree[i].val),tree[i].sum=tree[i].val;
74     scanf("%d",&m);
75     while(m--){
76         scanf("%s%d%d",id,&x,&y);
77         if(id[0]=='e'){
78             if(getfa(x)!=getfa(y))printf("impossible\n");else printf("%d\n",query(x,y));
79         }else if(id[0]=='b'){
80             if(getfa(x)==getfa(y))printf("no\n");
81             else printf("yes\n"),link(x,y);
82         }else if(id[0]=='p')change(x,y);
83     }
84     return 0;
85 }
View Code

 16.1.8:重寫了一發spa

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 using namespace std;
 5 const int maxn=30023;
 6 int ch[maxn][2],fa[maxn],sum[maxn],num[maxn],st[maxn];
 7 int i,j,x,y,n,m;
 8 bool rev[maxn];
 9  
10 int ra;char rx;
11 inline int read(){
12     rx=getchar(),ra=0;
13     while(rx<'0'||rx>'9')rx=getchar();
14     while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra;
15 }
16  
17 inline bool isrt(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
18 inline void upd(int x){sum[x]=sum[ch[x][0]]+num[x]+sum[ch[x][1]];}
19 inline void pushdown(int x){
20     if(!rev[x])return;
21     rev[x]=0,swap(ch[x][0],ch[x][1]),rev[ch[x][0]]^=1,rev[ch[x][1]]^=1;
22 }
23 inline void rotate(int x){
24     int f=fa[x],gfa=fa[f],l=ch[f][1]==x,r=l^1;
25     if(!isrt(f))ch[gfa][ch[gfa][1]==f]=x;
26     ch[f][l]=ch[x][r],ch[x][r]=f,fa[x]=gfa,fa[f]=x,fa[ch[f][l]]=f;
27     sum[x]=sum[f],upd(f);
28 }
29 void splay(int x){
30     int f,gfa;
31     for(st[st[0]=1]=f=x;!isrt(f);)st[++st[0]]=(f=fa[f]);
32     while(st[0])pushdown(st[st[0]--]);
33     for(f=fa[x],gfa=fa[f];!isrt(x);rotate(x),f=fa[x],gfa=fa[f])
34         if(!isrt(f))rotate(((ch[f][0]==x)^(ch[gfa][0]==f))?x:f);
35 }
36 inline void access(int x){
37     for(int t=0;x;t=x,x=fa[x])splay(x),ch[x][1]=t,upd(x);
38 }
39 inline void makert(int x){
40     access(x),splay(x),rev[x]^=1;
41 }
42 void link(int x,int y){
43     makert(x),fa[x]=y;if(!(x&233))splay(x);
44 }
45 inline int getfa(int x){
46     for(access(x),splay(x);ch[x][0];x=ch[x][0]);
47     return x;
48 }
49 int main(){
50     n=read();for(i=1;i<=n;i++)num[i]=read();
51     m=read();char id;
52     while(m--){
53         for(id=getchar();id<'a'||id>'z';id=getchar());
54         x=read(),y=read();
55         if(id=='b')if(getfa(x)!=getfa(y))link(x,y),puts("yes");else puts("no");
56         if(id=='e')if(getfa(x)==getfa(y))makert(x),access(y),splay(y),printf("%d\n",sum[y]);else puts("impossible");
57         if(id=='p')makert(x),num[x]=y,upd(x);
58     }
59     return 0;
60 }
View Code

 

 

2843: 極地旅行社

Time Limit: 10 Sec  Memory Limit: 256 MBcode

Description

不久以前,Mirko創建了一個旅行社,名叫「極地之夢」。這家旅行社在北極附近購買了N座冰島,而且提供觀光服務。當地最受歡迎的固然是帝企鵝了,這些小傢伙常常三五成羣的遊走在各個冰島之間。
Mirko的旅行社遭受一次重大打擊,以致於觀光遊輪已經不划算了。旅行社將在冰島之間建造大橋,並用觀光巴士來運載遊客。Mirko但願開發一個電腦程序來管理這些大橋的建造過程,以避免有不可預料的錯誤發生。
這些冰島從1到N標號。一開始時這些島嶼沒有大橋鏈接,而且全部島上的帝企鵝數量都是知道的。每座島上的企鵝數量雖然會有所改變,可是始終在[0, 1000]之間。
你的程序須要處理如下三種命令:
1."bridge A B"——在A與B之間創建一座大橋(A與B是不一樣的島嶼)。因爲經費限制,這項命令被接受,當且僅當A與B不聯通。若這項命令被接受,你的程序須要輸出"yes",以後會建造這座大橋。不然,你的程序須要輸出"no"。
2."penguins A X"——根據可靠消息,島嶼A此時的帝企鵝數量變爲X。這項命令只是用來提供信息的,你的程序不須要回應。
3."excursion A B"——一個旅行團但願從A出發到B。若A與B連通,你的程序須要輸出這個旅行團一路上所能看到的帝企鵝數量(包括起點A與終點B),若不聯通,你的程序須要輸出"impossible"。

Input

第一行一個正整數N,表示冰島的數量。blog

第二行N個範圍[0, 1000]的整數,爲每座島嶼初始的帝企鵝數量。ip

第三行一個正整數M,表示命令的數量。開發

接下來M行即命令,爲題目描述所示。get

 

Output

對於每一個bridge命令與excursion命令,輸出一行,爲題目描述所示。string

Sample Input

5
4 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

4
impossible
yes
6
yes
yes
15
yes
15
16
 

HINT

1<=N<=30000,1<=M<=100000(bzoj2843)

1<=n<=30000, 1<=m<=300000 (bzoj1180)

相關文章
相關標籤/搜索