题目链接
点此跳转
代码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;
}