[poj2985]The k-th Largest Group[Treap]

  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <cstring>
  6 #include <cmath>
  7 #include <ctime>
  8
  9 using namespace std;
 10
 11 class    Treap
 12 {
 13     private:
 14         struct    Treap_Point
 15         {
 16             int    l,r,val,size,key,num;
 17             Treap_Point() { l=r=val=size=key=num=0; }
 18         };
 19         typedef    Treap_Point    P;
 20         P    d[210000];
 21         int    root,cnt;
 22
 23         void    update(const int t)
 24         {
 25             d[t].size=d[d[t].l].size+
 26                 d[d[t].r].size+d[t].num;
 27             return ;
 28         }
 29
 30         void    rturn(int & t)
 31         {
 32             int    temp=d[t].l; d[t].l=d[temp].r; d[temp].r=t;
 33             d[temp].size=d[t].size; update(t);t=temp; return ;
 34         }
 35
 36         void    lturn(int & t)
 37         {
 38             int    temp=d[t].r; d[t].r=d[temp].l; d[temp].l=t;
 39             d[temp].size=d[t].size; update(t);t=temp; return ;
 40         }
 41
 42         void    insert(int & t,const int x)
 43         {
 44             if(!t)
 45             {
 46                 cnt++;t=cnt;
 47                 d[t].size=d[t].num=1;d[t].val=x;
 48                 d[t].key=rand();
 49                 return ;
 50             }
 51             d[t].size++;
 52             if(d[t].val==x)d[t].num++;
 53             else if(x>d[t].val)
 54             {
 55                 insert(d[t].r,x);
 56                 if(d[d[t].r].key<d[t].key)lturn(t);
 57             }
 58             else
 59             {
 60                 insert(d[t].l,x);
 61                 if(d[d[t].l].key<d[t].key)rturn(t);
 62             }
 63             return ;
 64         }
 65
 66         void    erase(int & t,const int x)
 67         {
 68             if(!t)return ;
 69             if(d[t].val==x)
 70             {
 71                 if(d[t].num>1)
 72                 {
 73                     d[t].num--;d[t].size--;return ;
 74                 }
 75                 if(d[t].l*d[t].r==0)t=d[t].l+d[t].r;
 76                 else if(d[d[t].l].key<d[d[t].r].key)
 77                     rturn(t),erase(t,x);
 78                 else    lturn(t),erase(t,x);
 79             }
 80             else if(x>d[t].val)d[t].size--,erase(d[t].r,x);
 81             else    d[t].size--,erase(d[t].l,x);
 82             return ;
 83         }
 84
 85         int    get(const int & t,const int x)
 86         {
 87             if(t==0)return 0;
 88             if(x<=d[d[t].l].size)
 89                 return get(d[t].l,x);
 90             else if(x>d[d[t].l].size+d[t].num)
 91                 return get(d[t].r,x-d[d[t].l].size-d[t].num);
 92             return d[t].val;
 93         }
 94     public:
 95         Treap() { root=0;cnt=0; }
 96
 97         void    insert(const int x) { insert(root,x); }
 98         void    erase(const int x) { erase(root,x); }
 99         int    get(const int x) { return get(root,x); }
100 };
101
102 int    n,m;
103 int    fa[210000],Size[210000];
104 Treap    S;
105
106 int    get_anc(const int x)
107 {
108     return fa[x]==x ? x:fa[x]=get_anc(fa[x]);
109 }
110
111 int main()
112 {
113     int    i,op,x,y,a,b;
114
115     scanf("%d%d",&n,&m);
116
117     for(i=1;i<=n;++i)fa[i]=i,S.insert(-1),Size[i]=1;
118
119     for(i=1;i<=m;++i)
120     {
121         scanf("%d",&op);
122         if(op==1)
123         {
124             scanf("%d",&x);
125             printf("%d\n",-S.get(x));
126         }
127         else
128         {
129             scanf("%d%d",&x,&y);
130             a=get_anc(x),b=get_anc(y);
131             if(a==b)continue;
132             else
133             {
134                 S.erase(-Size[a]);
135                 S.erase(-Size[b]);
136                 S.insert(-Size[a]-Size[b]);
137                 fa[a]=b;
138                 Size[b]+=Size[a];
139             }
140         }
141     }
142
143     return 0;
144 }
时间: 2024-08-06 03:41:07

[poj2985]The k-th Largest Group[Treap]的相关文章

poj 2985 The k-th Largest Group 求第K大数 Treap, Binary Index Tree, Segment Tree

题目链接:点击打开链接 题意:有两种操作,合并集合,查询第K大集合的元素个数.(总操作次数为2*10^5) 解法: 1.Treap 2.树状数组 |-二分找第K大数 |-二进制思想,逼近第K大数 3.线段树 4.... Treap模板(静态数组) #include <math.h> #include <time.h> #include <stdio.h> #include <limits.h> #include <stdlib.h> const

【POJ2985】【Treap + 并查集】The k-th Largest Group

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 occ

POJ2985 The k-th Largest Group[树状数组求第k大值 并查集]

The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8807   Accepted: 2875 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 g

POJ 2985 The k-th Largest Group(树状数组 并查集/查找第k大的数)

传送门 The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8690   Accepted: 2847 Description Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants

hdu 2985 The k-th Largest Group 树状数组求第K大

The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8353   Accepted: 2712 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 g

POJ2985 The k-th Largest Group

题解: 并查集+树状数组+二分 注意将第k大数,转换为第tot+1-k小数, 就可以用用树状数组维护了 注意tot是动态的,在每次合并时都需要更新 代码: #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<map> #include<set> using namespace std; using namespace st

Gym - 101915D Largest Group 最大团

给你一个二分图 问你最大团为多大 解一:状压DP 解二:二分图最大匹配 二分图的最大团=补图的最大独立集 二分图最大独立集=二分图定点个数-最大匹配 //Hungary #include<bits/stdc++.h> using namespace std; #define N 50 int useif[N]; //记录y中节点是否使用 0表示没有访问过,1为访问过 int link[N]; //记录当前与y节点相连的x的节点 int mat[N][N]; //记录连接x和y的边,如果i和j之

Gym - 101915D Largest Group 最大独立集 Or 状态压缩DP

题面题意:给你N个男生,N个女生,男生与男生之间都是朋友,女生之间也是,再给你m个关系,告诉你哪些男女是朋友,最后问你最多选几个人出来,大家互相是朋友. N最多为20 题解:很显然就像二分图了,男生一边女生一边的,然后一种解法就是 求图的最大独立集,(看起来很巧,实则也是一种套路) (最大独立集是一个点集,其中任意两点在图中无对应边,对于一般图来说,最大独立集是一个NP完全问题,对于二分图来说最大独立集=|V|-二分图的最大匹配数) 我们本来连边是,所有的朋友关系连边(男女就行了,同性都可以忽略

POJ 2761 Feed the dogs(树状数组求区间第K大)

题目链接: 戳我 题目大意:Jiajia要为宠物狗,宠物狗按成一排站好(1 < i <= n),第 i 只狗的喜欢程度是 a[i], 之后他会先喂某个区间内第k个 即 n 个数, m个询问,接着是 n个数 接下来 m 行,每行是 l r k即 l 到 r 这个区间第 k 小的数,每个询问输出一个答案,即 a[i] 求区间第k大有很多算法, 详见此博客 [数据结构练习] 求区间第K大数的几种方法 我用的树状数组解法,来自 树状数组从前往后求和,用来解第k大(或小)的数 poj 2985 The