模板题,以后要学splay,大概看一下treap就好了。

 #include <cstdio>
#include <algorithm>
#include <cstring> using namespace std; const int maxn = 3e5+;
int num[maxn],st[maxn]; struct Treap{
int size;
int key,fix;
Treap *ch[]; Treap(int key)
{
size = ;
fix = rand();
this->key = key;
ch[] = ch[] = NULL;
}
int compare(int x) const
{
if(x == key) return -;
return x < key ? : ;
}
void Maintain()
{
size = ;
if(ch[] != NULL) size += ch[]->size;
if(ch[] != NULL) size += ch[]->size;
}
}; void Rotate(Treap* &t ,int d)
{
Treap *k = t->ch[d^];
t->ch[d^] = k->ch[d];
k->ch[d] = t;
t->Maintain();
k->Maintain();
t = k;
} void Insert(Treap* &t,int x)
{
if(t == NULL) t = new Treap(x);
else
{
int d = x < t->key ? : ;
Insert(t->ch[d],x);
if(t->ch[d]->fix > t->fix)
Rotate(t,d^);
}
t->Maintain();
} void Delete(Treap* &t,int x)
{
int d = t->compare(x);
if(d == -)
{
Treap *tmp = t;
if(t->ch[] == NULL)
{
t = t->ch[];
delete tmp;
tmp = NULL;
}
else if(t->ch[] == NULL)
{
t = t->ch[];
delete tmp;
tmp = NULL;
}
else
{
int k = t->ch[]->fix > t->ch[]->fix ? : ;
Rotate(t,k);
Delete(t->ch[k],x);
}
}
else Delete(t->ch[d],x);
if(t!=NULL) t->Maintain();
} bool Find(Treap *t,int x)
{
while(t != NULL)
{
int d = t->compare(x);
if(d == -) return true;
t = t->ch[d];
}
return false;
} int Kth(Treap *t,int k)
{
if(t == NULL || k <= || k > t->size)
return -;
if(t->ch[] == NULL && k == )
return t->key;
if(t->ch[] == NULL)
return Kth(t->ch[],k-);
if(t->ch[]->size >= k)
return Kth(t->ch[],k);
if(t->ch[]->size + == k)
return t->key;
return Kth(t->ch[],k--t->ch[]->size);
} int Rank(Treap *t,int x)
{
int r;
if(t->ch[] == NULL) r = ;
else r = t->ch[]->size;
if(x == t->key) return r+;
if(x < t->key)
return Rank(t->ch[],x);
return r++Rank(t->ch[],x);
} void DeleteTreap(Treap* &t)
{
if(t == NULL) return;
if(t->ch[] != NULL) DeleteTreap(t->ch[]);
if(t->ch[] != NULL) DeleteTreap(t->ch[]);
delete t;
t = NULL;
} int save[maxn];
int M,N; int main()
{
while(~scanf("%d%d",&M,&N))
{
for(int i=;i<=M;i++)
scanf("%d",&save[i]);
Treap *root = NULL;
int cnt = ;
for(int i=,x;i<=N;i++)
{
scanf("%d",&x);
for(int j = cnt;j <= x;j++)
Insert(root,save[j]);
cnt = x+;
printf("%d\n",Kth(root,i));
}
DeleteTreap(root);
}
}

