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. JAVA程序员面试笔试宝典2

    1.Java集合框架 2.迭代器 使用容器的iterator()方法返回一个iterator,然后通过iterator的next()方法返回第一个元素 使用iterator的hasnext()方法判断 ...

  2. 事件的节流(throttle)与防抖(debounce)

    事件的节流(throttle)与防抖(debounce) 有些浏览器事件可以在短时间内快速触发多次,比如调整窗口大小或向下滚动页面.例如,监听页面窗口滚动事件,并且用户持续快速地向下滚动页面,那么滚动 ...

  3. BZOJ1509: [NOI2003]逃学的小孩 (树形DP)

    题意:给一棵树 选三个点A,B,C 求A到B的再从B到C的距离最大值 需要满足AB的距离小于AC的距离 题解:首先树上的最大距离就想到了直径 但是被样例误导了TAT BC两点构成了直径 我一开始以为A ...

  4. [Git]Please make sure you have the correct access rights and the repository exists

    这个问题是这样,需要在已有github账号的A机器上,再创建一个github账号,新账号创建完毕,将代码通过机器A push上之后,再另一台机器B,clone 这个项目时报出了如下错误: Permis ...

  5. CCF201604-1 折点计数 java(100分)

    试题编号: 201604-1 试题名称: 折点计数 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定n个整数表示一个商店连续n天的销售量.如果某天之前销售量在增长,而后一天 ...

  6. Hashing - Hard Version

    Hashing - Hard Version Given a hash table of size N, we can define a hash function . Suppose that th ...

  7. 【Codeforces 489D】Unbearable Controversy of Being

    [链接] 我是链接,点我呀:) [题意] 让你找到(a,b,c,d)的个数 这4个点之间有4条边有向边 (a,b)(b,c) (a,d)(d,c) 即有两条从a到b的路径,且这两条路径分别经过b和d到 ...

  8. Linux下汇编语言学习笔记80 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  9. Stealing Harry Potter's Precious BFS+DFS

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  10. 几道splay

    hdu 1890 题意:每次将第i位到第i小数字所在的位置之间的位置翻转,每次输出第i小数字所在的位置 分析: 简单的splay处理区间翻转问题 有三点需要注意: 1.区间是1~n+2 2.此题里的查 ...