1180: [CROATIAN2009]OTOCI

Description

给出n个结点以及每个点初始时对应的权值wi。起始时点与点之间没有连边。有3类操作: 1、bridge A B:询问结点A与结点B是否连通。如果是则输出“no”。否则输出“yes”,并且在结点A和结点B之间连一条无向边。 2、penguins A X:将结点A对应的权值wA修改为X。 3、excursion A B:如果结点A和结点B不连通,则输出“impossible”。否则输出结点A到结点B的路径上的点对应的权值的和。给出q个操作,要求在线处理所有操作。数据范围:1<=n<=30000, 1<=q<=300000, 0<=wi<=1000。

Input

第一行包含一个整数n(1<=n<=30000),表示节点的数目。第二行包含n个整数,第i个整数表示第i个节点初始时对应的权值。第三行包含一个整数q(1<=n<=300000),表示操作的数目。以下q行,每行包含一个操作,操作的类别见题目描述。任意时刻每个节点对应的权值都是1到1000的整数。

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
 
可以,这很在线。。。
双倍经验。。。
把2843交上去就好了。。。
  1 #include<iostream>
  2 #include<cstdlib>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<algorithm>
  7 #include<string>
  8 #include<map>
  9 #include<queue>
 10 #include<vector>
 11 #include<set>
 12 #define inf 1000000000
 13 #define maxn 30000+5
 14 #define maxm 10000+5
 15 #define eps 1e-10
 16 #define ll long long
 17 #define for0(i,n) for(int i=0;i<=(n);i++)
 18 #define for1(i,n) for(int i=1;i<=(n);i++)
 19 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 20 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 21 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
 22 using namespace std;
 23 int read(){
 24     int x=0,f=1;char ch=getchar();
 25     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 26     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
 27     return x*f;
 28 }
 29 int n,m,c[maxn][2],fa[maxn],v[maxn],sum[maxn],sta[maxn],f[maxn];
 30 bool rev[maxn];
 31 bool isroot(int x){
 32     return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
 33 }
 34 void pushup(int x){
 35     sum[x]=sum[c[x][1]]+sum[c[x][0]]+v[x];
 36 }
 37 void rever(int x){
 38     rev[x]^=1;
 39     swap(c[x][1],c[x][0]);
 40 }
 41 void pushdown(int x){
 42     if(!rev[x])return ;
 43     rever(c[x][1]);rever(c[x][0]);
 44     rev[x]=0;
 45 }
 46 void rotate(int x){
 47     int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1;
 48     if(!isroot(y))c[z][c[z][1]==y]=x;
 49     fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
 50     c[y][l]=c[x][r];c[x][r]=y;
 51     pushup(y);pushup(x);
 52 }
 53 void splay(int x){
 54     int top=0;sta[++top]=x;
 55     for(int y=x;!isroot(y);y=fa[y])sta[++top]=fa[y];
 56     for(;top;)pushdown(sta[top--]);
 57     while(!isroot(x)){
 58         int y=fa[x],z=fa[y];
 59         if(!isroot(y)){
 60             if(c[z][0]==y^c[y][0]==x)
 61                 rotate(x);
 62             else rotate(y);
 63         }
 64         rotate(x);
 65     }
 66 }
 67 void access(int x){
 68     for(int y=0;x;x=fa[x]){
 69         splay(x);c[x][1]=y;pushup(x);y=x;
 70     }
 71 }
 72 void makeroot(int x){
 73     access(x);splay(x);rever(x);
 74 }
 75 void split(int x,int y){
 76     makeroot(x);access(y);splay(y);
 77 }
 78 int findroot(int x){
 79     return f[x]==x?x:f[x]=findroot(f[x]);
 80 }
 81 void link(int x,int y){
 82     if(findroot(x)==findroot(y)){
 83         printf("no\n");
 84         return ;
 85     }
 86     makeroot(x);fa[x]=y;splay(x);f[findroot(x)]=findroot(y);
 87     printf("yes\n");
 88 }
 89 void query(int x,int y){
 90     if(findroot(x)==findroot(y)){
 91         split(x,y),printf("%d\n",sum[y]);
 92         return ;
 93     }
 94     printf("impossible\n");
 95 }
 96 int main(){
 97     //freopen("input.txt","r",stdin);
 98     //freopen("output.txt","w",stdout);
 99     n=read();
100     for1(i,n)v[i]=sum[i]=read(),f[i]=i;
101     m=read();
102     while(m--){
103         char ch=getchar();
104         while(ch!='e'&&ch!='b'&&ch!='p')ch=getchar();
105         int x=read(),y=read();
106         if(ch=='e')query(x,y);
107         else if(ch=='b')link(x,y);
108         else makeroot(x),v[x]=y,pushup(x);
109     }
110     return 0;
111 }
View Code

 

posted @ 2016-07-11 10:26  HTWX  阅读(108)  评论(0编辑  收藏  举报