POJ2985 The k-th Largest Group treap
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的更多相关文章
- POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8807 Accepted ...
- 【POJ2985】【Treap + 并查集】The k-th Largest Group
Description Newman likes playing with cats. He possesses lots of cats in his home. Because the numbe ...
- 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 ...
- POJ 2985 The k-th Largest Group(树状数组 并查集/查找第k大的数)
传送门 The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8690 Acce ...
- poj 2985 The k-th Largest Group 树状数组求第K大
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8353 Accepted ...
- [POJ2985]The k-th Largest Group
Problem 刚开始,每个数一个块. 有两个操作:0 x y 合并x,y所在的块 1 x 查询第x大的块 Solution 用并查集合并时,把原来的大小删去,加上两个块的大小和. Notice 非旋 ...
- Gym - 101915D Largest Group 最大团
给你一个二分图 问你最大团为多大 解一:状压DP 解二:二分图最大匹配 二分图的最大团=补图的最大独立集 二分图最大独立集=二分图定点个数-最大匹配 //Hungary #include<bit ...
- 【LeetCode】1399. 统计最大组的数目 Count Largest Group
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接求 日期 题目地址:https://leetcod ...
- Gym-101915D Largest Group 最大独立集 Or 状态压缩DP
题面题意:给你N个男生,N个女生,男生与男生之间都是朋友,女生之间也是,再给你m个关系,告诉你哪些男女是朋友,最后问你最多选几个人出来,大家互相是朋友. N最多为20 题解:很显然就像二分图了,男生一 ...
随机推荐
- Microsoft SQL Server 存储过程
Microsoft SQL Server 存储过程 TRIGGER DDL触发器:主要用于防止对数据库架构.视图.表.存储过程等进行的某些修改:DDL事件是指对数据库CREATE,ALTER,DROP ...
- 文艺平衡树-splay的区间操作
真的是个神题,蒟蒻表示无力吐槽.刚开始以为是一个板子题,看着题解打了一遍,大概也理解了他是怎么实现的,然后我就去做别的题了,然后就在Three_D大佬的询问下蒙*了.最后还是问的nc哥,并思考了一个中 ...
- UVA - 1611 Crane (思路题)
题目: 输入一个1~n(1≤n≤300)的排列,用不超过96次操作把它变成升序.每次操作都可以选一个长度为偶数的连续区间,交换前一半和后一半.输出每次操作选择的区间的第一个和最后一个元素. 思路: 注 ...
- UVA - 12325 Zombie's Treasure Chest (分类搜索)
题目: 有一个体积为N的箱子和两种数量无限的宝物.宝物1的体积为S1,价值为V1:宝物2的体积为S2,价值为V2.输入均为32位带符号整数.计算最多能装多大价值的宝物,每种宝物都必须拿非负整数个. 思 ...
- Oracle 数据库启动与关闭 各种方式详解整理
概述 只有具备sysdba和sysoper系统特权的用户才能启动和关闭数据库. 在启动数据库之前应该启动监听程序,否则就不能利用命令方式来管理数据库,包括启动和关闭数据库. 虽然数据库正常运行,但如果 ...
- python正则表达式提取字符串
用python正则表达式提取字符串 在日常工作中经常遇见在文本中提取特定位置字符串的需求.python的正则性能好,很适合做这类字符串的提取,这里讲一下提取的技巧,正则表达式的基础知识就不说了,有兴趣 ...
- BNUOJ 19792 Airport Express
Airport Express Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on UVA. Origin ...
- noip模拟赛 猜数字
题目描述 LYK在玩猜数字游戏. 总共有n个互不相同的正整数,LYK每次猜一段区间的最小值.形如[li,ri]这段区间的数字的最小值一定等于xi. 我们总能构造出一种方案使得LYK满意.直到…… LY ...
- hdu 2255KM算法模板
#include<stdio.h> #include<string.h> #define N 400 #define inf 0x7fffffff int Max(int a ...
- Mzc家中的男家丁
题目背景 mzc与djn的…还没有众人皆知,所以我们要来宣传一下. 题目描述 mzc家很有钱(开玩笑),他家有n个男家丁,现在mzc要将她们全都聚集起来(干什么就不知道了).现在知道mzc与男家丁们互 ...