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 题解:很显然就像二分图了,男生一 ...
随机推荐
- (转)MySQL中的索引详讲
序言 之前写到MySQL对表的增删改查(查询最为重要)后,就感觉MySQL就差不多学完了,没有想继续学下去的心态了,原因可能是由于别人的影响,觉得对于MySQL来说,知道了一些复杂的查询,就够了,但是 ...
- telnet mysql3306端口失败
在linux上telnet远程mysql端口失败,经过上网查找后,找到多种方法. (1)我在本地的Navicat上新增了一个用户,主机名是linux的ip,也可以是 %(百分号代表这个用户可以在任何地 ...
- php第二十六节课
会话购物车 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- 洛谷——P3919 【模板】可持久化数组(可持久化线段树/平衡树)
P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目背景 UPDATE : 最后一个点时间空间已经放大 标题即题意 有了可持久化数组,便可以实现很多衍生的可持久化功能(例如:可持久化并查集 ...
- JS中遍历EL表达式中后台传过来的Java集合
前言:在我的项目里有这么一个情况,后台直接model.addAttribute()存储了一个对象,此对象内部有一个集合,前端JSP处理的方法正常情况下就是直接使用EL表达式即可.但是如果在JS中需要使 ...
- JAVA之文件操作1,2,3,4
package first_program; import java.io.File; import java.io.IOException; public class num_1v { public ...
- POJ - 2007 极角排序(Java 实现)
POJ 2007 将所有的点按逆时针输出 import java.io.*; import java.util.*; public class Main { static class Point im ...
- https://gitee.com/tomsun28/bootshiro-------需要研究的项目
https://gitee.com/tomsun28/bootshiro-------需要研究的项目
- Codeforces Round #219 (Div. 2) D题
D. Counting Rectangles is Fun time limit per test 4 seconds memory limit per test 256 megabytes inpu ...
- 【页面传值6种方式】- 【JSP 页面传值方法总结:4种】 - 【跨页面传值的几种简单方式3种】
阅读目录 1. URL 链接后追加参数 2. Form 3. 设置 Cookie 4. 设置 Session JSP 页面间传递参数是项目中经常需要的,这应该算是 web 基本功吧. 试着将各种方式总 ...