59. 双重队列

题目链接

点此跳转

代码1(set版本)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

#define LL long long
#define mem(a, b) memset(a, b, sizeof a)
#define lowbit(x) (-x&x)
#define IOS ios::sync_with_stdio(false),cin.tie(0)
#define endl '\n'
#define rev(x) reverse(x.begin(), x.end())

using namespace std;

set<int> S;
unordered_map<int, int> H;
int op;

void solve() {
	int k, p;
	while (cin >> op, op) {
		if (op == 1) {
			cin >> k >> p;
			H[p] = k;
			S.insert(p);
		} else if (op == 2) {
			if (!S.size()) {
				cout << 0 << endl;
				continue;
			}
			auto t = S.rbegin();
			cout << H[*t] << endl;
			S.erase(*t);
		} else if (op == 3) {
			if (!S.size()) {
				cout << 0 << endl;
				continue;
			}
			auto t = S.begin();
			cout << H[*t] << endl;
			S.erase(*t);
		}
	}
}

int main() {
	IOS;
	
	solve();
	
	return 0;
}
代码2(Treap版本)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

#define LL long long
#define mem(a, b) memset(a, b, sizeof a)
#define lowbit(x) (-x&x)
#define IOS ios::sync_with_stdio(false),cin.tie(0)
#define endl '\n'
#define rev(x) reverse(x.begin(), x.end())

using namespace std;

const int N = 1e5 + 10;

struct Node {
	int l, r;
	int key, val;
	int num;
}tr[N];

int rt, idx;

int newnode(int v, int num) {
	int x = ++ idx;
	tr[x].val = v;
	tr[x].key = rand();
	tr[x].l = tr[x].r = 0;
	tr[x].num = num;
	return x;
}

int zig(int &p) {
	int q = tr[p].l;
	tr[p].l = tr[q].r;
	tr[q].r = p;
	p = q;
}

int zag(int &p) {
	int q = tr[p].r;
	tr[p].r = tr[q].l;
	tr[q].l = p;
	p = q;
}

void insert(int &p, int v, int num) {
	if (!p) {
		p = newnode(v, num);
		return;
	}
	
	if (v <= tr[p].val) {
		insert(tr[p].l, v, num);
		if (tr[tr[p].l].key > tr[p].key) zig(p); 
	} else {
		insert(tr[p].r, v, num);
		if (tr[tr[p].r].key > tr[p].key) zag(p);
	}
}

void del(int &p, int v){
	if (!p) return;
	
	if (v == tr[p].val) {
		if (!tr[p].l || !tr[p].r) {
			p = tr[p].l + tr[p].r;
			return;
		} else if (tr[tr[p].l].key > tr[p].key) {
			zig(p);
			del(tr[p].r, v);
		} else if (tr[tr[p].r].key > tr[p].key) {
			zag(p);
			del(tr[p].l, v);
		}
	}
	
	if (v < tr[p].val) del(tr[p].l, v);
	else del(tr[p].r, v);
}

int findmax(int p) {
	while (tr[p].r) {
		p = tr[p].r;
	}
	cout << tr[p].num << endl;
	return p;
}

int findmin(int p) {
	while (tr[p].l) {
		p = tr[p].l;
	}
	cout << tr[p].num << endl;
	return p;
}

void solve() {
	int op, k, p;
	while (cin >> op, op) {
		switch (op) {
		case 1:
			cin >> k >> p;
			insert(rt, p, k);
			break;
		case 2:
			if (!rt) cout << 0 << endl;
			else {
				int id = findmax(rt);
				del(rt, tr[id].val);
			}
			break;
		case 3:
			if (!rt) cout << 0 << endl;
			else {
				int id = findmin(rt);
				del(rt, tr[id].val);
			}
			break;
		}
	}
}

int main() {
	IOS;
	
	srand(0);
	
	solve();
	
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值