bzoj1503 Splay 维护名次数,支持删除
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1503
题解:
维护一颗Splay和一个外部变量,树中每个节点表示一个人,节点权值a + 外部变量delta = 该员工工资。
细节看代码。
注意:一进来工资就低于最低工资的人不能算是“离开公司”的人。
#include <cstdio>
#define fprintf(...)
#define maxn 100100 struct Splay {
int key[maxn], pre[maxn], son[maxn][], siz[maxn], ntot, root;
int trash[maxn], rtot; Splay():ntot(),root(),rtot(){}
void update( int nd ) {
siz[nd] = siz[son[nd][]] + siz[son[nd][]] + ;
}
void rotate( int nd, int d ) {
int p = pre[nd];
int s = son[nd][!d];
int ss = son[s][d]; if( p ) son[p][ nd==son[p][] ] = s;
else root = s;
son[nd][!d] = ss;
son[s][d] = nd; if( ss ) pre[ss] = nd;
pre[nd] = s;
pre[s] = p; update( nd );
update( s );
}
void splay( int nd, int top= ) {
while( pre[nd]!=top ) {
int p = pre[nd];
int nl = nd==son[p][];
if( pre[p]==top ) {
rotate( p, nl );
} else {
int pp = pre[p];
int pl = p==son[pp][];
if( nl==pl ) {
rotate( pp, pl );
rotate( p, nl );
} else {
rotate( p, nl );
rotate( pp, pl );
}
}
}
}
int newnode( int k, int p ) {
int nd;
if( rtot ) nd = trash[rtot--];
else nd = ++ntot;
key[nd] = k;
pre[nd] = p;
son[nd][] = son[nd][] = ;
siz[nd] = ;
return nd;
}
void insert( int k ) {
fprintf( stderr, "insert(%d)\n", k );
if( !root ) {
root = newnode( k, );
return;
}
int nd = root;
while( son[nd][ k<key[nd] ] )
nd = son[nd][ k<key[nd] ];
son[nd][ k<key[nd] ] = newnode( k, nd );
update( nd );
splay( nd, );
}
void erase_subtree( int nd ) {
fprintf( stderr, "erase_subtree(%d)\n", nd );
if( !nd ) return;
erase_subtree( son[nd][] );
erase_subtree( son[nd][] );
trash[++rtot] = nd;
}
void erase( int k ) {
fprintf( stderr, "erase(%d)\n", k );
int nd = root;
int active = nd;
while( nd ) {
if( key[nd]<=k ) {
int p = pre[nd];
int ls= son[nd][]; if( p ) son[p][ nd==son[p][] ] = son[nd][];
else root = son[nd][];
pre[son[nd][]] = p; pre[nd] = ;
son[nd][] = ; erase_subtree( nd ); if( p ) update( p );
nd = ls;
} else {
active = nd;
nd = son[nd][];
}
}
splay(active);
}
int nth( int n ) {
fprintf( stderr, "nth(%d)\n", n );
int nd = root;
while() {
int ls = siz[son[nd][]];
if( n<=ls ) {
nd=son[nd][];
} else if( n>=ls+ ) {
nd=son[nd][];
n -= ls+;
} else
break;
}
splay( nd );
return key[nd];
}
void print( int nd ) {
if(!nd) return;
print( son[nd][] );
fprintf( stderr, "%d %d %d %d %d\n", nd, pre[nd], son[nd][], son[nd][],
key[nd] );
print( son[nd][] );
}
}; Splay T;
int n, delta, limit, cnt; int main() {
//freopen( "input", "r", stdin );
scanf( "%d%d", &n, &limit );
delta = ;
cnt = ;
while(n--) {
char ch[];
int k;
scanf( "%s%d", ch, &k );
switch(ch[]) {
case 'I':
if( k<limit ) break;
cnt++;
k -= delta;
T.insert( k );
break;
case 'A':
delta += k;
break;
case 'S':
delta -= k;
T.erase( limit-delta- );
break;
case 'F':
if( !(<=k && k<=T.siz[T.root]) ) printf( "-1\n" );
else printf( "%d\n", T.nth(k)+delta );
break;
}
fprintf( stderr, "delta=%d\n", delta );
//T.print( T.root );
fprintf( stderr, "\n" );
}
printf( "%d\n", cnt-T.siz[T.root] );
}
bzoj1503 Splay 维护名次数,支持删除的更多相关文章
- 【BZOJ 3729】3729: Gty的游戏 (Splay维护dfs序+博弈)
未经博主同意不得转载 3729: Gty的游戏 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 448 Solved: 150 Description ...
- HNOI2004宠物收养所(splay维护二叉搜索树模板题)
描述 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- Box HDU - 2475 (Splay 维护森林)
Box \[ Time Limit: 5000 ms \quad Memory Limit: 32768 kB \] 题意 给出 \(n\) 个箱子的包含关系,每次两种操作. 操作 \(1\):把 \ ...
- iOS 限制TextField输入长度(支持删除)
if (textField == _phoneTF) { //支持删除 && ) { return YES; } ) { _phoneTF.text = [textField.text ...
- BZOJ 3729 splay维护DFS序+博弈论
思路: 这像是 阶梯Nim之类的东西 我们 直接把sg函数 设成mod(L+1)的 一棵子树 向下的奇数层上的石子xor起来 就是答案 有加点和改值的操作 就splay维护一下 //By Sirius ...
- BZOJ 1492 [NOI2007]货币兑换Cash (CDQ分治/splay 维护凸包)
题目大意:太长了略 splay调了两天一直WA弃疗了 首先,我们可以猜一个贪心,如果买/卖,就一定都买/卖掉,否则不买/卖 反正货币的行情都是已知的,没有任何风险,所以肯定要选择最最最优的方案了 容易 ...
- BZOJ 1251 Splay维护序列
思路: splay维护序列的裸题 啊woc调了一天 感谢yzy大佬的模板-- //By SiriusRen #include <cstdio> #include <cstring&g ...
- BZOJ1014[JSOI2008]火星人prefix(splay维护hash)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- bzoj3786星系探索(splay维护dfs序)
Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...
随机推荐
- ARM Linux 3.x的设备树(Device Tree)【转】
转自:http://blog.csdn.net/21cnbao/article/details/8457546 宋宝华 Barry Song <21cnbao@gmail.com> 1. ...
- 使用Cache缓存
存放位置:服务器内存,用于频繁访问且不轻易更改的内容缓存. string CacheKey = "CT1"; //检索指定项, object objModel = Cache.Ge ...
- Oracle11g用户、权限、角色、概要文件管理及审计
第10章 安全管理 1 用户管理 2 权限管理 3 角色管理 : 4 概要文件管理 5 审计 操作系统:win7 Oracle安装目录:E盘 数据库名字:orcl 密码:123456 先 ...
- 【hdu6334】【2018Multi-University-Training Contest04】Problem C. Problems on a Tree
维护1边的联通块和2边的联通块,合并的时候直接启发式合并. cdqz的大爷好强啊. #include<bits/stdc++.h> #define lson (o<<1) #d ...
- Nginx实现代理和用户验证
1.下载Nginx 首先去官网http://nginx.org/en/download.html下载需要的版本即可,无需安装,只需要打开nginx.exe文件,nginx.exe的服务就开启了.打开h ...
- (二)Mybatis项目配置
第一节:environments Mybatis支持多个环境,可以任意配置 <environments default="development"> <envir ...
- Linux 系统目录结构和常用指令
一.系统目录结构 /bin 经常使用的命令 /etc 所有系统管理所需的配置文件和子目录 /home 用户主目录 /usr 应用程序目录 /usr/bin 系统用户使用的应用程序 /usr/sbin ...
- 使用Oracle数据库,对某个表频繁更新
使用Oracle数据库,对某个表频繁更新,查询时要联合这张表,查询速度非常慢,有什么解决办法? 一般的pc机oracle更新的效率均可达到500+/s, 可能的问题,你更新这个不会是每次都新建jdbc ...
- maven:missing artifact jdk.tools:jar:1.7
http://stackoverflow.com/questions/11118070/buiding-hadoop-with-eclipse-maven-missing-artifact-jdk-t ...
- bzoj 3144
3144 思路: xxy: 代码: #include <cstdio> #include <cstring> #include <iostream> #includ ...