题解:

splay,维护当前第k大

并查集维护当前集合

合并x,y时,del(num[x]),del(num[y]),insert(num[x]+num[y])

代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=;
int pre[N],tot,xx,q,size[N],c[N][],fa[N],data[N],root,n,m,x,y,num[N];
void rot(int x)
{
int y=pre[x],k=(c[y][]==x);
size[y]=size[c[y][k]]+size[c[x][k]]+;
size[x]=size[c[x][!k]]+size[y]+;
c[y][!k]=c[x][k];
pre[c[y][!k]]=y;
pre[x]=pre[y];
if(pre[y])c[pre[y]][c[pre[y]][]==y]=x;
c[x][k]=y;pre[y]=x;
}
void splay(int x,int g)
{
for(int y=pre[x];y!=g;rot(x),y=pre[x])
if(pre[y]!=g)rot((x==c[y][])==(y==c[pre[y]][])?y:x);
if(g==)root=x;
}
void insert(int x)
{
int y=root;
while(c[y][x>data[y]]) y=c[y][x>data[y]];
data[++tot]=x;
c[tot][]=c[tot][]=;
pre[tot]=y;
if(y)c[y][x>data[y]]=tot;
splay(tot,);
}
void del(int x)
{
int y=root;
while(data[y]!=x) y=c[y][x>data[y]];
splay(y,);
y=c[root][];
bool b;
if(!y) b=,y=c[root][];else b=;
while(c[y][b]) y=c[y][b];
splay(y,root);
c[y][b]=c[root][b];pre[c[root][b]]=y;pre[y]=;root=y;
size[y]=size[c[y][!b]]+size[c[y][b]];
}
int find(int x)
{
if (x==fa[x])return x;
return fa[x]=find(fa[x]);
}
int findkth(int x)
{
int y=root;
while(x)
if(size[c[y][]]+<x)x-=size[c[y][]]+,y=c[y][];
else if(size[c[y][]]+==x) return data[y];
else y=c[y][];
return data[y];
}
int read()
{
char c;int x=;
for (;c<''||c>'';c=getchar());
for (;c>=''&&c<='';c=getchar())x=x*+c-;
return x;
}
void write(int x)
{
if (x>=)write(x/);
putchar(x%+);
}
int main()
{
n=read();m=read();
for (int i=;i<=n;i++)insert();
for (int i=;i<=n;i++)fa[i]=i,num[i]=;
while (m--)
{
xx=read();
if (xx==)
{
x=read();y=read();
int dx=find(x),dy=find(y);
if (dx!=dy)
{
fa[dx]=dy;
del(num[dy]);del(num[dx]);
num[dy]+=num[dx];
insert(num[dy]);
n--;
}
}
else
{
x=read();
write(findkth(n-x+)),puts("");
}
}
}

poj2895的更多相关文章

随机推荐

  1. C#中的基本类型理解

    1.C#把所有基本类型都封装成自己的类型了,如下图,int被封装成了一个struct结构体.如果定义一个int对象,是可以调用int结构体里的函数的 2.和C\C++不同,C#的char就是单纯的代表 ...

  2. JS 获取浏览器的宽和高

    网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...

  3. 三点估算和PERT技术

    三点估算是PMP考试中的必考题目,每次约2-4道题目.现在就三点估算和PERT技术做详细讲解,以飨读者. 通过考虑估算中的不确定性和风险,可以提高活动持续时间估算的准确性.这个概念起源于计划评审技术( ...

  4. 论文笔记——MobileNets(Efficient Convolutional Neural Networks for Mobile Vision Applications)

    论文地址:MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications MobileNet由Go ...

  5. 项目中的一个分页功能pagination

    项目中的一个分页功能pagination <script> //总页数 ; ; //分页总数量 $(function () { // $("#pagination"). ...

  6. c++ 静态函数

    //对象与对象之间的成员变量是相互独立的.要想共用数据,则需要使用静态成员或静态方法 //#只要在类中声明静态成员变量,即使不定义对象,也可以为静态成员变量分配空间,进而可以使用静态成员变量.(因为静 ...

  7. shell 函数调用

    例一 #!/bin/bash create_link() { filelist=`ls $` for file in $filelist do echo $/$file done } create_l ...

  8. 转载:oracle RAC集群启动和关闭

    http://www.cnblogs.com/yhfssp/p/8184761.html oracle 11G RAC集群启动和关闭: 1.停止数据库 $srvctl stop database –d ...

  9. Mask R-CNN论文理解

    摘要: Mask RCNN可以看做是一个通用实例分割架构. Mask RCNN以Faster RCNN原型,增加了一个分支用于分割任务. Mask RCNN比Faster RCNN速度慢一些,达到了5 ...

  10. R-CNN(Rich feature hierarchies for accurate object detection and semantic segmentation)论文理解

    论文地址:https://arxiv.org/pdf/1311.2524.pdf 翻译请移步: https://www.cnblogs.com/xiaotongtt/p/6691103.html ht ...