POJ 2985 名次树
题意:1~n个猫,有合并操作,有询问操作,合并两只猫所在的集合,询问第K大的集合。
分析:合并操作用并查集,用size维护,询问操作用Treap。注意优化,不能刚开始就把所有size = 1放到名次树中,是1的不做处理,而是不够的时候返回一个1;
#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std; struct Node {
Node *ch[];
int r;
int v;
int s;
Node(int v):v(v) {ch[]=ch[] = NULL;r=rand();s=;}
bool operator < (const Node& rhs) const {
return r < rhs.r;
} int cmp(int x) const {
if(x==v) return -;
return x < v ? : ;
} void maintain() {
s = ;
if(ch[]!=NULL) s+=ch[]->s;
if(ch[]!=NULL) s+=ch[]->s;
} }*root; void rotate(Node* &o,int d)
{
Node *k=o->ch[d^];
o->ch[d^]=k->ch[d];
k->ch[d]=o;
o->maintain();
k->maintain();
o=k;
}
void insert(Node* &o,int v)//可插入重复值
{
if(o==NULL) o=new Node(v);
else
{
int d=v < o->v? :;
insert(o->ch[d],v);
if(o->ch[d]->r > o->r)
rotate(o,d^);
}
o->maintain();
}
void remove(Node* &o,int v)
{
int d=o->cmp(v);
if(d==-)
{
Node *u=o;
if(o->ch[] && o->ch[])
{
int d2= o->ch[]->r < o->ch[]->r ?:;
rotate(o,d2);
remove(o->ch[d2],v);
}
else
{
if(o->ch[]==NULL) o=o->ch[];
else o=o->ch[];
delete u;
}
}
else remove(o->ch[d],v);
if(o) o->maintain();
} int kth(Node *o,int k)//返回第k大的值,不是第k小
{
if(o==NULL || k<= || k> o->s) return ;
int s = (o->ch[]==NULL)?:o->ch[]->s;
if(k==s+) return o->v;
else if(k<=s) return kth(o->ch[],k);
else return kth(o->ch[],k-s-);
} const int maxn=+;
int n,m;
int father[maxn],size[maxn]; int Find_Set(int x) {
if(father[x]!=x)
father[x] = Find_Set(father[x]);
return father[x];
} int main()
{
//freopen("in.txt","r",stdin);
root = NULL;
for(int i=;i<maxn;i++) {
father[i] = i;
size[i] = ;
}
scanf("%d%d",&n,&m);
//for(int i=0;i<n;i++)
//insert(root,1);
while(m--) {
int cmp;
scanf("%d",&cmp);
if(cmp==) {
int u,v;
scanf("%d%d",&u,&v);
int fx = Find_Set(u);
int fy = Find_Set(v);
if(fx!=fy)
{
if(size[fx]!=) remove(root,size[fx]);
if(size[fy]!=) remove(root,size[fy]);
father[fy] = fx;
size[fx]+=size[fy];
insert(root,size[fx]);
}
}
else {
int k;
scanf("%d",&k);
printf("%d\n",kth(root,k));
}
} return ;
}
POJ 2985 名次树的更多相关文章
- poj 1442 名次树
这回要求的是第k小的元素, 参考了ljl大神的模板,orz //insert 插入 //remove 删除 //_find 查找 //kth 返回root为根的树中第k小的元素 //treap插入.删 ...
- Treap和名次树
Treap名字的来源:Tree+Heap,正如名字一样,就是一颗简单的BST,一坨堆的合体.BST的不平衡的根本原因在于基于左<=根<=右的模式吃单调序列时候会无脑成长链,而Treap则添 ...
- UVa 1479 (Treap 名次树) Graph and Queries
这题写起来真累.. 名次树就是多了一个附加信息记录以该节点为根的树的总结点的个数,由于BST的性质再根据这个附加信息,我们可以很容易找到这棵树中第k大的值是多少. 所以在这道题中用一棵名次树来维护一个 ...
- uvalive 5031 Graph and Queries 名次树+Treap
题意:给你个点m条边的无向图,每个节点都有一个整数权值.你的任务是执行一系列操作.操作分为3种... 思路:本题一点要逆向来做,正向每次如果删边,复杂度太高.逆向到一定顺序的时候添加一条边更容易.详见 ...
- bzoj 3196 Tyvj 1730 二逼平衡树(线段树套名次树)
3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1807 Solved: 772[Submit][Stat ...
- bzoj3224 Tyvj 1728 普通平衡树(名次树+处理相同)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5354 Solved: 2196[Submit][Sta ...
- UVaLive5031 Graph and Queries(时光倒流+名次树)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20332 [思路] 时光倒流+名次树(rank tree). 所谓“ ...
- bzoj1503 [NOI2004]郁闷的出纳员(名次树+懒惰标记)
1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 8705 Solved: 3027[Submit][Statu ...
- poj 2886 线段树+反素数
Who Gets the Most Candies? Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12744 Acc ...
随机推荐
- MVC参数自动装配
在拿到一个类型的所有属性以及字段的描述信息后,就可以通过循环的方式,根据这些数据成员的名字去QueryString,Form,Session,Cookie读取所需的数据了. 就是遍历参数,然后用反射遍 ...
- Python-2.7 配置 tab 自动补全功能
作者博文地址:http://www.cnblogs.com/liu-shuai/ 之前一直使用shell编程,习惯了shell的 tab 自动补全功能,而Python的命令行却不支持 tab 自动补全 ...
- 一个优秀的app应该考虑的问题
带着团队做了3个app,需求是客户决定的,甚至连进度都不是项目经理可以控制的(譬如说一个app要在6周内,3个人完成).现在的状态是基本上没有用户量,当然原因是多方面的,下面说一说我认为app设计的原 ...
- 06-struts2与ognl的结合
1 参数接收 2 配置文件中 1 Demo2Action package www.test.c_config; import com.opensymphony.xwork2.ActionSupport ...
- 【ExtJS】自定义组件datetimefield(一)
目的: ExtJS中提供了下拉日期选择控件Ext.form.field.Date与下拉时间选择控件Ext.form.field.Time.不过没有一个在选择日期时选择时间的控件datetimefiel ...
- FZU 1922——非主流——————【技巧题】
非主流 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status P ...
- Visual Studio 安装OpenCV及问题总结
1.VS安装OpenCV基本步骤 1)安装Visual Studio 下载网址https://opencv.org/releases.html# 2)安装OpenCV 下载网址https://www. ...
- sql server数据库打不开
- Dictionary集合 字典
1 Dictionary<int, string> dic = new Dictionary<int, string>(); dic.Add(,"张三"); ...
- 重构指南 - 移除重复内容(Remove Duplication)
在项目中或多或少的都存在着重复的或者功能相似的代码,如果要对代码做改动,就要修改多个地方,所以我们需要将多处重复的代码提取到一个公共的地方供统一调用,以减少代码量,提高代码可维护性. 重构前代码 pu ...