Time Limit: 2000MS | Memory Limit: 131072K | |
Total Submissions: 9120 | Accepted: 2979 |
Description
Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants to group some of the cats. To do that, he first offers a number to each of the cat (1, 2, 3, …, n). Then he occasionally combines the group cat i is in and the group cat j is in, thus creating a new group. On top of that, Newman wants to know the size of the k-th biggest group at any time. So, being a friend of Newman, can you help him?
Input
1st line: Two numbers N and M (1 ≤ N, M ≤ 200,000), namely the number of cats and the number of operations.
2nd to (m + 1)-th line: In each line, there is number C specifying the kind of operation Newman wants to do. If C = 0, then there are two numbers i and j (1 ≤ i, j ≤ n) following indicating Newman wants to combine the group containing the two cats (in case these two cats are in the same group, just do nothing); If C = 1, then there is only one number k (1 ≤ k ≤ the current number of groups) following indicating Newman wants to know the size of the k-th largest group.
Output
For every operation “1” in the input, output one number per line, specifying the size of the kth largest group.
Sample Input
10 10 0 1 2 1 4 0 3 4 1 2 0 5 6 1 1 0 7 8 1 1 0 9 10 1 1
Sample Output
1 2 2 2 2
Hint
When there are three numbers 2 and 2 and 1, the 2nd largest number is 2 and the 3rd largest number is 1.
Source
思路:Treap中存的是每个团队的猫的数量。每次合并的时候,是属于两个团队,那就将原来两个团队的数量从Treap中删除,然后在Treap中加入两个团队数量和,再用并查集合并两个团队。输出的时候,优先从右子树开始查找就行了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX_N 200005
using namespace std;
int pre[MAX_N];
bool t[MAX_N];
int root,size;
int Find(int x)
{
if(x!=pre[x]) pre[x]=Find(pre[x]);
return pre[x];
}
int mix(int x,int y)
{
int fx=Find(x),fy=Find(y);
if(fx!=fy)
{
pre[fy]=fx;
return 1;
}
return 0;
}
struct data
{
int l,r,v,w,size,rnd;
}tr[MAX_N];
void update(int k)
{
tr[k].size=tr[tr[k].l].size+tr[tr[k].r].size+tr[k].w;
}
void rturn(int &k)
{
int t=tr[k].l;tr[k].l=tr[t].r;tr[t].r=k;
tr[t].size=tr[k].size;update(k);k=t;
}
void lturn(int &k)
{
int t=tr[k].r;tr[k].r=tr[t].l;tr[t].l=k;
tr[t].size=tr[k].size;update(k);k=t;
}
void insert(int &k,int x)
{
if(k==0)
{
size++;k=size;
tr[k].w=tr[k].size=1;tr[k].rnd=rand();
tr[k].v=x;
return;
}
tr[k].size++;
if(x==tr[k].v) tr[k].w++;
else if(x>tr[k].v)
{
insert(tr[k].r,x);
if(tr[tr[k].r].rnd<tr[k].rnd) lturn(k);
}
else
{
insert(tr[k].l,x);
if(tr[tr[k].l].rnd<tr[k].rnd) rturn(k);
}
}
void del(int &k,int x)
{
if(k==0) return;
if(tr[k].v==x)
{
if(tr[k].w>1)
{
tr[k].w--;tr[k].size--;return ;
}
if(tr[k].l*tr[k].r==0) k=tr[k].l+tr[k].r;
else if(tr[tr[k].l].rnd<tr[k].rnd)
rturn(k),del(k,x);
else
lturn(k),del(k,x);
}
else if(x>tr[k].v)
tr[k].size--,del(tr[k].r,x);
else tr[k].size--,del(tr[k].l,x);
}
int query_num(int k,int x)
{
if(k==0) return 0;
if(x<=tr[tr[k].r].size)
return query_num(tr[k].r,x);
else if(x>tr[tr[k].r].size+tr[k].w)
return query_num(tr[k].l,x-tr[tr[k].r].size-tr[k].w);
else return tr[k].v;
}
int res[MAX_N];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
root=0,size=0;
memset(res,0,sizeof(res));
for(int i=0;i<n;i++)
{
tr[i].l=0;
tr[i].r=0;
}
for(int i=1;i<=n;i++)
insert(root,1),pre[i]=i,res[i]=1;
int c,x,y;
for(int i=1;i<=m;i++)
{
scanf("%d",&c);
if(c==0)
{
scanf("%d%d",&x,&y);
if(Find(x)!=Find(y))//当两个猫是属于两个团队的时候才进行操作
{
del(root,res[Find(x)]);
del(root,res[Find(y)]);
res[Find(x)]+=res[Find(y)];
mix(x,y);
insert(root,res[Find(x)]);
}
}
else
{
scanf("%d",&x);
printf("%d\n",query_num(root,x));
}
}
}
return 0;
}