题意:

一开始有n个非负整数h[i],接下来会进行m次操作,第i次会给出一个数c[i],要求选出c[i]个大于0的数并将它们-1,问最多可以进行多少次?

分析:

首先一个显然的贪心就是每次都将最大的c[i]个数-1,于是就可以用无旋式treap来维护,基本操作中split_k和split_v都使用普通的merge,但在提取区间并打完标记后,因为整个序列的单调性发生改变,需要使用启发式合并(只在修改过后使用)。

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<ctime>
#include<queue>
using namespace std;
template<typename T>
inline void read(T &x) {
T i = 0, f = 1;
char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
x = i * f;
}
template<typename T>
inline void wr(T x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
const int N = 1e6 + 50, OO = 0x3f3f3f3f;
int n, m, h[N], c[N];
inline int Rand(){
static int RAND_VAL = 1388593021;
return RAND_VAL += RAND_VAL << 2 | 1;
}
#define SZ(x) (x?x->sze:0)
#define V(x) (x?x->val:OO)
struct node{
node *lc, *rc;
int sze, pri, val, tag;
node():lc(NULL), rc(NULL){}
inline void add(int v){
val += v, tag += v;
}
inline void pushDown(){
if(tag != 0){
if(lc) lc->add(tag);
if(rc) rc->add(tag);
tag = 0;
}
}
inline node* upt(){
sze = SZ(lc) + SZ(rc) + 1;
return this;
}
inline void print(){
pushDown();
if(lc) lc->print();
cout<< val<<" ";
if(rc) rc->print();
}
}pool[N], *tail = pool, *rt = NULL;
inline node* newNode(int v){
node *x = tail++;
x->val = v;
x->lc = x->rc = NULL;
x->pri = Rand();
x->tag = 0;
x->sze = 1;
return x;
}
inline node* Merge_o(node *x, node *y){
if(!x) return y;
if(!y) return x;
node *L, *R;
if(x->pri < y->pri){
x->pushDown();
x->rc = Merge_o(x->rc, y);
return x->upt();
}
else {
y->pushDown();
y->lc = Merge_o(x, y->lc);
return y->upt();
}
}
inline void Split_v(node *x, int v, node *&L, node *&R);
inline node* Merge(node *x, node *y){
if(!x) return y;
if(!y) return x;
x->pushDown(), y->pushDown();
if(x->pri > y->pri) swap(x, y);
node *L, *R;
Split_v(y, x->val, L, R);
x->lc = Merge(x->lc, L);
x->rc = Merge(x->rc, R);
x->upt();
return x;
}
inline void Split_k(node *x, int k, node *&L, node *&R){
if(x == NULL){L = NULL, R = NULL; return;}
x->pushDown();
if(SZ(x->lc) < k){
Split_k(x->rc, k - SZ(x->lc) - 1, L, R);
x->rc = NULL; x->upt();
L = Merge_o(x, L);
}
else{
Split_k(x->lc, k, L, R);
x->lc = NULL; x->upt();
R = Merge_o(R, x);
}
}
inline void Split_v(node *x, int v, node *&L, node *&R){
if(x == NULL){L = NULL, R = NULL; return;}
x->pushDown();
if(V(x) <= v){
Split_v(x->rc, v, L, R);
x->rc = NULL; x->upt();
L = Merge_o(x, L);
}
else{
Split_v(x->lc, v, L, R);
x->lc = NULL, x->upt();
R = Merge_o(R, x);
}
}
inline node* build(){
static node* stk[N];
node* pre;
int top = 0;
for(register int i = 1; i <= n; i++){
node *x = newNode(h[i]);
pre = NULL;
while(top && stk[top]->pri > x->pri)
pre = stk[top]->upt(), stk[top--] = NULL;
if(stk[top]) stk[top]->rc = x;
x->lc = pre; stk[++top] = x;
}
while(top) stk[top--]->upt();
return stk[1];
}
int main(){
freopen("h.in", "r", stdin);
read(n), read(m);
for(register int i = 1; i <= n; i++) read(h[i]);
for(register int i = 1; i <= m; i++) read(c[i]);
sort(h + 1, h + n + 1);
rt = build();
for(register int i = 1; i <= m; i++){
if(!c[i]) continue;
if(c[i] > n){
wr(i - 1);
return 0;
}
node *L, *R, *p, *q;
Split_v(rt, 0, L, R);
if(SZ(R) >= c[i]){
Split_k(R, SZ(R) - c[i], p, q);
if(q) q->add(-1);
R = Merge(p, q);
}
else{
wr(i - 1);
return 0;
}
rt = Merge(L, R);
}
wr(m);
return 0;
}

NOIP 模拟 序列操作 - 无旋treap的更多相关文章

  1. 【算法学习】Fhq-Treap(无旋Treap)

    Treap——大名鼎鼎的随机二叉查找树,以优异的性能和简单的实现在OIer们中广泛流传. 这篇blog介绍一种不需要旋转操作来维护的Treap,即无旋Treap,也称Fhq-Treap. 它的巧妙之处 ...

  2. 【序列操作V】平衡树(无旋treap)

    题目描述 维护一个队列,初始为空.依次加入 n(1≤n≤105)个数 ai(-109≤ai≤109),第 i(1≤i≤n)个数加入到当前序列第 bi(0≤bi≤当前序列长度)个数后面.输出最终队列. ...

  3. 无旋treap的区间操作实现

    最近真的不爽...一道维修数列就做了我1上午+下午1h+1晚上+晚上1h+上午2h... 一道不错的自虐题... 由于这一片主要讲思想,代码我放这里了 不会无旋treap的童鞋可以进这里 呵呵... ...

  4. [转载]无旋treap:从单点到区间(例题 BZOJ1500&NOI2005 维护数列 )

    转自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182631.html 1500: [NOI2005]维修数列 Time Limit: 10 Sec  Mem ...

  5. [BZOJ3223]文艺平衡树 无旋Treap

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Description 您需要写一种数据结构(可参考题目标题),来维护一个 ...

  6. [您有新的未分配科技点] 无旋treap:从单点到区间(例题 BZOJ1500&NOI2005 维护数列 )

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MB Description Input 输入的第1 行包含两个数N 和M(M ≤20 ...

  7. 无旋treap大法好

    无旋Treap大法好 原理? 是一棵二叉查找树: 一个节点左子树权值都比他小,右子树权值都比他大 所以可以维护序列(以位置为权值),或数值(以数值为权值) 是一个堆: 每个节点除了上述提到的权值外,还 ...

  8. 模板 - 数据结构 - 可持久化无旋Treap/PersistentFHQTreap

    有可能当树中有键值相同的节点时,貌似是要对Split和Merge均进行复制的,本人实测:只在Split的时候复制得到了一个WA,但只在Merge的时候复制还是AC,可能是恰好又躲过去了.有人说假如确保 ...

  9. BZOJ3678 wangxz与OJ (平衡树 无旋treap)

    题面 维护一个序列,支持以下操作: 1.在某个位置插入一段值连续的数. 2.删除在当前序列位置连续的一段数. 3.查询某个位置的数是多少. 题解 显然平衡树,一个点维护一段值连续的数,如果插入或者删除 ...

随机推荐

  1. 几个经常使用的cmd命令

    compmgmt.msc 计算机管理  devmgmt.msc 设备管理器  diskmgmt.msc 磁盘管理工具  dfrg.msc 磁盘碎片整理  eventvwr.msc 事件查看器  fsm ...

  2. HTML基础第十一讲---背景标志

    转自:https://i.cnblogs.com/posts?categoryid=1121494 您是否老觉得网页「空空的」,没错!一个可能是我们还没有很多内容,另一个可能则是我们还没有设定网页背景 ...

  3. 获取input file 选中的图片,并在一个div的img里面赋值src实现预览

    代码如下利用html5实现:几乎兼容所有主流浏览器,当然IE必须是IE 6以上 [jquery代码] $(function() { $("#file_upload").change ...

  4. 2. Vue基础语法

      模板语法: Mustache语法: {{}} Html赋值: v-html="" 绑定属性: v-bind:id="" 使用表达式: {{ok?'Yes': ...

  5. LED恒流设计

  6. 关于Clipboard和GlobalAlloc函数的关系

    一句话:为了满足进程间通信,使用了clipboard的方法,clipboard是系统提供的一段任何进程都可以访问的公共内存块,malloc 和new分配的动态内存块是在进程的私有地址空间分配的,所以必 ...

  7. Loadrunner--负载生成器

    对场景进行设计后,接着需要对负载生成器进行管理和设置.Load Generator是运行脚本的负载引擎,在默认情况下使用本地的负载生成器来运行脚本,但是模拟用户行为也需要消耗一定的系统资源,所以在一台 ...

  8. 使用Profiles分析SQL语句运行时间和消耗资源

    打开profiling,默认是没开启的. mysql> set profiling=1; 运行要分析的SQL语句 mysql> select count(1) from wechat_em ...

  9. javascript进阶课程--第三章--匿名函数和闭包

    javascript进阶课程--第三章--匿名函数和闭包 一.总结 二.学习要点 掌握匿名函数和闭包的应用 三.匿名函数和闭包 匿名函数 没有函数名字的函数 单独的匿名函数是无法运行和调用的 可以把匿 ...

  10. Spring5源码深度解析(一)之理解Configuration注解

    代码地址:https://github.com/showkawa/spring-annotation/tree/master/src/main/java/com/brian 1.Spring体系结构 ...