无旋treap启发式搜索 [HNOI2012]永无乡

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

输入
输入文件第一行是用空格隔开的两个正整数 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

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

样例输入
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
样例输出
-1
2
5
1
2
提示

这只是第二篇,那必然有第一篇:线段树合并的做法,没多少人跟我一样打了两遍。。
貌似大部分人都打的splay或普通treap,也许就我在复习无旋treap。。。
因为要找第k大,很容易想到平衡树,也不存在区间修改(其实splay和无旋treap没必要。。。),问题在于如何合并。
为了节约时间,把小树合并到大树上去。一遍dfs枚举小树所有节点insert入大树即可。我偷懒枚举的每一个rank。。
用并查集维护块的联通,其他就没什么了。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 101000
using namespace std;
struct treap
{
    treap* ch[2];
    int id,h,sz;
    treap(){sz=h=0;id=rand();ch[0]=ch[1]=NULL;}
    inline void updata(){sz=ch[0]->sz+ch[1]->sz+1;}
} *null=new treap(),*root[N];

typedef pair<treap*,treap*> D;

int n,m,sz,Q,f[N],sum[N],id[N];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
treap* Treap(int k)
{
    treap* o=new treap();
    o->ch[0]=o->ch[1]=null;
    o->h=k;o->sz=1;
    return o;
}
D split(treap *x,int k)
{
    if(x==null) return D(null,null);
    D y;
    if(x->ch[0]->sz>=k){y=split(x->ch[0],k);x->ch[0]=y.second;x->updata();y.second=x;}
    else {y=split(x->ch[1],k-x->ch[0]->sz-1);x->ch[1]=y.first;x->updata();y.first=x;}
    return y;
}
treap* merge(treap *a,treap *b)
{
    if(a==null)return b;
    if(b==null)return a;
    if(a->id<b->id){a->ch[1]=merge(a->ch[1],b);a->updata();return a;}
    else{b->ch[0]=merge(a,b->ch[0]);b->updata();return b;}
}
int rank_(treap *x,int k)
{
    if(x==null)return 0;
    if(x->h>=k)return rank_(x->ch[0],k);
    else return rank_(x->ch[1],k)+x->ch[0]->sz+1;
}
int q(treap* &f,int k)
{
    D x=split(f,k-1);
    D y=split(x.second,1);
    int ans=y.first->h;
    f=merge(merge(x.first,y.first),y.second);
    return ans;
}
void insert(treap* &x,int k)
{
    int l=rank_(x,k);
    D y=split(x,l);
    treap *w=Treap(k);
    x=merge(merge(y.first,w),y.second);
}
void hb(treap* &x,treap* &y)
{
    int l=x->sz;
    for(int i=1;i<=l;i++)
    {
        int k=q(x,i);
        insert(y,k);
    }
    //x=null;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        f[i]=i;
        scanf("%d",&sum[i]);
        root[i]=Treap(sum[i]);
        id[sum[i]]=i;
    }
    int x,y;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        int fx=find(x),fy=find(y);if(fx==fy)continue;
        if(root[fx]->sz>root[fy]->sz)swap(fx,fy);
        f[fx]=fy;
        hb(root[fx],root[fy]);
    }
    scanf("%d",&Q);char s[2];
    while(Q--)
    {
        scanf("%s%d%d",s,&x,&y);
        if(s[0]=='Q')
        {
            int fx=find(x);//cout<<fx<<" "<<root[fx]->sz<<endl;
            if(root[fx]->sz<y){printf("-1
");continue;}
            int k=q(root[fx],y);
            printf("%d
",id[k]);
        }
        else
        {
            int fx=find(x),fy=find(y);
            if(fx^fy)
            {
                if(root[fx]->sz>root[fy]->sz)swap(fx,fy);
                f[fx]=fy;
                hb(root[fx],root[fy]);
            }
        }
    }
}
原文地址:https://www.cnblogs.com/QTY2001/p/7632657.html