POJ 2985 Treap平衡树(求第k大的元素)
这题也能够用树状数组做,并且树状数组姿势更加优美。代码更加少,只是这个Treap树就是求第K大元素的专家……所以速度比較快。
这个也是从那本红书上拿的模板……自己找了资料百度了好久,才理解这个Treap主要的知识,要是自己写真的得写到什么时候啊!。。
然后输入的时候是写n-k+1反着找的,就是这里又浪费了好多时间debug。唉……
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define sc(a,b) scanf("%d%d",&a,&b)
#define pri(a) printf("%d\n",a)
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define MM 440004
#define MN 1008
#define INF 100000007
#define eps 1e-7
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
struct Treap
{
int root,treapCnt,key[MM],priority[MM],childs[MM][2],cnt[MM],size[MM];
Treap()
{
root=0;
treapCnt=1;
priority[0]=INF;
size[0]=0;
}
void update(int x)
{
size[x]=size[childs[x][0]]+cnt[x]+size[childs[x][1]];
}
void rotate(int &x,int t)
{
int y=childs[x][t];
childs[x][t]=childs[y][1-t];
childs[y][1-t]=x;
update(x);
update(y);
x=y;
}
void _insert(int &x,int k)
{
if(x)
{
if(key[x]==k) cnt[x]++;
else
{
int t=key[x]<k;
_insert(childs[x][t],k);
if(priority[childs[x][t]]<priority[x]) rotate(x,t);
}
}
else
{
x=treapCnt++;
key[x]=k;
cnt[x]=1;
priority[x]=rand();
childs[x][0]=childs[x][1]=0;
}
update(x);
}
void _erase(int &x,int k)
{
if(key[x]==k)
{
if(cnt[x]>1) cnt[x]--;
else
{
if(childs[x][0]==0&&childs[x][1]==0)
{
x=0;
return ;
}
int t=priority[childs[x][0]]>priority[childs[x][1]];
rotate(x,t);
_erase(x,k);
}
}
else _erase(childs[x][key[x]<k],k);
update(x);
}
int _getkth(int &x,int k)
{
if(k<=size[childs[x][0]]) return _getkth(childs[x][0],k);
k-=size[childs[x][0]]+cnt[x];
if(k<=0) return key[x];
return _getkth(childs[x][1],k);
}
void insert(int k)
{
_insert(root,k);
}
void erase(int k)
{
_erase(root,k);
}
int getkth(int k)
{
return _getkth(root,k);
}
}treap;
int f[MM],a[MM];
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
int main()
{
int n,m,i,k,x,y;
sc(n,m);
for(i=0;i<=n;i++) f[i]=i,a[i]=1; //初始每一个区间的元素都是1咯。自身嘛。
for(i=1;i<=n;i++) treap.insert(1);
for(i=1;i<=m;i++)
{
sca(k);
if(!k)
{
sc(x,y);
x=find(x),y=find(y);
if(x==y) continue;
f[y]=x;
treap.erase(a[x]); //先把这两个区间删除了
treap.erase(a[y]);
a[x]+=a[y]; //区间合并。元素相加
treap.insert(a[x]); //然后把合并的加到treap平衡树中
n--; //合并区间,总区间少1
}
else
{
sca(k);
pri(treap.getkth(n-k+1)); //反着找
}
}
return 0;
}
POJ 2985 Treap平衡树(求第k大的元素)的更多相关文章
- [LeetCode] Kth Largest Element in a Stream 数据流中的第K大的元素
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
- poj 2985 The k-th Largest Group 树状数组求第K大
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8353 Accepted ...
- 《数据结构与算法分析:C语言描述》读书笔记------练习1.1 求第K大的数
求一组N个数中的第k个最大者,设k=N/2. import java.util.Random; public class K_Max { /** * @param args */ //求第K大的数,保 ...
- 面试题:求第K大元素(topK)?
一.引言二.普通算法算法A:算法B:三.较好算法算法C:算法D:四.总结 一.引言 这就是类似求Top(K)问题,什么意思呢?怎么在无序数组中找到第几(K)大元素?我们这里不考虑海量数据,能装入内 ...
- 快排法求第k大
快排法求第k大,复杂度为O(n) import com.sun.media.sound.SoftTuning; import java.util.Arrays; import java.util.Ra ...
- HDU 5249 离线树状数组求第k大+离散化
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 2639 01背包求第k大
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- BZOJ2006:超级钢琴(ST表+堆求前K大区间和)
Description 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙度 ...
- ACM_求第k大元素(两次二分)
求第k大 Time Limit: 6000/3000ms (Java/Others) Problem Description: 给定两个数组A和B,大小为N,M,每次从两个数组各取一个数相乘放入数组C ...
随机推荐
- mac 安装 python 配置||虚拟环境
前篇:http://www.cnblogs.com/ostrich-sunshine/p/8747791.html 介绍了 Mac 下 python 的一些相关知识. 这篇介绍 python3 的安装 ...
- pat 甲级 1038. Recover the Smallest Number (30)
1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- .bat 处理错误码
某些命令只会返回error level,而要添加互操作性,.bat文件的返回值,exit code应该最后指定,那么需要,在这个单行的命令后面单个添加处理error level 的语句,最后再做统一的 ...
- linux解决无法打开资源管理器
前两天升级系统,使用命令pacman -Syyu,大概是使用的是testing缘故,今天发现dolphin无法打开了,使用命令行打开,提示ldmp.so有问题. 解决方法如下: 一,使用命令:pacm ...
- 写文章 TEE技术分析【转】
转自:https://zhuanlan.zhihu.com/p/24222064 首先介绍一下TEE的主要关键技术: 1.安全启动(Secure Boot) 安全启动技术可以用于需要防止篡改系统镜像, ...
- Qualcomm MPM introduction
在 Qualcomm chip 裡有一個 hardware block 名為 MPM. 在 RPM CPU halts 後,MPM 會保持在 on 的狀態用來處理 全系統 sleep process ...
- CentOS 7系统添加启动项
CentOS 7系统已经把CentOS 6的 runlevel 系统服务管理替换成了systemd.在 /etc/rc[0-6S].d 下添加启动项已经不能在系统启动的时候自动执行,需要通过新的 sy ...
- 【剑指offer】数组中重复的数字
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- asp.net core 开发环境自定义域名及端口
web项目上右键-> 选择属性-> 选择调试->编辑“应用url” ,再用vs启动web项目即可实现自定义url. 值得注意的是如果报“visual studio无法连接到iis e ...
- 基于 OpenResty 的动态服务路由方案
2019 年 5 月 11 日,OpenResty 社区联合又拍云,举办 OpenResty × Open Talk 全国巡回沙龙武汉站,又拍云首席布道师在活动上做了< 基于 OpenResty ...