【BZOJ 1503】【NOI 2004】郁闷的出纳员
$Splay$模板题。
复习一下伸展树的模板。
一定不要忘了push啊!!!
对于减工资后删掉员工的操作,我选择插入一个$min+delta_{减少的工资}$的节点,把它$Splay$到根,砍掉它自己和左子树,保留右子树,这样该走的员工就会从这个世界上消失啦~~~
#include<cstdio>
#include<cstring>
#include<algorithm>
#define read(x) x = getint()
using namespace std;
struct node {
node *ch[2], *fa;
int d, sz, sum, lazy;
bool pl() {return this->fa->ch[1] == this;}
void setc(node *r, bool c) {ch[c] = r; r->fa = this;}
void count() {sum = ch[0]->sum + ch[1]->sum + sz;}
void push() {
if (lazy != 0) {
ch[0]->lazy += lazy;
ch[1]->lazy += lazy;
ch[0]->d += lazy;
ch[1]->d += lazy;
lazy = 0;
}
}
}*ROOT, *null;
node pool[100003];
int top = 0, M, n, gone = 0;
namespace Splay{
void Build() {
null = &pool[0];
null->d = null->sz = null->sum = 0;
null->ch[0] = null->ch[1] = null->fa = null;
ROOT = null;
}
node *newnode() {
node *t = &pool[++top];
t->d = t->sz = t->sum = 0;
t->ch[0] = t->ch[1] = t->fa = null;
return t;
}
void rotate(node *r) {
node *f = r->fa; if (r == null || f == null) return;
bool c = r->pl();
if (f->fa == null) ROOT = r, r->fa = null;
else f->fa->setc(r, f->pl());
f->setc(r->ch[!c], c);
r->setc(f, !c);
f->count();
}
void update(node *r) {if (r != ROOT) update(r->fa); r->push();}
void splay(node *r, node *tar = null) {
update(r);
for(; r->fa != tar; rotate(r))
if (r->fa->fa != tar) rotate(r->pl() == r->fa->pl() ? r->fa : r);
r->count();
}
void ins(int num) {
if (ROOT == null) {
ROOT = newnode();
ROOT->d = num;
ROOT->sz = ROOT->sum = 1;
return;
}
node *r = ROOT;
while (1) {
r->push();
int k = r->d; bool c;
if (num < k) c = 0;
else if (num > k) c = 1;
else {++r->sz; ++r->sum; splay(r); return;}
if (r->ch[c] == null) {
r->ch[c] = newnode();
r->setc(r->ch[c], c);
r->ch[c]->sz = r->ch[c]->sum = 1;
r->ch[c]->d = num;
splay(r->ch[c]);
return;
} else r = r->ch[c];
}
}
void ins2(int num) {
node *r = ROOT; bool c;
while (1) {
r->push();
if (num <= r->d) c = 0;
else c = 1;
if (r->ch[c] == null) {
r->ch[c] = newnode();
r->setc(r->ch[c], c);
splay(r->ch[c]);
gone += ROOT->ch[0]->sum;
ROOT->ch[1]->fa = null;
ROOT = ROOT->ch[1];
return;
} else r = r->ch[c];
}
}
int kth(int k) {
node *r = ROOT;
while (r != null) {
r->push();
if (r->ch[1]->sum >= k) r = r->ch[1];
else if (r->ch[1]->sum + r->sz >= k) return r->d;
else k -= r->ch[1]->sum + r->sz, r = r->ch[0];
}
return 0;
}
} inline int getint() {
int k = 0, fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = k * 10 + c - '0';
return k * fh;
}
int main() {
Splay::Build();
read(n); read(M);
int num;
for(int i = 1; i <= n; ++i) {
char c = getchar(); while (c < 'A' || c > 'Z') c = getchar();
switch (c) {
case 'I':
read(num);
if (num >= M)
Splay::ins(num);
break;
case 'A':
read(num);
ROOT->d += num;
ROOT->lazy += num;
ROOT->push();
break;
case 'S':
read(num);
Splay::ins2(num + M);
ROOT->d -= num;
ROOT->lazy -= num;
ROOT->push();
break;
case 'F':
read(num);
if (num > ROOT->sum) puts("-1");
else printf("%d\n", Splay::kth(num));
break;
}
}
printf("%d\n", gone);
return 0;
}
【BZOJ 1503】【NOI 2004】郁闷的出纳员的更多相关文章
- [bzoj 1503][NOI 2004]郁闷的出纳员(平衡树)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 分析: 经典的平衡树题,我用Treap做的 下面有几点注意的: 1.可能出现新加入的人的 ...
- 洛谷 P1486 BZOJ 1503 NOI 2004 郁闷的出纳员 fhq treap
思路: 1. 此处的fhq treap的分裂是按照权值分裂然后插入的.将小于k的分为一棵子树,大于等于k的分为另一棵子树. 2. 删除的时候只要将大于等于min的分裂到以root为根的树中,另一部分不 ...
- bzoj 1503[NOI 2004] 郁闷的出纳员
题目大意: 给4种操作 I:添加一个员工工资信息 A:增加所有员工的工资 S:减少所有员工的工资 F:询问工资第k高的员工的工资情况 自己做的第一道splay树的题目,初学找找感觉 #include ...
- 数据结构(跳跃表):NOI 2004 郁闷的出纳员
郁闷的出纳员 [问题描述] OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常, ...
- NOI 2004 郁闷的出纳员(平衡树)
题目描述 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资 ...
- NOI 2004 郁闷的出纳员
Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...
- 【BZOJ 1503】[NOI2004]郁闷的出纳员
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 因为所有人工资同时递减. 所以可以设置一个变化值delta. 然后每个人的初始值为k 则把k-delta加入伸展树中. 会发现del ...
- 【NOI】2004 郁闷的出纳员
[算法]平衡树(treap) [题解] treap知识见数据结构. 解法,具体细节见程序. #include<cstdio> #include<algorithm> #incl ...
- 【BZOJ】【1503】 【NOI2004】郁闷的出纳员
Splay Splay的模板题吧……妥妥的序列操作= =(好像有段时间没写过这种纯数据结构题了……) /************************************************ ...
- bzoj 1503郁闷的出纳员(splay)
1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 11759 Solved: 4163[Submit][Stat ...
随机推荐
- Unity3D中Update()与FixedUpdate()的区别
Unity3D中Update()与FixedUpdate()的区别是什么呢?从字面上理解,它们都是在更新时会被调用,并且会循环的调用.但是Update会在每次渲染新的一帧时,被调用.而FixedUpd ...
- python日期格式化与绘图
画一个量随着时间变化的曲线是经常会遇到的需求,比如画软件用户数的变化曲线.画随时间变化的曲线主要用到的函数是matplotlib.pyplot.plot_date(date,num).由于其第一个变量 ...
- git push时错误提示的解决办法 By default, updating the current branch in a non-bare repository error: is denied,
在使用git将客户端的修改push到服务器上的时候,出现无法push,提示和stackoverflow上的http://stackoverflow.com/questions/2816369/git- ...
- java (基本语法)
2.五大内存区 方法区就是存储共享数据的地方 3.一个实体多处引用 只有还有实体被指向,这个实体就不能消失.当所有的指向都消失之后,这个实体被视为垃圾,被垃圾回收机制不定期的回收. 堆里的实体能存储多 ...
- box-shadow 的一些使用
1.只有左侧有阴影 box-shadow: -10px 0px 3px 1px #aaaaaa;
- 关于webpack.optimize.CommonsChunkPlugin的使用二
Note:当有多个入口节点的时候,只有所有入口节点都引入了同一个模块的时候,webpack.optimize.CommonsChunkPlugin才会将那个模块提取出来,如果其中一个入口节点没有引入该 ...
- js的client详解
clientWidth:可视区宽 说明:样式宽+padding clientHeight:可视区高 说明:样式高+padding elem.clientWidth //获取元素 ...
- FastFourierTransform (FFT)
FastFourierTransform.h #pragma once #include <stdio.h> #include <math.h> #ifndef INCLUDE ...
- 实现Linux与Windows下一致的命令行
这其实是个非常简单的东西. 我们会写一些命令行的工具,一般跨平台的话,会用python或者perl写,比如叫foo.py,然后在Windows和Linux下调用这个脚本: Linux: foo.py ...
- PRML读书会第八章 Graphical Models(贝叶斯网络,马尔科夫随机场)
主讲人 网神 (新浪微博: @豆角茄子麻酱凉面) 网神(66707180) 18:52:10 今天的内容主要是: 1.贝叶斯网络和马尔科夫随机场的概念,联合概率分解,条件独立表示:2.图的概率推断in ...