原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4217

可用线段树写,效率要高点。

这道题以前用c语言写的treap水过了。。

现在接触了c++重写一遍。。。

不带重复元素的插入删除第k大带垃圾回收,具体如下:

 #include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int MAX_N = ;
struct Node{
int v, s, fix;
Node *ch[];
inline void
set(int _fix, int _v = , int _s = , Node *p = NULL){
fix = _fix, v = _v, s = _s;
ch[] = ch[] = p;
}
inline void push_up(){
s = ch[]->s + ch[]->s + ;
}
};
int run(){
static int x = ;
x += (x << ) | ;
return x;
}
struct Treap{
Node *tail, *null, *root;
Node stack[MAX_N];
int top;
Node *store[MAX_N];
void init(){
tail = &stack[];
null = tail++;
null->set(0x7fffffff);
root = null;
top = ;
}
inline Node *newNode(int v){
Node *p = null;
if (top) p = store[--top];
else p = tail++;
p->set(run(), v, , null);
return p;
}
inline void rotate(Node* &x, int d){
Node *k = x->ch[!d];
x->ch[!d] = k->ch[d];
k->ch[d] = x;
k->s = x->s;
x->push_up();
x = k;
}
inline void insert(Node* &x, int v){
if (x == null){
x = newNode(v);
return;
} else {
int d = v > x->v;
insert(x->ch[d], v);
if (x->ch[d]->fix < x->fix) rotate(x, !d);
x->push_up();
}
}
inline void del(Node* &x, int v){
if (x == null) return;
x->s--;
if (x->v == v){
if (x->ch[] == null || x->ch[] == null){
store[top++] = x;
x = x->ch[] == null ? x->ch[] : x->ch[];
} else {
int d = x->ch[]->fix < x->ch[]->fix;
rotate(x, !d);
del(x->ch[!d], v);
}
} else {
del(x->ch[v>x->v], v);
}
if (x != null) x->push_up();
}
inline int find_kth(Node *x, int k){
int t = ;
for (;;){
t = x->ch[]->s;
if (k == t + ) break;
else if (k < t + ) x = x->ch[];
else k -= t + , x = x->ch[];
}
return x->v;
}
inline void insert(int v){
insert(root, v);
}
inline void del(int v){
del(root, v);
}
inline int find_kth(int k){
return find_kth(root, k);
}
}treap;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
ll ans;
int t, n, m, k, tmp, c = ;
scanf("%d", &t);
while (t--){
treap.init(), ans = ;
scanf("%d %d", &n, &m);
for (int i = ; i <= n; i++) treap.insert(i);
while (m--){
scanf("%d", &k);
ans += tmp = treap.find_kth(k);
treap.del(tmp);
}
printf("Case %d: %lld\n", c++, ans);
}
return ;
}

hdu 4217 Data Structure?/treap的更多相关文章

  1. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. HDU 2217 Data Structure?

    C - Data Structure? Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  3. HDU 6649 Data Structure Problem(凸包+平衡树)

    首先可以证明,点积最值的点对都是都是在凸包上,套用题解的证明:假设里两个点都不在凸包上, 考虑把一个点换成凸包上的点(不动的那个点), 不管你是要点积最大还是最小, 你都可以把那个不动的点跟原点拉一条 ...

  4. HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  5. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  6. Basic Data Structure HDU - 5929 (这个模拟我要报警了)

    Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operati ...

  7. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

  8. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  9. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

随机推荐

  1. 进制格式转换 c#

    Console.WriteLine());//即17是十六进制位 得到的结果是23 得到十进制数 将字符串转换成二进制 public static string mdFiveGet(string md ...

  2. PAT1069. The Black Hole of Numbers

    //这是到水题,之前因为四位数的原因一直不能A,看了别人的程序,才明白,不够四位的时候没考虑到,坑啊.....脸打肿 #include<cstdio>#include<algorit ...

  3. Artifacts Gallery - Seahorse Collector, The

    Artifacts Gallery - Seahorse Collector, The

  4. 关于web前端开发

    刚接触前端,有很多不了解,一个table都搞了很久. 记录一些简单内容为日后查看. div是创建了一块块区域,而css则负责具体的展示用的包括位置的调整,块的展示.其实一个html页面中,一个tabl ...

  5. 014安装Linux系统到开发板

    SD卡----->开发板 1.安装准备: 硬件连接 USB下载线,一端连到开发板,另一端连到PC机: 串口线连好: 电源线连好: 设置开发板从SD卡启动: 2.打开开发板进入选单界面: 进入选单 ...

  6. 图片轮播jQuery

          <script type="text/javascript">         //图片轮播         var bannerIndex = 0; ba ...

  7. VBA删除表格最后一行

    Sub 删除最后一行() If MsgBox("要为所有表格添加列吗?", vbYesNo + vbQuestion) = vbYes Then To ActiveDocument ...

  8. C#局域网桌面共享软件制作(三)

    到周末了,继续做这个桌面共享软件,下面是前两篇的链接, 链接 C#局域网桌面共享软件制作(一) 链接 C#局域网桌面共享软件制作(二) 通过对图片进行压缩以后,每张图片大小38K左右(win7/102 ...

  9. SqlServer存储过程

    新上线的车管系统,今天要给User添加权限,才发现这个后台加权限简直how to play .. 比如有人申请合肥的关务权限: 1.SITE 是合肥 2.ORG 有B81,P81,S81,M81等 3 ...

  10. SMTP sendMail 失败解决办法

    If you are seeing messages like this in your message log when running a process through the process ...