poj3481——treap
来源:互联网 发布:sql数据库实训心得 编辑:程序博客网 时间:2024/05/21 03:57
题意:动态求一个集合的最大值和最小值。
treap的模板题。
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std;struct node{ node * ch[2]; int r, v, q; 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; }};node * root;void rotate(node * & o, int d) // d = 0 左旋 d = 1 右旋{ node * k = o->ch[d ^ 1]; o->ch[d ^ 1] = k->ch[d]; k->ch[d] = o; o = k;}void insert(node * & o, int x, int q){ if(o == NULL) { o = new node(); o->ch[0] = o->ch[1] = NULL; o->v = x; o->r = rand(); o->q = q; } else { int d = o->cmp(x); insert(o->ch[d], x, q); if(o->ch[d] > o) rotate(o, d ^ 1); }}void remove(node * & o, int x){ int d = o->cmp(x); if(d == -1) { if(o->ch[0] == NULL) { o = o->ch[1]; } else if(o ->ch[1] == NULL) { o = o->ch[0]; } else { int d2 = (o->ch[0] > o->ch[1]) ? 1 : 0; rotate(o, d2); remove(o->ch[d2], x); } } else remove(o->ch[d], x);}int findmax(node * & o){ if(o->ch[1] == NULL) { int q = o->q; remove(o, o->v); return q; } else return findmax(o->ch[1]);}int findmin(node * & o){ if(o->ch[0] == NULL) { int q = o->q; remove(o, o->v); return q; } else return findmin(o->ch[0]);}int main(){ freopen("in", "r", stdin); root = NULL; int cas; while(~scanf("%d", &cas) && cas != 0) { if(cas == 1) { int k, p; scanf("%d %d", &k, &p); insert(root, p, k); } else if(cas == 2) { if(root == NULL) { printf("0\n"); continue; } printf("%d\n", findmax(root)); } else { if(root == NULL) { printf("0\n"); continue; } printf("%d\n", findmin(root)); } } return 0;}/*21 20 141 30 321 10 99322002030100*/
- poj3481——treap
- poj3481(Treap)
- poj3481——Double Queue
- 【POJ3481】Double Queue——伸展树
- poj3481
- poj3481
- Treap——模板
- 平衡树——Treap
- 数据结构入门1—Treap
- 一个有趣的结构——Treap
- hdu3726——treap,并查集
- 偷懒专用平衡树——Treap
- Treap——题目方法总结
- ACM:平衡树(1)——Treap
- 模板——Treap实现名次树
- SBT poj3481
- Treap
- Treap
- 令牌桶和漏桶算法(流量整形算法)
- 软件开发最佳实践
- Windows XP下MinGW的安装与配置,dos小技巧
- 5.6.10能用的参数(验证)
- oracle 安装脚本
- poj3481——treap
- 设计模式(18)-Command Pattern
- poj1155TELE - 树形dp
- 在用户空间发生中断时,上下文切换的过程
- shell编程 if语句【转】
- 命令模式简介及应用
- android应用如何自动创建快捷方式到Launcher
- 模板元编程简介
- 第七周上机报告(项目二:两点间距离之不同函数的实现)