poj2895
题解:
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的更多相关文章
随机推荐
- Sizeof与Strlen的区别【转】
本文转载自:http://www.cnblogs.com/carekee/articles/1630789.html Sizeof与Strlen的区别与联系 一.sizeof sizeof(.. ...
- openwrt如何单独编译uboot
答:make package/boot/uboot-<chip series>/compile
- poj 2481 Cows(树状数组)题解
Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...
- zeptojs库解读3之ajax模块
对于ajax,三步骤,第一,创建xhr对象:第二,发送请求:第三,处理响应. 但在编写过程中,实际中会碰到以下问题, 1.超时 2.跨域 3.后退 解决方法: 1.超时 设置定时器,规定的时间内未返回 ...
- RTC(x86)
RTC 原创,转载请写明出处. 一直以来想写一篇关于RTC的总结,可是人太懒,在读完John Z. Sonmez大伽的<软技能代码之外的生存技能>后,终于下定决心,完成这项早已计划中的任务 ...
- codeforces 766E Mahmoud and a xor trip
题目链接:http://codeforces.com/problemset/problem/766/E 大意,给出一个$n$个点的树,每个点都有一个权值,令$Disx$为$u$到$v$路径上的异或和求 ...
- WPF基础学习笔记整理 (五) DependencyObject & DependencyProperty
参考资料: 1.http://www.cnblogs.com/Zhouyongh/archive/2009/10/20/1586278.html 基础知识: DependencyObject & ...
- 退出shell 脚本
#!/bin/bash export TOP_PID=$$ trap 'exit 1' TERM exit_script(){ kill -s TERM $TOP_PID } echo "b ...
- Node.js结合Selenium做Web自动化测试
发现腾讯课堂上有个node.js结合Selenium做Web自动化测试的教学视频, 听来感觉不错,一来老师讲的还不错,二来node.js这门语言会越来越热,学会总没什么坏处,三来发现CukeTest这 ...
- Meta referrer标签的简要介绍
在某些情况下,出于一些原因,网站想要控制页面发送给 server 的 referer 信息的情况下,可以使用这一 referer metadata 参数. 参数 referer 的 metedata ...