BZOJ2733 永无乡 [启发式合并]

来源:互联网 发布:redis php 编辑:程序博客网 时间:2024/06/04 19:16

2733: [HNOI2012]永无乡

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 3578  Solved: 1922
[Submit][Status][Discuss]

Description

永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示。某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛。如果从岛 a 出发经过若干座(含 0 座)桥可以到达岛 b,则称岛 a 和岛 b 是连 通的。现在有两种操作:B x y 表示在岛 x 与岛 y 之间修建一座新桥。Q x k 表示询问当前与岛 x连通的所有岛中第 k 重要的是哪座岛,即所有与岛 x 连通的岛中重要度排名第 k 小的岛是哪 座,请你输出那个岛的编号。 

 

Input

输入文件第一行是用空格隔开的两个正整数 n 和 m,分别 表示岛的个数以及一开始存在的桥数。接下来的一行是用空格隔开的 n 个数,依次描述从岛 1 到岛 n 的重要度排名。随后的 m 行每行是用空格隔开的两个正整数 ai 和 bi,表示一开始就存 在一座连接岛 ai 和岛 bi 的桥。后面剩下的部分描述操作,该部分的第一行是一个正整数 q, 表示一共有 q 个操作,接下来的 q 行依次描述每个操作,操作的格式如上所述,以大写字母 Q 或B 开始,后面跟两个不超过 n 的正整数,字母与数字以及两个数字之间用空格隔开。 对于 20%的数据 n≤1000,q≤1000

 

对于 100%的数据 n≤100000,m≤n,q≤300000 

 

Output

对于每个 Q x k 操作都要依次输出一行,其中包含一个整数,表 示所询问岛屿的编号。如果该岛屿不存在,则输出-1。 

 

Sample Input


5 1

4 3 2 5 1

1 2

7

Q 3 2

Q 2 1

B 2 3

B 1 5

Q 2 1

Q 2 4

Q 2 3

Sample Output


-1

2

5

1

2

HINT


Source


[Submit][Status][Discuss]



我写的是splay,合并效率是nlogn2的,十分优秀,因为其他平衡树的合并效率是nlogn22的复杂度。

#include<bits/stdc++.h>using namespace std;const int N = 200005;int n,m,q,a[N];char c;struct Splay{    int lc,rc,val[N],ch[N][2],sz[N],fa[N],root[N];    void init(){        for(register int i=1;i<=n;i++)val[i]=a[i],root[i]=i,sz[i]=1,fa[i]=ch[i][0]=ch[i][1]=0;    }    int update(int x){        sz[x]=sz[ch[x][1]]+sz[ch[x][0]]+1;    }    bool J(int x){        return ch[fa[x]][1]==x;    }    void rotate(int x){        int f=fa[x],ff=fa[f],d=J(x);        ch[f][d]=ch[x][d^1],fa[ch[f][d]]=f;        ch[x][d^1]=f,fa[f]=x;        if(ff)ch[ff][ch[ff][1]==f]=x;        fa[x]=ff;        update(f),update(x);    }    void splay(int x,int y){        for(int f;f=fa[x];rotate(x))            if(fa[f])rotate((J(x)==J(f))?f:x);        root[y]=x;    }    void insert(int x,int key,int y){        if(!root[y]){root[y]=x,ch[x][1]=ch[x][0]=fa[0]=0;sz[x]=1;return;}        int rt=root[y],f;        for(;;){            f=rt;            rt=ch[rt][key>val[rt]];            if(!rt){                fa[x]=f;                ch[x][0]=ch[x][1]=0,sz[x]=1;                ch[f][key>val[f]]=x;                update(x),update(f),splay(x,y);                return;            }        }    }    void merge(int x,int y){        if(ch[x][0])merge(ch[x][0],y);        if(ch[x][1])merge(ch[x][1],y);        insert(x,val[x],y);    }    int query(int x,int k){        for(;;){            if(ch[x][0]&&sz[ch[x][0]]>=k)x=ch[x][0];            else{                int temp=1;                if(ch[x][0])temp+=sz[ch[x][0]];                if(temp==k)return x;                k-=temp,x=ch[x][1];            }        }       }}splay;struct UFS{    int fa[N];    void init(){        for(register int i=1;i<=n;i++)fa[i]=i;    }    int find(int x){        return fa[x]==x?x:fa[x]=find(fa[x]);    }    void unionn(int x,int y){        int fx=find(x),fy=find(y);        if(fx==fy)return;        if(splay.sz[fx]>splay.sz[fy])swap(x,y),swap(fx,fy);        splay.merge(splay.root[fx],fy),fa[fx]=fy;    }    void query(int x,int y){        int fx=find(x);        if(y>splay.sz[splay.root[fx]])printf("-1\n");        else printf("%d\n",splay.query(splay.root[fx],y));    }}ufs;inline void read(int &res){    static char ch;    while((ch=getchar())<'0'||ch>'9');res=ch-48;    while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-48;}inline void readc(char &res){    static char ch;    while((ch=getchar())<'A'||ch>'Z');res=ch;}int main(){    read(n),read(m);    for(register int i=1;i<=n;i++)read(a[i]);splay.init();ufs.init();    for(register int x,y,i=1;i<=m;i++)        read(x),read(y),ufs.unionn(x,y);    read(q);    for(register int x,y,i=1;i<=q;i++){        readc(c),read(x),read(y);        if(c=='Q')ufs.query(x,y);        else ufs.unionn(x,y);    }    return 0;}

就效率来说挺好的。

原创粉丝点击