POJ2985 比较简单的平衡树题目 树内不要添加容量为1的节点 否则会超时。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=500000+10,INF=1e+7;
int root,treapcnt,group[maxn],num[maxn],key[maxn],priority[maxn],child[maxn][2],size[maxn];
int treaptot=0;
inline int cmp(int a,int b)
{
if(num[a]!=num[b])return num[a]<num[b];
if(a!=b)return a<b;
return -1;
}
void Treap()
{
root=0;
treapcnt=1;
priority[0]=INF;
size[0]=0;
} inline void update(int x)
{
size[x]=size[child[x][0]]+1+size[child[x][1]];
} void rotate(int &x,int t)
{
int y=child[x][t];
child[x][t]=child[y][1-t];
child[y][1-t]=x;
update(x);update(y);
x=y;
} void _insert(int &x,int k)
{
if(x)
{
if(cmp(key[x],k)==-1)
{
return ;
}
else
{
int t=cmp(key[x],k);
_insert(child[x][t],k);
if(priority[child[x][t]]<priority[x])rotate(x,t);
}
}
else
{
x=treapcnt++;
key[x]=k;
priority[x]=rand();
child[x][0]=child[x][1]=0;
}
update(x);
} void _erase(int &x,int k)
{
int flag=cmp(key[x],k);
if(flag==-1)
{ if(child[x][0]==0&&child[x][1]==0){x=0;return ;}
int t=priority[child[x][0]]>priority[child[x][1]];
rotate(x,t);
_erase(x,k); }else
{
_erase(child[x][flag],k);
}
update(x);
} int _getKth(int &x,int k)
{
if(k>size[x])return -1;
if(k<=size[child[x][0]])return _getKth(child[x][0],k);
k-=size[child[x][0]]+1;
if(k<=0)return key[x];
return _getKth(child[x][1],k);
}
inline int find(int x)
{
if(0==group[x])return x;
else return group[x]=find(group[x]);
}
inline void combine(int a,int b)
{
a=find(a);b=find(b);
if(a==b)return ;
if(num[a]>1){treaptot--;_erase(root,a);}
if(num[b]>1){treaptot--;_erase(root,b);}
group[b]=a;
num[a]+=num[b];
treaptot++;_insert(root,a);
}
int main()
{freopen("t.txt","r",stdin);
freopen("1.txt","w",stdout);
srand(100717);
int n,m;
Treap();
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{num[i]=1;group[i]=0;}
int flag,a,b;
for(int i=1;i<=m;i++)
{ scanf("%d",&flag);
if(flag)
{
scanf("%d",&a);
if(a>size[root])printf("1\n");
else printf("%d\n",num[_getKth(root,treaptot-a+1)]);
}
else
{
n--;
scanf("%d%d",&a,&b);
combine(a,b);
}
} return 0;
}

  

POJ2985 The k-th Largest Group treap的更多相关文章

  1. POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8807   Accepted ...

  2. 【POJ2985】【Treap + 并查集】The k-th Largest Group

    Description Newman likes playing with cats. He possesses lots of cats in his home. Because the numbe ...

  3. POJ2985 The k-th Largest Group (并查集+treap)

    Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is ...

  4. POJ 2985 The k-th Largest Group(树状数组 并查集/查找第k大的数)

    传送门 The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8690   Acce ...

  5. poj 2985 The k-th Largest Group 树状数组求第K大

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8353   Accepted ...

  6. [POJ2985]The k-th Largest Group

    Problem 刚开始,每个数一个块. 有两个操作:0 x y 合并x,y所在的块 1 x 查询第x大的块 Solution 用并查集合并时,把原来的大小删去,加上两个块的大小和. Notice 非旋 ...

  7. Gym - 101915D Largest Group 最大团

    给你一个二分图 问你最大团为多大 解一:状压DP 解二:二分图最大匹配 二分图的最大团=补图的最大独立集 二分图最大独立集=二分图定点个数-最大匹配 //Hungary #include<bit ...

  8. 【LeetCode】1399. 统计最大组的数目 Count Largest Group

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接求 日期 题目地址:https://leetcod ...

  9. Gym-101915D Largest Group 最大独立集 Or 状态压缩DP

    题面题意:给你N个男生,N个女生,男生与男生之间都是朋友,女生之间也是,再给你m个关系,告诉你哪些男女是朋友,最后问你最多选几个人出来,大家互相是朋友. N最多为20 题解:很显然就像二分图了,男生一 ...

随机推荐

  1. 习题练习(视觉slam14讲课后习题)

    设有⼩萝⼘1⼀号和⼩萝⼘⼆号位于世界坐标系中. ⼩萝⼘⼀号的位姿为:q1 = [0.55, 0.3, 0.2, 0.2], t1 = [0.7, 1.1, 0.2]T(q 的第⼀项为实部).这⾥的 q ...

  2. CMU Database Systems - Two-phase Locking

    首先锁是用来做互斥的,解决并发执行时的数据不一致问题 如图会导致,不可重复读 如果这里用lock就可以解决,数据库里面有个LockManager来作为master,负责锁的记录和授权 数据库里面的基本 ...

  3. 获取最新ADT下载地址的方法

    最近网络不给力,谷歌上不去,想下个最新的ADT插件也难,于是寻找方法,最后找到一个不错的方法,问题解决过程如下(别嫌我啰嗦啊). 网上有人分享过下载ADT插件的页面地址:install-adt.htm ...

  4. PHP 生成器Generators的入门理解和学习

    什么是生成器Generators 生成器允许你在 foreach 代码块中写代码来迭代一组数据而不需要在内存中创建一个数组, 那会使你的内存达到上限,或者会占据可观的处理时间.相反,你可以写一个生成器 ...

  5. TensorFlow — 相关 API

    TensorFlow — 相关 API TensorFlow 相关函数理解 任务时间:时间未知 tf.truncated_normal truncated_normal( shape, mean=0. ...

  6. Python基础(八)装饰器

    今天我们来介绍一下可以提升python代码逼格的东西——装饰器.在学习装饰器之前我们先来复习一下函数的几个小点,方便更好的理解装饰器的含义. 一.知识点复习 1, 在函数中f1和f1()有什么不同,f ...

  7. 2014年武汉的IT行情好像不太好(续):20个月过后,再看当时面试过的几个公司--武汉财富基石-崩盘,辣妈萌宝-创业失败,朋友公司转交他人管理

     2014年9月的时候,写过一篇面试的总结性质的文章,"2014年武汉的IT行情好像不太好". 原文地址:blog.csdn.net/fansunion/article/detai ...

  8. [luoguP1736] 创意吃鱼法(DP)

    传送门 f[i][j][0] 表示从右下角到左上角,以(i,j)为起点能延伸的最大值 f[i][j][1] 表示从左下角到右上角,以(i,j)为起点能延伸的最大值 up[i][j] 表示(i,j)上面 ...

  9. 夜话JAVA设计模式之策略模式

    策略模式     定义了算法簇,分别封装起来,让他们之间可以互相替换,让算法簇的变化独立于使用算法的客户.设计原则1     找出应用中可能需要变化之处,把他们独立出来,不要和那些不需要变化的代码混在 ...

  10. BZOJ(5) 1083: [SCOI2005]繁忙的都市

    1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4105  Solved: 2595[Submit][Sta ...