POJ1442-查询第K大-Treap模板题的更多相关文章

  1. PAT天梯赛练习题 L3-002. 堆栈(线段树查询第K大值或主席树)

    L3-002. 堆栈 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家都知道“堆栈”是一种“先进后出”的线性结构,基本操作有 ...

  2. 在洛谷3369 Treap模板题 中发现的Splay详解

    本题的Splay写法(无指针Splay超详细) 前言 首先来讲...终于调出来了55555...调了整整3天..... 看到大部分大佬都是用指针来实现的Splay.小的只是按照Splay的核心思想和原 ...

  3. 树状数组+二分答案查询第k大的数 (团体程序设计天梯赛 L3-002. 堆栈)

    前提是数的范围较小 1 数据范围:O(n) 2 查第k大的数i:log(n)(树状数组查询小于等于i的数目)*log(n)(二分找到i) 3 添加:log(n) (树状数组) 4 删除:log(n) ...

  4. BZOJ 3224 普通平衡树(Treap模板题)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 14301  Solved: 6208 [Submit][ ...

  5. POJ 3481 Double Queue(Treap模板题)

    Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15786   Accepted: 6998 Des ...

  6. hdu-3549 Flow Problem---最大流模板题(dinic算法模板)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 题目大意: 给有向图,求1-n的最大流 解题思路: 直接套模板,注意有重边 传送门:网络流入门 ...

  7. hdu-1532 Drainage Ditches---最大流模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意: 给出有向图以及边的最大容量,求从1到n的最大流 思路: 传送门:最大流的增广路算法 ...

  8. 【大杀器】利用划分树秒杀区间内第k大的数

    最近看了一道题,大概就是给出一个序列,不断询问其子区间内第k大的数,下面是个截图 绕了一圈没找到中文版题目,if(你是大佬) then 去看截图:else{我来解释:给出一个整数n,和一个整数m,分别 ...

  9. poj2104 划分树 区间K大 在线 无修改

    博主sbit....对于高级数据结构深感无力,然后这些东西在OI竟然烂大街了,不搞就整个人都不好了呢. 于是我勇猛的跳进了这个大坑 ——sbit 区间K大的裸题,在线,无修改. 可以用归并树(\(O( ...

随机推荐

  1. [翻译] .NET Standard 2.1 公布

    [翻译] .NET Standard 2.1 公布 原文: Announcing .NET Standard 2.1 校对: Cloud 自从大约一年前发布 .NET Standard 2.0以来,我 ...

  2. zookeeper-架构设计与角色分工-《每日五分钟搞定大数据》

    本篇文章阅读时间5分钟左右 点击看<每日五分钟搞定大数据>完整思维导图   zookeeper作为一个分布式协调系统,很多组件都会依赖它,那么此时它的可用性就非常重要了,那么保证可用性的同 ...

  3. Windows Community Toolkit 4.0 - DataGrid - Part01

    概述 在上面一篇 Windows Community Toolkit 4.0 - DataGrid - Overview 中,我们对 DataGrid 控件做了一个概览的介绍,今天开始我们会做进一步的 ...

  4. C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 增强服务安全、阻止非授权的用户非法调用

    多一道防线,多一些安全保障,当程序发布到互联网上,再有成千上万的用户在用,总会有各种牛人出现,万一遇到破坏分子,那会有灾难性的打击. 只要跟利益有关系的,跟资金有关系,跟财务有关系,有竞争对手,软件系 ...

  5. VirtualBox安装复制Centos6.6配置网络

    由于要搭建mongodb的集群,先用虚拟机做下相关实验,以前都用VM Vare,但是现在这个电脑的配置不是太好,VM Vare比较耗资源,所以选择VirtualBox. 1.下载VirtualBox和 ...

  6. Python-collections模块-57

    返回顶部 模块的导入和使用 模块的导入应该在程序开始的地方 更多相关内容 http://www.cnblogs.com/Eva-J/articles/7292109.html   常用模块 colle ...

  7. case when then的用法-leetcode交换工资

    case具有两种格式:简单case函数和case搜索函数. --简单case函数 case sex when ' then '男' when ' then '女’ else '其他' end --ca ...

  8. Linux 环境变量梳理

    Linux中的环境变量有两种:全局变量和局部变量: 定义.访问.删除局部变量 查看全局变量 可以使用printenv或者env命令来打印所有的全局变量. 访问某一项全局变量,可以使用printenv ...

  9. Requires: libc.so.6(GLIBC_2.14)(64bit)

    centos6 - CentOS 6 - libc.so.6(GLIBC_2.14)(64bit) is needed by - Server Faulthttps://serverfault.com ...

  10. Laravel技巧:使用load、with预加载 区别

    1.使用load $posts = Post::all(); $posts->load('user'); 2.使用with $posts = Post::with('user')->all ...