POJ 1442 Black Box treap求区间第k大
题目来源:POJ 1442 Black Box
题意:输入xi 输出前xi个数的第i大的数
思路:试了下自己的treap模版
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std; struct Node
{
Node *ch[2];
int r;
int v;
int s;
Node(){}
Node(int v): v(v) {
ch[0] = ch[1] = NULL; r = rand(); s = 1;
}
bool operator < (const Node& rhs) const{
return r < rhs.r;
}
int cmp(int x) const{
if(x == v) return -1;
return x < v ? 0 : 1;
}
void maintain(){
s = 1;
if(ch[0] != NULL) s += ch[0]->s;
if(ch[1] != NULL) s += ch[1]->s;
}
}; void rotate(Node* &o, int d){
Node* k = o->ch[d^1]; o->ch[d^1] = k->ch[d]; k->ch[d] = o;
o->maintain(); k->maintain(); o = k;
} void insert(Node* &o, int x){
if(o == NULL){
o = new Node(x);
}
else{
int d = (x < o->v ? 0 : 1);
insert(o->ch[d], x);
if((o->ch[d]->r) > (o->r)) rotate(o, d^1);
}
o->maintain();
} void remove(Node* &o, int x){
int d= o->cmp(x);
if(d == -1){
Node* u = o;
if(o->ch[0] != NULL && o->ch[1] != NULL){
int d2 = (o->ch[0] > o->ch[1] ? 1 : 0);
rotate(o, d2); remove(o->ch[d2], x);
}
else{
if(o->ch[0] == NULL) o = o->ch[1];
else o = o->ch[0];
delete u;
}
}
else
remove(o->ch[d], x);
if(o != NULL) o->maintain();
} int kth(Node* o, int k){
if(o == NULL || k <= 0 || k > o->s)
return 0;
int s = (o->ch[0] == NULL ? 0 : o->ch[0]->s);
if(k == s+1) return o->v;
else if(k <= s) return kth(o->ch[0], k);
else return kth(o->ch[1], k-s-1);
} void removetree(Node* &x){
if(x->ch[0] != NULL) removetree(x->ch[0]);
if(x->ch[1] != NULL) removetree(x->ch[1]);
delete x;
x = NULL;
}
int n, m, a[30010];
Node *rt = NULL;
int main()
{
while(scanf("%d %d", &n, &m) != EOF)
{
srand(time(0));
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
int l = 1;
for(int i = 1; i <= m; i++)
{
int x;
scanf("%d", &x);
while(l <= x)
{
insert(rt, a[l]);
l++;
}
printf("%d\n", kth(rt, i));
}
//removetree(rt);
}
return 0;
}
POJ 1442 Black Box treap求区间第k大的更多相关文章
- POJ2761---Feed the dogs (Treap求区间第k大)
		
题意 就是求区间第k大,区间 不互相包含. 尝试用treap解决一下 第k大的问题. #include <set> #include <map> #include <cm ...
 - POJ 2104 K-th Number 主席树(区间第k大)
		
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
 - [hdu2665]Kth number(划分树求区间第k大)
		
解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...
 - HDU 3473 Minimum Sum (划分树求区间第k大带求和)(转)
		
题意:在区间中找一个数,求出该区间每个数与这个数距离的总和,使其最小 找的数字是中位数(若是偶数个,则中间随便哪个都可)接着找到该区间比此数大的数的总和 区间中位数可以使用划分树,然后在其中记录:每层 ...
 - G - KiKi's K-Number(树状数组求区间第k大)
		
For the k-th number, we all should be very familiar with it. Of course,to kiki it is also simple. No ...
 - [poj2104]kth-number(归并树求区间第k大)
		
复杂度:$O(nlog^3n)$ #include<cstdio> #include<cstring> #include<algorithm> #include&l ...
 - HDU2665 求区间第K大 主席树
		
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2665 代码: //#include<bits/stdc++.h> #include< ...
 - hdu2665可持久化线段树,求区间第K大
		
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
 - 【POJ2761】【区间第k大】Feed the dogs(吐槽)
		
Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...
 
随机推荐
- CentOS7.3 下开放防火墙的端口
			
CentOS 7.3默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1:关闭firewall: systemctl stop firewalld.service system ...
 - celery 学习
			
1. 列出计划的ETA任务(worker) celery -A proj inspect scheduled 参考文档:http://docs.celeryproject.org/en/latest/ ...
 - 一个Web报表项目的性能分析和优化实践(四):MySQL建立索引,唯一索引和组合索引
			
先大致介绍下项目的数据库信息. 数据库A:主要存放的通用的表,如User.Project.Report等. 数据库B.C.D:一个项目对应一个数据库,而且这几个项目的表是完全一样的. 数据库表的特点 ...
 - token登录验证机制
			
一张图解释 token登录验证机制
 - java设计模式学习 ----- 单例模式(Singleton)
			
单例模式(Singleton) 单例对象(Singleton)是一种经常使用的设计模式. 在Java应用中,单例对象能保证在一个JVM中,该对象仅仅有一个实例存在.单例模式也分三种:懒汉式单例.饿汉式 ...
 - sass02
			
新建一个文件夹 1 cd 进入文件夹 ,cd E:\360data\重要数据\桌面\sass, 2 compass creat hello:当前目录创建sass工程, 3 sass文件夹放置sass文 ...
 - style="background-image: url(__HOMEPAGE__/views/IMJ2V2/images/banner2.jpg)"
			
style="background-image: url(__HOMEPAGE__/views/IMJ2V2/images/banner2.jpg)" 一.问题 backgroun ...
 - 【Codeforces Round #425 (Div. 2) B】Petya and Exam
			
[Link]:http://codeforces.com/contest/832/problem/B [Description] *能代替一个字符串(由坏字母组成); ?能代替单个字符(由好字母组成) ...
 - 五十个UI设计资源网站
			
五十个UI设计资源网站 用户体验团队网站 1.UCD大社区 http://ucdchina.com/ 2.腾讯WSD http://wsd.tencent.com/ 3.腾讯CDC http://cd ...
 - jvm vmthread
			
http://www.360doc.com/content/15/0615/16/15758456_478311946.shtml http://www.51testing.com/html/95/1 ...