POJ 2761 Feed the dogs 基础Treap

同样是插入和寻找第k大,这里因为区间不存在包含的情况,所以可以现将区间排序然后直接搞就行了。如果存在包含的情况那就只能上主席树或者是莫队算法来搞了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdlib>

using namespace std;

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

void rotate(Node *&x, int d) {
	Node *k = x->ch[d ^ 1];
	x->ch[d ^ 1] = k->ch[d];
	k->ch[d] = x;
	x->maintain();
	k->maintain();
	x = k;
}

void insert(Node *&o, int x) {
	if (o == NULL) o = new Node(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();
}

void remove(Node *&o, int x) {
	if (o->val == x) {
		if (o->ch[0] == NULL || o->ch[1] == NULL) {
			Node *k = o;
			if (o->ch[0] == NULL) o = o->ch[1];
			else o = o->ch[0];
			delete k;
		}
		else {
			int d = o->ch[0]->rkey > o->ch[1]->rkey;
			rotate(o, d);
			remove(o->ch[d], x);
		}
	}
	else {
		int d = x > o->val;
		remove(o->ch[d], x);
	}
}

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 (k <= lsize) return findkth(o->ch[0], k);
	else if (k == lsize + 1) return o->val;
	else return findkth(o->ch[1], k - lsize - 1);
}

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

struct Seg {
	int l, r, k, id;
	Seg(int l, int r, int k, int id) :
		l(l), r(r), k(k), id(id) {}
	Seg() {}
	bool operator < (const Seg &x) const {
		if (r == x.r) return l < x.l;
		return r < x.r;
	}
};

const int maxn = 1e6 + 10;

Seg query[maxn];
Node *treap;
int ans[maxn], n, m, a[maxn];

int main() {
	while (scanf("%d%d", &n, &m) != EOF) {
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
		}
		for (int i = 1; i <= m; i++) {
			int l, r, k; scanf("%d%d%d", &l, &r, &k);
			query[i] = Seg(l, r, k, i);
		}
		sort(query + 1, query + 1 + m);
		int nowl = 1, nowr = 1;
		remove_tree(treap);
		for (int i = 1; i <= m; i++) {
			while (nowr <= query[i].r) {
				insert(treap, a[nowr++]);
			}
			while (nowl < query[i].l) {
				remove(treap, a[nowl++]);
			}
			ans[query[i].id] = findkth(treap, query[i].k);
		}
		for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
	}
	return 0;
}

  

时间: 2024-10-10 10:52:14

POJ 2761 Feed the dogs 基础Treap的相关文章

POJ 2761 Feed the dogs

静态区间第K大,主席树.... Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 15491   Accepted: 4780 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wi

Splay树(区间第k小)——POJ 2761 Feed the dogs

对应POJ题目:点击打开链接 Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 16655   Accepted: 5203 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Win

POJ 2761 Feed the dogs (主席树)(K-th 值)

                                                            Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 20634   Accepted: 6494 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed

POJ 2761 Feed the dogs(树状数组求区间第K大)

题目链接: 戳我 题目大意:Jiajia要为宠物狗,宠物狗按成一排站好(1 < i <= n),第 i 只狗的喜欢程度是 a[i], 之后他会先喂某个区间内第k个 即 n 个数, m个询问,接着是 n个数 接下来 m 行,每行是 l r k即 l 到 r 这个区间第 k 小的数,每个询问输出一个答案,即 a[i] 求区间第k大有很多算法, 详见此博客 [数据结构练习] 求区间第K大数的几种方法 我用的树状数组解法,来自 树状数组从前往后求和,用来解第k大(或小)的数 poj 2985 The

POJ2761 Feed the dogs

Time Limit: 6000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a sp

平衡树---Feed the dogs

POJ  2761 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on

POJ 2761(求区间第k小值)

Feed the dogs Time Limit: 6000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but no

【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 every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on one line,

POJ 1273 Drainage Ditches 网络流基础

Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage