POJ 1442 Treap模板
// 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模板的更多相关文章
- POJ 1442 treap
裸treap. 只需增加一个size记录其儿子个数便可找到第k大数. #include <cstdio> #include <cstring> #include <cti ...
- POJ 1442 Black Box treap求区间第k大
题目来源:POJ 1442 Black Box 题意:输入xi 输出前xi个数的第i大的数 思路:试了下自己的treap模版 #include <cstdio> #include < ...
- BZOJ 1588: Treap 模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12171 Solved: 4352 Description ...
- [luogu3369]普通平衡树(treap模板)
解题关键:treap模板保存. #include<cstdio> #include<cstring> #include<algorithm> #include< ...
- Poj 2187 凸包模板求解
Poj 2187 凸包模板求解 传送门 由于整个点数是50000,而求凸包后的点也不会很多,因此直接套凸包之后两重循环即可求解 #include <queue> #include < ...
- POJ 1442 Black Box(优先队列)
题目地址:POJ 1442 这题是用了两个优先队列,当中一个是较大优先.还有一个是较小优先. 让较大优先的队列保持k个.每次输出较大优先队列的队头. 每次取出一个数之后,都要先进行推断,假设这个数比較 ...
- poj 1442 Black Box(优先队列&Treap)
题目链接:http://poj.org/problem?id=1442 思路分析: <1>维护一个最小堆与最大堆,最大堆中存储最小的K个数,其余存储在最小堆中; <2>使用Tr ...
- POJ 3481 Double Queue(Treap模板题)
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15786 Accepted: 6998 Des ...
- POJ 3481 Double Queue (treap模板)
Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest ...
随机推荐
- SLAM: Inverse Depth Parametrization for Monocular SALM
首语: 此文实现客观的评测了使线性化的反转深度的效果.整篇只在表明反转可以线性化,解决距离增加带来的增长问题,有多少优势--%! 我的天呢!我竟然完整得翻译了一遍. 使用标记点地图构建SLAM的方法, ...
- 分块编码(Transfer-Encoding: chunked)VS Content-length
参考链接: HTTP 协议中的 Transfer-Encoding 分块传输编码 https://www.cnblogs.com/xuehaoyue/p/6639029.html 一.背景: 持续连接 ...
- Js 字符串中提取数字
一 parseInt()方法: 首先想到的是js提供的parseInt方法,例子: var str ="4500元"; var num = parseInt(str); alert ...
- python 字符串模糊匹配 Fuzzywuzzy
Python提供fuzzywuzzy模块,不仅可用于计算两个字符串之间的相似度,而且还提供排序接口能从大量候选集中找到最相似的句子. (1)安装 pip install fuzzywuzzy (2)接 ...
- swift-计算字符串长度
text.characters.count(记得text一定要是String类型)
- SDOI2017数字表格
求$\prod_{i=1}^n\prod_{j=1}^n\text{Fib}[\gcd(i,j)]\;\text{mod}\;10^9+7$的值 令$n\leq m$,则有: \begin{align ...
- UDP、线程、mutex锁(day15)
一.基于UDP的网络编程模型 服务器端 .创建socket. .将fd和服务器的ip地址和端口号绑定 .recvfrom阻塞等待接收客户端数据 .业务处理 .响应客户端 客户端: .创建socket ...
- 11gR2 集群(CRS/GRID)新功能—— SCAN(Single Client Access Name)
https://blogs.oracle.com/database4cn/11gr2-crsgrid-scansingle-client-access-name
- Android Studio 导入的项目编码错误问题
错误提示: Error:(4, 35) 閿欒: 缂栫爜UTF-8鐨勪笉鍙槧灏勫瓧绗? 解决方法: 1). 2). 的下面添加下面的一行语句: android{compileOptions.enco ...
- UVA 10025(数学)
The ? 1 ? 2 ? ... ? n = k problem The problem Given the following formula, one can set operators ' ...