原题链接: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. 慕课网-安卓工程师初养成-2-2 认识Java标识符

    来源:http://www.imooc.com/code/1177 问:标识符是神马? 答:标识符就是用于给 Java 程序中变量.类.方法等命名的符号. 使用标识符时,需要遵守几条规则: 1.  标 ...

  2. 关于 mysql 2003 客户端连接报错的处理方法

    在连接到 mysql 数据库服务器时,有时会在客户端报出 2003 的错误代码,并提示: 无法连接到服务器,但服务器却可以 ping 通,可能的原因如下: 1.网络不通.检查能不能ping通. 2.防 ...

  3. Mysql-5.7.10启动失败 。

    Mysql-5.7.10在免安装后启动服务失败. 查看日志得到如下: 2016-02-19T03:41:05.557095Z 0 [Warning] TIMESTAMP with implicit D ...

  4. form表单 无法提交js动态添加的表单元素问题。。

    第一种情况, 这种情况js动态添加的表单元素是不能提交到服务器端的 <table> <form method="post" action=" url   ...

  5. MongoDB 3: 使用中的问题,及其应用场景

    导读:用MongoDB去存储非关系型的数据,是一个比较正确的选择.但是,如果只是用MongoDB,那么也会出现一些问题.MongoDB,尤其使用的最佳场景,更多的时候,需要结合关系型数据库共同解决问题 ...

  6. Linux下的多进程编程

    1.进程 1.1进程的定义 <计算机操作系统>这门课对进程有这样的描述:进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统 ...

  7. 在ASP.NET开始执行HTTP请求的处理程序之前

    using Dscf.Client.Web.Class; using Dscf.Client.Web.DscfService; using Dscf.Client.Web.Handler; using ...

  8. Validform使用

    原文链接:http://validform.rjboy.cn/document.html Validform v5.3.2 文档 Validform使用入门 绑定附加属性 初始化参数说明 Validf ...

  9. 会"说话"的勒索病毒Cerber

    最近有个案子与勒索病毒有关,证物是个台式机,运行Windows 7 64bit操作系统,委托方是某高科技公司,希望能调查出事发的关键时间点.感染来源及途径.恶意程序文件名等相关信息. 在对证物计算机进 ...

  10. 第六章_PHP数组(二)

    这篇随笔是对预定义数组变量的总结.通过预定义数组变量,我们可以获得系统环境.用户对话.表单数据等信息. 1.服务器变量:$_SERVER 利用foreach语句打印$_SERVER中的所有元素: &l ...