题目链接:https://www.luogu.org/problemnew/show/P3224#sub

题目:

题目大意:

维护多个联通块,没有删除操作,每次询问某一联通块的第k大

解法:

维护联通块我们用并查集,询问第k大用splay,合并的时候splay暴力启发式合并就是了

启发式合并:把size小的splay合并到size大的splay上,暴力插入就好了

这道题的具体做法就是我们记录rt数组表示每个点的splay的根,在每次连边的时候就是把一方的根的所有节点全部插入到另一方的根去

其他的可以参考我在洛谷的博客里写的东西:https://www.luogu.org/blog/xxzh2425/solution-p3224

AC代码如下:

// luogu-judger-enable-o2
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cctype>
#include<cstdlib>
#define ri register int
using namespace std; const int N=1e5+;
int n,m;
int f[N],rt[N],w[N],fa[N];
std::queue <int> dl;
struct Splay
{
int ch[];
int ff,size;
}t[N];
inline int read()
{
char ch=getchar();
int s=,f=;
while (ch<''||ch>'') {if (ch=='-') f=-;ch=getchar();}
while (ch>=''&&ch<='') {s=(s<<)+(s<<)+ch-'';ch=getchar();}
return s*f;
}
inline int find(int x)
{
if (fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
}
inline void pushup(int x)
{
t[x].size=t[t[x].ch[]].size+t[t[x].ch[]].size+;
}
inline void rotate(int x)
{
int y=t[x].ff;
int z=t[y].ff;
int k=t[y].ch[]==x;
t[z].ch[t[z].ch[]==y]=x;
t[x].ff=z;
t[y].ch[k]=t[x].ch[k^];
t[t[x].ch[k^]].ff=y;
t[x].ch[k^]=y;
t[y].ff=x;
pushup(y);pushup(x);
}
inline void splay(int x,int goal)
{
while (t[x].ff!=goal)
{
int y=t[x].ff,z=t[y].ff;
if (z!=goal) (t[y].ch[]==x)^(t[z].ch[]==y)?rotate(x):rotate(y);
rotate(x);
}
}
inline void insert(int x,int &now,int fat)
{
if (!now)
{
now=x;
t[x].ff=fat;
return;
}
t[now].ff=fat;
t[now].size++;
if (w[x]<=w[now]) insert(x,t[now].ch[],now);
else insert(x,t[now].ch[],now);
}
inline void mergy(int x,int y)
{
if (x==y) return;
if (t[rt[x]].size>t[rt[y]].size) std::swap(x,y);
fa[rt[x]]=rt[y];
dl.push(rt[x]);
while (!dl.empty())
{
int k=dl.front();
dl.pop();
if (t[k].ch[]) dl.push(t[k].ch[]);
if (t[k].ch[]) dl.push(t[k].ch[]);
insert(k,rt[y],);
rt[k]=rt[y];
//splay(k,rt[y]);
}
}
inline int kth(int x,int k)
{
int now=rt[x];
if (t[now].size<k) return -;
while ()
{
if (t[t[now].ch[]].size>=k) now=t[now].ch[];
else if (t[t[now].ch[]].size+==k) return now;
else k-=t[t[now].ch[]].size+,now=t[now].ch[];
}
}
inline void write(int x)
{
if(x<) putchar('-'),x=-x;
if(x>) write(x/);
putchar(x%+'');
}
int main()
{
n=read();m=read();
for (ri i=;i<=n;i++)
{
w[i]=read();
rt[i]=i;fa[i]=i;t[i].size=;
}
for (ri i=;i<=m;i++)
{
int u=read(),v=read();
mergy(u,v);
}
int q=read();
while (q--)
{
char ch=getchar();
while (!(ch=='Q'||ch=='B')) ch=getchar();
int x=read(),y=read();
if (ch=='Q')
{
int ans=kth(find(x),y);
write(ans);putchar('\n');
}
else
{
mergy(find(x),find(y));
}
}
return ;
}

诚恳地建议:

去看看我在洛谷博客里写的东西

[HNOI2012] 永无乡 解题报告 (splay+启发式合并)的更多相关文章

  1. 洛谷 P3224 [HNOI2012]永无乡 解题报告

    P3224 [HNOI2012]永无乡 题目描述 永无乡包含 \(n\) 座岛,编号从 \(1\) 到 \(n\) ,每座岛都有自己的独一无二的重要度,按照重要度可以将这 \(n\) 座岛排名,名次用 ...

  2. [BZOJ2733][HNOI2010]永无乡 解题报告 启发式合并,线段树合并

    好久没更新博客了,前段时间一直都在考试,都没时间些,现在终于有点闲了(cai guai)... 写了一道题,[HNOI2012]永无乡,其实是一道板子题,我发现我写了好多板子题...还是太菜了... ...

  3. 2733. [HNOI2012]永无乡【平衡树-splay】

    Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...

  4. [HNOI2012]永无乡 线段树合并

    [HNOI2012]永无乡 LG传送门 线段树合并练手题,写这篇博客只是为了给我的这篇文章找个板子题. 并查集维护连通性,对于不在同一个连通块内的合并操作每次直接合并两颗线段树,复杂度\(O(n \l ...

  5. BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]

    2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...

  6. Bzoj 2733: [HNOI2012]永无乡 数组Splay+启发式合并

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3955  Solved: 2112[Submit][Statu ...

  7. bzoj2733: [HNOI2012]永无乡 启发式合并

    地址:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 题目: 2733: [HNOI2012]永无乡 Time Limit: 10 Sec   ...

  8. bzoj2733: [HNOI2012]永无乡(splay)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3778  Solved: 2020 Description 永 ...

  9. Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...

随机推荐

  1. [CSS3] Responsive Table -- no more table

    When the screen size is small, we can use "no more table" solution. So instead of render t ...

  2. HDU 5416 CRB and Tree (2015多校第10场)

    欢迎參加--每周六晚的BestCoder(有米!) CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536 ...

  3. 同一个TextView设置不同的颜色和大小

    //strategy1是一个TextView SpannableStringBuilder builder1 = new SpannableStringBuilder(strategy1.getTex ...

  4. OpenMp之reduction求和

    // OpenMP1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include"omp.h" #include& ...

  5. leetCode(46):Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  6. poj_3071概率dp

    确定好对手就简单了. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...

  7. HMM(隐马尔科夫模型)——本质上就是要预测出股市的隐藏状态(牛市、熊市、震荡、反弹等)和他们之间的转移概率

    摘自:http://blog.csdn.net/baskbeast/article/details/51218777 可以看 <统计学习方法>里的介绍 举一个日常生活中的例子,我们希望根据 ...

  8. 【参考】IBM sun.io.MalformedInputException and text encoding conversions transforms numerals to their word equivalents - United States

    Problem(Abstract) When converting contents from a file or string using WebSphere Application Server, ...

  9. pthread_cleanup_push vs Autorelease VS 异常处理

    黑幕背后的Autorelease http://www.cnblogs.com/feng9exe/p/7239552.html objc_autoreleasePoolPush的返回值正是这个哨兵对象 ...

  10. PKI和加密,散列算法

    Day 11-PKI和加密,散列算法 PKI(Public Key Infrastructure公钥基础设施) 1 PKI(Public Key Infrastructure公钥基础设施)回顾学习 - ...