POJ1442-查询第K大-Treap模板题
模板题,以后要学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模板题的更多相关文章
- PAT天梯赛练习题 L3-002. 堆栈(线段树查询第K大值或主席树)
L3-002. 堆栈 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家都知道“堆栈”是一种“先进后出”的线性结构,基本操作有 ...
- 在洛谷3369 Treap模板题 中发现的Splay详解
本题的Splay写法(无指针Splay超详细) 前言 首先来讲...终于调出来了55555...调了整整3天..... 看到大部分大佬都是用指针来实现的Splay.小的只是按照Splay的核心思想和原 ...
- 树状数组+二分答案查询第k大的数 (团体程序设计天梯赛 L3-002. 堆栈)
前提是数的范围较小 1 数据范围:O(n) 2 查第k大的数i:log(n)(树状数组查询小于等于i的数目)*log(n)(二分找到i) 3 添加:log(n) (树状数组) 4 删除:log(n) ...
- BZOJ 3224 普通平衡树(Treap模板题)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 14301 Solved: 6208 [Submit][ ...
- POJ 3481 Double Queue(Treap模板题)
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15786 Accepted: 6998 Des ...
- hdu-3549 Flow Problem---最大流模板题(dinic算法模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 题目大意: 给有向图,求1-n的最大流 解题思路: 直接套模板,注意有重边 传送门:网络流入门 ...
- hdu-1532 Drainage Ditches---最大流模板题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意: 给出有向图以及边的最大容量,求从1到n的最大流 思路: 传送门:最大流的增广路算法 ...
- 【大杀器】利用划分树秒杀区间内第k大的数
最近看了一道题,大概就是给出一个序列,不断询问其子区间内第k大的数,下面是个截图 绕了一圈没找到中文版题目,if(你是大佬) then 去看截图:else{我来解释:给出一个整数n,和一个整数m,分别 ...
- poj2104 划分树 区间K大 在线 无修改
博主sbit....对于高级数据结构深感无力,然后这些东西在OI竟然烂大街了,不搞就整个人都不好了呢. 于是我勇猛的跳进了这个大坑 ——sbit 区间K大的裸题,在线,无修改. 可以用归并树(\(O( ...
随机推荐
- vue中使用sass
1.npm安装 npm install sass-loader --save-dev npm install node-sass --save-dev //--save写入到package.json里 ...
- Jenkins - 构建Allure Report
前言 本文为Pytest+Allure定制报告进阶篇,集成Jenkins,在Jenkins中直接生成报告,更方便测试人员查看. 一.安装插件allure-jenkins-plugin 1.进入系统管理 ...
- Linux—CentOS7下python开发环境配置
CentOS7下python开发环境配置 上一篇博客讲了如何在Centos7下安装python3(https://www.cnblogs.com/zivli/p/9937608.html),这一次配置 ...
- Python—包介绍
包(Package) 当你的模块文件越来越多,就需要对模块文件进行划分,比如把负责跟数据库交互的都放一个文件夹,把与页面交互相关的放一个文件夹, . └── my_proj ├── crm #代码目录 ...
- Joyride (spaf)
题目链接:https://codeforces.com/gym/101873/problem/C spaf的复杂度有点迷,按道理来说,一个简单的spaf在这题的复杂度是1e9,所以不敢写,然后用优先队 ...
- 【RSYSLOG】The Property Replacer【转】
最近在调整日志平台的日志格式,一下是RSYSLOG的 Property Replacer 说明.鉴于RSYSLOG官网略坑,转发一下,原地址忘记了- - ||| The property replac ...
- Vicious Keyboard CodeForces - 801A (暴力+模拟)
题目链接 题意: 给定一个字符串,最多更改一个字符,问最多可以有多少个“VK”子串? 思路: 由于数据量很小,不妨尝试暴力写.首先算出不更改任何字符的情况下有多个VK字串,然后尝试每一次更改一个位置的 ...
- Java参数是值传递还是引用传递?
先来看看参数是如何传递的. 一.参数的传递 1.基本类型的参数传递 public static void main(String[] args) { int a = 1; fun(a); } priv ...
- Jenkins redeploy artifacts
jenkins redeploy artifacts 按钮 - 开源中国https://www.oschina.net/question/3045293_2247829 Jenkins 构建失败后通过 ...
- vue组件star开发基于vue-cli
<template> <div class="stars"> <div v-for="(item,ind) in num" :ke ...