平衡树。。

熟悉些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. Appium-实现手势密码登陆

    前言: 前几天有人问我,手势登陆如何做?于是我找了一个APP试了试,所以本文来总结使用Python+Appium来实现手势密码登陆APP. 环境: MacOS:10.13.4 Appium-deskt ...

  2. python之subprocess模块详解--小白博客

    subprocess模块 subprocess是Python 2.4中新增的一个模块,它允许你生成新的进程,连接到它们的 input/output/error 管道,并获取它们的返回(状态)码.这个模 ...

  3. HDU 3478 Catch (连通性&&二分图判断)

    链接 [https://vjudge.net/contest/281085#problem/C] 题意 一个n个点,m条边的图,开始的点是s 每次必须移动到相邻的位置,问你是否存在某个时刻所有点都可能 ...

  4. Python学习第十二篇——切片的使用

    Python中使用函数切片可以创建副本,保留原本.现在给出如下代码 magicians_list = ['mole','jack','lucy'] new_lists = [] def make_gr ...

  5. 类装饰器,元类,垃圾回收GC,内建属性、内建方法,集合,functools模块,常见模块

    '''''''''类装饰器'''class Test(): def __init__(self,func): print('---初始化---') print('func name is %s'%fu ...

  6. MySQL 主从同步遇到的问题及解决方案

    在做某个项目的时候,使用主从数据库,master负责update.delete.insert操作,而slave负责select操作. 情景1:发表文章与查看文章 可以认为这个项目是一个博客系统,这里就 ...

  7. shell脚本--eval执行shell命令

    和其他语言的eval功能差不多,都是将一个保存执行语句的变量作为参数,eval会让变量所保存的语句执行. 下面是一个执行表单提交的命令:注意,这里只是示例,应用中不要这么使用,很危险 #!/bin/b ...

  8. 解决ImportError: cannot import name 'webdriver' from 'selenium' (C:\Users\Mr.Su\PycharmProjects\***\venv\selenium.py)

    报错信息如下图所示: 解决方案:将项目根目录下的自己创建的selenium.py文件重命名.

  9. Swagger UI 用法

    Swagger - 简书https://www.jianshu.com/p/4115f2b53983 Swagger简介 - Ghost Stories - CSDN博客https://blog.cs ...

  10. [转帖]中关村:LED屏幕和OLED屏幕有什么区别?答案在这里

    LED屏幕和OLED屏幕有什么区别?答案在这里   中关村在线 01-0810:40 目前的电视市场,更新换代的频率越来越快,无论是国产品牌还是合资品牌,都不约而同的推出了全新产品.这离不开人们对更好 ...