题意:

一开始有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. iOS Threading编程指南 官方文档翻译第一篇(序言)

    序言   Thread是能够使多个code paths 在同一个APP内并发运行的几种技术之一.虽然新的技术为并发运行提供了先进.高效的工具(例如operation 对象和GCD),但是OS X和iO ...

  2. 韦东山网课https://edu.csdn.net/course/play/207/1117

    接口讲解https://edu.csdn.net/course/play/207/1117

  3. mysql 语句优化心得

    排序导致性能较慢 优化策略:1.尽量不使用排序 2.只查有索引的结果然后 内连接查询 select  bizchance0_.*  from biz_chance bizchance0_, biz_b ...

  4. ajax缓存 header头文件

    浏览器第一次访问服务器的时候,需要从服务器加载很多静态资源,并将这些资源文件缓存在浏览器中,当再次访问页面的时候,如果有相同资源文件就直接到缓存中去加载,这样就会降低服务器的负载和带宽,加快用户访问, ...

  5. 使用Struts2和jQuery EasyUI实现简单CRUD系统(七)——数据分页处理

    上篇完毕多选删除的功能之后,接下来就是做分页功能了.曾经的分页是一个麻烦的问题.并且数据量巨大的时候,直接把这些元素取出来显然速度较慢,所以取一定区间的数据还是高效的. watermark/2/tex ...

  6. .NetCore微服务Surging新手傻瓜式 入门教程 学习日志---先让程序跑起来(一)

    原文:.NetCore微服务Surging新手傻瓜式 入门教程 学习日志---先让程序跑起来(一) 写下此文章只为了记录Surging微服务学习过程,并且分享给广大想学习surging的基友,方便广大 ...

  7. Spring Boot 动态数据源(Spring 注解数据源)

    本文实现案例场景:某系统除了需要从自己的主要数据库上读取和管理数据外,还有一部分业务涉及到其他多个数据库,要求可以在任何方法上可以灵活指定具体要操作的数据库. 为了在开发中以最简单的方法使用,本文基于 ...

  8. 步步为营(十五)搜索(一)DFS 深度优先搜索

    前方大坑预警! 先讲讲什么是搜索吧. 有一天你去一个果园摘梨子,果农告诉你.有一棵树上有一个金子做的梨子,找到就是你的,你该怎么找? 地图例如以下: S 0 0 0 0 0 0 0 0 0 0 0 0 ...

  9. vscode markdown-all-in-one 源码编译成vsix

    https://marketplace.visualstudio.com/items?itemName=yzhang.markdown-all-in-one 有链接 Download Extensio ...

  10. 用CSS实现阴阳八卦图等图形

    CSS还是比较强大的,可以实现中国古典的"阴阳八卦图"等形状. 正方形 #rectangle { width: 200px; height: 100px; backgrount-c ...