平衡树。。

熟悉些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. 二、xadmin----简单使用

    1.中文设置: settings.py中直接配置: LANGUAGE_CODE = "zh-hans" TIME_ZONG = "Asia/Shanghai" ...

  2. UnderWater+SDN论文之二

    ---- Software-defined underwater acoustic networking platform and its applications source: Ad Hoc Ne ...

  3. Telnet服务器和客户端请求处理

    Telnet服务器和客户端请求处理 本文的控制台项目是根据SuperSocket官方Telnet示例代码进行调试的,官方示例代码:Telnet示例. 开始我的第一个Telnet控制台项目之旅: 创建控 ...

  4. 在做stark中一些反射的问题。

    hasattr(obj,name): 判断一个对象里面是否有name属性或者name方法,返回BOOL值,有name特性返回True, 否则返回False.需要注意的是name要用括号括起来   1 ...

  5. 爬虫——cookies池的搭建

    https://github.com/Python3WebSpider/cookiesPool

  6. HDU 1089 到1096 a+b的输入输出练习

    http://acm.hdu.edu.cn/showproblem.php?pid=1089 Problem Description Your task is to Calculate a + b.T ...

  7. python内涵段子爬取练习

    # -*- coding:utf-8 -*-from urllib import request as urllib2import re# 利用正则表达式爬取内涵段子url = r'http://ww ...

  8. oracle查询不走索引的一些情况(索引失效)

    Oracle建立索引的目的是为了避免全表扫描,提高查询的效率. 但是有些情况下发现即使建立了索引,但是写出来的查询还是很慢,然后会发现是索引失效导致的,所以需要了解一下那些情况会导致索引失效,即查询不 ...

  9. [转帖]linux下的X server:linux图形界面原理

    linux下的X server:linux图形界面原理 https://www.cnblogs.com/liangxiaofeng/p/5034912.html linux下的X server:lin ...

  10. 网络编程--使用UDP发送接收数据

    package com.zhangxueliang.udp; import java.io.IOException; import java.net.DatagramPacket; import ja ...