原题链接: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++需要注意的地方和小算法

    C++11的标准 auto //可以自动类型, auto cars=//自动转化为int 强制转换 (long)thorn =long (thorn) //前者是c标准,后者是c++ 还有一种 sta ...

  2. 应用OpenCV进行OCR字符识别

    opencv自带一个字符识别的例子,它的重点不是OCR字符识别,而主要是演示机器学习的应用.它应用的是UCI提供的字符数据(特征数据). DAMILES在网上发布了一个应用OpenCV进行OCR的例子 ...

  3. No.007 Reverse Integer

    7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...

  4. Java基础——左移和右移

    首先要明白一点,这里面所有的操作都是针对存储在计算机中中二进制的操作,那么就要知道,正数在计算机中是用二进制表示的,负数在计算机中使用补码表示的. 左移位:<<,有符号的移位操作 左移操作 ...

  5. Android创建自定义dialog方法详解-样式去掉阴影效果

    在自定义组件时,从已有组件源码中会很大收获.就拿progressDialog来说     间接父类是dialog,想了解dialog继承结构可以去百度,或者    从构造器来说ProgressDial ...

  6. 集群监控系统Ganglia应用案例

    集群监控系统Ganglia应用案例 --我们把集群系统投入生产环境后,这时就需要一套可视化的工具来监视集群系统,这将有助于我们迅速地了解机群的整体配置情况,准确地把握机群各个监控节点的信息,全面地察看 ...

  7. asp.net ashx 一般处理程序 使用async await异步直接 copy可用哦

    以前一直很懒  碰到ashx要用await异步就绕开  用aspx  或者mvc异步控制器  这次公司需要  我查了国内的文章基本都不能简单copy来处理一堆错关键的过程中函数BeginProcess ...

  8. meta 标签属性(网站兼容与优化需要)

    概要 标签提供关于HTML文档的元数据.元数据不会显示在页面上,但是对于机器是可读的.它可用于浏览器(如何显示内容或重新加载页面),搜索引擎(关键词),或其他 web 服务. —— W3School ...

  9. JMeter笔记4:测试结果-聚合报告的字段说明

    1.Lable :定义 HTTP 请求名称2.Samples :表示这次测试中一共发出了多少个请求3.Average :平均响应时长---默认情况下是单个Request的平均响应时长,当使用Trans ...

  10. UTF-8 GBK GB2312 之间的区别和关系

    UTF-8:Unicode TransformationFormat-8bit,允许含BOM,但通常不含BOM.是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24为(三 ...