// by SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int cnt=0,jy,a[30500],n,m,root=-1;
struct node{
int left,right,count_left,count_right,key,priority;
}treap[30500];
void zig(int &x){
int y=treap[x].right;
treap[x].right=treap[y].left;
treap[x].count_right=treap[y].count_left;
treap[y].left=x;
treap[y].count_left=treap[x].count_left+treap[x].count_right+1;
x=y;
}
void zag(int &x){
int y=treap[x].left;
treap[x].left=treap[y].right;
treap[x].count_left=treap[y].count_right;
treap[y].right=x;
treap[y].count_right=treap[x].count_left+treap[x].count_right+1;
x=y;
}
void insert(int &x,int new_key){
if(x==-1){
x=cnt++;
treap[x].left=treap[x].right=-1;
treap[x].priority=rand();
treap[x].key=new_key;
treap[x].count_left=treap[x].count_right=0;
}
else if(new_key<treap[x].key){
treap[x].count_left++;
insert(treap[x].left,new_key);
if(treap[x].priority>treap[treap[x].left].priority)zag(x);
}
else{
treap[x].count_right++;
insert(treap[x].right,new_key);
if(treap[x].priority>treap[treap[x].right].priority)zig(x);
}
}
int query(int &x,int k_th){
if(treap[x].count_left+1==k_th)return treap[x].key;
if(treap[x].count_left+1<k_th)return query(treap[x].right,k_th-treap[x].count_left-1);
return query(treap[x].left,k_th);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;i++){
scanf("%d",&jy);
while(cnt<jy)insert(root,a[cnt+1]);
printf("%d\n",query(root,i));
}
}

POJ 1442 Treap模板的更多相关文章

  1. POJ 1442 treap

    裸treap. 只需增加一个size记录其儿子个数便可找到第k大数. #include <cstdio> #include <cstring> #include <cti ...

  2. POJ 1442 Black Box treap求区间第k大

    题目来源:POJ 1442 Black Box 题意:输入xi 输出前xi个数的第i大的数 思路:试了下自己的treap模版 #include <cstdio> #include < ...

  3. BZOJ 1588: Treap 模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 12171  Solved: 4352 Description ...

  4. [luogu3369]普通平衡树(treap模板)

    解题关键:treap模板保存. #include<cstdio> #include<cstring> #include<algorithm> #include< ...

  5. Poj 2187 凸包模板求解

    Poj 2187 凸包模板求解 传送门 由于整个点数是50000,而求凸包后的点也不会很多,因此直接套凸包之后两重循环即可求解 #include <queue> #include < ...

  6. POJ 1442 Black Box(优先队列)

    题目地址:POJ 1442 这题是用了两个优先队列,当中一个是较大优先.还有一个是较小优先. 让较大优先的队列保持k个.每次输出较大优先队列的队头. 每次取出一个数之后,都要先进行推断,假设这个数比較 ...

  7. poj 1442 Black Box(优先队列&Treap)

    题目链接:http://poj.org/problem?id=1442 思路分析: <1>维护一个最小堆与最大堆,最大堆中存储最小的K个数,其余存储在最小堆中; <2>使用Tr ...

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

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

  9. POJ 3481 Double Queue (treap模板)

    Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest ...

随机推荐

  1. 用VS Code Debug Python

  2. 【sqli-labs】 less26a GET- Blind based -All you SPACES and COMMENTS belong to us -String-single quotes-Parenthesis(GET型基于盲注的去除了空格和注释的单引号括号注入)

    这个和less26差不多,空格还是用%a0代替,26过了这个也就简单了 ;%00 可以代替注释,尝试一下 order by 3 http://192.168.136.128/sqli-labs-mas ...

  3. Java中RunTime.getRunTime().addShutdownHook用法

    今天在阅读Tomcat源码的时候,catalina这个类中使用了下边的代码,不是很了解,所以google了一下,然后测试下方法,Tomcat中的相关代码如下: Runtime.getRuntime() ...

  4. 企业版 Linux 附加软件包(EPEL)

    企业版 Linux 附加软件包(以下简称 EPEL)是一个由特别兴趣小组创建.维护并管理的,针对 红帽企业版 Linux(RHEL)及其衍生发行版(比如 CentOS.Scientific Linux ...

  5. 09.正则表达式re-2.complie函数

    compile 函数用于编译正则表达式,生成一个 Pattern 对象,它的一般使用形式如下: import re # 将正则表达式编译成 Pattern 对象 pattern = re.compil ...

  6. 继续聊WPF——Thumb控件

    这个控件,真不好介绍,MSDN上也是草草几句,反正就是可以让用户拖动的玩意儿,但是,你会发现,当你在该控件上拖动时,它没有反响,也就是说这个东西默认不做任何操作的,它是赖在那里什么都不干,除非你去踢上 ...

  7. node源码详解(三)

    本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/nodesource3 本博客同步在https://cnodejs.o ...

  8. QT中tableview不能更新数据,why?

    model->select(); //model->removeColumn(0);++++++++++++++++++++ //model->setHeaderData(model ...

  9. node环境变量配置,npm环境变量配置

    引言:很久没有在windows上配过node, 记得以前node环境变量是要加 NODE_PATH 到用户变量,再在系统变量引入NODE_PATH的,而npm install的全局包目录会存放在C:/ ...

  10. (31)Spring Boot导入XML配置【从零开始学Spring Boot】

    [来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论: 您的认可是我最大的动力,感谢您的支持] Spring Boot理念就是零配置编程,但是如果绝对需要使用XML的配置,我们建议您仍旧从一个@Co ...