平衡树。。

熟悉些fhq-Treap,为啥我在poj读入优化不能用啊

#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
} const int N = 1000005;
int tree[N][2], key[N], val[N], rnd[N], size[N], tot, root;
int tx, ty, tz, cnt; int rndom(){
return rand() << 15 | rand();
} int newNode(int k, int p){
key[++tot] = k, val[tot] = p, rnd[tot] = rndom(), size[tot] = 1;
return tot;
} void push_up(int x){
size[x] = size[tree[x][0]] + size[tree[x][1]] + 1;
} int merge(int x, int y){
if(!x || !y) return x + y;
if(rnd[x] < rnd[y]){
tree[x][1] = merge(tree[x][1], y);
push_up(x);
return x;
}
else{
tree[y][0] = merge(x, tree[y][0]);
push_up(y);
return y;
}
} void split(int cur, int k, int &x, int &y){
if(!cur) { x = 0, y = 0; return; }
if(val[cur] <= k) x = cur, split(tree[cur][1], k, tree[cur][1], y);
else y = cur, split(tree[cur][0], k, x, tree[cur][0]);
push_up(cur);
} void insert(int k, int p){
split(root, p, tx, ty);
root = merge(merge(tx, newNode(k, p)), ty);
} void del(int p){
split(root, p, tx, tz);
split(tx, p - 1, tx, ty);
ty = merge(tree[ty][0], tree[ty][1]);
root = merge(merge(tx, ty), tz);
} int minimum(){
int cur = root;
while(tree[cur][0] != 0) cur = tree[cur][0];
return cur;
} int maximum(){
int cur = root;
while(tree[cur][1] != 0) cur = tree[cur][1];
return cur;
} int select(int cur, int k){
while(1){
if(size[tree[cur][0]] >= k) cur = tree[cur][0];
else{
if(size[tree[cur][0]] + 1 == k) return cur;
k = k - size[tree[cur][0]] - 1, cur = tree[cur][1];
}
}
} void init(){
full(tree, 0), full(val, 0), full(size, 0);
full(key, 0), full(rnd, 0);
cnt = tot = root = 0;
} int main(){ srand(time(0));
int opt; init();
while(scanf("%d", &opt) != EOF && opt){
if(opt == 1) {
int k, p; scanf("%d%d", &k, &p);
insert(k, p), cnt ++;
}
else if(opt == 2){
if(cnt == 0){
puts("0");
continue;
}
int res = maximum();
//int res = select(root, size[root]);
printf("%d\n", key[res]); del(val[res]), cnt --;
}
else if(opt == 3){
if(cnt == 0){
puts("0");
continue;
}
int res = minimum();
//int res = select(root, 1);
printf("%d\n", key[res]); del(val[res]), cnt --;
}
}
return 0;
}

POJ 3481 Double Queue的更多相关文章

  1. POJ 3481 Double Queue STLmap和set新学到的一点用法

    2013-08-08 POJ 3481  Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...

  2. POJ 3481 Double Queue(Treap模板题)

    Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15786   Accepted: 6998 Des ...

  3. POJ 3481 Double Queue(STL)

    题意  模拟银行的排队系统  有三种操作  1-加入优先级为p 编号为k的人到队列  2-服务当前优先级最大的   3-服务当前优先级最小的  0-退出系统 能够用stl中的map   由于map本身 ...

  4. POJ 3481 Double Queue(set实现)

    Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...

  5. POJ 3481 Double Queue (treap模板)

    Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest ...

  6. poj 3841 Double Queue (AVL树入门)

    /****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...

  7. hdu 1908 Double Queue

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1908 Double Queue Description The new founded Balkan ...

  8. 【Map】Double Queue

    Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13258   Accepted: 5974 Des ...

  9. poj 2259 Team Queue

    Team Queue Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2977   Accepted: 1092 Descri ...

随机推荐

  1. 图解SSH原理及两种登录方法

    SSH(Secure Shell)是一套协议标准,可以用来实现两台机器之间的安全登录以及安全的数据传送,其保证数据安全的原理是非对称加密. 传统的对称加密使用的是一套秘钥,数据的加密以及解密用的都是这 ...

  2. Python_装饰器复习_30

    复习: # 装饰器的进阶 # functools.wraps # 带参数的装饰器 # 多个装饰器装饰同一个函数# 周末的作业 # 文件操作 # 字符串处理 # 输入输出 # 流程控制 # 装饰器# 开 ...

  3. vuex原理

    Vuex 框架原理与源码分析 vuex状态管理到底是怎样一个原理? 状态管理 Vuex框架原理与源码分析 Vuex实现原理解析 Vue刚出不久,Vuex 就出来了,想请教下Vuex做了什么事情? 个人 ...

  4. Python学习第三篇——访问列表部分元素

    dongman =["huoying","sishen","si wang bi ji","pan ni de lu lu xiu ...

  5. 一个6亿的表a,一个3亿的表b,通过外间tid关联,你如何最快的查询出满足条件的第50000到第50200中的这200条数据记录

    1.如果A表TID是自增长,并且是连续的,B表的ID为索引 select * from a,b where a.tid = b.id and a.tid>500000 limit 200; 2. ...

  6. PS 十分钟教你做出文字穿插效果

  7. RabbitMQ消息的交换

    消息的交换 目录 RabbitMQ-从基础到实战(1)— Hello RabbitMQ RabbitMQ-从基础到实战(2)— 防止消息丢失 1.简介 在前面的例子中,每个消息都只对应一个消费者,即使 ...

  8. 最全的Django入门及常用配置

    Django 常用配置 Django 安装 pipx install django x 为python解释器版本2 or 3 如果你想安装指定版本的django,使用pip install djang ...

  9. rest-framework的认证组件

    认证组件 1.登录认证(与组件无关): 首先要在model表内添加用户表和token表: from django.db import models # Create your models here. ...

  10. Python之拆分目录

    成分目录的好习惯,使得代码保持整洁,为以后的代码管理提供方便. 一.概念 一般目录有以下几个: bin:程序入口,存放start文件. conf:存放固定的配置信息,比如:连接redis的配置信息.连 ...