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 题解:很显然就像二分图了,男生一 ...
随机推荐
- oracle_backup
#!/bin/bash DAYS=`date +"%Y%m%d"` . /home/oracle/.bash_profile # /home/opt/oracle/11g/bin/ ...
- 我的第一个随笔——MarkDown的简单用法!
目录 我的第一个笔记 1. 学习简单的markdown语法 1.1 标题 1.2 引用 1.3 倾斜与加粗 1.4无序列表 1.5有序列表 1.6图片和网页 1.7分割线 1.8代码块 1.9结尾 2 ...
- [Luogu] P1407 [国家集训队]稳定婚姻
题目描述 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的姗姗和男友谈恋爱半年就结婚,结婚不到 ...
- FileInputStream实现读取文件内容并输出到屏幕上
java输入输出流是站在程序的角度来说的.从文件中读取数据用输入流,向文件中写数据用输出流. package com.janson.day20180827; import java.io.FileIn ...
- POJ 3468 A Simple Problem with Integers(线段树水题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 135904 ...
- Introduction to TensorFlow
Lecture note 1: Introduction to TensorFlow Why TensorFlow TensorFlow was originally created by resea ...
- [luoguP1130] 红牌(DP)
传送门 幼儿园DP. ——代码 #include <cstdio> #include <iostream> ; << ); int a[MAXN][MAXN], f ...
- noip模拟赛 whzzt-Conscience
分析:数据中并不存在无解的情况...... 每个摄像头都要覆盖尽可能多的点,按照y从小到大排序.对于每一列,只用判断第一个没有被观测到的就可以了,这个点必须要放摄像头,因为除了它自己没有其它的摄像头能 ...
- [K/3Cloud]ksql翻译札记
2011-11-16 又学一招,集合转化临时表的方法: var sql = string.Format(@"select b.FENTRYID,a.{2} from {0} a inner ...
- Inversion
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4176 Accepted: 1857 Description The i ...