POJ 1442 Black Box 基础Treap

高中的时候做过这个,是用两个堆搞的,现在看来其实就是实现一个很简答的数据结构,能够插入元素,找第k大,用平衡树来搞其实是大材小用了,就当做是练习吧。

Treap是利用除了键值之外另外一个rand_key域的随机性来保证平衡的,所以说只要随机函数够好,理论上应该是平衡的,而且写起来比较方便。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>

using namespace std;

typedef long long LL;

//treap
struct Node {
	Node *ch[2];
	int rkey, val, size;
	void maintain() {
		size = 1;
		if(ch[0] != NULL) size += ch[0]->size;
		if(ch[1] != NULL) size += ch[1]->size;
	}
};

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();
		o->ch[0] = o->ch[1] = NULL;
		o->rkey = rand();
		o->val = x;
	}
	else {
		int d = (x > o->val);
		insert(o->ch[d], x);
		if(o->ch[d]->rkey > o->rkey) rotate(o, d ^ 1);
	}
	o->maintain();
}

int findkth(Node *o, int k) {
	if(o == NULL || k > o->size || k <= 0) return 0;
	int lsize = o->ch[0] == NULL ? 0 : o->ch[0]->size;
	if(lsize + 1 == k) return o->val;
	if(k <= lsize) return findkth(o->ch[0], k);
	return findkth(o->ch[1], k - 1 - lsize);
}

void remove_tree(Node *&o) {
	if(o == NULL) return;
	for(int i = 0; i < 1; i++) if(o->ch[i] != NULL) {
		remove_tree(o->ch[i]);
	}
	delete o; o = NULL;
}

const int maxn = 5e4 + 10;
int n, m, a[maxn];
Node *root;

int main() {
	while(scanf("%d%d", &n, &m) != EOF) {
		remove_tree(root);
		for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
		int nowi = 0, nsz;
		for(int i = 1, j = 0; i <= m; i++) {
			scanf("%d", &nsz);
			while(j < nsz)  {
				j++; insert(root, a[j]);
			}
			nowi++;
			printf("%d\n", findkth(root, nowi));
		}
	}
	return 0;
}

  

时间: 2024-10-07 21:59:07

POJ 1442 Black Box 基础Treap的相关文章

POJ 1442 Black Box(treap树)

题目链接:点击打开链接 思路:treap树模板题, 可以动态维护一个有序表, 支持在O(logN)的时间内完成插入.删除一个元素和查找第K大元素的任务. 当然, treap树能做到的还远远不止这些, 常常与其他数据结构嵌套. treap树是一种平衡二叉搜索树, 既满足堆的条件, 又满足排序二叉树的条件. 细节参见代码: #include <cstdio> #include <cstring> #include <algorithm> #include <iostr

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)

poj 1442 -- Black Box

Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7183   Accepted: 2920 Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empt

[ACM] POJ 1442 Black Box (堆,优先队列)

Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7099   Accepted: 2888 Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empt

POJ 1442 Black Box(优先队列)

题目地址:POJ 1442 这题是用了两个优先队列,其中一个是较大优先,另一个是较小优先.让较大优先的队列保持k个.每次输出较大优先队列的队头. 每次取出一个数之后,都要先进行判断,如果这个数比较大优先的队列的队头要小,就让它加入这个队列,队列头移到较小优先的队列中.然后当较大优先的数不足k个的时候,就让较小优先的队列的队头移到较大优先的队头中. 代码如下: #include <iostream> #include <cstdio> #include <string>

poj 1442 Black Box (treap树入门题)

1 /**************************************************** 2 题目: Black Box(poj 1442) 3 链接: http://poj.org/problem?id=1442 4 题意: 给n个数,m个询问,对第i数个询问前Xi个数中第 5 i小的是那个数. 6 算法: treap树 7 *****************************************************/ 8 #include<iostream

Treap [POJ 1442] Black Box

Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7770   Accepted: 3178 Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empt

数据结构(堆):POJ 1442 Black Box

Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10658   Accepted: 4390 Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is emp

Black Box(POJ 1442&#183;TREAP实现)

传送门:http://poj.org/problem?id=1442 Black Box Time Limit: 1000MS   Memory Limit: 10000K       Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empt