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

双倍经验双倍的幸福。。。

所以另一道是300大洋的世界T_T。。。虽然题目是一样的,不过2843数据范围小了一点。。。

都是lct基本操作

 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:重写了一发

 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 MB

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,表示冰岛的数量。

第二行N个范围[0, 1000]的整数,为每座岛屿初始的帝企鹅数量。

第三行一个正整数M,表示命令的数量。

接下来M行即命令,为题目描述所示。

 

Output

对于每个bridge命令与excursion命令,输出一行,为题目描述所示。

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)

posted @ 2015-08-12 10:13  czllgzmzl  阅读(202)  评论(0编辑  收藏  举报