模板题,以后要学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. Spring Cloud (十四):Spring Cloud 开源软件都有哪些?

    学习一门新的技术如果有优秀的开源项目,对初学者的学习将会是事半功倍,通过研究和学习优秀的开源项目,可以快速的了解此技术的相关应用场景和应用示例,参考优秀开源项目会降低将此技术引入到项目中的成本.为此抽 ...

  2. xampp 使用过程中刚遇到的问题记录

    开始使用XAMPP的时候,都是可以正常连接的,但是过一段时间后,在用它创建表或其他操作,会报错,提示如下错误 Access denied for user ''@'localhost' to data ...

  3. UnderWater+SDN论文之三

    Software-Defined Underwater Acoustic Modems: Historical Review and the NILUS Approach Source: IEEE J ...

  4. python中变量、函数、类名、模块名等命名方式

    摘要:模块名:小写字母,单词之间用_分割ad_stats.py包名:和模块名一样类名:单词首字母大写AdStatsConfigUtil全局变量名(类变量,在java中相当于static变量):大写字母 ...

  5. Linux下php安装redis扩展(redis已经安装)

     1. 下载需要的php操作redis的扩展包 (1).切换到 cd  /usr/local/src (2).   wget https://github.com/nicolasff/phpredis ...

  6. NSAssert和NSParameterAssert

    2016.05.05 18:34* 字数 861 阅读 5127评论 0喜欢 17 https://www.jianshu.com/p/3072e174554f NSAssert和NSParamete ...

  7. windows 环境下 eclipse + maven + tomcat 的 hello world 创建和部署

    主要记录自己一个新手用 eclipse + maven + tomcat 搭建 hello world 的过程,以及遇到的问题.讲真都是自己通过百度和谷歌一步步搭建的项目,没问过高手,也没高手可问,由 ...

  8. Django之ORM操作(聚合 分组、F Q)

    Django之ORM操作(聚合 分组.F Q) 聚合 aggregate()是QuerySet的一个终止子句,也就是说,他返回一个包含一些键值对的字典,在它的后面不可以再进行点(.)操作.   键的名 ...

  9. echarts使用笔记一:基本属性

    1.包括一些基本的设置 app.title = '坐标轴刻度与标签对齐'; option = { title : { //标题 x : 'center', y : 5, text : '单通趋势图' ...

  10. 【学习总结】【Java】Git学习-上传本地已有代码到GitHub

    < Git学成归来后的第一次实战 > 上传本地已有代码到GitHub 以之前学了一小半的Java基础教程代码为例 <深坑预警:在GitHub新建仓库那一步,不要勾选readme,不然 ...